test_stream.py 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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 twisted.test.proto_helpers import MemoryReactor
  16. from synapse.api.constants import EventTypes, RelationTypes
  17. from synapse.api.filtering import Filter
  18. from synapse.rest import admin
  19. from synapse.rest.client import login, room
  20. from synapse.server import HomeServer
  21. from synapse.types import JsonDict
  22. from synapse.util import Clock
  23. from tests.unittest import HomeserverTestCase
  24. class PaginationTestCase(HomeserverTestCase):
  25. """
  26. Test the pre-filtering done in the pagination code.
  27. This is similar to some of the tests in tests.rest.client.test_rooms but here
  28. we ensure that the filtering done in the database is applied successfully.
  29. """
  30. servlets = [
  31. admin.register_servlets_for_client_rest_resource,
  32. room.register_servlets,
  33. login.register_servlets,
  34. ]
  35. def default_config(self) -> JsonDict:
  36. config = super().default_config()
  37. config["experimental_features"] = {"msc3874_enabled": True}
  38. return config
  39. def prepare(
  40. self, reactor: MemoryReactor, clock: Clock, homeserver: HomeServer
  41. ) -> None:
  42. self.user_id = self.register_user("test", "test")
  43. self.tok = self.login("test", "test")
  44. self.room_id = self.helper.create_room_as(self.user_id, tok=self.tok)
  45. self.second_user_id = self.register_user("second", "test")
  46. self.second_tok = self.login("second", "test")
  47. self.helper.join(
  48. room=self.room_id, user=self.second_user_id, tok=self.second_tok
  49. )
  50. self.third_user_id = self.register_user("third", "test")
  51. self.third_tok = self.login("third", "test")
  52. self.helper.join(room=self.room_id, user=self.third_user_id, tok=self.third_tok)
  53. # Store a token which is after all the room creation events.
  54. self.from_token = self.get_success(
  55. self.hs.get_event_sources().get_current_token_for_pagination(self.room_id)
  56. )
  57. # An initial event with a relation from second user.
  58. res = self.helper.send_event(
  59. room_id=self.room_id,
  60. type=EventTypes.Message,
  61. content={"msgtype": "m.text", "body": "Message 1"},
  62. tok=self.tok,
  63. )
  64. self.event_id_1 = res["event_id"]
  65. res = self.helper.send_event(
  66. room_id=self.room_id,
  67. type="m.reaction",
  68. content={
  69. "m.relates_to": {
  70. "rel_type": RelationTypes.ANNOTATION,
  71. "event_id": self.event_id_1,
  72. "key": "👍",
  73. }
  74. },
  75. tok=self.second_tok,
  76. )
  77. self.event_id_annotation = res["event_id"]
  78. # Another event with a relation from third user.
  79. res = self.helper.send_event(
  80. room_id=self.room_id,
  81. type=EventTypes.Message,
  82. content={"msgtype": "m.text", "body": "Message 2"},
  83. tok=self.tok,
  84. )
  85. self.event_id_2 = res["event_id"]
  86. res = self.helper.send_event(
  87. room_id=self.room_id,
  88. type="m.reaction",
  89. content={
  90. "m.relates_to": {
  91. "rel_type": RelationTypes.REFERENCE,
  92. "event_id": self.event_id_2,
  93. }
  94. },
  95. tok=self.third_tok,
  96. )
  97. self.event_id_reference = res["event_id"]
  98. # An event with no relations.
  99. res = self.helper.send_event(
  100. room_id=self.room_id,
  101. type=EventTypes.Message,
  102. content={"msgtype": "m.text", "body": "No relations"},
  103. tok=self.tok,
  104. )
  105. self.event_id_none = res["event_id"]
  106. def _filter_messages(self, filter: JsonDict) -> List[str]:
  107. """Make a request to /messages with a filter, returns the chunk of events."""
  108. events, next_key = self.get_success(
  109. self.hs.get_datastores().main.paginate_room_events(
  110. room_id=self.room_id,
  111. from_key=self.from_token.room_key,
  112. to_key=None,
  113. direction="f",
  114. limit=10,
  115. event_filter=Filter(self.hs, filter),
  116. )
  117. )
  118. return [ev.event_id for ev in events]
  119. def test_filter_relation_senders(self) -> None:
  120. # Messages which second user reacted to.
  121. filter = {"related_by_senders": [self.second_user_id]}
  122. chunk = self._filter_messages(filter)
  123. self.assertEqual(chunk, [self.event_id_1])
  124. # Messages which third user reacted to.
  125. filter = {"related_by_senders": [self.third_user_id]}
  126. chunk = self._filter_messages(filter)
  127. self.assertEqual(chunk, [self.event_id_2])
  128. # Messages which either user reacted to.
  129. filter = {"related_by_senders": [self.second_user_id, self.third_user_id]}
  130. chunk = self._filter_messages(filter)
  131. self.assertCountEqual(chunk, [self.event_id_1, self.event_id_2])
  132. def test_filter_relation_type(self) -> None:
  133. # Messages which have annotations.
  134. filter = {"related_by_rel_types": [RelationTypes.ANNOTATION]}
  135. chunk = self._filter_messages(filter)
  136. self.assertEqual(chunk, [self.event_id_1])
  137. # Messages which have references.
  138. filter = {"related_by_rel_types": [RelationTypes.REFERENCE]}
  139. chunk = self._filter_messages(filter)
  140. self.assertEqual(chunk, [self.event_id_2])
  141. # Messages which have either annotations or references.
  142. filter = {
  143. "related_by_rel_types": [
  144. RelationTypes.ANNOTATION,
  145. RelationTypes.REFERENCE,
  146. ]
  147. }
  148. chunk = self._filter_messages(filter)
  149. self.assertCountEqual(chunk, [self.event_id_1, self.event_id_2])
  150. def test_filter_relation_senders_and_type(self) -> None:
  151. # Messages which second user reacted to.
  152. filter = {
  153. "related_by_senders": [self.second_user_id],
  154. "related_by_rel_types": [RelationTypes.ANNOTATION],
  155. }
  156. chunk = self._filter_messages(filter)
  157. self.assertEqual(chunk, [self.event_id_1])
  158. def test_duplicate_relation(self) -> None:
  159. """An event should only be returned once if there are multiple relations to it."""
  160. self.helper.send_event(
  161. room_id=self.room_id,
  162. type="m.reaction",
  163. content={
  164. "m.relates_to": {
  165. "rel_type": RelationTypes.ANNOTATION,
  166. "event_id": self.event_id_1,
  167. "key": "A",
  168. }
  169. },
  170. tok=self.second_tok,
  171. )
  172. filter = {"related_by_senders": [self.second_user_id]}
  173. chunk = self._filter_messages(filter)
  174. self.assertEqual(chunk, [self.event_id_1])
  175. def test_filter_rel_types(self) -> None:
  176. # Messages which are annotations.
  177. filter = {"org.matrix.msc3874.rel_types": [RelationTypes.ANNOTATION]}
  178. chunk = self._filter_messages(filter)
  179. self.assertEqual(chunk, [self.event_id_annotation])
  180. # Messages which are references.
  181. filter = {"org.matrix.msc3874.rel_types": [RelationTypes.REFERENCE]}
  182. chunk = self._filter_messages(filter)
  183. self.assertEqual(chunk, [self.event_id_reference])
  184. # Messages which are either annotations or references.
  185. filter = {
  186. "org.matrix.msc3874.rel_types": [
  187. RelationTypes.ANNOTATION,
  188. RelationTypes.REFERENCE,
  189. ]
  190. }
  191. chunk = self._filter_messages(filter)
  192. self.assertCountEqual(
  193. chunk,
  194. [self.event_id_annotation, self.event_id_reference],
  195. )
  196. def test_filter_not_rel_types(self) -> None:
  197. # Messages which are not annotations.
  198. filter = {"org.matrix.msc3874.not_rel_types": [RelationTypes.ANNOTATION]}
  199. chunk = self._filter_messages(filter)
  200. self.assertEqual(
  201. chunk,
  202. [
  203. self.event_id_1,
  204. self.event_id_2,
  205. self.event_id_reference,
  206. self.event_id_none,
  207. ],
  208. )
  209. # Messages which are not references.
  210. filter = {"org.matrix.msc3874.not_rel_types": [RelationTypes.REFERENCE]}
  211. chunk = self._filter_messages(filter)
  212. self.assertEqual(
  213. chunk,
  214. [
  215. self.event_id_1,
  216. self.event_id_annotation,
  217. self.event_id_2,
  218. self.event_id_none,
  219. ],
  220. )
  221. # Messages which are neither annotations or references.
  222. filter = {
  223. "org.matrix.msc3874.not_rel_types": [
  224. RelationTypes.ANNOTATION,
  225. RelationTypes.REFERENCE,
  226. ]
  227. }
  228. chunk = self._filter_messages(filter)
  229. self.assertEqual(chunk, [self.event_id_1, self.event_id_2, self.event_id_none])