annotate libervia/backend/plugins/plugin_xep_0308.py @ 4306:94e0968987cd

plugin XEP-0033: code modernisation, improve delivery, data validation: - Code has been rewritten using Pydantic models and `async` coroutines for data validation and cleaner element parsing/generation. - Delivery has been completely rewritten. It now works even if server doesn't support multicast, and send to local multicast service first. Delivering to local multicast service first is due to bad support of XEP-0033 in server (notably Prosody which has an incomplete implementation), and the current impossibility to detect if a sub-domain service handles fully multicast or only for local domains. This is a workaround to have a good balance between backward compatilibity and use of bandwith, and to make it work with the incoming email gateway implementation (the gateway will only deliver to entities of its own domain). - disco feature checking now uses `async` corountines. `host` implementation still use Deferred return values for compatibility with legacy code. rel 450
author Goffi <goffi@goffi.org>
date Thu, 26 Sep 2024 16:12:01 +0200
parents 0d7bb4df2343
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
4163
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
1 #!/usr/bin/env python3
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
2
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
3 # Copyright (C) 2009-2023 Jérôme Poisson (goffi@goffi.org)
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
4
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
5 # This program is free software: you can redistribute it and/or modify
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
6 # it under the terms of the GNU Affero General Public License as published by
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
7 # the Free Software Foundation, either version 3 of the License, or
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
8 # (at your option) any later version.
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
9
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
10 # This program is distributed in the hope that it will be useful,
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
13 # GNU Affero General Public License for more details.
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
14
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
15 # You should have received a copy of the GNU Affero General Public License
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
16 # along with this program. If not, see <http://www.gnu.org/licenses/>.
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
17
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
18 import time
4198
b1207332cea2 plugin XEP-0308: fix ID used, recipients and author check.
Goffi <goffi@goffi.org>
parents: 4166
diff changeset
19 import uuid
4163
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
20
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
21 from sqlalchemy.orm.attributes import flag_modified
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
22 from twisted.internet import defer
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
23 from twisted.words.protocols.jabber import xmlstream
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
24 from twisted.words.protocols.jabber import jid
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
25 from twisted.words.xish import domish
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
26 from wokkel import disco
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
27 from zope.interface import implementer
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
28
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
29 from libervia.backend.core import exceptions
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
30 from libervia.backend.core.constants import Const as C
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
31 from libervia.backend.core.core_types import SatXMPPEntity
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
32 from libervia.backend.core.i18n import _
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
33 from libervia.backend.core.log import getLogger
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
34 from libervia.backend.memory.sqla import History, Message, Subject, joinedload, select
4166
a1f7040b5a15 plugin XEP-0424: message retraction update:
Goffi <goffi@goffi.org>
parents: 4163
diff changeset
35 from libervia.backend.models.core import MessageData, MessageEdition
4163
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
36 from libervia.backend.tools.common import data_format
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
37 from libervia.backend.tools.utils import aio
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4198
diff changeset
38
4163
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
39 log = getLogger(__name__)
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
40
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
41
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
42 PLUGIN_INFO = {
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
43 C.PI_NAME: "Last Message Correction",
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
44 C.PI_IMPORT_NAME: "XEP-0308",
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
45 C.PI_TYPE: "XEP",
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
46 C.PI_PROTOCOLS: ["XEP-0308"],
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
47 C.PI_DEPENDENCIES: ["XEP-0334"],
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
48 C.PI_MAIN: "XEP_0308",
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
49 C.PI_HANDLER: "yes",
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
50 C.PI_DESCRIPTION: _("""Implementation of XEP-0308 (Last Message Correction)"""),
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
51 }
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
52
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
53 NS_MESSAGE_CORRECT = "urn:xmpp:message-correct:0"
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
54
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
55
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
56 class XEP_0308:
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
57 def __init__(self, host):
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
58 log.info(f"plugin {PLUGIN_INFO[C.PI_NAME]!r} initialization")
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
59 self.host = host
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
60 host.register_namespace("message_correct", NS_MESSAGE_CORRECT)
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
61 host.trigger.add("message_received", self._message_received_trigger)
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
62 host.bridge.add_method(
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
63 "message_edit",
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
64 ".plugin",
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
65 in_sign="sss",
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
66 out_sign="",
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
67 method=self._message_edit,
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
68 async_=True,
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
69 )
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
70 self._h = host.plugins["XEP-0334"]
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
71
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
72 def get_handler(self, client):
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
73 return XEP_0308_handler()
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
74
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
75 @aio
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
76 async def get_last_history(
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
77 self, client: SatXMPPEntity, message_elt: domish.Element
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
78 ) -> History | None:
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
79 profile_id = self.host.memory.storage.profiles[client.profile]
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
80 from_jid = jid.JID(message_elt["from"])
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
81 message_type = message_elt.getAttribute("type", C.MESS_TYPE_NORMAL)
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
82 async with self.host.memory.storage.session() as session:
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
83 stmt = (
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
84 select(History)
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
85 .where(
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
86 History.profile_id == profile_id,
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
87 History.source == from_jid.userhost(),
4198
b1207332cea2 plugin XEP-0308: fix ID used, recipients and author check.
Goffi <goffi@goffi.org>
parents: 4166
diff changeset
88 History.dest == client.jid.userhost(),
4163
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
89 History.type == message_type,
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
90 )
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
91 .options(joinedload(History.messages))
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
92 .options(joinedload(History.subjects))
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
93 .options(joinedload(History.thread))
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
94 )
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
95 if message_elt.type == C.MESS_TYPE_GROUPCHAT:
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
96 stmt = stmt.where(History.source_res == from_jid.resource)
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
97
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
98 # we want last message
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
99 stmt = stmt.order_by(History.timestamp.desc()).limit(1)
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
100 result = await session.execute(stmt)
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
101 history = result.unique().scalar_one_or_none()
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
102 return history
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
103
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
104 async def update_history(
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
105 self,
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
106 client: SatXMPPEntity,
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
107 edited_history: History,
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
108 edit_timestamp: float,
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
109 new_message: dict[str, str],
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
110 new_subject: dict[str, str],
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
111 new_extra: dict,
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
112 previous_message: dict[str, str],
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
113 previous_subject: dict[str, str],
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
114 previous_extra: dict | None,
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
115 store: bool = True,
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
116 ) -> None:
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
117 # FIXME: new_extra is not handled by now
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
118 edited_history.messages = [
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
119 Message(message=mess, language=lang) for lang, mess in new_message.items()
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
120 ]
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
121 edited_history.subjects = [
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
122 Subject(subject=mess, language=lang) for lang, mess in new_subject.items()
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
123 ]
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
124 previous_version = {
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
125 # this is the timestamp when this version was published
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
126 "timestamp": edited_history.extra.get("updated", edited_history.timestamp),
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
127 "message": previous_message,
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
128 "subject": previous_subject,
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
129 }
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
130 edited_history.extra["updated"] = edit_timestamp
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
131 if previous_extra:
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
132 previous_extra = previous_extra.copy()
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
133 # we must not have editions in each edition
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
134 try:
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
135 del previous_extra[C.MESS_EXTRA_EDITIONS]
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
136 except KeyError:
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
137 pass
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
138 # extra may be important for rich content
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
139 previous_version["extra"] = previous_extra
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
140
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
141 if store:
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
142 flag_modified(edited_history, "extra")
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4198
diff changeset
143 edited_history.extra.setdefault(C.MESS_EXTRA_EDITIONS, []).append(
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4198
diff changeset
144 previous_version
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4198
diff changeset
145 )
4163
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
146 await self.host.memory.storage.add(edited_history)
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
147
4166
a1f7040b5a15 plugin XEP-0424: message retraction update:
Goffi <goffi@goffi.org>
parents: 4163
diff changeset
148 edit_data = MessageData(edited_history.serialise())
4163
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
149 self.host.bridge.message_update(
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
150 edited_history.uid,
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
151 C.MESS_UPDATE_EDIT,
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
152 data_format.serialise(edit_data),
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
153 client.profile,
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
154 )
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
155
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
156 async def _message_received_trigger(
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
157 self,
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
158 client: SatXMPPEntity,
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
159 message_elt: domish.Element,
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
160 post_treat: defer.Deferred,
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
161 ) -> bool:
4198
b1207332cea2 plugin XEP-0308: fix ID used, recipients and author check.
Goffi <goffi@goffi.org>
parents: 4166
diff changeset
162 from_jid = jid.JID(message_elt["from"])
4163
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
163 replace_elt = next(message_elt.elements(NS_MESSAGE_CORRECT, "replace"), None)
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
164 if not replace_elt:
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
165 return True
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
166 try:
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
167 replace_id = replace_elt["id"].strip()
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
168 if not replace_id:
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
169 raise KeyError
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
170 except KeyError:
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
171 log.warning(f"Invalid message correction: {message_elt.toXml()}")
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
172 else:
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
173 edited_history = await self.get_last_history(client, message_elt)
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
174 if edited_history is None:
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
175 log.warning(
4198
b1207332cea2 plugin XEP-0308: fix ID used, recipients and author check.
Goffi <goffi@goffi.org>
parents: 4166
diff changeset
176 f"No message found from {from_jid.full()}, can't correct "
4163
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
177 f"anything: {message_elt.toXml()}"
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
178 )
4198
b1207332cea2 plugin XEP-0308: fix ID used, recipients and author check.
Goffi <goffi@goffi.org>
parents: 4166
diff changeset
179 return True
b1207332cea2 plugin XEP-0308: fix ID used, recipients and author check.
Goffi <goffi@goffi.org>
parents: 4166
diff changeset
180 check_id = edited_history.origin_id or edited_history.extra.get("message_id")
b1207332cea2 plugin XEP-0308: fix ID used, recipients and author check.
Goffi <goffi@goffi.org>
parents: 4166
diff changeset
181 if check_id != replace_id:
4163
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
182 log.warning(
4198
b1207332cea2 plugin XEP-0308: fix ID used, recipients and author check.
Goffi <goffi@goffi.org>
parents: 4166
diff changeset
183 "Can't apply correction: it doesn't reference the last one:\n"
b1207332cea2 plugin XEP-0308: fix ID used, recipients and author check.
Goffi <goffi@goffi.org>
parents: 4166
diff changeset
184 f"{message_elt.toXml()}"
4163
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
185 )
4198
b1207332cea2 plugin XEP-0308: fix ID used, recipients and author check.
Goffi <goffi@goffi.org>
parents: 4166
diff changeset
186 return True
b1207332cea2 plugin XEP-0308: fix ID used, recipients and author check.
Goffi <goffi@goffi.org>
parents: 4166
diff changeset
187 # TODO: this only accept same full JID, if we do more than last message
b1207332cea2 plugin XEP-0308: fix ID used, recipients and author check.
Goffi <goffi@goffi.org>
parents: 4166
diff changeset
188 # correction, we may want to accept different resource for non groupchat
b1207332cea2 plugin XEP-0308: fix ID used, recipients and author check.
Goffi <goffi@goffi.org>
parents: 4166
diff changeset
189 # messages (in case of reconnection with resource change for instance).
b1207332cea2 plugin XEP-0308: fix ID used, recipients and author check.
Goffi <goffi@goffi.org>
parents: 4166
diff changeset
190 if edited_history.source_jid != from_jid:
b1207332cea2 plugin XEP-0308: fix ID used, recipients and author check.
Goffi <goffi@goffi.org>
parents: 4166
diff changeset
191 log.warning(
b1207332cea2 plugin XEP-0308: fix ID used, recipients and author check.
Goffi <goffi@goffi.org>
parents: 4166
diff changeset
192 "Can't apply correction: correction doesn't come from same author "
b1207332cea2 plugin XEP-0308: fix ID used, recipients and author check.
Goffi <goffi@goffi.org>
parents: 4166
diff changeset
193 f"{edited_history.source_jid.full()}:\n{message_elt.toXml()}"
b1207332cea2 plugin XEP-0308: fix ID used, recipients and author check.
Goffi <goffi@goffi.org>
parents: 4166
diff changeset
194 )
b1207332cea2 plugin XEP-0308: fix ID used, recipients and author check.
Goffi <goffi@goffi.org>
parents: 4166
diff changeset
195 return True
b1207332cea2 plugin XEP-0308: fix ID used, recipients and author check.
Goffi <goffi@goffi.org>
parents: 4166
diff changeset
196
4163
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
197 previous_message_data = edited_history.serialise()
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
198 message_data = client.messageProt.parse_message(message_elt)
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
199 if not message_data["message"] and not message_data["subject"]:
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
200 log.warning(
4198
b1207332cea2 plugin XEP-0308: fix ID used, recipients and author check.
Goffi <goffi@goffi.org>
parents: 4166
diff changeset
201 "Message correction doesn't have body nor subject, we can't edit "
4163
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
202 "anything"
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
203 )
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
204 return False
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
205
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
206 await self.update_history(
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
207 client,
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
208 edited_history,
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
209 message_data.get("received_timestamp") or message_data["timestamp"],
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
210 message_data["message"],
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
211 message_data["subject"],
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
212 message_data["extra"],
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
213 previous_message_data["message"],
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
214 previous_message_data["subject"],
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
215 previous_message_data.get("extra"),
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
216 )
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
217
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
218 return False
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
219
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
220 async def message_edit(
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
221 self,
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
222 client: SatXMPPEntity,
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
223 message_id: str,
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
224 edit_data: MessageEdition,
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
225 ) -> None:
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
226 """Edit a message
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
227
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
228 The message can only be edited if it's the last one of the discussion.
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
229 @param client: client instance
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
230 @param message_id: UID of the message to edit
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
231 @param edit_data: data to update in the message
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
232 """
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
233 timestamp = time.time()
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
234 edited_history = await self.host.memory.storage.get(
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
235 client,
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
236 History,
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
237 History.uid,
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
238 message_id,
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
239 joined_loads=[History.messages, History.subjects, History.thread],
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
240 )
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
241 if edited_history is None:
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
242 raise exceptions.NotFound(
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
243 f"message to edit not found in database ({message_id})"
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
244 )
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
245 if edited_history.type == C.MESS_TYPE_GROUPCHAT:
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
246 is_group_chat = True
4198
b1207332cea2 plugin XEP-0308: fix ID used, recipients and author check.
Goffi <goffi@goffi.org>
parents: 4166
diff changeset
247 # In the case of group chat, our message is sent by ourself with our room JID.
b1207332cea2 plugin XEP-0308: fix ID used, recipients and author check.
Goffi <goffi@goffi.org>
parents: 4166
diff changeset
248 peer_jid = edited_history.source_jid
4163
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
249 else:
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
250 is_group_chat = False
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
251 peer_jid = jid.JID(edited_history.dest)
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
252 history_data = await self.host.memory.history_get(
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
253 client.jid, peer_jid, limit=1, profile=client.profile
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
254 )
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
255 if not history_data:
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
256 raise exceptions.NotFound(
4198
b1207332cea2 plugin XEP-0308: fix ID used, recipients and author check.
Goffi <goffi@goffi.org>
parents: 4166
diff changeset
257 "No message found in conversation with {peer_jid.userhost()}"
4163
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
258 )
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
259 last_mess = history_data[0]
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
260 if last_mess[0] != message_id:
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
261 raise ValueError(
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
262 f"{message_id} is not the last message of the discussion, we can't edit "
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
263 "it"
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
264 )
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
265
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
266 await self.update_history(
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
267 client,
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
268 edited_history,
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
269 timestamp,
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
270 edit_data.message,
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
271 edit_data.subject,
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
272 edit_data.extra,
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
273 last_mess[4],
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
274 last_mess[5],
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
275 last_mess[-1],
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
276 # message will be updated and signal sent on reception in group chat
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4198
diff changeset
277 store=not is_group_chat,
4163
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
278 )
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
279
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
280 serialised = edited_history.serialise()
4198
b1207332cea2 plugin XEP-0308: fix ID used, recipients and author check.
Goffi <goffi@goffi.org>
parents: 4166
diff changeset
281 serialised["uid"] = str(uuid.uuid4())
b1207332cea2 plugin XEP-0308: fix ID used, recipients and author check.
Goffi <goffi@goffi.org>
parents: 4166
diff changeset
282 serialised["from"] = client.jid
b1207332cea2 plugin XEP-0308: fix ID used, recipients and author check.
Goffi <goffi@goffi.org>
parents: 4166
diff changeset
283 # for group chat, we want to send the correction to the room
b1207332cea2 plugin XEP-0308: fix ID used, recipients and author check.
Goffi <goffi@goffi.org>
parents: 4166
diff changeset
284 serialised["to"] = peer_jid.userhostJID() if is_group_chat else peer_jid
4163
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
285
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
286 message_elt = client.generate_message_xml(serialised)["xml"]
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
287 replace_elt = message_elt.addElement((NS_MESSAGE_CORRECT, "replace"))
4198
b1207332cea2 plugin XEP-0308: fix ID used, recipients and author check.
Goffi <goffi@goffi.org>
parents: 4166
diff changeset
288 replace_elt["id"] = edited_history.origin_id
4163
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
289 self._h.add_hint_elements(message_elt, [self._h.HINT_STORE])
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
290 client.send(message_elt)
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
291
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
292 def _message_edit(self, message_id: str, edit_data_s: str, profile: str) -> None:
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
293 client = self.host.get_client(profile)
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
294 edit_data = MessageEdition.model_validate_json(edit_data_s)
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
295 defer.ensureDeferred(self.message_edit(client, message_id, edit_data))
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
296
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
297
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
298 @implementer(disco.IDisco)
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
299 class XEP_0308_handler(xmlstream.XMPPHandler):
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
300 def getDiscoInfo(self, __, target, nodeIdentifier=""):
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
301 return [disco.DiscoFeature(NS_MESSAGE_CORRECT)]
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
302
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
303 def getDiscoItems(self, requestor, target, nodeIdentifier=""):
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
304 return []