From 240b72c31f089215e0551c594459fbbbce43430f Mon Sep 17 00:00:00 2001 From: Maxime Ripard Date: Mon, 15 Jul 2024 12:08:32 +0200 Subject: [PATCH] public_inbox: Handle missing parent is __get_thread_root() A message can have an In-Reply-To header but the message it points to might not be available for some reason. In such a case, the Message.parent_id() method will return a proper value, but __fetch_thread_root() on that message might not and would return None. Let's handle that case by returning the latest message we found as the root message. Signed-off-by: Maxime Ripard --- did/plugins/public_inbox.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/did/plugins/public_inbox.py b/did/plugins/public_inbox.py index 3279c61f..984aa57a 100644 --- a/did/plugins/public_inbox.py +++ b/did/plugins/public_inbox.py @@ -145,7 +145,7 @@ def __fetch_thread_root(self, msg: Message) -> typing.Optional[Message]: log.warn("Couldn't find message root") return None - def __get_thread_root(self, msg: Message) -> typing.Optional[Message]: + def __get_thread_root(self, msg: Message) -> Message: log.debug("Looking for thread root of message %s" % msg.id()) if msg.is_thread_root(): log.debug("Message is thread root already. Returning.") @@ -154,6 +154,10 @@ def __get_thread_root(self, msg: Message) -> typing.Optional[Message]: parent_id = msg.parent_id() if parent_id not in self.messages_cache: root = self.__fetch_thread_root(msg) + if root is None: + log.debug("Can't retrieve the thread root, returning.") + return msg + log.debug("Found root message %s for message %s" % (root.id(), msg.id())) return root @@ -167,7 +171,11 @@ def __get_thread_root(self, msg: Message) -> typing.Optional[Message]: parent_id = parent.parent_id() if parent_id not in self.messages_cache: - root = self.__fetch_thread_root(msg) + root = self.__fetch_thread_root(parent) + if root is None: + log.debug("Can't retrieve the message parent, returning.") + return parent + log.debug( "Found root message %s for message %s" % (root.id(), msg.id()))