annotate sat/plugins/plugin_xep_0470.py @ 3913:944f51f9c2b4

core (xmpp): make `send` a blocking method, fix `sendMessageData` calls: original `send` method is blocking, and it is used as such by Wokkel and thus can't be changed to an async method easily. However, an Async method is necessary to have an async trigger at the very end of the send workflow for end-to-end encryption. To workaround that, `send` is an async method which call `a_send`, an async method which actually does the sending. This way legacy method can still call `send` while `a_send` can be await otherwise. Fix calls to `sendMessageData`: the method now being an `async` one, `ensureDeferred` had to be used in some calls.
author Goffi <goffi@goffi.org>
date Sat, 24 Sep 2022 16:31:39 +0200
parents 43024e50b701
children 2b2856ae5eeb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3864
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
1 #!/usr/bin/env python3
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
2
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
3 # Libervia plugin for Pubsub Attachments
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
4 # Copyright (C) 2009-2022 Jérôme Poisson (goffi@goffi.org)
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
5
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
6 # This program is free software: you can redistribute it and/or modify
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
7 # it under the terms of the GNU Affero General Public License as published by
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
8 # the Free Software Foundation, either version 3 of the License, or
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
9 # (at your option) any later version.
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
10
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
11 # This program is distributed in the hope that it will be useful,
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
14 # GNU Affero General Public License for more details.
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
15
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
16 # You should have received a copy of the GNU Affero General Public License
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
18
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
19 from typing import List, Tuple, Dict, Any, Callable, Optional
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
20
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
21 from twisted.words.protocols.jabber import jid, xmlstream, error
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
22 from twisted.words.xish import domish
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
23 from twisted.internet import defer
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
24 from zope.interface import implementer
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
25 from wokkel import pubsub, disco, iwokkel
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
26
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
27 from sat.core.constants import Const as C
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
28 from sat.core.i18n import _
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
29 from sat.core.log import getLogger
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
30 from sat.core.core_types import SatXMPPEntity
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
31 from sat.core import exceptions
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
32 from sat.tools.common import uri, data_format, date_utils
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
33 from sat.tools.utils import xmpp_date
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
34
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
35
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
36 log = getLogger(__name__)
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
37
3888
aa7197b67c26 component AP gateway: AP <=> XMPP reactions conversions:
Goffi <goffi@goffi.org>
parents: 3869
diff changeset
38 IMPORT_NAME = "XEP-0470"
3864
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
39
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
40 PLUGIN_INFO = {
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
41 C.PI_NAME: "Pubsub Attachments",
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
42 C.PI_IMPORT_NAME: IMPORT_NAME,
3888
aa7197b67c26 component AP gateway: AP <=> XMPP reactions conversions:
Goffi <goffi@goffi.org>
parents: 3869
diff changeset
43 C.PI_TYPE: C.PLUG_TYPE_XEP,
3864
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
44 C.PI_MODES: C.PLUG_MODE_BOTH,
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
45 C.PI_PROTOCOLS: [],
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
46 C.PI_DEPENDENCIES: ["XEP-0060"],
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
47 C.PI_MAIN: "PubsubAttachments",
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
48 C.PI_HANDLER: "yes",
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
49 C.PI_DESCRIPTION: _("""Pubsub Attachments implementation"""),
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
50 }
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
51 NS_PREFIX = "urn:xmpp:pubsub-attachments:"
3888
aa7197b67c26 component AP gateway: AP <=> XMPP reactions conversions:
Goffi <goffi@goffi.org>
parents: 3869
diff changeset
52 NS_PUBSUB_ATTACHMENTS = f"{NS_PREFIX}1"
aa7197b67c26 component AP gateway: AP <=> XMPP reactions conversions:
Goffi <goffi@goffi.org>
parents: 3869
diff changeset
53 NS_PUBSUB_ATTACHMENTS_SUM = f"{NS_PREFIX}summary:1"
3864
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
54
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
55
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
56 class PubsubAttachments:
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
57 namespace = NS_PUBSUB_ATTACHMENTS
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
58
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
59 def __init__(self, host):
3888
aa7197b67c26 component AP gateway: AP <=> XMPP reactions conversions:
Goffi <goffi@goffi.org>
parents: 3869
diff changeset
60 log.info(_("XEP-0470 (Pubsub Attachments) plugin initialization"))
3864
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
61 host.registerNamespace("pubsub-attachments", NS_PUBSUB_ATTACHMENTS)
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
62 self.host = host
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
63 self._p = host.plugins["XEP-0060"]
3869
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
64 self.handlers: Dict[Tuple[str, str], dict[str, Any]] = {}
3864
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
65 host.trigger.add("XEP-0277_send", self.onMBSend)
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
66 self.registerAttachmentHandler(
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
67 "noticed", NS_PUBSUB_ATTACHMENTS, self.noticedGet, self.noticedSet
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
68 )
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
69 self.registerAttachmentHandler(
3888
aa7197b67c26 component AP gateway: AP <=> XMPP reactions conversions:
Goffi <goffi@goffi.org>
parents: 3869
diff changeset
70 "reactions", NS_PUBSUB_ATTACHMENTS, self.reactionsGet, self.reactionsSet
3864
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
71 )
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
72 host.bridge.addMethod(
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
73 "psAttachmentsGet",
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
74 ".plugin",
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
75 in_sign="sssasss",
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
76 out_sign="(ss)",
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
77 method=self._get,
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
78 async_=True,
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
79 )
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
80 host.bridge.addMethod(
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
81 "psAttachmentsSet",
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
82 ".plugin",
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
83 in_sign="ss",
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
84 out_sign="",
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
85 method=self._set,
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
86 async_=True,
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
87 )
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
88
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
89 def getHandler(self, client):
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
90 return PubsubAttachments_Handler()
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
91
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
92 def registerAttachmentHandler(
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
93 self,
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
94 name: str,
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
95 namespace: str,
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
96 get_cb: Callable[
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
97 [SatXMPPEntity, domish.Element, Dict[str, Any]],
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
98 None],
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
99 set_cb: Callable[
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
100 [SatXMPPEntity, Dict[str, Any], Optional[domish.Element]],
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
101 Optional[domish.Element]],
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
102 ) -> None:
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
103 """Register callbacks to handle an attachment
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
104
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
105 @param name: name of the element
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
106 @param namespace: namespace of the element
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
107 (name, namespace) couple must be unique
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
108 @param get: method to call when attachments are retrieved
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
109 it will be called with (client, element, data) where element is the
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
110 <attachments> element to parse, and data must be updated in place with
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
111 parsed data
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
112 @param set: method to call when the attachment need to be set or udpated
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
113 it will be called with (client, data, former_elt of None if there was no
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
114 former element). When suitable, ``operation`` should be used to check if we
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
115 request an ``update`` or a ``replace``.
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
116 """
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
117 key = (name, namespace)
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
118 if key in self.handlers:
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
119 raise exceptions.ConflictError(
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
120 f"({name}, {namespace}) attachment handlers are already registered"
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
121 )
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
122 self.handlers[(name, namespace)] = {
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
123 "get": get_cb,
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
124 "set": set_cb
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
125 }
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
126
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
127 def getAttachmentNodeName(self, service: jid.JID, node: str, item: str) -> str:
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
128 """Generate name to use for attachment node"""
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
129 target_item_uri = uri.buildXMPPUri(
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
130 "pubsub",
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
131 path=service.userhost(),
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
132 node=node,
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
133 item=item
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
134 )
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
135 return f"{NS_PUBSUB_ATTACHMENTS}/{target_item_uri}"
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
136
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
137 def isAttachmentNode(self, node: str) -> bool:
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
138 """Return True if node name is an attachment node"""
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
139 return node.startswith(f"{NS_PUBSUB_ATTACHMENTS}/")
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
140
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
141 def attachmentNode2Item(self, node: str) -> Tuple[jid.JID, str, str]:
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
142 """Retrieve service, node and item from attachement node's name"""
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
143 if not self.isAttachmentNode(node):
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
144 raise ValueError("this is not an attachment node!")
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
145 prefix_len = len(f"{NS_PUBSUB_ATTACHMENTS}/")
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
146 item_uri = node[prefix_len:]
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
147 parsed_uri = uri.parseXMPPUri(item_uri)
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
148 if parsed_uri["type"] != "pubsub":
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
149 raise ValueError(f"unexpected URI type, it must be a pubsub URI: {item_uri}")
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
150 try:
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
151 service = jid.JID(parsed_uri["path"])
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
152 except RuntimeError:
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
153 raise ValueError(f"invalid service in pubsub URI: {item_uri}")
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
154 node = parsed_uri["node"]
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
155 item = parsed_uri["item"]
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
156 return (service, node, item)
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
157
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
158 async def onMBSend(
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
159 self,
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
160 client: SatXMPPEntity,
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
161 service: jid.JID,
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
162 node: str,
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
163 item: domish.Element,
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
164 data: dict
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
165 ) -> bool:
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
166 """trigger to create attachment node on each publication"""
3901
43024e50b701 plugin XEP-0470: add `create_attachments_node` method:
Goffi <goffi@goffi.org>
parents: 3888
diff changeset
167 await self.create_attachments_node(client, service, node, item["id"])
43024e50b701 plugin XEP-0470: add `create_attachments_node` method:
Goffi <goffi@goffi.org>
parents: 3888
diff changeset
168 return True
43024e50b701 plugin XEP-0470: add `create_attachments_node` method:
Goffi <goffi@goffi.org>
parents: 3888
diff changeset
169
43024e50b701 plugin XEP-0470: add `create_attachments_node` method:
Goffi <goffi@goffi.org>
parents: 3888
diff changeset
170 async def create_attachments_node(
43024e50b701 plugin XEP-0470: add `create_attachments_node` method:
Goffi <goffi@goffi.org>
parents: 3888
diff changeset
171 self,
43024e50b701 plugin XEP-0470: add `create_attachments_node` method:
Goffi <goffi@goffi.org>
parents: 3888
diff changeset
172 client: SatXMPPEntity,
43024e50b701 plugin XEP-0470: add `create_attachments_node` method:
Goffi <goffi@goffi.org>
parents: 3888
diff changeset
173 service: jid.JID,
43024e50b701 plugin XEP-0470: add `create_attachments_node` method:
Goffi <goffi@goffi.org>
parents: 3888
diff changeset
174 node: str,
43024e50b701 plugin XEP-0470: add `create_attachments_node` method:
Goffi <goffi@goffi.org>
parents: 3888
diff changeset
175 item_id: str
43024e50b701 plugin XEP-0470: add `create_attachments_node` method:
Goffi <goffi@goffi.org>
parents: 3888
diff changeset
176 ):
43024e50b701 plugin XEP-0470: add `create_attachments_node` method:
Goffi <goffi@goffi.org>
parents: 3888
diff changeset
177 """Create node for attachements if necessary"""
3864
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
178 node_config = await self._p.getConfiguration(client, service, node)
3901
43024e50b701 plugin XEP-0470: add `create_attachments_node` method:
Goffi <goffi@goffi.org>
parents: 3888
diff changeset
179 try:
43024e50b701 plugin XEP-0470: add `create_attachments_node` method:
Goffi <goffi@goffi.org>
parents: 3888
diff changeset
180 # FIXME: check if this is the best publish_model option
43024e50b701 plugin XEP-0470: add `create_attachments_node` method:
Goffi <goffi@goffi.org>
parents: 3888
diff changeset
181 node_config.fields["pubsub#publish_model"].value = "open"
43024e50b701 plugin XEP-0470: add `create_attachments_node` method:
Goffi <goffi@goffi.org>
parents: 3888
diff changeset
182 except KeyError:
43024e50b701 plugin XEP-0470: add `create_attachments_node` method:
Goffi <goffi@goffi.org>
parents: 3888
diff changeset
183 log.warning("pubsub#publish_model field is missing")
43024e50b701 plugin XEP-0470: add `create_attachments_node` method:
Goffi <goffi@goffi.org>
parents: 3888
diff changeset
184 attachment_node = self.getAttachmentNodeName(service, node, item_id)
3864
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
185 # we use the same options as target node
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
186 try:
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
187 await self._p.createIfNewNode(
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
188 client, service, attachment_node, options=dict(node_config)
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
189 )
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
190 except Exception as e:
3901
43024e50b701 plugin XEP-0470: add `create_attachments_node` method:
Goffi <goffi@goffi.org>
parents: 3888
diff changeset
191 log.warning(f"Can't create attachment node {attachment_node}: {e}")
3864
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
192
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
193 def items2attachmentData(
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
194 self,
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
195 client: SatXMPPEntity,
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
196 items: List[domish.Element]
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
197 ) -> List[Dict[str, Any]]:
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
198 """Convert items from attachment node to attachment data"""
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
199 list_data = []
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
200 for item in items:
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
201 try:
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
202 attachments_elt = next(
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
203 item.elements(NS_PUBSUB_ATTACHMENTS, "attachments")
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
204 )
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
205 except StopIteration:
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
206 log.warning(
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
207 "item is missing <attachments> elements, ignoring it: {item.toXml()}"
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
208 )
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
209 continue
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
210 item_id = item["id"]
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
211 publisher_s = item.getAttribute("publisher")
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
212 # publisher is not filled by all pubsub service, so we can't count on it
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
213 if publisher_s:
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
214 publisher = jid.JID(publisher_s)
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
215 if publisher.userhost() != item_id:
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
216 log.warning(
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
217 f"publisher {publisher.userhost()!r} doesn't correspond to item "
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
218 f"id {item['id']!r}, ignoring. This may be a hack attempt.\n"
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
219 f"{item.toXml()}"
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
220 )
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
221 continue
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
222 try:
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
223 jid.JID(item_id)
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
224 except RuntimeError:
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
225 log.warning(
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
226 "item ID is not a JID, this is not compliant and is ignored: "
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
227 f"{item.toXml}"
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
228 )
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
229 continue
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
230 data = {
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
231 "from": item_id
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
232 }
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
233 for handler in self.handlers.values():
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
234 handler["get"](client, attachments_elt, data)
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
235 if len(data) > 1:
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
236 list_data.append(data)
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
237 return list_data
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
238
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
239 def _get(
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
240 self,
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
241 service_s: str,
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
242 node: str,
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
243 item: str,
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
244 senders_s: List[str],
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
245 extra_s: str,
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
246 profile_key: str
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
247 ) -> defer.Deferred:
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
248 client = self.host.getClient(profile_key)
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
249 extra = data_format.deserialise(extra_s)
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
250 senders = [jid.JID(s) for s in senders_s]
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
251 d = defer.ensureDeferred(
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
252 self.getAttachments(client, jid.JID(service_s), node, item, senders)
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
253 )
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
254 d.addCallback(
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
255 lambda ret:
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
256 (data_format.serialise(ret[0]),
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
257 data_format.serialise(ret[1]))
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
258 )
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
259 return d
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
260
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
261 async def getAttachments(
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
262 self,
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
263 client: SatXMPPEntity,
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
264 service: jid.JID,
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
265 node: str,
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
266 item: str,
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
267 senders: Optional[List[jid.JID]],
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
268 extra: Optional[dict] = None
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
269 ) -> Tuple[List[Dict[str, Any]], dict]:
3888
aa7197b67c26 component AP gateway: AP <=> XMPP reactions conversions:
Goffi <goffi@goffi.org>
parents: 3869
diff changeset
270 """Retrieve data attached to a pubsub item
aa7197b67c26 component AP gateway: AP <=> XMPP reactions conversions:
Goffi <goffi@goffi.org>
parents: 3869
diff changeset
271
aa7197b67c26 component AP gateway: AP <=> XMPP reactions conversions:
Goffi <goffi@goffi.org>
parents: 3869
diff changeset
272 @param service: pubsub service where the node is
aa7197b67c26 component AP gateway: AP <=> XMPP reactions conversions:
Goffi <goffi@goffi.org>
parents: 3869
diff changeset
273 @param node: pubsub node containing the item
aa7197b67c26 component AP gateway: AP <=> XMPP reactions conversions:
Goffi <goffi@goffi.org>
parents: 3869
diff changeset
274 @param item: ID of the item for which attachments will be retrieved
aa7197b67c26 component AP gateway: AP <=> XMPP reactions conversions:
Goffi <goffi@goffi.org>
parents: 3869
diff changeset
275 @param senders: bare JIDs of entities that are checked. Attachments from those
aa7197b67c26 component AP gateway: AP <=> XMPP reactions conversions:
Goffi <goffi@goffi.org>
parents: 3869
diff changeset
276 entities will be retrieved.
aa7197b67c26 component AP gateway: AP <=> XMPP reactions conversions:
Goffi <goffi@goffi.org>
parents: 3869
diff changeset
277 If None, attachments from all entities will be retrieved
aa7197b67c26 component AP gateway: AP <=> XMPP reactions conversions:
Goffi <goffi@goffi.org>
parents: 3869
diff changeset
278 @param extra: extra data, will be used as ``extra`` argument when doing
aa7197b67c26 component AP gateway: AP <=> XMPP reactions conversions:
Goffi <goffi@goffi.org>
parents: 3869
diff changeset
279 ``getItems`` call.
aa7197b67c26 component AP gateway: AP <=> XMPP reactions conversions:
Goffi <goffi@goffi.org>
parents: 3869
diff changeset
280 @return: A tuple with:
aa7197b67c26 component AP gateway: AP <=> XMPP reactions conversions:
Goffi <goffi@goffi.org>
parents: 3869
diff changeset
281 - the list of attachments data, one item per found sender. The attachments
aa7197b67c26 component AP gateway: AP <=> XMPP reactions conversions:
Goffi <goffi@goffi.org>
parents: 3869
diff changeset
282 data are dict containing attachment, no ``extra`` field is used here
aa7197b67c26 component AP gateway: AP <=> XMPP reactions conversions:
Goffi <goffi@goffi.org>
parents: 3869
diff changeset
283 (contrarily to attachments data used with ``setAttachments``).
aa7197b67c26 component AP gateway: AP <=> XMPP reactions conversions:
Goffi <goffi@goffi.org>
parents: 3869
diff changeset
284 - metadata returned by the call to ``getItems``
aa7197b67c26 component AP gateway: AP <=> XMPP reactions conversions:
Goffi <goffi@goffi.org>
parents: 3869
diff changeset
285 """
3864
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
286 if extra is None:
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
287 extra = {}
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
288 attachment_node = self.getAttachmentNodeName(service, node, item)
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
289 item_ids = [e.userhost() for e in senders] if senders else None
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
290 items, metadata = await self._p.getItems(
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
291 client, service, attachment_node, item_ids=item_ids, extra=extra
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
292 )
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
293 list_data = self.items2attachmentData(client, items)
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
294
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
295 return list_data, metadata
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
296
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
297 def _set(
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
298 self,
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
299 attachments_s: str,
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
300 profile_key: str
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
301 ) -> None:
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
302 client = self.host.getClient(profile_key)
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
303 attachments = data_format.deserialise(attachments_s) or {}
3869
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
304 return defer.ensureDeferred(self.setAttachments(client, attachments))
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
305
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
306 def applySetHandler(
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
307 self,
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
308 client: SatXMPPEntity,
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
309 attachments_data: dict,
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
310 item_elt: Optional[domish.Element],
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
311 handlers: Optional[List[Tuple[str, str]]] = None,
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
312 from_jid: Optional[jid.JID] = None,
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
313 ) -> domish.Element:
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
314 """Apply all ``set`` callbacks to an attachments item
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
315
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
316 @param attachments_data: data describing the attachments
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
317 ``extra`` key will be used, and created if not found
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
318 @param from_jid: jid of the author of the attachments
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
319 ``client.jid.userhostJID()`` will be used if not specified
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
320 @param item_elt: item containing an <attachments> element
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
321 will be modified in place
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
322 if None, a new element will be created
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
323 @param handlers: list of (name, namespace) of handlers to use.
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
324 if None, all registered handlers will be used.
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
325 @return: updated item_elt if given, otherwise a new item_elt
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
326 """
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
327 attachments_data.setdefault("extra", {})
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
328 if item_elt is None:
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
329 item_id = client.jid.userhost() if from_jid is None else from_jid.userhost()
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
330 item_elt = pubsub.Item(item_id)
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
331 item_elt.addElement((NS_PUBSUB_ATTACHMENTS, "attachments"))
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
332
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
333 try:
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
334 attachments_elt = next(
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
335 item_elt.elements(NS_PUBSUB_ATTACHMENTS, "attachments")
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
336 )
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
337 except StopIteration:
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
338 log.warning(
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
339 f"no <attachments> element found, creating a new one: {item_elt.toXml()}"
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
340 )
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
341 attachments_elt = item_elt.addElement((NS_PUBSUB_ATTACHMENTS, "attachments"))
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
342
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
343 if handlers is None:
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
344 handlers = list(self.handlers.keys())
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
345
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
346 for name, namespace in handlers:
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
347 try:
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
348 handler = self.handlers[(name, namespace)]
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
349 except KeyError:
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
350 log.error(
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
351 f"unregistered handler ({name!r}, {namespace!r}) is requested, "
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
352 "ignoring"
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
353 )
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
354 continue
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
355 try:
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
356 former_elt = next(attachments_elt.elements(namespace, name))
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
357 except StopIteration:
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
358 former_elt = None
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
359 new_elt = handler["set"](client, attachments_data, former_elt)
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
360 if new_elt != former_elt:
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
361 if former_elt is not None:
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
362 attachments_elt.children.remove(former_elt)
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
363 if new_elt is not None:
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
364 attachments_elt.addChild(new_elt)
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
365 return item_elt
3864
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
366
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
367 async def setAttachments(
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
368 self,
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
369 client: SatXMPPEntity,
3869
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
370 attachments_data: Dict[str, Any]
3864
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
371 ) -> None:
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
372 """Set or update attachments
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
373
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
374 Former <attachments> element will be retrieved and updated. Individual
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
375 attachments replace or update their elements individually, according to the
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
376 "operation" key.
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
377
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
378 "operation" key may be "update" or "replace", and defaults to update, it is only
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
379 used in attachments where "update" makes sense (e.g. it's used for "reactions"
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
380 but not for "noticed").
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
381
3869
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
382 @param attachments_data: data describing attachments. Various keys (usually stored
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
383 in attachments_data["extra"]) may be used depending on the attachments
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
384 handlers registered. The keys "service", "node" and "id" MUST be set.
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
385 ``attachments_data`` is thought to be compatible with microblog data.
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
386
3864
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
387 """
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
388 try:
3869
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
389 service = jid.JID(attachments_data["service"])
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
390 node = attachments_data["node"]
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
391 item = attachments_data["id"]
3864
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
392 except (KeyError, RuntimeError):
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
393 raise ValueError(
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
394 'data must have "service", "node" and "id" set'
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
395 )
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
396 attachment_node = self.getAttachmentNodeName(service, node, item)
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
397 try:
3869
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
398 items, __ = await self._p.getItems(
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
399 client, service, attachment_node, item_ids=[client.jid.userhost()]
3864
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
400 )
3869
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
401 except exceptions.NotFound:
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
402 item_elt = None
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
403 else:
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
404 if not items:
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
405 item_elt = None
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
406 else:
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
407 item_elt = items[0]
3864
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
408
3869
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
409 item_elt = self.applySetHandler(
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
410 client,
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
411 attachments_data,
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
412 item_elt=item_elt,
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
413 )
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
414
3864
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
415 try:
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
416 await self._p.sendItems(client, service, attachment_node, [item_elt])
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
417 except error.StanzaError as e:
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
418 if e.condition == "item-not-found":
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
419 # the node doesn't exist, we can't publish attachments
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
420 log.warning(
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
421 f"no attachment node found at {service} on {node!r} for item "
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
422 f"{item!r}, we can't update attachments."
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
423 )
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
424 raise exceptions.NotFound("No attachment node available")
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
425 else:
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
426 raise e
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
427
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
428 async def subscribe(
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
429 self,
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
430 client: SatXMPPEntity,
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
431 service: jid.JID,
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
432 node: str,
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
433 item: str,
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
434 ) -> None:
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
435 """Subscribe to attachment node targeting the item
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
436
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
437 @param service: service of target item (will also be used for attachment node)
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
438 @param node: node of target item (used to get attachment node's name)
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
439 @param item: name of target item (used to get attachment node's name)
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
440 """
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
441 attachment_node = self.getAttachmentNodeName(service, node, item)
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
442 await self._p.subscribe(client, service, attachment_node)
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
443
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
444
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
445 def setTimestamp(self, attachment_elt: domish.Element, data: dict) -> None:
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
446 """Check if a ``timestamp`` attribute is set, parse it, and fill data
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
447
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
448 @param attachments_elt: element where the ``timestamp`` attribute may be set
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
449 @param data: data specific to the attachment (i.e. not the whole microblog data)
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
450 ``timestamp`` field will be set there if timestamp exists and is parsable
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
451 """
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
452 timestamp_raw = attachment_elt.getAttribute("timestamp")
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
453 if timestamp_raw:
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
454 try:
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
455 timestamp = date_utils.date_parse(timestamp_raw)
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
456 except date_utils.ParserError:
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
457 log.warning(f"can't parse timestamp: {timestamp_raw}")
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
458 else:
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
459 data["timestamp"] = timestamp
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
460
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
461 def noticedGet(
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
462 self,
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
463 client: SatXMPPEntity,
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
464 attachments_elt: domish.Element,
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
465 data: Dict[str, Any],
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
466 ) -> None:
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
467 try:
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
468 noticed_elt = next(
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
469 attachments_elt.elements(NS_PUBSUB_ATTACHMENTS, "noticed")
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
470 )
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
471 except StopIteration:
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
472 pass
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
473 else:
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
474 noticed_data = {
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
475 "noticed": True
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
476 }
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
477 self.setTimestamp(noticed_elt, noticed_data)
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
478 data["noticed"] = noticed_data
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
479
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
480 def noticedSet(
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
481 self,
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
482 client: SatXMPPEntity,
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
483 data: Dict[str, Any],
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
484 former_elt: Optional[domish.Element]
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
485 ) -> Optional[domish.Element]:
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
486 """add or remove a <noticed> attachment
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
487
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
488 if data["noticed"] is True, element is added, if it's False, it's removed, and
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
489 it's not present or None, the former element is kept.
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
490 """
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
491 noticed = data["extra"].get("noticed")
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
492 if noticed is None:
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
493 return former_elt
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
494 elif noticed:
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
495 return domish.Element(
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
496 (NS_PUBSUB_ATTACHMENTS, "noticed"),
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
497 attribs = {
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
498 "timestamp": xmpp_date()
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
499 }
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
500 )
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
501 else:
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
502 return None
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
503
3888
aa7197b67c26 component AP gateway: AP <=> XMPP reactions conversions:
Goffi <goffi@goffi.org>
parents: 3869
diff changeset
504 def reactionsGet(
3864
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
505 self,
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
506 client: SatXMPPEntity,
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
507 attachments_elt: domish.Element,
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
508 data: Dict[str, Any],
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
509 ) -> None:
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
510 try:
3888
aa7197b67c26 component AP gateway: AP <=> XMPP reactions conversions:
Goffi <goffi@goffi.org>
parents: 3869
diff changeset
511 reactions_elt = next(
aa7197b67c26 component AP gateway: AP <=> XMPP reactions conversions:
Goffi <goffi@goffi.org>
parents: 3869
diff changeset
512 attachments_elt.elements(NS_PUBSUB_ATTACHMENTS, "reactions")
3864
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
513 )
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
514 except StopIteration:
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
515 pass
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
516 else:
3888
aa7197b67c26 component AP gateway: AP <=> XMPP reactions conversions:
Goffi <goffi@goffi.org>
parents: 3869
diff changeset
517 reactions_data = {"reactions": []}
aa7197b67c26 component AP gateway: AP <=> XMPP reactions conversions:
Goffi <goffi@goffi.org>
parents: 3869
diff changeset
518 reactions = reactions_data["reactions"]
aa7197b67c26 component AP gateway: AP <=> XMPP reactions conversions:
Goffi <goffi@goffi.org>
parents: 3869
diff changeset
519 for reaction_elt in reactions_elt.elements(NS_PUBSUB_ATTACHMENTS, "reaction"):
aa7197b67c26 component AP gateway: AP <=> XMPP reactions conversions:
Goffi <goffi@goffi.org>
parents: 3869
diff changeset
520 reactions.append(str(reaction_elt))
aa7197b67c26 component AP gateway: AP <=> XMPP reactions conversions:
Goffi <goffi@goffi.org>
parents: 3869
diff changeset
521 self.setTimestamp(reactions_elt, reactions_data)
aa7197b67c26 component AP gateway: AP <=> XMPP reactions conversions:
Goffi <goffi@goffi.org>
parents: 3869
diff changeset
522 data["reactions"] = reactions_data
3864
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
523
3888
aa7197b67c26 component AP gateway: AP <=> XMPP reactions conversions:
Goffi <goffi@goffi.org>
parents: 3869
diff changeset
524 def reactionsSet(
3864
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
525 self,
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
526 client: SatXMPPEntity,
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
527 data: Dict[str, Any],
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
528 former_elt: Optional[domish.Element]
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
529 ) -> Optional[domish.Element]:
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
530 """update the <reaction> attachment"""
3888
aa7197b67c26 component AP gateway: AP <=> XMPP reactions conversions:
Goffi <goffi@goffi.org>
parents: 3869
diff changeset
531 reactions_data = data["extra"].get("reactions")
aa7197b67c26 component AP gateway: AP <=> XMPP reactions conversions:
Goffi <goffi@goffi.org>
parents: 3869
diff changeset
532 if reactions_data is None:
3864
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
533 return former_elt
3888
aa7197b67c26 component AP gateway: AP <=> XMPP reactions conversions:
Goffi <goffi@goffi.org>
parents: 3869
diff changeset
534 operation_type = reactions_data.get("operation", "update")
3864
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
535 if operation_type == "update":
3888
aa7197b67c26 component AP gateway: AP <=> XMPP reactions conversions:
Goffi <goffi@goffi.org>
parents: 3869
diff changeset
536 former_reactions = {
aa7197b67c26 component AP gateway: AP <=> XMPP reactions conversions:
Goffi <goffi@goffi.org>
parents: 3869
diff changeset
537 str(r) for r in former_elt.elements(NS_PUBSUB_ATTACHMENTS, "reaction")
aa7197b67c26 component AP gateway: AP <=> XMPP reactions conversions:
Goffi <goffi@goffi.org>
parents: 3869
diff changeset
538 } if former_elt is not None else set()
aa7197b67c26 component AP gateway: AP <=> XMPP reactions conversions:
Goffi <goffi@goffi.org>
parents: 3869
diff changeset
539 added_reactions = set(reactions_data.get("add") or [])
aa7197b67c26 component AP gateway: AP <=> XMPP reactions conversions:
Goffi <goffi@goffi.org>
parents: 3869
diff changeset
540 removed_reactions = set(reactions_data.get("remove") or [])
aa7197b67c26 component AP gateway: AP <=> XMPP reactions conversions:
Goffi <goffi@goffi.org>
parents: 3869
diff changeset
541 reactions = list((former_reactions | added_reactions) - removed_reactions)
3864
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
542 elif operation_type == "replace":
3888
aa7197b67c26 component AP gateway: AP <=> XMPP reactions conversions:
Goffi <goffi@goffi.org>
parents: 3869
diff changeset
543 reactions = reactions_data.get("reactions") or []
3864
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
544 else:
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
545 raise exceptions.DataError(f"invalid reaction operation: {operation_type!r}")
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
546 if reactions:
3888
aa7197b67c26 component AP gateway: AP <=> XMPP reactions conversions:
Goffi <goffi@goffi.org>
parents: 3869
diff changeset
547 reactions_elt = domish.Element(
aa7197b67c26 component AP gateway: AP <=> XMPP reactions conversions:
Goffi <goffi@goffi.org>
parents: 3869
diff changeset
548 (NS_PUBSUB_ATTACHMENTS, "reactions"),
3864
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
549 attribs = {
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
550 "timestamp": xmpp_date()
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
551 }
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
552 )
3888
aa7197b67c26 component AP gateway: AP <=> XMPP reactions conversions:
Goffi <goffi@goffi.org>
parents: 3869
diff changeset
553 for reactions_data in reactions:
aa7197b67c26 component AP gateway: AP <=> XMPP reactions conversions:
Goffi <goffi@goffi.org>
parents: 3869
diff changeset
554 reactions_elt.addElement("reaction", content=reactions_data)
aa7197b67c26 component AP gateway: AP <=> XMPP reactions conversions:
Goffi <goffi@goffi.org>
parents: 3869
diff changeset
555 return reactions_elt
3864
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
556 else:
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
557 return None
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
558
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
559
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
560 @implementer(iwokkel.IDisco)
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
561 class PubsubAttachments_Handler(xmlstream.XMPPHandler):
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
562
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
563 def getDiscoInfo(self, requestor, service, nodeIdentifier=""):
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
564 return [disco.DiscoFeature(NS_PUBSUB_ATTACHMENTS)]
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
565
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
566 def getDiscoItems(self, requestor, service, nodeIdentifier=""):
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
567 return []