Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement state resolution #22

Merged
merged 5 commits into from
Oct 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
9 changes: 9 additions & 0 deletions examples/basic-fork.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{"type": "m.room.create", "depth": 1, "hashes": {"sha256": "3zF8Kk+J/ZGrfUjfgg3OjtzMVsvOVDIC+Ew0BS3L4gY"}, "origin": "matrix.org", "sender": "@kegan:matrix.org", "content": {"creator": "@kegan:matrix.org", "room_version": "10"}, "room_id": "!jpViMZBlnuKxAKpsem:matrix.org", "event_id": "$ayBJ5cK-zydid3RXCV7OaqK-Wh9NZcpBwSqK0H5O1NY", "unsigned": {"age_ts": 1728652174091}, "state_key": "", "signatures": {"matrix.org": {"ed25519:a_RXGa": "4/m8rQEZupyPn+SyNMesB5BqNux66Tyyl/bmBKVQay3jsNZuhUdelN056kQJUhIg7HPRes17ghKifHOvaOvvAw"}}, "auth_events": [], "prev_events": [], "origin_server_ts": 1728652174091}
{"type": "m.room.member", "depth": 2, "hashes": {"sha256": "VVIP2Wxb+nyzhgcvrJD82nNQYb3Vd5ZbOOIRbu8YGP8"}, "origin": "matrix.org", "sender": "@kegan:matrix.org", "content": {"avatar_url": "mxc://matrix.org/gImPYMAYQWpffViAorQsjbIs", "membership": "join", "displayname": "Kegan"}, "room_id": "!jpViMZBlnuKxAKpsem:matrix.org", "event_id": "$dVlIkqbTnz6_RRggTuTxuYNLdAtbkVSY47Lb-1rUWIo", "unsigned": {"age_ts": 1728652174308}, "state_key": "@kegan:matrix.org", "signatures": {"matrix.org": {"ed25519:a_RXGa": "c01Fydab2/KdQ+EqVouiii+W1YGxHD4PmNn/U3A4qsF9Bwc9Y0A9iyTXMvUR4Nsz/1YfkDgchkj0/2qBJZ9ECQ"}}, "auth_events": ["$ayBJ5cK-zydid3RXCV7OaqK-Wh9NZcpBwSqK0H5O1NY"], "prev_events": ["$ayBJ5cK-zydid3RXCV7OaqK-Wh9NZcpBwSqK0H5O1NY"], "origin_server_ts": 1728652174308}
{"type": "m.room.power_levels", "depth": 3, "hashes": {"sha256": "mQxzMn0yPhfD+KxoQFIDQVQvMWX9I3ujaTShRSYYKk8"}, "origin": "matrix.org", "sender": "@kegan:matrix.org", "content": {"ban": 50, "kick": 50, "users": {"@kegan:matrix.org": 100}, "events": {"m.room.name": 50, "m.room.avatar": 50, "m.room.tombstone": 100, "m.room.encryption": 100, "m.room.server_acl": 100, "m.room.power_levels": 100, "m.room.canonical_alias": 50, "m.room.history_visibility": 100}, "invite": 50, "redact": 50, "historical": 100, "m.call.invite": 50, "state_default": 50, "users_default": 0, "events_default": 0}, "room_id": "!jpViMZBlnuKxAKpsem:matrix.org", "event_id": "$OaedSihFH9ThMiJsVKDIatVL5CAgtETWGuUUVgAjOAg", "unsigned": {"age_ts": 1728652174519}, "state_key": "", "signatures": {"matrix.org": {"ed25519:a_RXGa": "ZHbQCeWaaHl249hlVpzm2UU8/1SWtAxDa6Z/ge5Mk3NHalC4WfbWtzjLzXrhfVYkhSTG0lfV6/EDao7bPuEjBQ"}}, "auth_events": ["$dVlIkqbTnz6_RRggTuTxuYNLdAtbkVSY47Lb-1rUWIo", "$ayBJ5cK-zydid3RXCV7OaqK-Wh9NZcpBwSqK0H5O1NY"], "prev_events": ["$dVlIkqbTnz6_RRggTuTxuYNLdAtbkVSY47Lb-1rUWIo"], "origin_server_ts": 1728652174519}
{"type": "m.room.canonical_alias", "depth": 4, "hashes": {"sha256": "WWyr4BL6gRVn2PWjSQ2wYWO6hVF3OQ/vM89QxpFMRnk"}, "origin": "matrix.org", "sender": "@kegan:matrix.org", "content": {"alias": "#test-resolve-room:matrix.org"}, "room_id": "!jpViMZBlnuKxAKpsem:matrix.org", "event_id": "$aCToy0aQOfhURh5jsakQ9Ovk6fMGbWnmnGh7gkZUiFI", "unsigned": {"age_ts": 1728652174544}, "state_key": "", "signatures": {"matrix.org": {"ed25519:a_RXGa": "eji69yq5bybnpE9m5OZh2sFeaB7MZKolMhDqlunnpyegPI9VgUtPSZF4kcXZ9ZczBoLA7eC4/HbhwpCQ7c1aAg"}}, "auth_events": ["$dVlIkqbTnz6_RRggTuTxuYNLdAtbkVSY47Lb-1rUWIo", "$OaedSihFH9ThMiJsVKDIatVL5CAgtETWGuUUVgAjOAg", "$ayBJ5cK-zydid3RXCV7OaqK-Wh9NZcpBwSqK0H5O1NY"], "prev_events": ["$OaedSihFH9ThMiJsVKDIatVL5CAgtETWGuUUVgAjOAg"], "origin_server_ts": 1728652174544}
{"type": "m.room.join_rules", "depth": 5, "hashes": {"sha256": "HSYkwp9nfNTJX8zMJt+s6Q96oapIDRHyfcuVs71P58M"}, "origin": "matrix.org", "sender": "@kegan:matrix.org", "content": {"join_rule": "public"}, "room_id": "!jpViMZBlnuKxAKpsem:matrix.org", "event_id": "$HLdW9q8VIIPTpyKfF85fDYrEu8UAp861wdpUZVzYgJQ", "unsigned": {"age_ts": 1728652174545}, "state_key": "", "signatures": {"matrix.org": {"ed25519:a_RXGa": "MDPlgzj97v5QwP60fzui6il5NHGU3BbUfm/BBxeLp79/HpPD6hYlkYYeLXoqI4N1AxtbLa3XtldGPU6vzR3HCw"}}, "auth_events": ["$dVlIkqbTnz6_RRggTuTxuYNLdAtbkVSY47Lb-1rUWIo", "$OaedSihFH9ThMiJsVKDIatVL5CAgtETWGuUUVgAjOAg", "$ayBJ5cK-zydid3RXCV7OaqK-Wh9NZcpBwSqK0H5O1NY"], "prev_events": ["$aCToy0aQOfhURh5jsakQ9Ovk6fMGbWnmnGh7gkZUiFI"], "origin_server_ts": 1728652174545}
{"type": "m.room.history_visibility", "depth": 6, "hashes": {"sha256": "7jjPzCZW37vDlpTVHhpNdt2a8ZEITtR8PrduIVAFOcw"}, "origin": "matrix.org", "sender": "@kegan:matrix.org", "content": {"history_visibility": "shared"}, "room_id": "!jpViMZBlnuKxAKpsem:matrix.org", "event_id": "$iojoMzC2XDpR0YHDdmQD1p0E9S1YCj4GsC63QWIUyJY", "unsigned": {"age_ts": 1728652174545}, "state_key": "", "signatures": {"matrix.org": {"ed25519:a_RXGa": "F9yzfRxG54oiWrYVW+G0Q7SuAhK42XAIUVizd/yLRbXG91uS/SSH7ZAA4OAAah8afmWYuzWXvUgImCqziQ+KBg"}}, "auth_events": ["$dVlIkqbTnz6_RRggTuTxuYNLdAtbkVSY47Lb-1rUWIo", "$OaedSihFH9ThMiJsVKDIatVL5CAgtETWGuUUVgAjOAg", "$ayBJ5cK-zydid3RXCV7OaqK-Wh9NZcpBwSqK0H5O1NY"], "prev_events": ["$HLdW9q8VIIPTpyKfF85fDYrEu8UAp861wdpUZVzYgJQ"], "origin_server_ts": 1728652174545}
{"type": "m.room.name", "depth": 7, "hashes": {"sha256": "Cd6glFNK04BzaNqHCaL/fKqSby3oMeEYIAKD3sxa8ok"}, "origin": "matrix.org", "sender": "@kegan:matrix.org", "content": {"name": "test resolve room"}, "room_id": "!jpViMZBlnuKxAKpsem:matrix.org", "event_id": "$A5z9GLL8ABCYg4SKujf_ehtW52yGoGhZuQGc0nspEeU", "unsigned": {"age_ts": 1728652174546}, "state_key": "", "signatures": {"matrix.org": {"ed25519:a_RXGa": "ypYzuw8nG+38o6WM4OrFeEI+xwk1QvIxZBRB3RtZGtf2y6Z3VgliO7hbsYx/AjbRCstMsrme1DEV8OiMeWIYDw"}}, "auth_events": ["$dVlIkqbTnz6_RRggTuTxuYNLdAtbkVSY47Lb-1rUWIo", "$OaedSihFH9ThMiJsVKDIatVL5CAgtETWGuUUVgAjOAg", "$ayBJ5cK-zydid3RXCV7OaqK-Wh9NZcpBwSqK0H5O1NY"], "prev_events": ["$iojoMzC2XDpR0YHDdmQD1p0E9S1YCj4GsC63QWIUyJY"], "origin_server_ts": 1728652174546}
{"type": "m.room.name", "depth": 7, "hashes": {"sha256": "Cd6glFNK04BzaNqHCaL/fKqSby3oMeEYIAKD3sxa8ok"}, "origin": "matrix.org", "sender": "@kegan:matrix.org", "content": {"name": "test resolve room forked"}, "room_id": "!jpViMZBlnuKxAKpsem:matrix.org", "event_id": "$A5z9GLL8ABCYg4SKujf_ehtW52yGoGhZuQGc0nspEeU-fork", "unsigned": {"age_ts": 1728652174546}, "state_key": "", "signatures": {"matrix.org": {"ed25519:a_RXGa": "ypYzuw8nG+38o6WM4OrFeEI+xwk1QvIxZBRB3RtZGtf2y6Z3VgliO7hbsYx/AjbRCstMsrme1DEV8OiMeWIYDw"}}, "auth_events": ["$dVlIkqbTnz6_RRggTuTxuYNLdAtbkVSY47Lb-1rUWIo", "$OaedSihFH9ThMiJsVKDIatVL5CAgtETWGuUUVgAjOAg", "$ayBJ5cK-zydid3RXCV7OaqK-Wh9NZcpBwSqK0H5O1NY"], "prev_events": ["$iojoMzC2XDpR0YHDdmQD1p0E9S1YCj4GsC63QWIUyJY"], "origin_server_ts": 1728652174546}

