You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

68 lines
2.0 KiB

  1. # Copyright 2014-2016 OpenMarket Ltd
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. from collections import namedtuple
  15. from synapse.api.constants import PresenceState
  16. class UserPresenceState(
  17. namedtuple(
  18. "UserPresenceState",
  19. (
  20. "user_id",
  21. "state",
  22. "last_active_ts",
  23. "last_federation_update_ts",
  24. "last_user_sync_ts",
  25. "status_msg",
  26. "currently_active",
  27. ),
  28. )
  29. ):
  30. """Represents the current presence state of the user.
  31. user_id (str)
  32. last_active (int): Time in msec that the user last interacted with server.
  33. last_federation_update (int): Time in msec since either a) we sent a presence
  34. update to other servers or b) we received a presence update, depending
  35. on if is a local user or not.
  36. last_user_sync (int): Time in msec that the user last *completed* a sync
  37. (or event stream).
  38. status_msg (str): User set status message.
  39. """
  40. def as_dict(self):
  41. return dict(self._asdict())
  42. @staticmethod
  43. def from_dict(d):
  44. return UserPresenceState(**d)
  45. def copy_and_replace(self, **kwargs):
  46. return self._replace(**kwargs)
  47. @classmethod
  48. def default(cls, user_id):
  49. """Returns a default presence state."""
  50. return cls(
  51. user_id=user_id,
  52. state=PresenceState.OFFLINE,
  53. last_active_ts=0,
  54. last_federation_update_ts=0,
  55. last_user_sync_ts=0,
  56. status_msg=None,
  57. currently_active=False,
  58. )