1414import logging
1515from typing import TYPE_CHECKING , Iterable , List , Optional , Tuple
1616
17- from synapse .api .constants import ReadReceiptEventFields , ReceiptTypes
17+ from synapse .api .constants import ReceiptTypes
1818from synapse .appservice import ApplicationService
1919from synapse .streams import EventSource
2020from synapse .types import JsonDict , ReadReceipt , UserID , get_domain_from_id
@@ -112,7 +112,7 @@ async def _handle_new_receipts(self, receipts: List[ReadReceipt]) -> bool:
112112 )
113113
114114 if not res :
115- # res will be None if this read receipt is 'old'
115+ # res will be None if this receipt is 'old'
116116 continue
117117
118118 stream_id , max_persisted_id = res
@@ -138,7 +138,7 @@ async def _handle_new_receipts(self, receipts: List[ReadReceipt]) -> bool:
138138 return True
139139
140140 async def received_client_receipt (
141- self , room_id : str , receipt_type : str , user_id : str , event_id : str , hidden : bool
141+ self , room_id : str , receipt_type : str , user_id : str , event_id : str
142142 ) -> None :
143143 """Called when a client tells us a local user has read up to the given
144144 event_id in the room.
@@ -148,16 +148,14 @@ async def received_client_receipt(
148148 receipt_type = receipt_type ,
149149 user_id = user_id ,
150150 event_ids = [event_id ],
151- data = {"ts" : int (self .clock .time_msec ()), "hidden" : hidden },
151+ data = {"ts" : int (self .clock .time_msec ())},
152152 )
153153
154154 is_new = await self ._handle_new_receipts ([receipt ])
155155 if not is_new :
156156 return
157157
158- if self .federation_sender and not (
159- self .hs .config .experimental .msc2285_enabled and hidden
160- ):
158+ if self .federation_sender and receipt_type != ReceiptTypes .READ_PRIVATE :
161159 await self .federation_sender .send_read_receipt (receipt )
162160
163161
@@ -168,6 +166,13 @@ def __init__(self, hs: "HomeServer"):
168166
169167 @staticmethod
170168 def filter_out_hidden (events : List [JsonDict ], user_id : str ) -> List [JsonDict ]:
169+ """
170+ This method takes in what is returned by
171+ get_linearized_receipts_for_rooms() and goes through read receipts
172+ filtering out m.read.private receipts if they were not sent by the
173+ current user.
174+ """
175+
171176 visible_events = []
172177
173178 # filter out hidden receipts the user shouldn't see
@@ -176,37 +181,21 @@ def filter_out_hidden(events: List[JsonDict], user_id: str) -> List[JsonDict]:
176181 new_event = event .copy ()
177182 new_event ["content" ] = {}
178183
179- for event_id in content .keys ():
180- event_content = content .get (event_id , {})
181- m_read = event_content .get (ReceiptTypes .READ , {})
182-
183- # If m_read is missing copy over the original event_content as there is nothing to process here
184- if not m_read :
185- new_event ["content" ][event_id ] = event_content .copy ()
186- continue
187-
188- new_users = {}
189- for rr_user_id , user_rr in m_read .items ():
190- try :
191- hidden = user_rr .get ("hidden" )
192- except AttributeError :
193- # Due to https://github.com/matrix-org/synapse/issues/10376
194- # there are cases where user_rr is a string, in those cases
195- # we just ignore the read receipt
196- continue
197-
198- if hidden is not True or rr_user_id == user_id :
199- new_users [rr_user_id ] = user_rr .copy ()
200- # If hidden has a value replace hidden with the correct prefixed key
201- if hidden is not None :
202- new_users [rr_user_id ].pop ("hidden" )
203- new_users [rr_user_id ][
204- ReadReceiptEventFields .MSC2285_HIDDEN
205- ] = hidden
206-
207- # Set new users unless empty
208- if len (new_users .keys ()) > 0 :
209- new_event ["content" ][event_id ] = {ReceiptTypes .READ : new_users }
184+ for event_id , event_content in content .items ():
185+ receipt_event = {}
186+ for receipt_type , receipt_content in event_content .items ():
187+ if receipt_type == ReceiptTypes .READ_PRIVATE :
188+ user_rr = receipt_content .get (user_id , None )
189+ if user_rr :
190+ receipt_event [ReceiptTypes .READ_PRIVATE ] = {
191+ user_id : user_rr .copy ()
192+ }
193+ else :
194+ receipt_event [receipt_type ] = receipt_content .copy ()
195+
196+ # Only include the receipt event if it is non-empty.
197+ if receipt_event :
198+ new_event ["content" ][event_id ] = receipt_event
210199
211200 # Append new_event to visible_events unless empty
212201 if len (new_event ["content" ].keys ()) > 0 :
0 commit comments