1414# limitations under the License.
1515
1616import logging
17+ import random
1718from collections import namedtuple
1819from typing import TYPE_CHECKING , List , Set , Tuple
1920
20- from synapse .api .errors import AuthError , SynapseError
21+ from synapse .api .errors import AuthError , ShadowBanError , SynapseError
2122from synapse .metrics .background_process_metrics import run_as_background_process
2223from synapse .replication .tcp .streams import TypingStream
2324from synapse .types import UserID , get_domain_from_id
@@ -227,16 +228,21 @@ def _handle_timeout_for_member(self, now: int, member: RoomMember):
227228 self ._stopped_typing (member )
228229 return
229230
230- async def started_typing (self , target_user , auth_user , room_id , timeout ):
231+ async def started_typing (self , target_user , requester , room_id , timeout ):
231232 target_user_id = target_user .to_string ()
232- auth_user_id = auth_user .to_string ()
233+ auth_user_id = requester . user .to_string ()
233234
234235 if not self .is_mine_id (target_user_id ):
235236 raise SynapseError (400 , "User is not hosted on this homeserver" )
236237
237238 if target_user_id != auth_user_id :
238239 raise AuthError (400 , "Cannot set another user's typing state" )
239240
241+ if requester .shadow_banned :
242+ # We randomly sleep a bit just to annoy the requester.
243+ await self .clock .sleep (random .randint (1 , 10 ))
244+ raise ShadowBanError ()
245+
240246 await self .auth .check_user_in_room (room_id , target_user_id )
241247
242248 logger .debug ("%s has started typing in %s" , target_user_id , room_id )
@@ -256,16 +262,21 @@ async def started_typing(self, target_user, auth_user, room_id, timeout):
256262
257263 self ._push_update (member = member , typing = True )
258264
259- async def stopped_typing (self , target_user , auth_user , room_id ):
265+ async def stopped_typing (self , target_user , requester , room_id ):
260266 target_user_id = target_user .to_string ()
261- auth_user_id = auth_user .to_string ()
267+ auth_user_id = requester . user .to_string ()
262268
263269 if not self .is_mine_id (target_user_id ):
264270 raise SynapseError (400 , "User is not hosted on this homeserver" )
265271
266272 if target_user_id != auth_user_id :
267273 raise AuthError (400 , "Cannot set another user's typing state" )
268274
275+ if requester .shadow_banned :
276+ # We randomly sleep a bit just to annoy the requester.
277+ await self .clock .sleep (random .randint (1 , 10 ))
278+ raise ShadowBanError ()
279+
269280 await self .auth .check_user_in_room (room_id , target_user_id )
270281
271282 logger .debug ("%s has stopped typing in %s" , target_user_id , room_id )
0 commit comments