Explorar o código

Simplify handling read receipts

Tulir Asokan %!s(int64=4) %!d(string=hai) anos
pai
achega
a6569c9ba8
Modificáronse 2 ficheiros con 13 adicións e 22 borrados
  1. 12 21
      mautrix_signal/matrix.py
  2. 1 1
      requirements.txt

+ 12 - 21
mautrix_signal/matrix.py

@@ -20,7 +20,8 @@ from mausignald.types import Address
 from mautrix.bridge import BaseMatrixHandler
 from mautrix.bridge import BaseMatrixHandler
 from mautrix.types import (Event, ReactionEvent, MessageEvent, StateEvent, EncryptedEvent, RoomID,
 from mautrix.types import (Event, ReactionEvent, MessageEvent, StateEvent, EncryptedEvent, RoomID,
                            EventID, UserID, ReactionEventContent, RelationType, EventType,
                            EventID, UserID, ReactionEventContent, RelationType, EventType,
-                           ReceiptEvent, TypingEvent, PresenceEvent, RedactionEvent, ReceiptType)
+                           ReceiptEvent, TypingEvent, PresenceEvent, RedactionEvent,
+                           SingleReceiptEventContent)
 
 
 from .db import Message as DBMessage
 from .db import Message as DBMessage
 from . import puppet as pu, portal as po, user as u, signal as s
 from . import puppet as pu, portal as po, user as u, signal as s
@@ -85,25 +86,15 @@ class MatrixHandler(BaseMatrixHandler):
         await portal.handle_matrix_reaction(user, event_id, content.relates_to.event_id,
         await portal.handle_matrix_reaction(user, event_id, content.relates_to.event_id,
                                             content.relates_to.key)
                                             content.relates_to.key)
 
 
-    async def handle_receipt(self, evt: ReceiptEvent) -> None:
-        for event_id, receipts in evt.content.items():
-            for user_id, data in receipts[ReceiptType.READ].items():
-                user = await u.User.get_by_mxid(user_id, create=False)
-                if not user or not user.username:
-                    continue
-
-                portal = await po.Portal.get_by_mxid(evt.room_id)
-                if not portal:
-                    continue
-
-                message = await DBMessage.get_by_mxid(event_id, portal.mxid)
-                if not message:
-                    continue
+    async def handle_read_receipt(self, user: 'u.User', portal: 'po.Portal', event_id: EventID,
+                                  data: SingleReceiptEventContent) -> None:
+        message = await DBMessage.get_by_mxid(event_id, portal.mxid)
+        if not message:
+            return
 
 
-                user.log.trace(f"Sending read receipt for {message.timestamp} to {message.sender}")
-                await self.signal.send_receipt(user.username, Address(uuid=message.sender),
-                                               timestamps=[message.timestamp], when=data.ts,
-                                               read=True)
+        user.log.trace(f"Sending read receipt for {message.timestamp} to {message.sender}")
+        await self.signal.send_receipt(user.username, Address(uuid=message.sender),
+                                       timestamps=[message.timestamp], when=data.ts, read=True)
 
 
     async def handle_typing(self, room_id: RoomID, typing: List[UserID]) -> None:
     async def handle_typing(self, room_id: RoomID, typing: List[UserID]) -> None:
         pass
         pass
@@ -126,5 +117,5 @@ class MatrixHandler(BaseMatrixHandler):
                                      ) -> None:
                                      ) -> None:
         if evt.type == EventType.TYPING:
         if evt.type == EventType.TYPING:
             await self.handle_typing(evt.room_id, evt.content.user_ids)
             await self.handle_typing(evt.room_id, evt.content.user_ids)
-        elif evt.type == EventType.RECEIPT:
-            await self.handle_receipt(evt)
+        else:
+            super().handle_ephemeral_event(evt)

+ 1 - 1
requirements.txt

@@ -4,5 +4,5 @@ commonmark>=0.8,<0.10
 aiohttp>=3,<3.7
 aiohttp>=3,<3.7
 yarl>=1,<1.6
 yarl>=1,<1.6
 attrs>=19.1
 attrs>=19.1
-mautrix==0.8.0.beta4
+mautrix==0.8.0.beta7
 asyncpg>=0.20,<0.22
 asyncpg>=0.20,<0.22