room_member_worker.py 3.5 KB

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