浏览代码

Provide fallback for sticker image if not supplied by signald

Manuel Kuhlmann 4 年之前
父节点
当前提交
d464b82555
共有 3 个文件被更改,包括 23 次插入5 次删除
  1. 3 2
      Dockerfile
  2. 19 3
      mautrix_signal/portal.py
  3. 1 0
      requirements.txt

+ 3 - 2
Dockerfile

@@ -19,9 +19,10 @@ RUN apk add --no-cache \
       ca-certificates \
       su-exec \
       # encryption
+      libressl \
       olm-dev \
       py3-cffi \
-	  py3-pycryptodome \
+      py3-pycryptodome \
       py3-unpaddedbase64 \
       py3-future \
       bash \
@@ -33,7 +34,7 @@ RUN apk add --no-cache \
 COPY requirements.txt /opt/mautrix-signal/requirements.txt
 COPY optional-requirements.txt /opt/mautrix-signal/optional-requirements.txt
 WORKDIR /opt/mautrix-signal
-RUN apk add --virtual .build-deps python3-dev libffi-dev build-base \
+RUN apk add --virtual .build-deps python3-dev libffi-dev libressl-dev build-base \
  && pip3 install -r requirements.txt -r optional-requirements.txt \
  && apk del .build-deps
 

+ 19 - 3
mautrix_signal/portal.py

@@ -24,6 +24,8 @@ import os.path
 import time
 import os
 
+from signalstickers_client import StickersClient
+
 from mausignald.types import (Address, MessageData, Reaction, Quote, Group, Contact, Profile,
                               Attachment, GroupID, GroupV2ID, GroupV2)
 from mautrix.appservice import AppService, IntentAPI
@@ -313,9 +315,23 @@ class Portal(DBPortal, BasePortal):
 
         if message.sticker:
             if not message.sticker.attachment.incoming_filename:
-                self.log.warning("Failed to bridge sticker, no incoming filename: %s",
-                                 message.sticker.attachment)
-            else:
+                self.log.debug("Downloading sticker from signal, as no incoming filename was defined: %s",
+                               message.sticker.attachment)
+                try:
+                    async with StickersClient() as client:
+                        sticker_data = await client.download_sticker(message.sticker.sticker_id,
+                                                                     message.sticker.pack_id,
+                                                                     message.sticker.pack_key)
+
+                    path = os.path.join(self.config["signal.outgoing_attachment_dir"],
+                                        f"{message.sticker.pack_id}_{message.sticker.sticker_id}")
+                    with open(path, "wb") as file:
+                        file.write(sticker_data)
+                    message.sticker.attachment.incoming_filename = path
+                except Exception as ex:
+                    self.log.warning("Failed to download sticker: %s", ex)
+
+            if message.sticker.attachment.incoming_filename:
                 content = await self._handle_signal_attachment(intent, message.sticker.attachment)
                 if reply_to:
                     content.set_reply(reply_to)

+ 1 - 0
requirements.txt

@@ -6,3 +6,4 @@ yarl>=1,<2
 attrs>=19.1
 mautrix>=0.8,<0.9
 asyncpg>=0.20,<0.22
+signalstickers-client>=3.0