Implement bridging Signal and XMPP
This commit is contained in:
@@ -9,6 +9,9 @@ from core.clients import signal
|
||||
from core.lib.prompts.functions import delete_messages
|
||||
from asgiref.sync import sync_to_async
|
||||
from django.conf import settings
|
||||
from core.clients import signalapi
|
||||
import asyncio
|
||||
|
||||
|
||||
|
||||
log = logs.get_logger("deferred")
|
||||
@@ -27,6 +30,7 @@ class DeferredRequest(BaseModel):
|
||||
msg: Optional[str] = None
|
||||
service: Optional[str] = None
|
||||
detail: Optional[DeferredDetail] = None
|
||||
attachments: Optional[list] = None
|
||||
|
||||
|
||||
async def process_deferred(data: dict, **kwargs):
|
||||
@@ -60,22 +64,40 @@ async def process_deferred(data: dict, **kwargs):
|
||||
else:
|
||||
log.warning(f"Protocol not supported: {message.session.identifier.service}")
|
||||
return
|
||||
elif method == "xmpp":
|
||||
elif method == "xmpp": # send xmpp message
|
||||
xmpp = kwargs.get("xmpp")
|
||||
service = validated_data.service
|
||||
msg = validated_data.msg
|
||||
attachments = validated_data.attachments
|
||||
|
||||
# Get User from identifier
|
||||
identifiers = PersonIdentifier.objects.filter(
|
||||
identifier=validated_data.identifier,
|
||||
service=service,
|
||||
)
|
||||
xmpp_attachments = []
|
||||
|
||||
# Asynchronously fetch all attachments
|
||||
tasks = [signalapi.fetch_signal_attachment(att["id"]) for att in attachments]
|
||||
fetched_attachments = await asyncio.gather(*tasks)
|
||||
|
||||
for fetched, att in zip(fetched_attachments, attachments):
|
||||
if not fetched:
|
||||
log.warning(f"Failed to fetch attachment {att['id']} from Signal.")
|
||||
continue
|
||||
|
||||
# Attach fetched file to XMPP
|
||||
xmpp_attachments.append({
|
||||
"content": fetched["content"],
|
||||
"content_type": fetched["content_type"],
|
||||
"filename": fetched["filename"],
|
||||
"size": fetched["size"],
|
||||
})
|
||||
for identifier in identifiers:
|
||||
# Fair is fair, we can have multiple
|
||||
log.info(f"Sending {msg} from {identifier}")
|
||||
xmpp.send_from_external(identifier, msg, validated_data.detail)
|
||||
|
||||
#recipient_jid = f"{identifier.user.username}@{settings.XMPP_ADDRESS}"
|
||||
|
||||
log.info(f"Sending {len(xmpp_attachments)} attachments from Signal to XMPP.")
|
||||
await xmpp.send_from_external(identifier, msg, validated_data.detail, attachments=xmpp_attachments)
|
||||
else:
|
||||
log.warning(f"Method not yet supported: {method}")
|
||||
return
|
||||
Reference in New Issue
Block a user