room_member_worker.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. import logging
  16. from twisted.internet import defer
  17. from synapse.api.errors import SynapseError
  18. from synapse.handlers.room_member import RoomMemberHandler
  19. from synapse.replication.http.membership import (
  20. get_or_register_3pid_guest,
  21. notify_user_membership_change,
  22. remote_join,
  23. remote_reject_invite,
  24. )
  25. logger = logging.getLogger(__name__)
  26. class RoomMemberWorkerHandler(RoomMemberHandler):
  27. @defer.inlineCallbacks
  28. def _remote_join(self, requester, remote_room_hosts, room_id, user, content):
  29. """Implements RoomMemberHandler._remote_join
  30. """
  31. if len(remote_room_hosts) == 0:
  32. raise SynapseError(404, "No known servers")
  33. ret = yield remote_join(
  34. self.simple_http_client,
  35. host=self.config.worker_replication_host,
  36. port=self.config.worker_replication_http_port,
  37. requester=requester,
  38. remote_room_hosts=remote_room_hosts,
  39. room_id=room_id,
  40. user_id=user.to_string(),
  41. content=content,
  42. )
  43. yield self._user_joined_room(user, room_id)
  44. defer.returnValue(ret)
  45. def _remote_reject_invite(self, requester, remote_room_hosts, room_id, target):
  46. """Implements RoomMemberHandler._remote_reject_invite
  47. """
  48. return remote_reject_invite(
  49. self.simple_http_client,
  50. host=self.config.worker_replication_host,
  51. port=self.config.worker_replication_http_port,
  52. requester=requester,
  53. remote_room_hosts=remote_room_hosts,
  54. room_id=room_id,
  55. user_id=target.to_string(),
  56. )
  57. def _user_joined_room(self, target, room_id):
  58. """Implements RoomMemberHandler._user_joined_room
  59. """
  60. return notify_user_membership_change(
  61. self.simple_http_client,
  62. host=self.config.worker_replication_host,
  63. port=self.config.worker_replication_http_port,
  64. user_id=target.to_string(),
  65. room_id=room_id,
  66. change="joined",
  67. )
  68. def _user_left_room(self, target, room_id):
  69. """Implements RoomMemberHandler._user_left_room
  70. """
  71. return notify_user_membership_change(
  72. self.simple_http_client,
  73. host=self.config.worker_replication_host,
  74. port=self.config.worker_replication_http_port,
  75. user_id=target.to_string(),
  76. room_id=room_id,
  77. change="left",
  78. )
  79. def get_or_register_3pid_guest(self, requester, medium, address, inviter_user_id):
  80. """Implements RoomMemberHandler.get_or_register_3pid_guest
  81. """
  82. return get_or_register_3pid_guest(
  83. self.simple_http_client,
  84. host=self.config.worker_replication_host,
  85. port=self.config.worker_replication_http_port,
  86. requester=requester,
  87. medium=medium,
  88. address=address,
  89. inviter_user_id=inviter_user_id,
  90. )