Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Allow modules to create and send events into rooms #8479

Merged
merged 16 commits into from
Oct 9, 2020
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 5 additions & 33 deletions synapse/module_api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

from twisted.internet import defer

from synapse.api.constants import EventTypes
from synapse.events import EventBase
from synapse.http.client import SimpleHttpClient
from synapse.http.site import SynapseRequest
Expand Down Expand Up @@ -322,22 +321,12 @@ def get_state_events_in_room(
state = yield defer.ensureDeferred(self._store.get_events(state_ids.values()))
return state.values()

async def create_and_send_event_into_room(
self,
sender: str,
room_id: str,
event_type: str,
content: JsonDict,
state_key: Optional[str] = None,
) -> EventBase:
"""Create and send an event into a room.
async def create_and_send_event_into_room(self, event_dict: JsonDict) -> EventBase:
"""Create and send an event into a room. Membership events are currently not supported.

Args:
sender: The user ID of the event sender.
room_id: The ID of the room to send the event into.
event_type: The type of the event.
content: The event content.
state_key: The event's state key. None if this is not a state event.
event_dict: A dictionary representing the event to send.
Required keys are `type`, `room_id`, `sender` and `content`.

Returns:
The event that was sent. If state event deduplication happened, then
Expand All @@ -346,25 +335,8 @@ async def create_and_send_event_into_room(
Raises:
SynapseError if the event was not allowed.
"""
if event_type == EventTypes.Member:
raise Exception(
"Synapse modules are not currently able to send m.room.member events"
)

# Build event dictionary
event_dict = {
"type": event_type,
"content": content,
"room_id": room_id,
"sender": sender,
}

# Check whether this is intended to be a state event
if state_key is not None:
event_dict["state_key"] = state_key

# Create a requester object
requester = create_requester(sender)
requester = create_requester(event_dict["sender"])

# Create and send the event
(
Expand Down