Cancel sending events when removing local echoes

This commit is contained in:
miruka 2020-04-03 07:13:33 -04:00
parent 0e04eafebd
commit b48e663007
2 changed files with 23 additions and 11 deletions

View File

@ -1,6 +1,5 @@
# TODO # TODO
- redact local echo effect
- being able to redact local echos - being able to redact local echos
- unselect before unfocusing - unselect before unfocusing

View File

@ -118,6 +118,7 @@ class MatrixClient(nio.AsyncClient):
self.upload_monitors: Dict[UUID, nio.TransferMonitor] = {} self.upload_monitors: Dict[UUID, nio.TransferMonitor] = {}
self.upload_tasks: Dict[UUID, asyncio.Task] = {} self.upload_tasks: Dict[UUID, asyncio.Task] = {}
self.send_message_tasks: Dict[UUID, asyncio.Task] = {}
self.first_sync_done: asyncio.Event = asyncio.Event() self.first_sync_done: asyncio.Event = asyncio.Event()
self.first_sync_date: Optional[datetime] = None self.first_sync_date: Optional[datetime] = None
@ -342,7 +343,7 @@ class MatrixClient(nio.AsyncClient):
room_id, tx_id, event_type, content=echo_body, mentions=mentions, room_id, tx_id, event_type, content=echo_body, mentions=mentions,
) )
await self._send_message(room_id, content) await self._send_message(room_id, content, tx_id)
async def toggle_pause_upload( async def toggle_pause_upload(
@ -592,7 +593,7 @@ class MatrixClient(nio.AsyncClient):
content["info"].get("thumbnail_info", {}).get("mimetype", ""), content["info"].get("thumbnail_info", {}).get("mimetype", ""),
) )
await self._send_message(room_id, content) await self._send_message(room_id, content, transaction_id)
async def _local_echo( async def _local_echo(
@ -648,9 +649,14 @@ class MatrixClient(nio.AsyncClient):
await self.set_room_last_event(room_id, event) await self.set_room_last_event(room_id, event)
async def _send_message(self, room_id: str, content: dict) -> None: async def _send_message(
self, room_id: str, content: dict, transaction_id: UUID,
) -> None:
"""Send a message event with `content` dict to a room.""" """Send a message event with `content` dict to a room."""
self.send_message_tasks[transaction_id] = \
asyncio.current_task() # type: ignore
async with self.backend.send_locks[room_id]: async with self.backend.send_locks[room_id]:
await self.room_send( await self.room_send(
room_id = room_id, room_id = room_id,
@ -915,6 +921,13 @@ class MatrixClient(nio.AsyncClient):
if not event: if not event:
continue continue
if event.is_local_echo:
if user_id == self.user_id:
uuid = UUID(event.id.replace("echo-", ""))
self.send_message_tasks[uuid].cancel()
event.is_local_echo = False
else:
if user_id == self.user_id: if user_id == self.user_id:
tasks.append( tasks.append(
self.room_redact(room_id, event.event_id, reason), self.room_redact(room_id, event.event_id, reason),