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

Implement knock feature #6739

Merged
merged 129 commits into from
Jun 9, 2021
Merged
Show file tree
Hide file tree
Changes from 128 commits
Commits
Show all changes
129 commits
Select commit Hold shift + click to select a range
079e995
Add new experimental room version for knocking
anoadragon453 Oct 5, 2020
f1f5fa7
Add `xyz.amorgan.knock` /versions string
anoadragon453 Oct 7, 2020
b81617e
Update the event auth rules for knocking
anoadragon453 Nov 10, 2020
25a341d
Add CS /_matrix/client/r0/knock/{roomIdOrAlias} endpoint
anoadragon453 Nov 10, 2020
66e263b
Add room_knock_state_types config option
anoadragon453 Nov 2, 2020
50998c7
Federation: make_knock and send_knock implementations
anoadragon453 Nov 11, 2020
eea7db9
Send stripped state events back to the knocking homeserver
anoadragon453 Nov 11, 2020
41f5490
Extend sync to inform clients about the progress of their knocks
anoadragon453 Nov 12, 2020
8e443dd
Auto-add displaynames to knock events if they're missing
anoadragon453 Nov 12, 2020
082f04b
Add some handy db methods for knocking
anoadragon453 Nov 12, 2020
e46ceb1
Implement locally rescinding a federated knock
anoadragon453 Nov 12, 2020
56cde8b
Add knock membership events to stats generation
anoadragon453 Nov 12, 2020
8e0ac8c
Changelog
anoadragon453 Nov 12, 2020
392b315
Update changelog to include link to MSC
anoadragon453 Nov 23, 2020
665e863
Update documentation comment for config option room_invite_state_types
anoadragon453 Nov 23, 2020
f08a20b
Move default_room_state_types to a module-level constant
anoadragon453 Nov 23, 2020
f8fa8fa
Explain and rename is_invite
anoadragon453 Nov 23, 2020
aa66525
Add docstrings to FederationServer knock methods
anoadragon453 Nov 23, 2020
d4960c8
context -> room_id
anoadragon453 Nov 23, 2020
51f6c5d
Remove old TODO
anoadragon453 Nov 23, 2020
98f96ec
Abstract method _remote_knock -> remote_knock
anoadragon453 Nov 23, 2020
9560d0a
Use get_domain_from_id instead of manually parsing
anoadragon453 Nov 23, 2020
e427bd2
Add some missing documented return types
anoadragon453 Nov 23, 2020
8cec504
Apply suggestions from code review
anoadragon453 Nov 23, 2020
162677b
Fix locally rescinding knocks on worker setups
anoadragon453 Nov 24, 2020
4b36862
Fix tense on newly_joined_or_invited_or_knocking_users
anoadragon453 Nov 24, 2020
ebc683a
Inline TransactionRestServlet
anoadragon453 Nov 24, 2020
a630222
lint
anoadragon453 Nov 24, 2020
a34be45
Create and user parse_list_from_args, add docstring and types to pars…
anoadragon453 Nov 24, 2020
9090465
Remove knock unstable_features
anoadragon453 Nov 24, 2020
ad929c3
Optimise retrieving previous invite or knock event during membership …
anoadragon453 Nov 24, 2020
42ecc28
Modify knock join_rules and membership states to use unstable prefix
anoadragon453 Nov 24, 2020
7f65f01
Update endpoint to use unstable prefix
anoadragon453 Nov 24, 2020
a49cd83
Update synapse/http/servlet.py
anoadragon453 Nov 24, 2020
c097c52
Apply suggestions from code review
anoadragon453 Nov 24, 2020
9337536
Merge branch 'develop' of github.com:matrix-org/synapse into soru/knock
anoadragon453 Nov 24, 2020
ab934b4
Fix FederationV2SendKnockServlet naming
anoadragon453 Nov 24, 2020
16da425
Remove config option room_knock_state_types
anoadragon453 Nov 24, 2020
acdfd4b
Update send_knock federation endpoint to v2
anoadragon453 Nov 24, 2020
afe242c
Remove event = pdu silliness
anoadragon453 Nov 24, 2020
7f3c188
Correct invite.membership == Membership.KNOCK typo
anoadragon453 Nov 24, 2020
5d3365e
Fix feturn value of RoomMemberWorkerHandler.remote_knock
anoadragon453 Nov 24, 2020
72d16b0
Add Matrix.org Foundation copyright to synapse/handlers/stats.py
anoadragon453 Nov 24, 2020
3fb055c
Remove unnecessary arguments from knocking _serialize_payload functions
anoadragon453 Nov 24, 2020
13b8e4a
Use unstable prefix on fed endpoints, fix send_knock v2 path
anoadragon453 Nov 25, 2020
8e43e63
Change CS and Federation endpoints to use unstable prefixes
anoadragon453 Nov 25, 2020
8604861
Speed up remote invite rejection database call (#8815)
anoadragon453 Nov 25, 2020
280eed3
Use get_local_current_membership_for_user_in_room for knock code
anoadragon453 Nov 26, 2020
d2cec48
Merge branch 'develop' of github.com:matrix-org/synapse into soru/knock
anoadragon453 Nov 26, 2020
50354ef
Prevent repeated knocks on a room
anoadragon453 Nov 26, 2020
7c09796
Revert "Remove unnecessary arguments from knocking _serialize_payload…
anoadragon453 Nov 26, 2020
510da34
Prevent invite->knock membership changes
anoadragon453 Nov 27, 2020
d73ea0c
Merge branch 'develop' of github.com:matrix-org/synapse into soru/knock
anoadragon453 Nov 27, 2020
c1f4b62
Make sure we actually include stripped room state for knocking users
anoadragon453 Nov 27, 2020
338861f
/unstable/*_xyz.amorgan.knock/ -> /unstable/xyz.amorgan.knock/*
anoadragon453 Nov 30, 2020
0c4bdb0
Prevent modification of the unsigned dict where we store knock room s…
anoadragon453 Nov 30, 2020
d394eb9
Provide reasoning for appending the knock to knock_state
anoadragon453 Nov 30, 2020
0f62e2d
Allow specifying room version in RestHelper.create_room_as. Add typing
anoadragon453 Dec 1, 2020
60466b8
make sure to include stripped state events for local knocks as well!
anoadragon453 Dec 1, 2020
aaa6c88
Add unit tests for knocking
anoadragon453 Dec 1, 2020
fa66189
Use an OrderedDict for knock room state
anoadragon453 Dec 2, 2020
220ec91
AssertEquals calls AssertDictEqual when acting on dicts
anoadragon453 Dec 2, 2020
328a274
Use an assertIn instead of raising an Exception
anoadragon453 Dec 2, 2020
06aa174
Remove unnecessary parameter
anoadragon453 Dec 3, 2020
8dcc9f8
Add tests for federated stripped room state. test only some room stat…
anoadragon453 Dec 3, 2020
51db6d3
Add license to test_knocking.py
anoadragon453 Dec 3, 2020
48bd084
Apply suggestions from code review
anoadragon453 Dec 3, 2020
65c0478
Explain the purpose behind each step in 'prepare'
anoadragon453 Dec 3, 2020
0353288
Replace hardcoded join rule with constant
anoadragon453 Dec 3, 2020
c9a5eb7
Replace instances of KNOCK_UNSTABLE_IDENTIFIER being used for members…
anoadragon453 Dec 3, 2020
a718381
Use a mixin instead of passing 'self' in to static methods
anoadragon453 Dec 3, 2020
fb90611
Move KnockingStrippedStateEventHelperMixin to the top of test_knocking
anoadragon453 Dec 3, 2020
c074132
Remove unnecessary check for excess knock state, and remove top-level…
anoadragon453 Dec 3, 2020
e1ddbe2
Extract values from dictionary in py3.5-friendly manner
anoadragon453 Dec 4, 2020
4047067
Add config option to enable experimental knocking support
anoadragon453 Dec 17, 2020
fbcf963
Only support knocking room version if knocking is enabled
anoadragon453 Dec 17, 2020
abff213
Add config flag to tests
anoadragon453 Dec 17, 2020
5362b49
Remove unnecessary homeserver setup steps
anoadragon453 Dec 17, 2020
671ed9b
Merge branch 'develop' of github.com:matrix-org/synapse into HEAD
anoadragon453 Dec 30, 2020
69a820e
Fix calls to make_request in knocking tests
anoadragon453 Dec 17, 2020
6bdf465
Fix FederationKnockingTestCase so that it no longer overrides event_a…
anoadragon453 Dec 30, 2020
8b4ca64
Merge branch 'develop' of github.com:matrix-org/synapse into soru/kno…
anoadragon453 Dec 30, 2020
604a4f9
Merge branch 'develop' of github.com:matrix-org/synapse into soru/knock
anoadragon453 Jan 18, 2021
3dffb77
Merge branch 'develop' of github.com:matrix-org/synapse into soru/knock
anoadragon453 Jan 18, 2021
785c7d0
Merge branch 'develop' of github.com:matrix-org/synapse into soru/knock
anoadragon453 Jan 19, 2021
8cf92f6
Merge branch 'develop' of github.com:matrix-org/synapse into HEAD
anoadragon453 Jan 22, 2021
5c220df
Merge branch 'develop' of github.com:matrix-org/synapse into soru/knock
anoadragon453 Feb 9, 2021
91802c2
Update config for enabling msc2403 to use experimental config section…
anoadragon453 Feb 9, 2021
474a38b
Send a ver query parameter for make_knock
anoadragon453 Feb 17, 2021
c34de4c
Merge branch 'develop' of github.com:matrix-org/synapse into soru/knock
anoadragon453 Feb 17, 2021
09e444e
Lint with black==20.8b1
anoadragon453 Feb 17, 2021
1475f2b
Decode room version query parameters
anoadragon453 Feb 17, 2021
4250f50
Use parse_list_from_args instead of manually parsing
anoadragon453 Feb 17, 2021
c05b150
Update knocking test to send ver query parameter
anoadragon453 Feb 17, 2021
b055a30
Merge branch 'develop' of github.com:matrix-org/synapse into soru/knock
anoadragon453 Feb 26, 2021
44b5026
Allow knock->knock transitions
anoadragon453 Feb 26, 2021
5c6aee7
Copyrig**n**t -> Copyright
anoadragon453 Mar 29, 2021
d778460
Merge branch 'develop' of github.com:matrix-org/synapse into soru/knock
anoadragon453 Apr 1, 2021
37a674d
Some fixes off the back of the merge
anoadragon453 Apr 1, 2021
6c72538
Standardise room version variable names
anoadragon453 Apr 1, 2021
024230c
Use room_prejoin_state types
anoadragon453 Apr 1, 2021
e7f250a
update schema version of table migration
anoadragon453 Apr 1, 2021
e23cdf7
Merge branch 'develop' of github.com:matrix-org/synapse into soru/knock
anoadragon453 Apr 6, 2021
44ce919
Merge branch 'develop' of github.com:matrix-org/synapse into soru/knock
anoadragon453 Jun 4, 2021
c838fd4
Add m.room.create as an expected event type to stripped state
anoadragon453 Jun 4, 2021
cfccaf4
Persist incoming knock event in addition to computing its context
anoadragon453 Jun 7, 2021
98367ba
Fix test after do_auth function rename
anoadragon453 Jun 7, 2021
ebcdd0d
Apply suggestions from code review
anoadragon453 Jun 8, 2021
662fe98
Apply suggestions from code review
anoadragon453 Jun 8, 2021
7932671
send_knock_v2 -> v1
anoadragon453 Jun 8, 2021
5d27f53
fix type hint
anoadragon453 Jun 8, 2021
4d1462f
Merge branch 'develop' of github.com:matrix-org/synapse into soru/knock
anoadragon453 Jun 8, 2021
56d84e5
Fix type hints on request.args
anoadragon453 Jun 8, 2021
2fa4274
Remove room_stats_historical database alteration
anoadragon453 Jun 8, 2021
3d1b132
Merge branch 'develop' of github.com:matrix-org/synapse into soru/knock
anoadragon453 Jun 8, 2021
8f24db5
check_sigs_and_hashes -> check_sigs_and_hash
anoadragon453 Jun 8, 2021
01457e6
lint
anoadragon453 Jun 8, 2021
1576f23
Restrict event_auth knock checks to room versions that support knocking
anoadragon453 Jun 8, 2021
813fc24
Generalise message for incompatible room version over federation
anoadragon453 Jun 8, 2021
bea438e
Room version checks for knocking-related actions
anoadragon453 Jun 8, 2021
277e952
Consolidate copyright headers
anoadragon453 Jun 9, 2021
2f73e32
Guard another check behind experimental knocking config option
anoadragon453 Jun 9, 2021
1db05ad
Remove try/except on KeyError for query parameter parsing
anoadragon453 Jun 9, 2021
9c034ef
Remove unnecessary coding: utf-8 lines
anoadragon453 Jun 9, 2021
a2bd345
lint
anoadragon453 Jun 9, 2021
0294248
Add knocked_members column back to room_stats_historical
anoadragon453 Jun 9, 2021
c5788b8
Nuke remaining coding: utf-8 line
anoadragon453 Jun 9, 2021
15c23c4
Ensure we check that we currently support the desired room version du…
anoadragon453 Jun 9, 2021
da85886
Don't accidentally use a RoomVersion object where we want a str
anoadragon453 Jun 9, 2021
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
1 change: 1 addition & 0 deletions changelog.d/6739.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Implement "room knocking" as per [MSC2403](https://github.com/matrix-org/matrix-doc/pull/2403). Contributed by Sorunome and anoa.
4 changes: 2 additions & 2 deletions synapse/api/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class Membership:

INVITE = "invite"
JOIN = "join"
KNOCK = "knock"
KNOCK = "xyz.amorgan.knock"
clokep marked this conversation as resolved.
Show resolved Hide resolved
LEAVE = "leave"
BAN = "ban"
LIST = (INVITE, JOIN, KNOCK, LEAVE, BAN)
Expand All @@ -58,7 +58,7 @@ class PresenceState:

class JoinRules:
PUBLIC = "public"
KNOCK = "knock"
KNOCK = "xyz.amorgan.knock"
INVITE = "invite"
PRIVATE = "private"
# As defined for MSC3083.
Expand Down
2 changes: 1 addition & 1 deletion synapse/api/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,7 @@ def __init__(self, room_version: str):
super().__init__(
code=400,
msg="Your homeserver does not support the features required to "
"join this room",
"interact with this room",
errcode=Codes.INCOMPATIBLE_ROOM_VERSION,
)

Expand Down
27 changes: 26 additions & 1 deletion synapse/api/room_versions.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class RoomVersion:
state_res = attr.ib(type=int) # one of the StateResolutionVersions
enforce_key_validity = attr.ib(type=bool)

# Before MSC2261/MSC2432, m.room.aliases had special auth rules and redaction rules
# Before MSC2432, m.room.aliases had special auth rules and redaction rules
special_case_aliases_auth = attr.ib(type=bool)
# Strictly enforce canonicaljson, do not allow:
# * Integers outside the range of [-2 ^ 53 + 1, 2 ^ 53 - 1]
Expand All @@ -70,6 +70,9 @@ class RoomVersion:
msc2176_redaction_rules = attr.ib(type=bool)
# MSC3083: Support the 'restricted' join_rule.
msc3083_join_rules = attr.ib(type=bool)
# MSC2403: Allows join_rules to be set to 'knock', changes auth rules to allow sending
# m.room.membership event with membership 'knock'.
msc2403_knocking = attr.ib(type=bool)


class RoomVersions:
Expand All @@ -84,6 +87,7 @@ class RoomVersions:
limit_notifications_power_levels=False,
msc2176_redaction_rules=False,
msc3083_join_rules=False,
msc2403_knocking=False,
)
V2 = RoomVersion(
"2",
Expand All @@ -96,6 +100,7 @@ class RoomVersions:
limit_notifications_power_levels=False,
msc2176_redaction_rules=False,
msc3083_join_rules=False,
msc2403_knocking=False,
)
V3 = RoomVersion(
"3",
Expand All @@ -108,6 +113,7 @@ class RoomVersions:
limit_notifications_power_levels=False,
msc2176_redaction_rules=False,
msc3083_join_rules=False,
msc2403_knocking=False,
)
V4 = RoomVersion(
"4",
Expand All @@ -120,6 +126,7 @@ class RoomVersions:
limit_notifications_power_levels=False,
msc2176_redaction_rules=False,
msc3083_join_rules=False,
msc2403_knocking=False,
)
V5 = RoomVersion(
"5",
Expand All @@ -132,6 +139,7 @@ class RoomVersions:
limit_notifications_power_levels=False,
msc2176_redaction_rules=False,
msc3083_join_rules=False,
msc2403_knocking=False,
)
V6 = RoomVersion(
"6",
Expand All @@ -144,6 +152,7 @@ class RoomVersions:
limit_notifications_power_levels=True,
msc2176_redaction_rules=False,
msc3083_join_rules=False,
msc2403_knocking=False,
)
MSC2176 = RoomVersion(
"org.matrix.msc2176",
Expand All @@ -156,6 +165,7 @@ class RoomVersions:
limit_notifications_power_levels=True,
msc2176_redaction_rules=True,
msc3083_join_rules=False,
msc2403_knocking=False,
)
MSC3083 = RoomVersion(
"org.matrix.msc3083",
Expand All @@ -168,6 +178,20 @@ class RoomVersions:
limit_notifications_power_levels=True,
msc2176_redaction_rules=False,
msc3083_join_rules=True,
msc2403_knocking=False,
)
MSC2403 = RoomVersion(
"xyz.amorgan.knock",
RoomDisposition.UNSTABLE,
EventFormatVersions.V3,
StateResolutionVersions.V2,
enforce_key_validity=True,
special_case_aliases_auth=False,
strict_canonicaljson=True,
limit_notifications_power_levels=True,
msc2176_redaction_rules=False,
msc3083_join_rules=False,
msc2403_knocking=True,
)


Expand All @@ -183,4 +207,5 @@ class RoomVersions:
RoomVersions.MSC2176,
RoomVersions.MSC3083,
)
# Note that we do not include MSC2043 here unless it is enabled in the config.
} # type: Dict[str, RoomVersion]
11 changes: 8 additions & 3 deletions synapse/appservice/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

from prometheus_client import Counter

from synapse.api.constants import EventTypes, ThirdPartyEntityKind
from synapse.api.constants import EventTypes, Membership, ThirdPartyEntityKind
from synapse.api.errors import CodeMessageException
from synapse.events import EventBase
from synapse.events.utils import serialize_event
Expand Down Expand Up @@ -247,9 +247,14 @@ def _serialize(self, service, events):
e,
time_now,
as_client_event=True,
is_invite=(
# If this is an invite or a knock membership event, and we're interested
# in this user, then include any stripped state alongside the event.
include_stripped_room_state=(
e.type == EventTypes.Member
and e.membership == "invite"
and (
e.membership == Membership.INVITE
or e.membership == Membership.KNOCK
)
and service.is_interested_in_user(e.state_key)
),
)
Expand Down
1 change: 0 additions & 1 deletion synapse/config/account_validity.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
# Copyright 2020 The Matrix.org Foundation C.I.C.
#
# Licensed under the Apache License, Version 2.0 (the "License");
Expand Down
7 changes: 7 additions & 0 deletions synapse/config/experimental.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from synapse.api.room_versions import KNOWN_ROOM_VERSIONS, RoomVersions
from synapse.config._base import Config
from synapse.types import JsonDict

Expand All @@ -29,3 +30,9 @@ def read_config(self, config: JsonDict, **kwargs):

# MSC3026 (busy presence state)
self.msc3026_enabled = experimental.get("msc3026_enabled", False) # type: bool

# MSC2403 (room knocking)
self.msc2403_enabled = experimental.get("msc2403_enabled", False) # type: bool
if self.msc2403_enabled:
# Enable the MSC2403 unstable room version
KNOWN_ROOM_VERSIONS[RoomVersions.MSC2403.identifier] = RoomVersions.MSC2403
33 changes: 29 additions & 4 deletions synapse/event_auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ def check(
if logger.isEnabledFor(logging.DEBUG):
logger.debug("Auth events: %s", [a.event_id for a in auth_events.values()])

# 5. If type is m.room.membership
if event.type == EventTypes.Member:
_is_membership_change_allowed(room_version_obj, event, auth_events)
logger.debug("Allowing! %s", event)
Expand Down Expand Up @@ -257,6 +258,11 @@ def _is_membership_change_allowed(

caller_in_room = caller and caller.membership == Membership.JOIN
caller_invited = caller and caller.membership == Membership.INVITE
caller_knocked = (
caller
and room_version.msc2403_knocking
and caller.membership == Membership.KNOCK
)

# get info about the target
key = (EventTypes.Member, target_user_id)
Expand All @@ -283,6 +289,7 @@ def _is_membership_change_allowed(
{
"caller_in_room": caller_in_room,
"caller_invited": caller_invited,
"caller_knocked": caller_knocked,
"target_banned": target_banned,
"target_in_room": target_in_room,
"membership": membership,
Expand All @@ -299,9 +306,14 @@ def _is_membership_change_allowed(
raise AuthError(403, "%s is banned from the room" % (target_user_id,))
return

if Membership.JOIN != membership:
# Require the user to be in the room for membership changes other than join/knock.
if Membership.JOIN != membership and (
RoomVersion.msc2403_knocking and Membership.KNOCK != membership
):
# If the user has been invited or has knocked, they are allowed to change their
# membership event to leave
if (
caller_invited
(caller_invited or caller_knocked)
and Membership.LEAVE == membership
and target_user_id == event.user_id
):
Expand Down Expand Up @@ -339,7 +351,9 @@ def _is_membership_change_allowed(
and join_rule == JoinRules.MSC3083_RESTRICTED
):
pass
elif join_rule == JoinRules.INVITE:
elif join_rule == JoinRules.INVITE or (
room_version.msc2403_knocking and join_rule == JoinRules.KNOCK
):
if not caller_in_room and not caller_invited:
raise AuthError(403, "You are not invited to this room.")
else:
Expand All @@ -358,6 +372,17 @@ def _is_membership_change_allowed(
elif Membership.BAN == membership:
if user_level < ban_level or user_level <= target_level:
raise AuthError(403, "You don't have permission to ban")
elif room_version.msc2403_knocking and Membership.KNOCK == membership:
if join_rule != JoinRules.KNOCK:
raise AuthError(403, "You don't have permission to knock")
elif target_user_id != event.user_id:
raise AuthError(403, "You cannot knock for other users")
elif target_in_room:
raise AuthError(403, "You cannot knock on a room you are already in")
elif caller_invited:
raise AuthError(403, "You are already invited to this room")
elif target_banned:
raise AuthError(403, "You are banned from this room")
else:
raise AuthError(500, "Unknown membership %s" % membership)

Expand Down Expand Up @@ -718,7 +743,7 @@ def auth_types_for_event(event: EventBase) -> Set[Tuple[str, str]]:

if event.type == EventTypes.Member:
membership = event.content["membership"]
if membership in [Membership.JOIN, Membership.INVITE]:
if membership in [Membership.JOIN, Membership.INVITE, Membership.KNOCK]:
auth_types.add((EventTypes.JoinRules, ""))

auth_types.add((EventTypes.Member, event.state_key))
Expand Down
19 changes: 12 additions & 7 deletions synapse/events/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,7 @@ def format_event_for_client_v1(d):
"replaces_state",
"prev_content",
"invite_room_state",
"knock_room_state",
)
for key in copy_keys:
if key in d["unsigned"]:
Expand Down Expand Up @@ -278,7 +279,7 @@ def serialize_event(
event_format=format_event_for_client_v1,
token_id=None,
only_event_fields=None,
is_invite=False,
include_stripped_room_state=False,
):
"""Serialize event for clients

Expand All @@ -289,8 +290,10 @@ def serialize_event(
event_format
token_id
only_event_fields
is_invite (bool): Whether this is an invite that is being sent to the
invitee
include_stripped_room_state (bool): Some events can have stripped room state
stored in the `unsigned` field. This is required for invite and knock
functionality. If this option is False, that state will be removed from the
event before it is returned. Otherwise, it will be kept.

Returns:
dict
Expand Down Expand Up @@ -322,11 +325,13 @@ def serialize_event(
if txn_id is not None:
d["unsigned"]["transaction_id"] = txn_id

# If this is an invite for somebody else, then we don't care about the
# invite_room_state as that's meant solely for the invitee. Other clients
# will already have the state since they're in the room.
if not is_invite:
# invite_room_state and knock_room_state are a list of stripped room state events
# that are meant to provide metadata about a room to an invitee/knocker. They are
# intended to only be included in specific circumstances, such as down sync, and
# should not be included in any other case.
if not include_stripped_room_state:
d["unsigned"].pop("invite_room_state", None)
d["unsigned"].pop("knock_room_state", None)

if as_client_event:
d = event_format(d)
Expand Down
Loading