summary refs log tree commit diff
path: root/synapse/module_api
diff options
context:
space:
mode:
authorAndrew Morgan <1342360+anoadragon453@users.noreply.github.com>2020-10-09 13:46:36 +0100
committerAndrew Morgan <andrew@amorgan.xyz>2020-10-12 13:24:15 +0100
commit62c7b10ea5f85ddf5903b1136f1345a69ffdf8a1 (patch)
tree6c248c9cbfdca84f65b673b037e54442c5c65327 /synapse/module_api
parentOnly assert valid next_link params when provided (#65) (diff)
downloadsynapse-62c7b10ea5f85ddf5903b1136f1345a69ffdf8a1.tar.xz
Allow modules to create and send events into rooms (#8479)
This PR allows Synapse modules making use of the `ModuleApi` to create and send non-membership events into a room. This can useful to have modules send messages, or change power levels in a room etc. Note that they must send event through a user that's already in the room.

The non-membership event limitation is currently arbitrary, as it's another chunk of work and not necessary at the moment.

This commit has been cherry-picked from mainline.
Diffstat (limited to 'synapse/module_api')
-rw-r--r--synapse/module_api/__init__.py30
1 files changed, 29 insertions, 1 deletions
diff --git a/synapse/module_api/__init__.py b/synapse/module_api/__init__.py

index 0861e0cfff..a885076282 100644 --- a/synapse/module_api/__init__.py +++ b/synapse/module_api/__init__.py
@@ -18,10 +18,11 @@ from typing import TYPE_CHECKING from twisted.internet import defer +from synapse.events import EventBase from synapse.http.client import SimpleHttpClient from synapse.http.site import SynapseRequest from synapse.logging.context import make_deferred_yieldable, run_in_background -from synapse.types import UserID +from synapse.types import JsonDict, UserID, create_requester if TYPE_CHECKING: from synapse.server import HomeServer @@ -310,3 +311,30 @@ class ModuleApi(object): await self._auth_handler.complete_sso_login( registered_user_id, request, client_redirect_url, ) + + 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: + 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 + the previous, duplicate event instead. + + Raises: + SynapseError if the event was not allowed. + """ + # Create a requester object + requester = create_requester(event_dict["sender"]) + + # Create and send the event + ( + event, + _, + ) = await self._hs.get_event_creation_handler().create_and_send_nonmember_event( + requester, event_dict, ratelimit=False + ) + + return event