test_stream.py 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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()
  99. )
  100. events, next_key = self.get_success(
  101. self.hs.get_datastore().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 = {"io.element.relation_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 = {"io.element.relation_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 = {
  124. "io.element.relation_senders": [self.second_user_id, self.third_user_id]
  125. }
  126. chunk = self._filter_messages(filter)
  127. self.assertEqual(len(chunk), 2, chunk)
  128. self.assertCountEqual(
  129. [c.event_id for c in chunk], [self.event_id_1, self.event_id_2]
  130. )
  131. def test_filter_relation_type(self):
  132. # Messages which have annotations.
  133. filter = {"io.element.relation_types": [RelationTypes.ANNOTATION]}
  134. chunk = self._filter_messages(filter)
  135. self.assertEqual(len(chunk), 1, chunk)
  136. self.assertEqual(chunk[0].event_id, self.event_id_1)
  137. # Messages which have references.
  138. filter = {"io.element.relation_types": [RelationTypes.REFERENCE]}
  139. chunk = self._filter_messages(filter)
  140. self.assertEqual(len(chunk), 1, chunk)
  141. self.assertEqual(chunk[0].event_id, self.event_id_2)
  142. # Messages which have either annotations or references.
  143. filter = {
  144. "io.element.relation_types": [
  145. RelationTypes.ANNOTATION,
  146. RelationTypes.REFERENCE,
  147. ]
  148. }
  149. chunk = self._filter_messages(filter)
  150. self.assertEqual(len(chunk), 2, chunk)
  151. self.assertCountEqual(
  152. [c.event_id for c in chunk], [self.event_id_1, self.event_id_2]
  153. )
  154. def test_filter_relation_senders_and_type(self):
  155. # Messages which second user reacted to.
  156. filter = {
  157. "io.element.relation_senders": [self.second_user_id],
  158. "io.element.relation_types": [RelationTypes.ANNOTATION],
  159. }
  160. chunk = self._filter_messages(filter)
  161. self.assertEqual(len(chunk), 1, chunk)
  162. self.assertEqual(chunk[0].event_id, self.event_id_1)
  163. def test_duplicate_relation(self):
  164. """An event should only be returned once if there are multiple relations to it."""
  165. self.helper.send_event(
  166. room_id=self.room_id,
  167. type="m.reaction",
  168. content={
  169. "m.relates_to": {
  170. "rel_type": RelationTypes.ANNOTATION,
  171. "event_id": self.event_id_1,
  172. "key": "A",
  173. }
  174. },
  175. tok=self.second_tok,
  176. )
  177. filter = {"io.element.relation_senders": [self.second_user_id]}
  178. chunk = self._filter_messages(filter)
  179. self.assertEqual(len(chunk), 1, chunk)
  180. self.assertEqual(chunk[0].event_id, self.event_id_1)