14
14
import logging
15
15
from typing import TYPE_CHECKING , Iterable , List , Optional , Tuple
16
16
17
- from synapse .api .constants import ReadReceiptEventFields , ReceiptTypes
17
+ from synapse .api .constants import ReceiptTypes
18
18
from synapse .appservice import ApplicationService
19
19
from synapse .streams import EventSource
20
20
from synapse .types import JsonDict , ReadReceipt , UserID , get_domain_from_id
@@ -112,7 +112,7 @@ async def _handle_new_receipts(self, receipts: List[ReadReceipt]) -> bool:
112
112
)
113
113
114
114
if not res :
115
- # res will be None if this read receipt is 'old'
115
+ # res will be None if this receipt is 'old'
116
116
continue
117
117
118
118
stream_id , max_persisted_id = res
@@ -138,7 +138,7 @@ async def _handle_new_receipts(self, receipts: List[ReadReceipt]) -> bool:
138
138
return True
139
139
140
140
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
142
142
) -> None :
143
143
"""Called when a client tells us a local user has read up to the given
144
144
event_id in the room.
@@ -148,16 +148,14 @@ async def received_client_receipt(
148
148
receipt_type = receipt_type ,
149
149
user_id = user_id ,
150
150
event_ids = [event_id ],
151
- data = {"ts" : int (self .clock .time_msec ()), "hidden" : hidden },
151
+ data = {"ts" : int (self .clock .time_msec ())},
152
152
)
153
153
154
154
is_new = await self ._handle_new_receipts ([receipt ])
155
155
if not is_new :
156
156
return
157
157
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 :
161
159
await self .federation_sender .send_read_receipt (receipt )
162
160
163
161
@@ -168,6 +166,13 @@ def __init__(self, hs: "HomeServer"):
168
166
169
167
@staticmethod
170
168
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
+
171
176
visible_events = []
172
177
173
178
# 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]:
176
181
new_event = event .copy ()
177
182
new_event ["content" ] = {}
178
183
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
210
199
211
200
# Append new_event to visible_events unless empty
212
201
if len (new_event ["content" ].keys ()) > 0 :
0 commit comments