Add filter

This commit is contained in:
2025-05-09 21:01:22 +00:00
parent f5c6b535d8
commit f0c4b350a9
9 changed files with 205 additions and 805 deletions

View File

@@ -2,7 +2,7 @@ from core.clients import ClientBase
from django.conf import settings
from slixmpp.componentxmpp import ComponentXMPP
from django.conf import settings
from core.models import User, Person, PersonIdentifier, ChatSession
from core.models import User, Person, PersonIdentifier, ChatSession, Manipulation
from asgiref.sync import sync_to_async
from django.utils.timezone import now
import asyncio
@@ -14,6 +14,7 @@ from slixmpp.xmlstream.stanzabase import ET
import aiohttp
from core.messaging import history
from core.util import logs
from core.messaging import replies, utils, ai
class XMPPComponent(ComponentXMPP):
@@ -503,17 +504,52 @@ class XMPPComponent(ComponentXMPP):
session=session,
sender="XMPP",
text=body,
ts=now().timestamp(),
ts=int(now().timestamp() * 1000),
#outgoing=detail.is_outgoing_message, ????????? TODO:
)
self.log.info("Stored a message sent from XMPP in the history.")
manipulations = Manipulation.objects.filter(
group__people=identifier.person,
user=identifier.user,
mode="mutate",
enabled=True,
)
self.log.info(f"MANIP11 {manipulations}")
if not manipulations:
tss = await signalapi.send_message_raw(
identifier.identifier,
body,
attachments,
)
self.log.info(f"Message sent unaltered")
return
manip = manipulations.first()
chat_history = await history.get_chat_history(session)
await utils.update_last_interaction(session)
prompt = replies.generate_mutate_reply_prompt(
body,
identifier.person,
manip,
chat_history,
)
self.log.info("Running XMPP context prompt")
result = await ai.run_prompt(prompt, manip.ai)
self.log.info(f"RESULT {result}")
await history.store_own_message(
session=session,
text=result,
ts=int(now().timestamp() * 1000),
)
tss = await signalapi.send_message_raw(
identifier.identifier,
body,
result,
attachments,
)
self.log.info(f"Message sent")
self.log.info(f"Message sent with modifications")
async def request_upload_slots(self, recipient_jid, attachments):
"""Requests upload slots for multiple attachments concurrently."""
@@ -567,30 +603,10 @@ class XMPPComponent(ComponentXMPP):
sender_jid = f"{person_identifier.person.name.lower()}|{person_identifier.service}@{settings.XMPP_JID}"
recipient_jid = f"{person_identifier.user.username}@{settings.XMPP_ADDRESS}"
if is_outgoing_message:
self.log.info(f"Forwarding outgoing message as {recipient_jid}")
# Create the message as if it were sent by the user
msg = self.make_message(mto=recipient_jid, mfrom=sender_jid, mtype="chat")
msg["body"] = text # Original message content
# Create the forwarded <message> inside the <forwarded> element
forwarded_elem = ET.Element("{urn:xmpp:forward:0}forwarded")
# Create a <message> element inside the forwarded stanza
message_elem = ET.Element("message", attrib={"from": recipient_jid, "to": recipient_jid, "type": "chat"})
body_elem = ET.SubElement(message_elem, "body")
body_elem.text = text
# Attach the forwarded message
forwarded_elem.append(message_elem)
msg.xml.append(forwarded_elem)
# Send the forwarded message
msg.send()
await self.send_xmpp_message(recipient_jid, sender_jid, f"YOU: {text}")
# Step 1: Send text message separately
if text:
elif text:
await self.send_xmpp_message(recipient_jid, sender_jid, text)
if not attachments: