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

Commit 5dea4d3

Browse files
committed
Update some comments
Add a couple of type annotations, docstrings, and other comments, in the interest of keeping track of what types I have. Merged from pull request #370.
1 parent fc27ca9 commit 5dea4d3

File tree

4 files changed

+98
-14
lines changed

4 files changed

+98
-14
lines changed

synapse/handlers/_base.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,12 @@
2929

3030

3131
class BaseHandler(object):
32+
"""
33+
Common base class for the event handlers.
34+
35+
:type store: synapse.storage.events.StateStore
36+
:type state_handler: synapse.state.StateHandler
37+
"""
3238

3339
def __init__(self, hs):
3440
self.store = hs.get_datastore()

synapse/handlers/sync.py

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,9 @@ def __nonzero__(self):
4747

4848

4949
class JoinedSyncResult(collections.namedtuple("JoinedSyncResult", [
50-
"room_id",
51-
"timeline",
52-
"state",
50+
"room_id", # str
51+
"timeline", # TimelineBatch
52+
"state", # list[FrozenEvent]
5353
"ephemeral",
5454
"private_user_data",
5555
])):
@@ -68,9 +68,9 @@ def __nonzero__(self):
6868

6969

7070
class ArchivedSyncResult(collections.namedtuple("JoinedSyncResult", [
71-
"room_id",
72-
"timeline",
73-
"state",
71+
"room_id", # str
72+
"timeline", # TimelineBatch
73+
"state", # list[FrozenEvent]
7474
"private_user_data",
7575
])):
7676
__slots__ = []
@@ -87,8 +87,8 @@ def __nonzero__(self):
8787

8888

8989
class InvitedSyncResult(collections.namedtuple("InvitedSyncResult", [
90-
"room_id",
91-
"invite",
90+
"room_id", # str
91+
"invite", # FrozenEvent: the invite event
9292
])):
9393
__slots__ = []
9494

@@ -507,6 +507,9 @@ def incremental_sync_with_gap(self, sync_config, since_token):
507507
@defer.inlineCallbacks
508508
def load_filtered_recents(self, room_id, sync_config, now_token,
509509
since_token=None):
510+
"""
511+
:returns a Deferred TimelineBatch
512+
"""
510513
limited = True
511514
recents = []
512515
filtering_factor = 2
@@ -680,8 +683,13 @@ def get_state_at_previous_sync(self, room_id, since_token):
680683
def compute_state_delta(self, since_token, previous_state, current_state):
681684
""" Works out the differnce in state between the current state and the
682685
state the client got when it last performed a sync.
683-
Returns:
684-
A list of events.
686+
687+
:param str since_token: the point we are comparing against
688+
:param list[synapse.events.FrozenEvent] previous_state: the state to
689+
compare to
690+
:param list[synapse.events.FrozenEvent] current_state: the new state
691+
692+
:returns: A list of events.
685693
"""
686694
# TODO(mjark) Check if the state events were received by the server
687695
# after the previous sync, since we need to include those state
@@ -696,6 +704,12 @@ def compute_state_delta(self, since_token, previous_state, current_state):
696704

697705
@defer.inlineCallbacks
698706
def check_joined_room(self, sync_config, room_id, state_delta):
707+
"""
708+
Check if the user has just joined the given room. If so, return the
709+
full state for the room, instead of the delta since the last sync.
710+
711+
:returns A deferred Tuple (state_delta, limited)
712+
"""
699713
joined = False
700714
limited = False
701715
for event in state_delta:

synapse/rest/client/v2_alpha/sync.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,20 @@ def encode_presence(self, events, filter, time_now):
165165
return {"events": filter.filter_presence(formatted)}
166166

