export-search: Add explicit usermessage message_id range filters.

(cherry picked from commit 7714ca3ff9)
This commit is contained in:
Alex Vandiver
2025-08-22 03:55:49 +00:00
committed by Tim Abbott
parent 570e32a771
commit 6042830850

View File

@@ -147,6 +147,7 @@ This is most often used for legal compliance.
realm = self.get_realm(options) realm = self.get_realm(options)
assert realm is not None assert realm is not None
need_distinct = False need_distinct = False
usermessage_joined = False
limits = Q() limits = Q()
limits = reduce( limits = reduce(
@@ -173,6 +174,7 @@ This is most often used for legal compliance.
usermessage__user_profile_id__in=[user_profile.id for user_profile in user_profiles] usermessage__user_profile_id__in=[user_profile.id for user_profile in user_profiles]
) )
need_distinct = len(user_profiles) > 1 need_distinct = len(user_profiles) > 1
usermessage_joined = True
elif options["sender"]: elif options["sender"]:
limits &= reduce( limits &= reduce(
or_, or_,
@@ -187,6 +189,7 @@ This is most often used for legal compliance.
usermessage__user_profile_id=user_profiles[0].id, usermessage__user_profile_id=user_profiles[0].id,
usermessage__flags__andnz=AbstractUserMessage.flags.is_private.mask, usermessage__flags__andnz=AbstractUserMessage.flags.is_private.mask,
) )
usermessage_joined = True
elif len(user_profiles) == 2: elif len(user_profiles) == 2:
user_a, user_b = user_profiles user_a, user_b = user_profiles
direct_message_group = get_direct_message_group(id_list=[user_a.id, user_b.id]) direct_message_group = get_direct_message_group(id_list=[user_a.id, user_b.id])
@@ -281,6 +284,10 @@ This is most often used for legal compliance.
min_id = 0 min_id = 0
while True: while True:
batch_query = messages_query.filter(id__gt=min_id) batch_query = messages_query.filter(id__gt=min_id)
if usermessage_joined:
batch_query = batch_query.extra(
where=["zerver_usermessage.message_id > %s"], params=[min_id]
)
batch = [transform_message(m) for m in batch_query[:BATCH_SIZE]] batch = [transform_message(m) for m in batch_query[:BATCH_SIZE]]
if len(batch) == 0: if len(batch) == 0:
break break