Fix creation of multiple Threads (#1552)

- fixes #1545
- Multiple messages can have the same thread_ts if the message reply is a "thread_broadcast" subtype (the user selected "also send to #channel")
- Use get_or_create on when creating threads and skip broadcasted messages when processing threads to avoid double counting.
This commit is contained in:
Brian Perrett
2024-12-12 13:59:08 -08:00
committed by GitHub
parent 2c993f71f5
commit c89aa36532

View File

@@ -140,13 +140,18 @@ def fill_channel_gap(gap: ChannelUpdateGap, debug: bool):
if message.get("thread_ts"):
# Track this thread in the db to be able to check for
# updates later.
Thread.objects.create(
# Thread replies that are broadcast back to the channel have
# the same thread_ts as the parent message thread_ts.
# get_or_create must be used since the thread may have already been
# created.
Thread.objects.get_or_create(
channel=gap.channel,
thread_ts=message["thread_ts"],
# None indicates that this thread still must be updated
# even if it's old.
last_update_ts=None,
defaults={"last_update_ts": None},
)
gap.save()
logger.debug(
"Channel %r retrieved up to %s (%s)",
@@ -189,6 +194,11 @@ def do_thread(thread: Thread, debug: bool):
# come first unlike for channels.
thread.last_update_ts = message["ts"]
if message.get("subtype") == "thread_broadcast":
# This message was broadcast to the channel, if we count it here
# we will be double counting it.
continue
if not should_track_message(message):
continue
@@ -248,6 +258,18 @@ def command(channels, debug):
This is resumable -- it can be interrupted and restarted without losing
progress.
A thread does not exist until a user replies to an existing message.
It is therefore, possible that thread messages are missed.
1. Message "M" is sent.
2. Import is run.
3. Reply "R" to "M" is sent. "M" is now the parent of a thread.
4. "R" will never be imported because the thread was not created
and there is no way for us to know that it was created, aside
from looking back at all historical messages.
Note: If a user replies to a message and selects "also send in channel",
then this import will find that thread and the thread messages will be
accounted for.
Do not run multiple instances of this command in parallel.
"""