test_stream.py 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. # Copyright 2021 The Matrix.org Foundation C.I.C.
  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 List
  15. from synapse.api.constants import EventTypes, RelationTypes
  16. from synapse.api.filtering import Filter
  17. from synapse.events import EventBase
  18. from synapse.rest import admin
  19. from synapse.rest.client import login, room
  20. from synapse.types import JsonDict
  21. from tests.unittest import HomeserverTestCase
  22. class PaginationTestCase(HomeserverTestCase):
  23. """
  24. Test the pre-filtering done in the pagination code.
  25. This is similar to some of the tests in tests.rest.client.test_rooms but here
  26. we ensure that the filtering done in the database is applied successfully.
  27. """
  28. servlets = [
  29. admin.register_servlets_for_client_rest_resource,
  30. room.register_servlets,
  31. login.register_servlets,
  32. ]
  33. def default_config(self):
  34. config = super().default_config()
  35. config["experimental_features"] = {"msc3440_enabled": True}
  36. return config
  37. def prepare(self, reactor, clock, homeserver):
  38. self.user_id = self.register_user("test", "test")
  39. self.tok = self.login("test", "test")
  40. self.room_id = self.helper.create_room_as(self.user_id, tok=self.tok)
  41. self.second_user_id = self.register_user("second", "test")
  42. self.second_tok = self.login("second", "test")
  43. self.helper.join(
  44. room=self.room_id, user=self.second_user_id, tok=self.second_tok
  45. )
  46. self.third_user_id = self.register_user("third", "test")
  47. self.third_tok = self.login("third", "test")
  48. self.helper.join(room=self.room_id, user=self.third_user_id, tok=self.third_tok)
  49. # An initial event with a relation from second user.
  50. res = self.helper.send_event(
  51. room_id=self.room_id,
  52. type=EventTypes.Message,
  53. content={"msgtype": "m.text", "body": "Message 1"},
  54. tok=self.tok,
  55. )
  56. self.event_id_1 = res["event_id"]
  57. self.helper.send_event(
  58. room_id=self.room_id,
  59. type="m.reaction",
  60. content={
  61. "m.relates_to": {
  62. "rel_type": RelationTypes.ANNOTATION,
  63. "event_id": self.event_id_1,
  64. "key": "👍",
  65. }
  66. },
  67. tok=self.second_tok,
  68. )
  69. # Another event with a relation from third user.
  70. res = self.helper.send_event(
  71. room_id=self.room_id,
  72. type=EventTypes.Message,
  73. content={"msgtype": "m.text", "body": "Message 2"},
  74. tok=self.tok,
  75. )
  76. self.event_id_2 = res["event_id"]
  77. self.helper.send_event(
  78. room_id=self.room_id,
  79. type="m.reaction",
  80. content={
  81. "m.relates_to": {
  82. "rel_type": RelationTypes.REFERENCE,
  83. "event_id": self.event_id_2,
  84. }
  85. },
  86. tok=self.third_tok,
  87. )
  88. # An event with no relations.
  89. self.helper.send_event(
  90. room_id=self.room_id,
  91. type=EventTypes.Message,
  92. content={"msgtype": "m.text", "body": "No relations"},
  93. tok=self.tok,
  94. )
  95. def _filter_messages(self, filter: JsonDict) -> List[EventBase]:
  96. """Make a request to /messages with a filter, returns the chunk of events."""
  97. from_token = self.get_success(
  98. self.hs.get_event_sources().get_current_token_for_pagination(self.room_id)
  99. )
  100. events, next_key = self.get_success(
  101. self.hs.get_datastores().main.paginate_room_events(
  102. room_id=self.room_id,
  103. from_key=from_token.room_key,
  104. to_key=None,
  105. direction="b",
  106. limit=10,
  107. event_filter=Filter(self.hs, filter),
  108. )
  109. )
  110. return events
  111. def test_filter_relation_senders(self):
  112. # Messages which second user reacted to.
  113. filter = {"related_by_senders": [self.second_user_id]}
  114. chunk = self._filter_messages(filter)
  115. self.assertEqual(len(chunk), 1, chunk)
  116. self.assertEqual(chunk[0].event_id, self.event_id_1)
  117. # Messages which third user reacted to.
  118. filter = {"related_by_senders": [self.third_user_id]}
  119. chunk = self._filter_messages(filter)
  120. self.assertEqual(len(chunk), 1, chunk)
  121. self.assertEqual(chunk[0].event_id, self.event_id_2)
  122. # Messages which either user reacted to.
  123. filter = {"related_by_senders": [self.second_user_id, self.third_user_id]}
  124. chunk = self._filter_messages(filter)
  125. self.assertEqual(len(chunk), 2, chunk)
  126. self.assertCountEqual(
  127. [c.event_id for c in chunk], [self.event_id_1, self.event_id_2]
  128. )
  129. def test_filter_relation_type(self):
  130. # Messages which have annotations.
  131. filter = {"related_by_rel_types": [RelationTypes.ANNOTATION]}
  132. chunk = self._filter_messages(filter)
  133. self.assertEqual(len(chunk), 1, chunk)
  134. self.assertEqual(chunk[0].event_id, self.event_id_1)
  135. # Messages which have references.
  136. filter = {"related_by_rel_types": [RelationTypes.REFERENCE]}
  137. chunk = self._filter_messages(filter)
  138. self.assertEqual(len(chunk), 1, chunk)
  139. self.assertEqual(chunk[0].event_id, self.event_id_2)
  140. # Messages which have either annotations or references.
  141. filter = {
  142. "related_by_rel_types": [
  143. RelationTypes.ANNOTATION,
  144. RelationTypes.REFERENCE,
  145. ]
  146. }
  147. chunk = self._filter_messages(filter)
  148. self.assertEqual(len(chunk), 2, chunk)
  149. self.assertCountEqual(
  150. [c.event_id for c in chunk], [self.event_id_1, self.event_id_2]
  151. )
  152. def test_filter_relation_senders_and_type(self):
  153. # Messages which second user reacted to.
  154. filter = {
  155. "related_by_senders": [self.second_user_id],
  156. "related_by_rel_types": [RelationTypes.ANNOTATION],
  157. }
  158. chunk = self._filter_messages(filter)
  159. self.assertEqual(len(chunk), 1, chunk)
  160. self.assertEqual(chunk[0].event_id, self.event_id_1)
  161. def test_duplicate_relation(self):
  162. """An event should only be returned once if there are multiple relations to it."""
  163. self.helper.send_event(
  164. room_id=self.room_id,
  165. type="m.reaction",
  166. content={
  167. "m.relates_to": {
  168. "rel_type": RelationTypes.ANNOTATION,
  169. "event_id": self.event_id_1,
  170. "key": "A",
  171. }
  172. },
  173. tok=self.second_tok,
  174. )
  175. filter = {"related_by_senders": [self.second_user_id]}
  176. chunk = self._filter_messages(filter)
  177. self.assertEqual(len(chunk), 1, chunk)
  178. self.assertEqual(chunk[0].event_id, self.event_id_1)