9 changes: 9 additions & 0 deletions examples/simple.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{"type": "m.room.create", "depth": 1, "hashes": {"sha256": "3zF8Kk+J/ZGrfUjfgg3OjtzMVsvOVDIC+Ew0BS3L4gY"}, "origin": "matrix.org", "sender": "@kegan:matrix.org", "content": {"creator": "@kegan:matrix.org", "room_version": "10"}, "room_id": "!jpViMZBlnuKxAKpsem:matrix.org", "event_id": "$ayBJ5cK-zydid3RXCV7OaqK-Wh9NZcpBwSqK0H5O1NY", "unsigned": {"age_ts": 1728652174091}, "state_key": "", "signatures": {"matrix.org": {"ed25519:a_RXGa": "4/m8rQEZupyPn+SyNMesB5BqNux66Tyyl/bmBKVQay3jsNZuhUdelN056kQJUhIg7HPRes17ghKifHOvaOvvAw"}}, "auth_events": [], "prev_events": [], "origin_server_ts": 1728652174091}
{"type": "m.room.member", "depth": 2, "hashes": {"sha256": "VVIP2Wxb+nyzhgcvrJD82nNQYb3Vd5ZbOOIRbu8YGP8"}, "origin": "matrix.org", "sender": "@kegan:matrix.org", "content": {"avatar_url": "mxc://matrix.org/gImPYMAYQWpffViAorQsjbIs", "membership": "join", "displayname": "Kegan"}, "room_id": "!jpViMZBlnuKxAKpsem:matrix.org", "event_id": "$dVlIkqbTnz6_RRggTuTxuYNLdAtbkVSY47Lb-1rUWIo", "unsigned": {"age_ts": 1728652174308}, "state_key": "@kegan:matrix.org", "signatures": {"matrix.org": {"ed25519:a_RXGa": "c01Fydab2/KdQ+EqVouiii+W1YGxHD4PmNn/U3A4qsF9Bwc9Y0A9iyTXMvUR4Nsz/1YfkDgchkj0/2qBJZ9ECQ"}}, "auth_events": ["$ayBJ5cK-zydid3RXCV7OaqK-Wh9NZcpBwSqK0H5O1NY"], "prev_events": ["$ayBJ5cK-zydid3RXCV7OaqK-Wh9NZcpBwSqK0H5O1NY"], "origin_server_ts": 1728652174308}
{"type": "m.room.power_levels", "depth": 3, "hashes": {"sha256": "mQxzMn0yPhfD+KxoQFIDQVQvMWX9I3ujaTShRSYYKk8"}, "origin": "matrix.org", "sender": "@kegan:matrix.org", "content": {"ban": 50, "kick": 50, "users": {"@kegan:matrix.org": 100}, "events": {"m.room.name": 50, "m.room.avatar": 50, "m.room.tombstone": 100, "m.room.encryption": 100, "m.room.server_acl": 100, "m.room.power_levels": 100, "m.room.canonical_alias": 50, "m.room.history_visibility": 100}, "invite": 50, "redact": 50, "historical": 100, "m.call.invite": 50, "state_default": 50, "users_default": 0, "events_default": 0}, "room_id": "!jpViMZBlnuKxAKpsem:matrix.org", "event_id": "$OaedSihFH9ThMiJsVKDIatVL5CAgtETWGuUUVgAjOAg", "unsigned": {"age_ts": 1728652174519}, "state_key": "", "signatures": {"matrix.org": {"ed25519:a_RXGa": "ZHbQCeWaaHl249hlVpzm2UU8/1SWtAxDa6Z/ge5Mk3NHalC4WfbWtzjLzXrhfVYkhSTG0lfV6/EDao7bPuEjBQ"}}, "auth_events": ["$dVlIkqbTnz6_RRggTuTxuYNLdAtbkVSY47Lb-1rUWIo", "$ayBJ5cK-zydid3RXCV7OaqK-Wh9NZcpBwSqK0H5O1NY"], "prev_events": ["$dVlIkqbTnz6_RRggTuTxuYNLdAtbkVSY47Lb-1rUWIo"], "origin_server_ts": 1728652174519}
{"type": "m.room.canonical_alias", "depth": 4, "hashes": {"sha256": "WWyr4BL6gRVn2PWjSQ2wYWO6hVF3OQ/vM89QxpFMRnk"}, "origin": "matrix.org", "sender": "@kegan:matrix.org", "content": {"alias": "#test-resolve-room:matrix.org"}, "room_id": "!jpViMZBlnuKxAKpsem:matrix.org", "event_id": "$aCToy0aQOfhURh5jsakQ9Ovk6fMGbWnmnGh7gkZUiFI", "unsigned": {"age_ts": 1728652174544}, "state_key": "", "signatures": {"matrix.org": {"ed25519:a_RXGa": "eji69yq5bybnpE9m5OZh2sFeaB7MZKolMhDqlunnpyegPI9VgUtPSZF4kcXZ9ZczBoLA7eC4/HbhwpCQ7c1aAg"}}, "auth_events": ["$dVlIkqbTnz6_RRggTuTxuYNLdAtbkVSY47Lb-1rUWIo", "$OaedSihFH9ThMiJsVKDIatVL5CAgtETWGuUUVgAjOAg", "$ayBJ5cK-zydid3RXCV7OaqK-Wh9NZcpBwSqK0H5O1NY"], "prev_events": ["$OaedSihFH9ThMiJsVKDIatVL5CAgtETWGuUUVgAjOAg"], "origin_server_ts": 1728652174544}
{"type": "m.room.join_rules", "depth": 5, "hashes": {"sha256": "HSYkwp9nfNTJX8zMJt+s6Q96oapIDRHyfcuVs71P58M"}, "origin": "matrix.org", "sender": "@kegan:matrix.org", "content": {"join_rule": "public"}, "room_id": "!jpViMZBlnuKxAKpsem:matrix.org", "event_id": "$HLdW9q8VIIPTpyKfF85fDYrEu8UAp861wdpUZVzYgJQ", "unsigned": {"age_ts": 1728652174545}, "state_key": "", "signatures": {"matrix.org": {"ed25519:a_RXGa": "MDPlgzj97v5QwP60fzui6il5NHGU3BbUfm/BBxeLp79/HpPD6hYlkYYeLXoqI4N1AxtbLa3XtldGPU6vzR3HCw"}}, "auth_events": ["$dVlIkqbTnz6_RRggTuTxuYNLdAtbkVSY47Lb-1rUWIo", "$OaedSihFH9ThMiJsVKDIatVL5CAgtETWGuUUVgAjOAg", "$ayBJ5cK-zydid3RXCV7OaqK-Wh9NZcpBwSqK0H5O1NY"], "prev_events": ["$aCToy0aQOfhURh5jsakQ9Ovk6fMGbWnmnGh7gkZUiFI"], "origin_server_ts": 1728652174545}
{"type": "m.room.history_visibility", "depth": 6, "hashes": {"sha256": "7jjPzCZW37vDlpTVHhpNdt2a8ZEITtR8PrduIVAFOcw"}, "origin": "matrix.org", "sender": "@kegan:matrix.org", "content": {"history_visibility": "shared"}, "room_id": "!jpViMZBlnuKxAKpsem:matrix.org", "event_id": "$iojoMzC2XDpR0YHDdmQD1p0E9S1YCj4GsC63QWIUyJY", "unsigned": {"age_ts": 1728652174545}, "state_key": "", "signatures": {"matrix.org": {"ed25519:a_RXGa": "F9yzfRxG54oiWrYVW+G0Q7SuAhK42XAIUVizd/yLRbXG91uS/SSH7ZAA4OAAah8afmWYuzWXvUgImCqziQ+KBg"}}, "auth_events": ["$dVlIkqbTnz6_RRggTuTxuYNLdAtbkVSY47Lb-1rUWIo", "$OaedSihFH9ThMiJsVKDIatVL5CAgtETWGuUUVgAjOAg", "$ayBJ5cK-zydid3RXCV7OaqK-Wh9NZcpBwSqK0H5O1NY"], "prev_events": ["$HLdW9q8VIIPTpyKfF85fDYrEu8UAp861wdpUZVzYgJQ"], "origin_server_ts": 1728652174545}
{"type": "m.room.name", "depth": 7, "hashes": {"sha256": "Cd6glFNK04BzaNqHCaL/fKqSby3oMeEYIAKD3sxa8ok"}, "origin": "matrix.org", "sender": "@kegan:matrix.org", "content": {"name": "test resolve room"}, "room_id": "!jpViMZBlnuKxAKpsem:matrix.org", "event_id": "$A5z9GLL8ABCYg4SKujf_ehtW52yGoGhZuQGc0nspEeU", "unsigned": {"age_ts": 1728652174546}, "state_key": "", "signatures": {"matrix.org": {"ed25519:a_RXGa": "ypYzuw8nG+38o6WM4OrFeEI+xwk1QvIxZBRB3RtZGtf2y6Z3VgliO7hbsYx/AjbRCstMsrme1DEV8OiMeWIYDw"}}, "auth_events": ["$dVlIkqbTnz6_RRggTuTxuYNLdAtbkVSY47Lb-1rUWIo", "$OaedSihFH9ThMiJsVKDIatVL5CAgtETWGuUUVgAjOAg", "$ayBJ5cK-zydid3RXCV7OaqK-Wh9NZcpBwSqK0H5O1NY"], "prev_events": ["$iojoMzC2XDpR0YHDdmQD1p0E9S1YCj4GsC63QWIUyJY"], "origin_server_ts": 1728652174546}
{"type": "m.room.message", "depth": 8, "hashes": {"sha256": "l02uJl91yHpVopMkChN4gCJIy4zFf4Go/iICtr0VKNk"}, "origin": "matrix.org", "sender": "@kegan:matrix.org", "content": {"body": "this is the first message in the room.", "msgtype": "m.text", "m.mentions": {}}, "room_id": "!jpViMZBlnuKxAKpsem:matrix.org", "event_id": "$_-YRKNmQfY6o9ibTvOPlHFVxkHvh1k3avAPUn49w9nE", "unsigned": {"age_ts": 1728652182530}, "signatures": {"matrix.org": {"ed25519:a_RXGa": "kjHV6A//PNmAmdz16MbhgqPi8/5BujXLTwcRcz1aPejZvWIg85NBWwqQpfy1jFIct4lMhADVEu4Ebjr8Odn/DQ"}}, "auth_events": ["$OaedSihFH9ThMiJsVKDIatVL5CAgtETWGuUUVgAjOAg", "$dVlIkqbTnz6_RRggTuTxuYNLdAtbkVSY47Lb-1rUWIo", "$ayBJ5cK-zydid3RXCV7OaqK-Wh9NZcpBwSqK0H5O1NY"], "prev_events": ["$A5z9GLL8ABCYg4SKujf_ehtW52yGoGhZuQGc0nspEeU"], "origin_server_ts": 1728652182530}

