event_injection.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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 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. from synapse.types import Collection
  23. """
  24. Utility functions for poking events into the storage of the server under test.
  25. """
  26. async def inject_member_event(
  27. hs: synapse.server.HomeServer,
  28. room_id: str,
  29. sender: str,
  30. membership: str,
  31. target: Optional[str] = None,
  32. extra_content: Optional[dict] = None,
  33. **kwargs
  34. ) -> EventBase:
  35. """Inject a membership event into a room."""
  36. if target is None:
  37. target = sender
  38. content = {"membership": membership}
  39. if extra_content:
  40. content.update(extra_content)
  41. return await inject_event(
  42. hs,
  43. room_id=room_id,
  44. type=EventTypes.Member,
  45. sender=sender,
  46. state_key=target,
  47. content=content,
  48. **kwargs
  49. )
  50. async def inject_event(
  51. hs: synapse.server.HomeServer,
  52. room_version: Optional[str] = None,
  53. prev_event_ids: Optional[Collection[str]] = None,
  54. **kwargs
  55. ) -> EventBase:
  56. """Inject a generic event into a room
  57. Args:
  58. hs: the homeserver under test
  59. room_version: the version of the room we're inserting into.
  60. if not specified, will be looked up
  61. prev_event_ids: prev_events for the event. If not specified, will be looked up
  62. kwargs: fields for the event to be created
  63. """
  64. event, context = await create_event(hs, room_version, prev_event_ids, **kwargs)
  65. await hs.get_storage().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[Collection[str]] = None,
  71. **kwargs
  72. ) -> Tuple[EventBase, EventContext]:
  73. if room_version is None:
  74. room_version = await hs.get_datastore().get_room_version_id(kwargs["room_id"])
  75. builder = hs.get_event_builder_factory().for_room_version(
  76. KNOWN_ROOM_VERSIONS[room_version], kwargs
  77. )
  78. event, context = await hs.get_event_creation_handler().create_new_client_event(
  79. builder, prev_event_ids=prev_event_ids
  80. )
  81. return event, context