RealmAuditLog: Fill subscription events with event_last_message_id=None.

Creating the very first organization administrator user and
subscribing them to streams before any messages were sent resulted in
RealmAuditLog entries being created with a `event_last_message_id` of
None, because that's the maximum ID in the empty set.

We correct this by fixing the incorrectly created RealmAuditLog
entries, both for new servers and also fixing old broken entries on
existing servers.

This fixes an issue where if a user setup a Zulip server with just the
organization administrator, and then forgot about it (so that the
initial user became soft-deactivated), trying to sign in 3 weeks later
would throw an exception.

This fixes the issue reported here:

https://chat.zulip.org/#narrow/stream/9-issues/subject/500.20error.20on.20login/near/511981
This commit is contained in:
Aditya Bansal
2018-02-22 22:15:43 +05:30
committed by Tim Abbott
parent 90c89dcc5a
commit f50e325075
2 changed files with 30 additions and 0 deletions

View File

@@ -2300,6 +2300,12 @@ def bulk_add_subscriptions(streams: Iterable[Stream],
# Log Subscription Activities in RealmAuditLog
event_time = timezone_now()
event_last_message_id = Message.objects.aggregate(Max('id'))['id__max']
if event_last_message_id is None:
# During initial realm creation, there might be 0 messages in
# the database; in that case, the `aggregate` query returns
# None. Since we want an int for "beginning of time", use -1.
event_last_message_id = -1
all_subscription_logs = [] # type: (List[RealmAuditLog])
for (sub, stream) in subs_to_add:
all_subscription_logs.append(RealmAuditLog(realm=sub.user_profile.realm,