presence.py 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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 typing import Any, Optional
  15. import attr
  16. from synapse.api.constants import PresenceState
  17. from synapse.types import JsonDict
  18. @attr.s(slots=True, frozen=True, auto_attribs=True)
  19. class UserPresenceState:
  20. """Represents the current presence state of the user.
  21. user_id
  22. last_active: Time in msec that the user last interacted with server.
  23. last_federation_update: Time in msec since either a) we sent a presence
  24. update to other servers or b) we received a presence update, depending
  25. on if is a local user or not.
  26. last_user_sync: Time in msec that the user last *completed* a sync
  27. (or event stream).
  28. status_msg: User set status message.
  29. """
  30. user_id: str
  31. state: str
  32. last_active_ts: int
  33. last_federation_update_ts: int
  34. last_user_sync_ts: int
  35. status_msg: Optional[str]
  36. currently_active: bool
  37. def as_dict(self) -> JsonDict:
  38. return attr.asdict(self)
  39. @staticmethod
  40. def from_dict(d: JsonDict) -> "UserPresenceState":
  41. return UserPresenceState(**d)
  42. def copy_and_replace(self, **kwargs: Any) -> "UserPresenceState":
  43. return attr.evolve(self, **kwargs)
  44. @classmethod
  45. def default(cls, user_id: str) -> "UserPresenceState":
  46. """Returns a default presence state."""
  47. return cls(
  48. user_id=user_id,
  49. state=PresenceState.OFFLINE,
  50. last_active_ts=0,
  51. last_federation_update_ts=0,
  52. last_user_sync_ts=0,
  53. status_msg=None,
  54. currently_active=False,
  55. )