1 change: 1 addition & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
<input id="stepbwd" type="button" value="<-" />
<input id="stepfwd" type="button" value="->" />
<input id="start" type="text" placeholder="Start event ID (optional)" value="" />
<input id="resolve" type="button" value="Resolve Current State" />
</div>
<div>
<div id="infocontainer">
Expand Down
111 changes: 99 additions & 12 deletions shims/synapse/shim.py
Original file line number Diff line number Diff line change
@@ -1,42 +1,52 @@
#!/usr/bin/env python
import asyncio
import json
import logging
import uuid
from typing import Dict, Optional, Sequence
from typing import Collection, Dict, Iterable, List, Optional, Sequence, Set
from pydantic import BaseModel
from websockets.asyncio.server import serve


from twisted.internet import defer
from synapse.api.room_versions import RoomVersions
from synapse.state.v2 import resolve_events_with_store
from synapse.events import EventBase, make_event_from_dict

# logging.basicConfig(level=logging.DEBUG)

class WebSocketMessage(BaseModel):
type: str
id: str
error: Optional[str] = None
data: dict

room_ver = RoomVersions.V10 # TODO: parameterise

class Connection:
event_map: Dict[str, EventBase] = {}

def __init__(self, ws):
self.ws = ws
self.outstanding_requests = {}

