@@ -208,6 +208,11 @@ def _update_states(self, new_states):
208
208
to_federation_ping = {} # These need sending keep-alives
209
209
for new_state in new_states :
210
210
user_id = new_state .user_id
211
+
212
+ # Its fine to not hit the database here, as the only thing not in
213
+ # the current state cache are OFFLINE states, where the only field
214
+ # of interest is last_active which is safe enough to assume is 0
215
+ # here.
211
216
prev_state = self .user_to_current_state .get (
212
217
user_id , UserPresenceState .default (user_id )
213
218
)
@@ -326,6 +331,7 @@ def _handle_timeouts(self):
326
331
if now - state .last_user_sync > SYNC_ONLINE_TIMEOUT :
327
332
changes [user_id ] = state .copy_and_replace (
328
333
state = PresenceState .OFFLINE ,
334
+ status_msg = None ,
329
335
)
330
336
else :
331
337
# We expect to be poked occaisonally by the other side.
@@ -335,6 +341,7 @@ def _handle_timeouts(self):
335
341
# The other side seems to have disappeared.
336
342
changes [user_id ] = state .copy_and_replace (
337
343
state = PresenceState .OFFLINE ,
344
+ status_msg = None ,
338
345
)
339
346
340
347
preserve_fn (self ._update_states )(changes .values ())
@@ -348,10 +355,13 @@ def bump_presence_active_time(self, user):
348
355
349
356
prev_state = yield self .current_state_for_user (user_id )
350
357
351
- yield self ._update_states ([prev_state .copy_and_replace (
352
- state = PresenceState .ONLINE ,
353
- last_active = self .clock .time_msec (),
354
- )])
358
+ new_fields = {
359
+ "last_active" : self .clock .time_msec (),
360
+ }
361
+ if prev_state .state == PresenceState .UNAVAILABLE :
362
+ new_fields ["state" ] = PresenceState .ONLINE
363
+
364
+ yield self ._update_states ([prev_state .copy_and_replace (** new_fields )])
355
365
356
366
@defer .inlineCallbacks
357
367
def user_syncing (self , user_id , affect_presence = True ):
@@ -618,7 +628,7 @@ def set_state(self, target_user, state):
618
628
619
629
new_fields = {
620
630
"state" : presence ,
621
- "status_msg" : status_msg
631
+ "status_msg" : status_msg if presence != PresenceState . OFFLINE else None
622
632
}
623
633
624
634
if presence == PresenceState .ONLINE :
0 commit comments