event_injection.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. # Copyright 2018 New Vector Ltd
  2. # Copyright 2020 The Matrix.org Foundation C.I.C
  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. from typing import List, Optional, Tuple
  16. import synapse.server
  17. from synapse.api.constants import EventTypes
  18. from synapse.api.room_versions import KNOWN_ROOM_VERSIONS
  19. from synapse.events import EventBase
  20. from synapse.events.snapshot import EventContext
  21. """
  22. Utility functions for poking events into the storage of the server under test.
  23. """
  24. async def inject_member_event(
  25. hs: synapse.server.HomeServer,
  26. room_id: str,
  27. sender: str,
  28. membership: str,
  29. target: Optional[str] = None,
  30. extra_content: Optional[dict] = None,
  31. **kwargs,
  32. ) -> EventBase:
  33. """Inject a membership event into a room."""
  34. if target is None:
  35. target = sender
  36. content = {"membership": membership}
  37. if extra_content:
  38. content.update(extra_content)
  39. return await inject_event(
  40. hs,
  41. room_id=room_id,
  42. type=EventTypes.Member,
  43. sender=sender,
  44. state_key=target,
  45. content=content,
  46. **kwargs,
  47. )
  48. async def inject_event(
  49. hs: synapse.server.HomeServer,
  50. room_version: Optional[str] = None,
  51. prev_event_ids: Optional[List[str]] = None,
  52. **kwargs,
  53. ) -> EventBase:
  54. """Inject a generic event into a room
  55. Args:
  56. hs: the homeserver under test
  57. room_version: the version of the room we're inserting into.
  58. if not specified, will be looked up
  59. prev_event_ids: prev_events for the event. If not specified, will be looked up
  60. kwargs: fields for the event to be created
  61. """
  62. event, context = await create_event(hs, room_version, prev_event_ids, **kwargs)
  63. persistence = hs.get_storage().persistence
  64. assert persistence is not None
  65. await persistence.persist_event(event, context)
  66. return event
  67. async def create_event(
  68. hs: synapse.server.HomeServer,
  69. room_version: Optional[str] = None,
  70. prev_event_ids: Optional[List[str]] = None,
  71. **kwargs,
  72. ) -> Tuple[EventBase, EventContext]:
  73. if room_version is None:
  74. room_version = await hs.get_datastores().main.get_room_version_id(
  75. kwargs["room_id"]
  76. )
  77. builder = hs.get_event_builder_factory().for_room_version(
  78. KNOWN_ROOM_VERSIONS[room_version], kwargs
  79. )
  80. event, context = await hs.get_event_creation_handler().create_new_client_event(
  81. builder, prev_event_ids=prev_event_ids
  82. )
  83. return event, context