167167
def encode_joined(self, rooms, filter, time_now, token_id):
168+
"""
169+
Encode the joined rooms in a sync result
170+
171+
:param list[synapse.handlers.sync.JoinedSyncResult] rooms: list of sync
172+
results for rooms this user is joined to
173+
:param FilterCollection filter: filters to apply to the results
174+
:param int time_now: current time - used as a baseline for age
175+
calculations
176+
:param int token_id: ID of the user's auth token - used for namespacing
177+
of transaction IDs
178+
179+
:return: the joined rooms list, in our response format
180+
:rtype: dict[str, dict[str, object]]
181+
"""
168182
joined = {}
169183
for room in rooms:
170184
joined[room.room_id] = self.encode_room(
@@ -174,6 +188,20 @@ def encode_joined(self, rooms, filter, time_now, token_id):
174188
return joined
175189

176190
def encode_invited(self, rooms, filter, time_now, token_id):
191+
"""
192+
Encode the invited rooms in a sync result
193+
194+
:param list[synapse.handlers.sync.InvitedSyncResult] rooms: list of
195+
sync results for rooms this user is joined to
196+
:param FilterCollection filter: filters to apply to the results
197+
:param int time_now: current time - used as a baseline for age
198+
calculations
199+
:param int token_id: ID of the user's auth token - used for namespacing
200+
of transaction IDs
201+
202+
:return: the invited rooms list, in our response format
203+
:rtype: dict[str, dict[str, object]]
204+
"""
177205
invited = {}
178206
for room in rooms:
179207
invite = serialize_event(
@@ -189,6 +217,20 @@ def encode_invited(self, rooms, filter, time_now, token_id):
189217
return invited
190218

191219
def encode_archived(self, rooms, filter, time_now, token_id):
220+
"""
221+
Encode the archived rooms in a sync result
222+
223+
:param list[synapse.handlers.sync.ArchivedSyncResult] rooms: list of
224+
sync results for rooms this user is joined to
225+
:param FilterCollection filter: filters to apply to the results
226+
:param int time_now: current time - used as a baseline for age
227+
calculations
228+
:param int token_id: ID of the user's auth token - used for namespacing
229+
of transaction IDs
230+
231+
:return: the invited rooms list, in our response format
232+
:rtype: dict[str, dict[str, object]]
233+
"""
192234
joined = {}
193235
for room in rooms:
194236
joined[room.room_id] = self.encode_room(
@@ -199,6 +241,20 @@ def encode_archived(self, rooms, filter, time_now, token_id):
199241

200242
@staticmethod
201243
def encode_room(room, filter, time_now, token_id, joined=True):
244+
"""
245+
:param JoinedSyncResult|ArchivedSyncResult room: sync result for a
246+
single room
247+
:param FilterCollection filter: filters to apply to the results
248+
:param int time_now: current time - used as a baseline for age
249+
calculations
250+
:param int token_id: ID of the user's auth token - used for namespacing
251+
of transaction IDs
252+
:param joined: True if the user is joined to this room - will mean
253+
we handle ephemeral events
254+
255+
:return: the room, encoded in our response format
256+
:rtype: dict[str, object]
257+
"""
202258
event_map = {}
203259
state_events = filter.filter_room_state(room.state)
204260
state_event_ids = []

synapse/state.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ def start_caching(self):
7171

7272
@defer.inlineCallbacks
7373
def get_current_state(self, room_id, event_type=None, state_key=""):
74-
""" Returns the current state for the room as a list. This is done by
74+
""" Retrieves the current state for the room. This is done by
7575
calling `get_latest_events_in_room` to get the leading edges of the
7676
event graph and then resolving any of the state conflicts.
7777
@@ -80,6 +80,8 @@ def get_current_state(self, room_id, event_type=None, state_key=""):
8080
8181
If `event_type` is specified, then the method returns only the one
8282
event (or None) with that `event_type` and `state_key`.
83+
84+
:returns map from (type, state_key) to event
8385
"""
8486
event_ids = yield self.store.get_latest_event_ids_in_room(room_id)
8587

@@ -177,9 +179,10 @@ def resolve_state_groups(self, room_id, event_ids, event_type=None, state_key=""
177179
""" Given a list of event_ids this method fetches the state at each
178180
event, resolves conflicts between them and returns them.
179181
180-
Return format is a tuple: (`state_group`, `state_events`), where the
181-
first is the name of a state group if one and only one is involved,
182-
otherwise `None`.
182+
:returns a Deferred tuple of (`state_group`, `state`, `prev_state`).
183+
`state_group` is the name of a state group if one and only one is
184+
involved. `state` is a map from (type, state_key) to event, and
185+
`prev_state` is a list of event ids.
183186
"""
184187
logger.debug("resolve_state_groups event_ids %s", event_ids)
185188

@@ -255,6 +258,11 @@ def resolve_events(self, state_sets, event):
255258
return self._resolve_events(state_sets)
256259

257260
def _resolve_events(self, state_sets, event_type=None, state_key=""):
261+
"""
262+
:returns a tuple (new_state, prev_states). new_state is a map
263+
from (type, state_key) to event. prev_states is a list of event_ids.
264+
:rtype: (dict[(str, str), synapse.events.FrozenEvent], list[str])
265+
"""
258266
state = {}
259267
for st in state_sets:
260268
for e in st:

0 commit comments

Comments
 (0)