Mercurial > libervia-backend
annotate libervia/backend/plugins/plugin_xep_0308.py @ 4230:314d3c02bb67
core (xmpp): Add a timeout for messages processing to avoid blocking the queue.
author | Goffi <goffi@goffi.org> |
---|---|
date | Sat, 06 Apr 2024 12:21:04 +0200 |
parents | b1207332cea2 |
children | 0d7bb4df2343 |
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 |
3b3cd9453d9b
plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
38 log = getLogger(__name__) |
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 |
3b3cd9453d9b
plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
41 PLUGIN_INFO = { |
3b3cd9453d9b
plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
42 C.PI_NAME: "Last Message Correction", |
3b3cd9453d9b
plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
43 C.PI_IMPORT_NAME: "XEP-0308", |
3b3cd9453d9b
plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
44 C.PI_TYPE: "XEP", |
3b3cd9453d9b
plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
45 C.PI_PROTOCOLS: ["XEP-0308"], |
3b3cd9453d9b
plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
46 C.PI_DEPENDENCIES: ["XEP-0334"], |
3b3cd9453d9b
plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
47 C.PI_MAIN: "XEP_0308", |
3b3cd9453d9b
plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
48 C.PI_HANDLER: "yes", |
3b3cd9453d9b
plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
49 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
|
50 } |
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 NS_MESSAGE_CORRECT = "urn:xmpp:message-correct:0" |
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 |
3b3cd9453d9b
plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
55 class XEP_0308: |
3b3cd9453d9b
plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
56 def __init__(self, host): |
3b3cd9453d9b
plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
57 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
|
58 self.host = host |
3b3cd9453d9b
plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
59 host.register_namespace("message_correct", NS_MESSAGE_CORRECT) |
3b3cd9453d9b
plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
60 host.trigger.add("message_received", self._message_received_trigger) |
3b3cd9453d9b
plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
61 host.bridge.add_method( |
3b3cd9453d9b
plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
62 "message_edit", |
3b3cd9453d9b
plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
63 ".plugin", |
3b3cd9453d9b
plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
64 in_sign="sss", |
3b3cd9453d9b
plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
65 out_sign="", |
3b3cd9453d9b
plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
66 method=self._message_edit, |
3b3cd9453d9b
plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
67 async_=True, |
3b3cd9453d9b
plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
68 ) |
3b3cd9453d9b
plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
69 self._h = host.plugins["XEP-0334"] |
3b3cd9453d9b
plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
70 |
3b3cd9453d9b
plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
71 def get_handler(self, client): |
3b3cd9453d9b
plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
72 return XEP_0308_handler() |
3b3cd9453d9b
plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
73 |
3b3cd9453d9b
plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
74 @aio |
3b3cd9453d9b
plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
75 async def get_last_history( |
3b3cd9453d9b
plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
76 self, client: SatXMPPEntity, message_elt: domish.Element |
3b3cd9453d9b
plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
77 ) -> History | None: |
3b3cd9453d9b
plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
78 profile_id = self.host.memory.storage.profiles[client.profile] |
3b3cd9453d9b
plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
79 from_jid = jid.JID(message_elt["from"]) |
3b3cd9453d9b
plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
80 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
|
81 async with self.host.memory.storage.session() as session: |
3b3cd9453d9b
plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
82 stmt = ( |
3b3cd9453d9b
plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
83 select(History) |
3b3cd9453d9b
plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
84 .where( |
3b3cd9453d9b
plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
85 History.profile_id == profile_id, |
3b3cd9453d9b
plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
86 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
|
87 History.dest == client.jid.userhost(), |
4163
3b3cd9453d9b
plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
88 History.type == message_type, |
3b3cd9453d9b
plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
89 ) |
3b3cd9453d9b
plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
90 .options(joinedload(History.messages)) |
3b3cd9453d9b
plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
91 .options(joinedload(History.subjects)) |
3b3cd9453d9b
plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
92 .options(joinedload(History.thread)) |
3b3cd9453d9b
plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
93 ) |
3b3cd9453d9b
plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
94 if message_elt.type == C.MESS_TYPE_GROUPCHAT: |
3b3cd9453d9b
plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
95 stmt = stmt.where(History.source_res == from_jid.resource) |
3b3cd9453d9b
plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
96 |
3b3cd9453d9b
plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
97 # we want last message |
3b3cd9453d9b
plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
98 stmt = stmt.order_by(History.timestamp.desc()).limit(1) |
3b3cd9453d9b
plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
99 result = await session.execute(stmt) |
3b3cd9453d9b
plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
100 history = result.unique().scalar_one_or_none() |
3b3cd9453d9b
plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
101 return history |
3b3cd9453d9b
plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
102 |
3b3cd9453d9b
plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
103 async def update_history( |
3b3cd9453d9b
plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
104 self, |
3b3cd9453d9b
plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
105 client: SatXMPPEntity, |
3b3cd9453d9b
plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
106 edited_history: History, |
3b3cd9453d9b
plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
107 edit_timestamp: float, |
3b3cd9453d9b
plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
108 new_message: dict[str, str], |
3b3cd9453d9b
plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
109 new_subject: dict[str, str], |
3b3cd9453d9b
plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
110 new_extra: dict, |
3b3cd9453d9b
plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
111 previous_message: dict[str, str], |
3b3cd9453d9b
plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
112 previous_subject: dict[str, str], |
3b3cd9453d9b
plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
113 previous_extra: dict | None, |
3b3cd9453d9b
plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
114 store: bool = True, |
3b3cd9453d9b
plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
115 ) -> None: |
3b3cd9453d9b
plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
116 # FIXME: new_extra is not handled by now |
3b3cd9453d9b
plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
117 edited_history.messages = [ |
3b3cd9453d9b
plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
118 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
|
119 ] |
3b3cd9453d9b
plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
120 edited_history.subjects = [ |
3b3cd9453d9b
plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
121 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
|
122 ] |
3b3cd9453d9b
plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
123 previous_version = { |
3b3cd9453d9b
plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
124 # this is the timestamp when this version was published |
3b3cd9453d9b
plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
125 "timestamp": edited_history.extra.get("updated", edited_history.timestamp), |
3b3cd9453d9b
plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
126 "message": previous_message, |
3b3cd9453d9b
plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
127 "subject": previous_subject, |
3b3cd9453d9b
plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
128 } |
3b3cd9453d9b
plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
129 edited_history.extra["updated"] = edit_timestamp |
3b3cd9453d9b
plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
130 if previous_extra: |
3b3cd9453d9b
plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
131 previous_extra = previous_extra.copy() |
3b3cd9453d9b
plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
132 # we must not have editions in each edition |
3b3cd9453d9b
plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
133 try: |
3b3cd9453d9b
plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
134 del previous_extra[C.MESS_EXTRA_EDITIONS] |
3b3cd9453d9b
plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
135 except KeyError: |
3b3cd9453d9b
plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
136 pass |
3b3cd9453d9b
plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
137 # extra may be important for rich content |
3b3cd9453d9b
plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
138 previous_version["extra"] = previous_extra |
3b3cd9453d9b
plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
139 |
3b3cd9453d9b
plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
140 if store: |
3b3cd9453d9b
plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
141 flag_modified(edited_history, "extra") |
3b3cd9453d9b
plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
142 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
|
143 await self.host.memory.storage.add(edited_history) |
3b3cd9453d9b
plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
144 |
4166
a1f7040b5a15
plugin XEP-0424: message retraction update:
Goffi <goffi@goffi.org>
parents:
4163
diff
changeset
|
145 edit_data = MessageData(edited_history.serialise()) |
4163
3b3cd9453d9b
plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
146 self.host.bridge.message_update( |
3b3cd9453d9b
plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
147 edited_history.uid, |
3b3cd9453d9b
plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
148 C.MESS_UPDATE_EDIT, |
3b3cd9453d9b
plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
149 data_format.serialise(edit_data), |
3b3cd9453d9b
plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
150 client.profile, |
3b3cd9453d9b
plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
151 ) |
3b3cd9453d9b
plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
152 |
3b3cd9453d9b
plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
153 async def _message_received_trigger( |
3b3cd9453d9b
plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
154 self, |
3b3cd9453d9b
plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
155 client: SatXMPPEntity, |
3b3cd9453d9b
plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
156 message_elt: domish.Element, |
3b3cd9453d9b
plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
157 post_treat: defer.Deferred, |
3b3cd9453d9b
plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
158 ) -> bool: |
4198
b1207332cea2
plugin XEP-0308: fix ID used, recipients and author check.
Goffi <goffi@goffi.org>
parents:
4166
diff
changeset
|
159 from_jid = jid.JID(message_elt["from"]) |
4163
3b3cd9453d9b
plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
160 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
|
161 if not replace_elt: |
3b3cd9453d9b
plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
162 return True |
3b3cd9453d9b
plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
163 try: |
3b3cd9453d9b
plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
164 replace_id = replace_elt["id"].strip() |
3b3cd9453d9b
plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
165 if not replace_id: |
3b3cd9453d9b
plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
166 raise KeyError |
3b3cd9453d9b
plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
167 except KeyError: |
3b3cd9453d9b
plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
168 log.warning(f"Invalid message correction: {message_elt.toXml()}") |
3b3cd9453d9b
plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
169 else: |
3b3cd9453d9b
plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
170 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
|
171 if edited_history is None: |
3b3cd9453d9b
plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
172 log.warning( |
4198
b1207332cea2
plugin XEP-0308: fix ID used, recipients and author check.
Goffi <goffi@goffi.org>
parents:
4166
diff
changeset
|
173 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
|
174 f"anything: {message_elt.toXml()}" |
3b3cd9453d9b
plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
175 ) |
4198
b1207332cea2
plugin XEP-0308: fix ID used, recipients and author check.
Goffi <goffi@goffi.org>
parents:
4166
diff
changeset
|
176 return True |
b1207332cea2
plugin XEP-0308: fix ID used, recipients and author check.
Goffi <goffi@goffi.org>
parents:
4166
diff
changeset
|
177 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
|
178 if check_id != replace_id: |
4163
3b3cd9453d9b
plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
179 log.warning( |
4198
b1207332cea2
plugin XEP-0308: fix ID used, recipients and author check.
Goffi <goffi@goffi.org>
parents:
4166
diff
changeset
|
180 "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
|
181 f"{message_elt.toXml()}" |
4163
3b3cd9453d9b
plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
182 ) |
4198
b1207332cea2
plugin XEP-0308: fix ID used, recipients and author check.
Goffi <goffi@goffi.org>
parents:
4166
diff
changeset
|
183 return True |
b1207332cea2
plugin XEP-0308: fix ID used, recipients and author check.
Goffi <goffi@goffi.org>
parents:
4166
diff
changeset
|
184 # 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
|
185 # 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
|
186 # 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
|
187 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
|
188 log.warning( |
b1207332cea2
plugin XEP-0308: fix ID used, recipients and author check.
Goffi <goffi@goffi.org>
parents:
4166
diff
changeset
|
189 "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
|
190 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
|
191 ) |
b1207332cea2
plugin XEP-0308: fix ID used, recipients and author check.
Goffi <goffi@goffi.org>
parents:
4166
diff
changeset
|
192 return True |
b1207332cea2
plugin XEP-0308: fix ID used, recipients and author check.
Goffi <goffi@goffi.org>
parents:
4166
diff
changeset
|
193 |
4163
3b3cd9453d9b
plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
194 previous_message_data = edited_history.serialise() |
3b3cd9453d9b
plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
195 message_data = client.messageProt.parse_message(message_elt) |
3b3cd9453d9b
plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
196 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
|
197 log.warning( |
4198
b1207332cea2
plugin XEP-0308: fix ID used, recipients and author check.
Goffi <goffi@goffi.org>
parents:
4166
diff
changeset
|
198 "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
|
199 "anything" |
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 await self.update_history( |
3b3cd9453d9b
plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
204 client, |
3b3cd9453d9b
plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
205 edited_history, |
3b3cd9453d9b
plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
206 message_data.get("received_timestamp") or message_data["timestamp"], |
3b3cd9453d9b
plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
207 message_data["message"], |
3b3cd9453d9b
plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
208 message_data["subject"], |
3b3cd9453d9b
plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
209 message_data["extra"], |
3b3cd9453d9b
plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
210 previous_message_data["message"], |
3b3cd9453d9b
plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
211 previous_message_data["subject"], |
3b3cd9453d9b
plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
212 previous_message_data.get("extra"), |
3b3cd9453d9b
plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
213 ) |
3b3cd9453d9b
plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
214 |
3b3cd9453d9b
plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
215 return False |
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 async def message_edit( |
3b3cd9453d9b
plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
218 self, |
3b3cd9453d9b
plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
219 client: SatXMPPEntity, |
3b3cd9453d9b
plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
220 message_id: str, |
3b3cd9453d9b
plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
221 edit_data: MessageEdition, |
3b3cd9453d9b
plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
222 ) -> None: |
3b3cd9453d9b
plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
223 """Edit a message |
3b3cd9453d9b
plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
224 |
3b3cd9453d9b
plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
225 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
|
226 @param client: client instance |
3b3cd9453d9b
plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
227 @param message_id: UID of the message to edit |
3b3cd9453d9b
plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
228 @param edit_data: data to update in the message |
3b3cd9453d9b
plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
229 """ |
3b3cd9453d9b
plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
230 timestamp = time.time() |
3b3cd9453d9b
plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
231 edited_history = await self.host.memory.storage.get( |
3b3cd9453d9b
plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
232 client, |
3b3cd9453d9b
plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
233 History, |
3b3cd9453d9b
plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
234 History.uid, |
3b3cd9453d9b
plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
235 message_id, |
3b3cd9453d9b
plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
236 joined_loads=[History.messages, History.subjects, History.thread], |
3b3cd9453d9b
plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
237 ) |
3b3cd9453d9b
plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
238 if edited_history is None: |
3b3cd9453d9b
plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
239 raise exceptions.NotFound( |
3b3cd9453d9b
plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
240 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
|
241 ) |
3b3cd9453d9b
plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
242 if edited_history.type == C.MESS_TYPE_GROUPCHAT: |
3b3cd9453d9b
plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
243 is_group_chat = True |
4198
b1207332cea2
plugin XEP-0308: fix ID used, recipients and author check.
Goffi <goffi@goffi.org>
parents:
4166
diff
changeset
|
244 # 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
|
245 peer_jid = edited_history.source_jid |
4163
3b3cd9453d9b
plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
246 else: |
3b3cd9453d9b
plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
247 is_group_chat = False |
3b3cd9453d9b
plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
248 peer_jid = jid.JID(edited_history.dest) |
3b3cd9453d9b
plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
249 history_data = await self.host.memory.history_get( |
3b3cd9453d9b
plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
250 client.jid, peer_jid, limit=1, profile=client.profile |
3b3cd9453d9b
plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
251 ) |
3b3cd9453d9b
plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
252 if not history_data: |
3b3cd9453d9b
plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
253 raise exceptions.NotFound( |
4198
b1207332cea2
plugin XEP-0308: fix ID used, recipients and author check.
Goffi <goffi@goffi.org>
parents:
4166
diff
changeset
|
254 "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
|
255 ) |
3b3cd9453d9b
plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
256 last_mess = history_data[0] |
3b3cd9453d9b
plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
257 if last_mess[0] != message_id: |
3b3cd9453d9b
plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
258 raise ValueError( |
3b3cd9453d9b
plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
259 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
|
260 "it" |
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 |
3b3cd9453d9b
plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
263 await self.update_history( |
3b3cd9453d9b
plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
264 client, |
3b3cd9453d9b
plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
265 edited_history, |
3b3cd9453d9b
plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
266 timestamp, |
3b3cd9453d9b
plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
267 edit_data.message, |
3b3cd9453d9b
plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
268 edit_data.subject, |
3b3cd9453d9b
plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
269 edit_data.extra, |
3b3cd9453d9b
plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
270 last_mess[4], |
3b3cd9453d9b
plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
271 last_mess[5], |
3b3cd9453d9b
plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
272 last_mess[-1], |
3b3cd9453d9b
plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
273 # 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
|
274 store = not is_group_chat |
3b3cd9453d9b
plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
275 ) |
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 serialised = edited_history.serialise() |
4198
b1207332cea2
plugin XEP-0308: fix ID used, recipients and author check.
Goffi <goffi@goffi.org>
parents:
4166
diff
changeset
|
278 serialised["uid"] = str(uuid.uuid4()) |
b1207332cea2
plugin XEP-0308: fix ID used, recipients and author check.
Goffi <goffi@goffi.org>
parents:
4166
diff
changeset
|
279 serialised["from"] = client.jid |
b1207332cea2
plugin XEP-0308: fix ID used, recipients and author check.
Goffi <goffi@goffi.org>
parents:
4166
diff
changeset
|
280 # 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
|
281 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
|
282 |
3b3cd9453d9b
plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
283 message_elt = client.generate_message_xml(serialised)["xml"] |
3b3cd9453d9b
plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
284 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
|
285 replace_elt["id"] = edited_history.origin_id |
4163
3b3cd9453d9b
plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
286 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
|
287 client.send(message_elt) |
3b3cd9453d9b
plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
288 |
3b3cd9453d9b
plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
289 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
|
290 client = self.host.get_client(profile) |
3b3cd9453d9b
plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
291 edit_data = MessageEdition.model_validate_json(edit_data_s) |
3b3cd9453d9b
plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
292 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
|
293 |
3b3cd9453d9b
plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
294 |
3b3cd9453d9b
plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
295 @implementer(disco.IDisco) |
3b3cd9453d9b
plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
296 class XEP_0308_handler(xmlstream.XMPPHandler): |
3b3cd9453d9b
plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
297 def getDiscoInfo(self, __, target, nodeIdentifier=""): |
3b3cd9453d9b
plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
298 return [disco.DiscoFeature(NS_MESSAGE_CORRECT)] |
3b3cd9453d9b
plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
299 |
3b3cd9453d9b
plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
300 def getDiscoItems(self, requestor, target, nodeIdentifier=""): |
3b3cd9453d9b
plugin XEP-0308: implement Last Message Correction
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
301 return [] |