Forráskód Böngészése

Switch portal sync to last activity filtering instead of hard limit

Tulir Asokan 4 éve
szülő
commit
7bb4c77172

+ 11 - 2
mauigpapi/http/thread.py

@@ -38,9 +38,18 @@ class ThreadAPI(BaseAndroidAPI):
         return await self.std_http_get(f"/api/v1/direct_v2/{inbox_type}/", query=query,
                                        response_type=DMInboxResponse)
 
-    async def iter_inbox(self, cursor: Optional[str] = None, seq_id: Optional[str] = None,
+    async def iter_inbox(self, start_at: Optional[DMInboxResponse] = None,
                          message_limit: int = 10) -> AsyncIterable[Thread]:
-        has_more = True
+        if start_at:
+            cursor = start_at.inbox.prev_cursor
+            seq_id = start_at.inbox.prev_cursor
+            has_more = start_at.inbox.has_older
+            for thread in start_at.inbox.threads:
+                yield thread
+        else:
+            cursor = None
+            seq_id = None
+            has_more = True
         while has_more:
             resp = await self.get_inbox(message_limit=message_limit, cursor=cursor, seq_id=seq_id)
             seq_id = resp.seq_id

+ 1 - 1
mautrix_instagram/config.py

@@ -59,7 +59,7 @@ class Config(BaseBridgeConfig):
 
         copy("bridge.displayname_max_length")
 
-        copy("bridge.initial_conversation_sync")
+        copy("bridge.portal_create_max_age")
         copy("bridge.sync_with_custom_puppets")
         copy("bridge.sync_direct_chat_list")
         copy("bridge.double_puppet_server_map")

+ 2 - 3
mautrix_instagram/example-config.yaml

@@ -76,9 +76,8 @@ bridge:
     # Maximum length of displayname
     displayname_max_length: 100
 
-    # Number of conversations to sync (and create portals for) on login.
-    # Set 0 to disable automatic syncing.
-    initial_conversation_sync: 10
+    # Maximum number of seconds since the last activity in a chat to automatically create portals.
+    portal_create_max_age: 86400
     # Whether or not to use /sync to get read receipts and typing notifications
     # when double puppeting is enabled
     sync_with_custom_puppets: true

+ 4 - 0
mautrix_instagram/portal.py

@@ -384,6 +384,10 @@ class Portal(DBPortal, BasePortal):
             # TODO send error message
             return None
         reuploaded = await method(source, media_data, intent)
+        if not reuploaded:
+            self.log.trace(f"Upload of {media_data} failed")
+            # TODO error message?
+            return None
         content = MediaMessageEventContent(body=reuploaded.file_name, external_url=reuploaded.url,
                                            url=reuploaded.mxc, file=reuploaded.decryption_info,
                                            info=reuploaded.info, msgtype=reuploaded.msgtype)

+ 8 - 6
mautrix_instagram/user.py

@@ -204,16 +204,18 @@ class User(DBUser, BaseUser):
 
     async def sync(self) -> None:
         resp = await self.client.get_inbox()
-        limit = self.config["bridge.initial_conversation_sync"]
-        threads = sorted(resp.inbox.threads, key=lambda thread: thread.last_activity_at)
-        if limit < 0:
-            limit = len(threads)
-        for i, thread in enumerate(threads):
+        max_age = self.config["bridge.portal_create_max_age"] * 1_000_000
+        min_active_at = (time.time() * 1_000_000) - max_age
+        async for thread in self.client.iter_inbox(start_at=resp):
             portal = await po.Portal.get_by_thread(thread, self.igpk)
             if portal.mxid:
+                self.log.debug(f"{thread.thread_id} has a portal, syncing and backfilling...")
                 await portal.update_matrix_room(self, thread, backfill=True)
-            elif i < limit:
+            elif thread.last_activity_at > min_active_at:
+                self.log.debug(f"{thread.thread_id} has been active recently, creating portal...")
                 await portal.create_matrix_room(self, thread)
+            else:
+                self.log.debug(f"{thread.thread_id} is not active and doesn't have a portal")
         await self.update_direct_chats()
 
         self._listen_task = self.loop.create_task(self.mqtt.listen(