# Array<Record<StateKeyTuple, EventID>>
async def resolve_state(self, id: str, state):
async def resolve_state(self, id: str, room_id: str, state):
print(f"resolve_state: {id}")
for x in state:
for _, event_id in x.items():
print(f" get_event {event_id}")
ev = await self.get_event(event_id)
print(f" get_event {event_id} obtained. type={ev["type"]}")
r = await resolve_events_with_store(FakeClock(),room_id, room_ver, state, event_map=None, state_res_store=self)
print(f"resolve_state: {id} responding")
# convert tuple keys to strings
r = {json.dumps(k):v for k,v in r.items()}
await self.ws.send(json.dumps({
"id": id,
"type": "resolve_state",
"data": {
"result": state, # echo it back for now
"result": r,
}
}))
return []

async def get_event(self, event_id: str) -> dict:
async def get_event(self, event_id: str) -> EventBase:
id = str(uuid.uuid4())
print(f" get_event {event_id} -> {id}")
loop = asyncio.get_running_loop()
Expand All @@ -50,7 +60,74 @@ async def get_event(self, event_id: str) -> dict:
}
}))
await fut
return self.outstanding_requests[id].result()
ev_dict = self.outstanding_requests[id].result()
ev_dict.pop("event_id") # wire format shouldn't have this, but tardis includes it.
return make_event_from_dict(ev_dict, room_version=room_ver)

