annotate libervia/backend/plugins/plugin_xep_0308.py @ 4177:0f1a4ffcd419

doc (cli/blog): Document new `--alt-link` option
author Goffi <goffi@goffi.org>
date Tue, 05 Dec 2023 13:14:55 +0100
parents a1f7040b5a15
children b1207332cea2
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
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
19
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
20 from sqlalchemy.orm.attributes import flag_modified
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
21 from twisted.internet import defer
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
22 from twisted.words.protocols.jabber import xmlstream
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
23 from twisted.words.protocols.jabber import jid
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
24 from twisted.words.xish import domish
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
25 from wokkel import disco
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
26 from zope.interface import implementer
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
27
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
28 from libervia.backend.core import exceptions
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
29 from libervia.backend.core.constants import Const as C
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
30 from libervia.backend.core.core_types import SatXMPPEntity
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
31 from libervia.backend.core.i18n import _
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
32 from libervia.backend.core.log import getLogger
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
33 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
34 from libervia.backend.models.core import MessageData, MessageEdition
4163
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
35 from libervia.backend.tools.common import data_format
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
36 from libervia.backend.tools.utils import aio
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
37 log = getLogger(__name__)
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
38
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
39
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
40 PLUGIN_INFO = {
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
41 C.PI_NAME: "Last Message Correction",
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
42 C.PI_IMPORT_NAME: "XEP-0308",
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
43 C.PI_TYPE: "XEP",
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
44 C.PI_PROTOCOLS: ["XEP-0308"],
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
45 C.PI_DEPENDENCIES: ["XEP-0334"],
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
46 C.PI_MAIN: "XEP_0308",
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
47 C.PI_HANDLER: "yes",
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
48 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
49 }
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
50
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
51 NS_MESSAGE_CORRECT = "urn:xmpp:message-correct:0"
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
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
54 class XEP_0308:
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
55 def __init__(self, host):
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
56 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
57 self.host = host
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
58 host.register_namespace("message_correct", NS_MESSAGE_CORRECT)
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
59 host.trigger.add("message_received", self._message_received_trigger)
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
60 host.bridge.add_method(
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
61 "message_edit",
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
62 ".plugin",
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
63 in_sign="sss",
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
64 out_sign="",
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
65 method=self._message_edit,
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
66 async_=True,
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
67 )
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
68 self._h = host.plugins["XEP-0334"]
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 def get_handler(self, client):
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
71 return XEP_0308_handler()
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
72
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
73 @aio
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
74 async def get_last_history(
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
75 self, client: SatXMPPEntity, message_elt: domish.Element
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
76 ) -> History | None:
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
77 profile_id = self.host.memory.storage.profiles[client.profile]
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
78 from_jid = jid.JID(message_elt["from"])
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
79 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
80 async with self.host.memory.storage.session() as session:
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
81 stmt = (
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
82 select(History)
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
83 .where(
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
84 History.profile_id == profile_id,
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
85 History.source == from_jid.userhost(),
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
86 History.type == message_type,
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
87 )
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
88 .options(joinedload(History.messages))
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
89 .options(joinedload(History.subjects))
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
90 .options(joinedload(History.thread))
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
91 )
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
92 if message_elt.type == C.MESS_TYPE_GROUPCHAT:
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
93 stmt = stmt.where(History.source_res == from_jid.resource)
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 # we want last message
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
96 stmt = stmt.order_by(History.timestamp.desc()).limit(1)
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
97 result = await session.execute(stmt)
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
98 history = result.unique().scalar_one_or_none()
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
99 return history
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
100
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
101 async def update_history(
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
102 self,
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
103 client: SatXMPPEntity,
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
104 edited_history: History,
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
105 edit_timestamp: float,
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
106 new_message: dict[str, str],
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
107 new_subject: dict[str, str],
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
108 new_extra: dict,
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
109 previous_message: dict[str, str],
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
110 previous_subject: dict[str, str],
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
111 previous_extra: dict | None,
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
112 store: bool = True,
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
113 ) -> None:
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
114 # FIXME: new_extra is not handled by now
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
115 edited_history.messages = [
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
116 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
117 ]
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
118 edited_history.subjects = [
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
119 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
120 ]
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
121 previous_version = {
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
122 # this is the timestamp when this version was published
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
123 "timestamp": edited_history.extra.get("updated", edited_history.timestamp),
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
124 "message": previous_message,
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
125 "subject": previous_subject,
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
126 }
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
127 edited_history.extra["updated"] = edit_timestamp
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
128 if previous_extra:
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
129 previous_extra = previous_extra.copy()
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
130 # we must not have editions in each edition
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
131 try:
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
132 del previous_extra[C.MESS_EXTRA_EDITIONS]
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
133 except KeyError:
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
134 pass
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
135 # extra may be important for rich content
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
136 previous_version["extra"] = previous_extra
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
137
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
138 if store:
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
139 flag_modified(edited_history, "extra")
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
140 edited_history.extra.setdefault(C.MESS_EXTRA_EDITIONS, []).append(previous_version)
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
141 await self.host.memory.storage.add(edited_history)
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
142
4166
a1f7040b5a15 plugin XEP-0424: message retraction update:
Goffi <goffi@goffi.org>
parents: 4163
diff changeset
143 edit_data = MessageData(edited_history.serialise())
4163
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
144 self.host.bridge.message_update(
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
145 edited_history.uid,
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
146 C.MESS_UPDATE_EDIT,
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
147 data_format.serialise(edit_data),
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
148 client.profile,
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
149 )
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
150
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
151 async def _message_received_trigger(
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
152 self,
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
153 client: SatXMPPEntity,
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
154 message_elt: domish.Element,
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
155 post_treat: defer.Deferred,
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
156 ) -> bool:
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
157 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
158 if not replace_elt:
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
159 return True
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
160 try:
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
161 replace_id = replace_elt["id"].strip()
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
162 if not replace_id:
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
163 raise KeyError
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
164 except KeyError:
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
165 log.warning(f"Invalid message correction: {message_elt.toXml()}")
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
166 else:
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
167 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
168 if edited_history is None:
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
169 log.warning(
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
170 f"No message found from {message_elt['from']}, can't correct "
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
171 f"anything: {message_elt.toXml()}"
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
172 )
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
173 return False
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
174 if edited_history.extra.get("message_id") != replace_id:
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
175 log.warning(
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
176 "Can't apply correction: it doesn't reference the last one: "
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
177 f"{message_elt.toXml}"
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
178 )
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
179 return False
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
180 previous_message_data = edited_history.serialise()
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
181 message_data = client.messageProt.parse_message(message_elt)
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
182 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
183 log.warning(
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
184 "Message correction doesn't have body not subject, we can't edit "
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
185 "anything"
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
186 )
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
187 return False
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
188
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
189 await self.update_history(
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
190 client,
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
191 edited_history,
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
192 message_data.get("received_timestamp") or message_data["timestamp"],
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
193 message_data["message"],
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
194 message_data["subject"],
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
195 message_data["extra"],
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
196 previous_message_data["message"],
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
197 previous_message_data["subject"],
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
198 previous_message_data.get("extra"),
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
199 )
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
200
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
201 return False
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
202
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
203 async def message_edit(
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
204 self,
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
205 client: SatXMPPEntity,
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
206 message_id: str,
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
207 edit_data: MessageEdition,
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
208 ) -> None:
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
209 """Edit a message
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
210
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
211 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
212 @param client: client instance
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
213 @param message_id: UID of the message to edit
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
214 @param edit_data: data to update in the message
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
215 """
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
216 timestamp = time.time()
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
217 edited_history = await self.host.memory.storage.get(
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
218 client,
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
219 History,
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
220 History.uid,
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
221 message_id,
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
222 joined_loads=[History.messages, History.subjects, History.thread],
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
223 )
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
224 if edited_history is None:
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
225 raise exceptions.NotFound(
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
226 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
227 )
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
228 if edited_history.type == C.MESS_TYPE_GROUPCHAT:
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
229 is_group_chat = True
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
230 peer_jid = edited_history.dest_jid
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
231 else:
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
232 is_group_chat = False
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
233 peer_jid = jid.JID(edited_history.dest)
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
234 history_data = await self.host.memory.history_get(
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
235 client.jid, peer_jid, limit=1, profile=client.profile
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
236 )
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
237 if not history_data:
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
238 raise exceptions.NotFound(
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
239 "No message found in conversation with {peer_jid.full()}"
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 last_mess = history_data[0]
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
242 if last_mess[0] != message_id:
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
243 raise ValueError(
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
244 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
245 "it"
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
246 )
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
247
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
248 await self.update_history(
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
249 client,
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
250 edited_history,
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
251 timestamp,
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
252 edit_data.message,
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
253 edit_data.subject,
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
254 edit_data.extra,
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
255 last_mess[4],
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
256 last_mess[5],
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
257 last_mess[-1],
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
258 # message will be updated and signal sent on reception in group chat
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
259 store = not is_group_chat
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
260 )
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
261
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
262 serialised = edited_history.serialise()
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
263 serialised["from"] = jid.JID(serialised["from"])
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
264 serialised["to"] = jid.JID(serialised["to"])
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 message_elt = client.generate_message_xml(serialised)["xml"]
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
267 replace_elt = message_elt.addElement((NS_MESSAGE_CORRECT, "replace"))
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
268 replace_elt["id"] = message_id
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
269 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
270 client.send(message_elt)
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
271
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
272 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
273 client = self.host.get_client(profile)
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
274 edit_data = MessageEdition.model_validate_json(edit_data_s)
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
275 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
276
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
277
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
278 @implementer(disco.IDisco)
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
279 class XEP_0308_handler(xmlstream.XMPPHandler):
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
280 def getDiscoInfo(self, __, target, nodeIdentifier=""):
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
281 return [disco.DiscoFeature(NS_MESSAGE_CORRECT)]
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
282
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
283 def getDiscoItems(self, requestor, target, nodeIdentifier=""):
3b3cd9453d9b plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff changeset
284 return []