Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

public_inbox: Fix email threads without an available root message #369

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 17 additions & 5 deletions did/plugins/public_inbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,18 @@ class Message(object):
def __init__(self, msg: mailbox.mboxMessage) -> None:
self.msg = msg

def __msg_id(self, keyid: str) -> str:
def __msg_id(self, keyid: str) -> typing.Optional[str]:
msgid = self.msg[keyid]
if msgid is None:
log.debug("Missing header %s" % keyid)
return None

return msgid.lstrip("<").rstrip(">")

def id(self) -> str:
def id(self) -> typing.Optional[str]:
return self.__msg_id("Message-Id")

def parent_id(self) -> str:
def parent_id(self) -> typing.Optional[str]:
return self.__msg_id("In-Reply-To")

def subject(self) -> str:
Expand Down Expand Up @@ -129,7 +130,7 @@ def __get_msgs_from_mbox(self, mbox: mailbox.mbox) -> list[Message]:

return msgs

def __fetch_thread_root(self, msg: Message) -> Message:
def __fetch_thread_root(self, msg: Message) -> typing.Optional[Message]:
msg_id = msg.id()
url = self.__get_url("/all/%s/t.mbox.gz" % msg_id)

Expand All @@ -141,6 +142,9 @@ def __fetch_thread_root(self, msg: Message) -> Message:
log.debug("Found message %s thread root: %s." % (msg_id, msg.id()))
return msg

log.warn("Couldn't find message root")
return None

def __get_thread_root(self, msg: Message) -> Message:
log.debug("Looking for thread root of message %s" % msg.id())
if msg.is_thread_root():
Expand All @@ -150,6 +154,10 @@ def __get_thread_root(self, msg: Message) -> 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

Expand All @@ -163,7 +171,11 @@ def __get_thread_root(self, msg: Message) -> 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()))
Expand Down
Loading