async def get_events(
self, event_ids: Collection[str], allow_rejected: bool = False
) -> "defer.Deferred[Dict[str, EventBase]]":
"""Get events from the database

Args:
event_ids: The event_ids of the events to fetch
allow_rejected: If True return rejected events.

Returns:
Dict from event_id to event.
"""
result = {}
for event_id in event_ids:
print(f" get_event {event_id}")
ev = await self.get_event(event_id)
print(f" get_event {event_id} obtained. type={ev["type"]}")
result[event_id] = ev

return result

async def _get_auth_chain(self, event_ids: Iterable[str]) -> List[str]:
"""Gets the full auth chain for a set of events (including rejected
events).

Includes the given event IDs in the result.

Note that:
1. All events must be state events.
2. For v1 rooms this may not have the full auth chain in the
presence of rejected events

Args:
event_ids: The event IDs of the events to fetch the auth
chain for. Must be state events.
Returns:
List of event IDs of the auth chain.
"""

# Simple DFS for auth chain
result = set()
stack = list(event_ids)
while stack:
event_id = stack.pop()
if event_id in result:
continue

result.add(event_id)

event = self.event_map.get(event_id, None)
if event is None:
event = await self.get_event(event_id)
self.event_map[event_id] = event
for aid in event.auth_event_ids():
stack.append(aid)

