presence.py 2.1 KB

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