1313# See the License for the specific language governing permissions and
1414# limitations under the License.
1515import logging
16- import operator
16+ from typing import Dict , FrozenSet , List , Optional
1717
1818from synapse .api .constants import (
1919 AccountDataTypes ,
2020 EventTypes ,
2121 HistoryVisibility ,
2222 Membership ,
2323)
24+ from synapse .events import EventBase
2425from synapse .events .utils import prune_event
2526from synapse .storage import Storage
2627from synapse .storage .state import StateFilter
27- from synapse .types import get_domain_from_id
28+ from synapse .types import StateMap , get_domain_from_id
2829
2930logger = logging .getLogger (__name__ )
3031
4849
4950async def filter_events_for_client (
5051 storage : Storage ,
51- user_id ,
52- events ,
53- is_peeking = False ,
54- always_include_ids = frozenset (),
55- filter_send_to_client = True ,
56- ):
52+ user_id : str ,
53+ events : List [ EventBase ] ,
54+ is_peeking : bool = False ,
55+ always_include_ids : FrozenSet [ str ] = frozenset (),
56+ filter_send_to_client : bool = True ,
57+ ) -> List [ EventBase ] :
5758 """
5859 Check which events a user is allowed to see. If the user can see the event but its
5960 sender asked for their data to be erased, prune the content of the event.
6061
6162 Args:
6263 storage
63- user_id(str) : user id to be checked
64- events(list[synapse.events.EventBase]) : sequence of events to be checked
65- is_peeking(bool) : should be True if:
64+ user_id: user id to be checked
65+ events: sequence of events to be checked
66+ is_peeking: should be True if:
6667 * the user is not currently a member of the room, and:
6768 * the user has not been a member of the room since the given
6869 events
69- always_include_ids (set(event_id)) : set of event ids to specifically
70+ always_include_ids: set of event ids to specifically
7071 include (unless sender is ignored)
71- filter_send_to_client (bool) : Whether we're checking an event that's going to be
72+ filter_send_to_client: Whether we're checking an event that's going to be
7273 sent to a client. This might not always be the case since this function can
7374 also be called to check whether a user can see the state at a given point.
7475
7576 Returns:
76- list[synapse. events.EventBase]
77+ The filtered events.
7778 """
7879 # Filter out events that have been soft failed so that we don't relay them
7980 # to clients.
@@ -90,7 +91,7 @@ async def filter_events_for_client(
9091 AccountDataTypes .IGNORED_USER_LIST , user_id
9192 )
9293
93- ignore_list = frozenset ()
94+ ignore_list = frozenset () # type: FrozenSet[str]
9495 if ignore_dict_content :
9596 ignored_users_dict = ignore_dict_content .get ("ignored_users" , {})
9697 if isinstance (ignored_users_dict , dict ):
@@ -107,19 +108,18 @@ async def filter_events_for_client(
107108 room_id
108109 ] = await storage .main .get_retention_policy_for_room (room_id )
109110
110- def allowed (event ) :
111+ def allowed (event : EventBase ) -> Optional [ EventBase ] :
111112 """
112113 Args:
113- event (synapse.events.EventBase) : event to check
114+ event: event to check
114115
115116 Returns:
116- None|EventBase:
117- None if the user cannot see this event at all
117+ None if the user cannot see this event at all
118118
119- a redacted copy of the event if they can only see a redacted
120- version
119+ a redacted copy of the event if they can only see a redacted
120+ version
121121
122- the original event if they can see it as normal.
122+ the original event if they can see it as normal.
123123 """
124124 # Only run some checks if these events aren't about to be sent to clients. This is
125125 # because, if this is not the case, we're probably only checking if the users can
@@ -252,48 +252,46 @@ def allowed(event):
252252
253253 return event
254254
255- # check each event: gives an iterable[None|EventBase]
255+ # Check each event: gives an iterable of None or (a potentially modified)
256+ # EventBase.
256257 filtered_events = map (allowed , events )
257258
258- # remove the None entries
259- filtered_events = filter (operator .truth , filtered_events )
260-
261- # we turn it into a list before returning it.
262- return list (filtered_events )
259+ # Turn it into a list and remove None entries before returning.
260+ return [ev for ev in filtered_events if ev ]
263261
264262
265263async def filter_events_for_server (
266264 storage : Storage ,
267- server_name ,
268- events ,
269- redact = True ,
270- check_history_visibility_only = False ,
271- ):
265+ server_name : str ,
266+ events : List [ EventBase ] ,
267+ redact : bool = True ,
268+ check_history_visibility_only : bool = False ,
269+ ) -> List [ EventBase ] :
272270 """Filter a list of events based on whether given server is allowed to
273271 see them.
274272
275273 Args:
276274 storage
277- server_name (str)
278- events (iterable[FrozenEvent])
279- redact (bool) : Whether to return a redacted version of the event, or
275+ server_name
276+ events
277+ redact: Whether to return a redacted version of the event, or
280278 to filter them out entirely.
281- check_history_visibility_only (bool) : Whether to only check the
279+ check_history_visibility_only: Whether to only check the
282280 history visibility, rather than things like if the sender has been
283281 erased. This is used e.g. during pagination to decide whether to
284282 backfill or not.
285283
286284 Returns
287- list[FrozenEvent]
285+ The filtered events.
288286 """
289287
290- def is_sender_erased (event , erased_senders ) :
288+ def is_sender_erased (event : EventBase , erased_senders : Dict [ str , bool ]) -> bool :
291289 if erased_senders and erased_senders [event .sender ]:
292290 logger .info ("Sender of %s has been erased, redacting" , event .event_id )
293291 return True
294292 return False
295293
296- def check_event_is_visible (event , state ) :
294+ def check_event_is_visible (event : EventBase , state : StateMap [ EventBase ]) -> bool :
297295 history = state .get ((EventTypes .RoomHistoryVisibility , "" ), None )
298296 if history :
299297 visibility = history .content .get (
0 commit comments