plugin XEP-0045: Notification on mention:

If a mention is detected in a MUC (`groupchat`, not DM), a notification is sent.
This commit is contained in:
Goffi 2026-06-26 23:03:24 +02:00
commit 7d2d97fa75
2 changed files with 59 additions and 7 deletions

View file

@ -2218,6 +2218,14 @@ class Memory:
data_format.serialise(extra) if extra else "", data_format.serialise(extra) if extra else "",
C.PROF_KEY_ALL if is_global else client.profile, C.PROF_KEY_ALL if is_global else client.profile,
) )
log.debug(
f"Notification sent:\n"
f" title: {title!r}\n"
f" body_plain: {body_plain!r}\n"
f" type: {type_.value}\n"
f" priority: {priority.value}\n"
f" profile: {C.PROF_KEY_ALL if is_global else client.profile}"
)
def _get_notifications(self, filters_s: str, profile_key: str) -> defer.Deferred: def _get_notifications(self, filters_s: str, profile_key: str) -> defer.Deferred:
"""Fetch notifications for bridge with given filters and profile key. """Fetch notifications for bridge with given filters and profile key.

View file

@ -17,24 +17,23 @@
# You should have received a copy of the GNU Affero General Public License # You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>. # along with this program. If not, see <http://www.gnu.org/licenses/>.
from collections.abc import Sequence
from dataclasses import dataclass from dataclasses import dataclass
import difflib import difflib
from enum import StrEnum from enum import StrEnum
import re
import time import time
from typing import TYPE_CHECKING, cast from typing import TYPE_CHECKING, cast
from collections.abc import Sequence
from twisted.python.failure import Failure
import uuid import uuid
from zope.interface import implementer from pydantic import BaseModel, Field
from pydantic import Field
import shortuuid import shortuuid
from twisted.internet import defer from twisted.internet import defer
from twisted.python import failure from twisted.python import failure
from twisted.words.protocols.jabber import jid from twisted.words.protocols.jabber import jid
from twisted.words.protocols.jabber.error import StanzaError from twisted.words.protocols.jabber.error import StanzaError
from tx_xmpp import disco, itx_xmpp, mam, muc, rsm from tx_xmpp import disco, itx_xmpp, mam, muc, rsm
from zope.interface import implementer
from libervia.backend import G from libervia.backend import G
from libervia.backend.core import exceptions from libervia.backend.core import exceptions
@ -43,6 +42,9 @@ from libervia.backend.core.core_types import SatXMPPEntity
from libervia.backend.core.i18n import D_, _ from libervia.backend.core.i18n import D_, _
from libervia.backend.core.log import getLogger from libervia.backend.core.log import getLogger
from libervia.backend.memory import memory from libervia.backend.memory import memory
from libervia.backend.memory.sqla_mapping import NotificationType
from libervia.backend.models.core import MessageData
from libervia.backend.models.types import JIDType
from libervia.backend.plugins import plugin_misc_jid_search from libervia.backend.plugins import plugin_misc_jid_search
from libervia.backend.plugins.plugin_xep_0313 import XEP_0313 from libervia.backend.plugins.plugin_xep_0313 import XEP_0313
from libervia.backend.plugins.plugin_xep_0359 import XEP_0359 from libervia.backend.plugins.plugin_xep_0359 import XEP_0359
@ -105,6 +107,15 @@ class OccupantInfo:
role: str role: str
class NotificationExtraMention(BaseModel):
"""Extra data for a MUC mention notification."""
message_uid: str
from_jid: JIDType
to_jid: JIDType
room_jid: JIDType
class PluginsMucSettings(PluginSettings, plugin_info=PLUGIN_INFO): class PluginsMucSettings(PluginSettings, plugin_info=PLUGIN_INFO):
default_muc: str = Field(default="libervia@chat.jabberfr.org") default_muc: str = Field(default="libervia@chat.jabberfr.org")
@ -279,13 +290,46 @@ class XEP_0045:
).muc_client.joined_rooms ).muc_client.joined_rooms
): ):
log.warning( log.warning(
"Received groupchat message for unjoined room, ignoring: %s", "Received groupchat message for unjoined room, ignoring: "
message_elt.toXml(), f"{message_elt.toXml()}"
) )
return False return False
post_treat.addCallback(
lambda data: defer.ensureDeferred(self._check_mention(data, client, room_jid))
)
return True return True
async def _check_mention(
self, data: MessageData, client: SatXMPPEntity, room_jid: jid.JID
) -> MessageData:
from_jid = data["from"]
room = self.get_room(client, room_jid)
if from_jid == room.occupantJID:
return data
body = data["message"].get("") or next(iter(data["message"].values()), "")
if not body:
return data
if not re.search(rf"(?<!\w){re.escape(room.nick)}(?!\w)", body, re.I):
return data
extra = NotificationExtraMention(
message_uid=data["uid"],
from_jid=from_jid,
to_jid=data["to"],
room_jid=room_jid,
)
await self.host.memory.add_notification(
client,
NotificationType.chat,
body[:200],
title=D_("{nick} ({room})").format(room=room_jid, nick=from_jid.resource),
extra=extra.model_dump(mode="json"),
)
return data
async def jid_search_perform_search_trigger( async def jid_search_perform_search_trigger(
self, self,
client: SatXMPPEntity, client: SatXMPPEntity,