event_injection.py 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2018 New Vector Ltd
  3. # Copyright 2020 The Matrix.org Foundation C.I.C
  4. #
  5. # Licensed under the Apache License, Version 2.0 (the "License");
  6. # you may not use this file except in compliance with the License.
  7. # You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS,
  13. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. # See the License for the specific language governing permissions and
  15. # limitations under the License.
  16. from typing import List, Optional, Tuple
  17. import synapse.server
  18. from synapse.api.constants import EventTypes
  19. from synapse.api.room_versions import KNOWN_ROOM_VERSIONS
  20. from synapse.events import EventBase
  21. from synapse.events.snapshot import EventContext
  22. """
  23. Utility functions for poking events into the storage of the server under test.
  24. """
  25. async def inject_member_event(
  26. hs: synapse.server.HomeServer,
  27. room_id: str,
  28. sender: str,
  29. membership: str,
  30. target: Optional[str] = None,
  31. extra_content: Optional[dict] = None,
  32. **kwargs
  33. ) -> EventBase:
  34. """Inject a membership event into a room."""
  35. if target is None:
  36. target = sender
  37. content = {"membership": membership}
  38. if extra_content:
  39. content.update(extra_content)
  40. return await inject_event(
  41. hs,
  42. room_id=room_id,
  43. type=EventTypes.Member,
  44. sender=sender,
  45. state_key=target,
  46. content=content,
  47. **kwargs
  48. )
  49. async def inject_event(
  50. hs: synapse.server.HomeServer,
  51. room_version: Optional[str] = None,
  52. prev_event_ids: Optional[List[str]] = None,
  53. **kwargs
  54. ) -> EventBase:
  55. """Inject a generic event into a room
  56. Args:
  57. hs: the homeserver under test
  58. room_version: the version of the room we're inserting into.
  59. if not specified, will be looked up
  60. prev_event_ids: prev_events for the event. If not specified, will be looked up
  61. kwargs: fields for the event to be created
  62. """
  63. event, context = await create_event(hs, room_version, prev_event_ids, **kwargs)
  64. persistence = hs.get_storage().persistence
  65. assert persistence is not None
  66. await persistence.persist_event(event, context)
  67. return event
  68. async def create_event(
  69. hs: synapse.server.HomeServer,
  70. room_version: Optional[str] = None,
  71. prev_event_ids: Optional[List[str]] = None,
  72. **kwargs
  73. ) -> Tuple[EventBase, EventContext]:
  74. if room_version is None:
  75. room_version = await hs.get_datastore().get_room_version_id(kwargs["room_id"])
  76. builder = hs.get_event_builder_factory().for_room_version(
  77. KNOWN_ROOM_VERSIONS[room_version], kwargs
  78. )
  79. event, context = await hs.get_event_creation_handler().create_new_client_event(
  80. builder, prev_event_ids=prev_event_ids
  81. )
  82. return event, context