return list(result)

async def get_auth_chain_difference(
self, room_id: str, auth_sets: List[Set[str]]
) -> "defer.Deferred[Set[str]]":
chains = [frozenset(await self._get_auth_chain(a)) for a in auth_sets]
common = set(chains[0]).intersection(*chains[1:])
return set(chains[0]).union(*chains[1:]) - common


async def handler(websocket):
Expand All @@ -68,17 +145,27 @@ async def handler(websocket):
# call get_event which needs more WS messages, so we cannot block the processing
# of incoming WS messages. When resolve_state concludes, it will send the response,
# hence why we pass in the id here so it can pair it up.
asyncio.create_task(c.resolve_state(wsm.id, wsm.data["state"]))
asyncio.create_task(c.resolve_state(wsm.id, wsm.data["room_id"], wsm.data["state"]))
else:
print(f"unknown type: {wsm.type}")
except Exception as err:
print(f"recv error {err}")

# Synapse specifics below
# -----------------------

class FakeClock:
def sleep(self, msec: float) -> "defer.Deferred[None]":
return defer.succeed(None)




async def main():
print("Listening on 0.0.0.0:1234")
async with serve(handler, "0.0.0.0", 1234):
await asyncio.get_running_loop().create_future() # run forever


if __name__ == '__main__':
asyncio.run(main())
asyncio.run(main())
Loading
Loading