server_notices_sender.py 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2018 New Vector 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 twisted.internet import defer
  16. from synapse.server_notices.consent_server_notices import ConsentServerNotices
  17. from synapse.server_notices.resource_limits_server_notices import (
  18. ResourceLimitsServerNotices,
  19. )
  20. class ServerNoticesSender(object):
  21. """A centralised place which sends server notices automatically when
  22. Certain Events take place
  23. """
  24. def __init__(self, hs):
  25. """
  26. Args:
  27. hs (synapse.server.HomeServer):
  28. """
  29. self._server_notices = (
  30. ConsentServerNotices(hs),
  31. ResourceLimitsServerNotices(hs)
  32. )
  33. @defer.inlineCallbacks
  34. def on_user_syncing(self, user_id):
  35. """Called when the user performs a sync operation.
  36. Args:
  37. user_id (str): mxid of user who synced
  38. """
  39. for sn in self._server_notices:
  40. yield sn.maybe_send_server_notice_to_user(
  41. user_id,
  42. )
  43. @defer.inlineCallbacks
  44. def on_user_ip(self, user_id):
  45. """Called on the master when a worker process saw a client request.
  46. Args:
  47. user_id (str): mxid
  48. """
  49. # The synchrotrons use a stubbed version of ServerNoticesSender, so
  50. # we check for notices to send to the user in on_user_ip as well as
  51. # in on_user_syncing
  52. for sn in self._server_notices:
  53. yield sn.maybe_send_server_notice_to_user(
  54. user_id,
  55. )