annotate sat/plugins/plugin_xep_0470.py @ 3951:4b8776eb6826

doc (blog, pubsub): documentation on new e2ee arguments for pubsub, and shared secrets handling: fix 380
author Goffi <goffi@goffi.org>
date Sat, 15 Oct 2022 20:38:33 +0200
parents 2b2856ae5eeb
children 3cb9ade2ab84
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"""
3945
2b2856ae5eeb plugin XEP-0470: fix autocreate in `create_attachments_node`:
Goffi <goffi@goffi.org>
parents: 3901
diff changeset
167 await self.create_attachments_node(
2b2856ae5eeb plugin XEP-0470: fix autocreate in `create_attachments_node`:
Goffi <goffi@goffi.org>
parents: 3901
diff changeset
168 client, service, node, item["id"], autocreate=True
2b2856ae5eeb plugin XEP-0470: fix autocreate in `create_attachments_node`:
Goffi <goffi@goffi.org>
parents: 3901
diff changeset
169 )
3901
43024e50b701 plugin XEP-0470: add `create_attachments_node` method:
Goffi <goffi@goffi.org>
parents: 3888
diff changeset
170 return True
43024e50b701 plugin XEP-0470: add `create_attachments_node` method:
Goffi <goffi@goffi.org>
parents: 3888
diff changeset
171
43024e50b701 plugin XEP-0470: add `create_attachments_node` method:
Goffi <goffi@goffi.org>
parents: 3888
diff changeset
172 async def create_attachments_node(
43024e50b701 plugin XEP-0470: add `create_attachments_node` method:
Goffi <goffi@goffi.org>
parents: 3888
diff changeset
173 self,
43024e50b701 plugin XEP-0470: add `create_attachments_node` method:
Goffi <goffi@goffi.org>
parents: 3888
diff changeset
174 client: SatXMPPEntity,
43024e50b701 plugin XEP-0470: add `create_attachments_node` method:
Goffi <goffi@goffi.org>
parents: 3888
diff changeset
175 service: jid.JID,
43024e50b701 plugin XEP-0470: add `create_attachments_node` method:
Goffi <goffi@goffi.org>
parents: 3888
diff changeset
176 node: str,
3945
2b2856ae5eeb plugin XEP-0470: fix autocreate in `create_attachments_node`:
Goffi <goffi@goffi.org>
parents: 3901
diff changeset
177 item_id: str,
2b2856ae5eeb plugin XEP-0470: fix autocreate in `create_attachments_node`:
Goffi <goffi@goffi.org>
parents: 3901
diff changeset
178 autocreate: bool = False
3901
43024e50b701 plugin XEP-0470: add `create_attachments_node` method:
Goffi <goffi@goffi.org>
parents: 3888
diff changeset
179 ):
3945
2b2856ae5eeb plugin XEP-0470: fix autocreate in `create_attachments_node`:
Goffi <goffi@goffi.org>
parents: 3901
diff changeset
180 """Create node for attachements if necessary
2b2856ae5eeb plugin XEP-0470: fix autocreate in `create_attachments_node`:
Goffi <goffi@goffi.org>
parents: 3901
diff changeset
181
2b2856ae5eeb plugin XEP-0470: fix autocreate in `create_attachments_node`:
Goffi <goffi@goffi.org>
parents: 3901
diff changeset
182 @param service: service of target node
2b2856ae5eeb plugin XEP-0470: fix autocreate in `create_attachments_node`:
Goffi <goffi@goffi.org>
parents: 3901
diff changeset
183 @param node: node where target item is published
2b2856ae5eeb plugin XEP-0470: fix autocreate in `create_attachments_node`:
Goffi <goffi@goffi.org>
parents: 3901
diff changeset
184 @param item_id: ID of target item
2b2856ae5eeb plugin XEP-0470: fix autocreate in `create_attachments_node`:
Goffi <goffi@goffi.org>
parents: 3901
diff changeset
185 @param autocrate: if True, target node is create if it doesn't exist
2b2856ae5eeb plugin XEP-0470: fix autocreate in `create_attachments_node`:
Goffi <goffi@goffi.org>
parents: 3901
diff changeset
186 """
2b2856ae5eeb plugin XEP-0470: fix autocreate in `create_attachments_node`:
Goffi <goffi@goffi.org>
parents: 3901
diff changeset
187 try:
2b2856ae5eeb plugin XEP-0470: fix autocreate in `create_attachments_node`:
Goffi <goffi@goffi.org>
parents: 3901
diff changeset
188 node_config = await self._p.getConfiguration(client, service, node)
2b2856ae5eeb plugin XEP-0470: fix autocreate in `create_attachments_node`:
Goffi <goffi@goffi.org>
parents: 3901
diff changeset
189 except error.StanzaError as e:
2b2856ae5eeb plugin XEP-0470: fix autocreate in `create_attachments_node`:
Goffi <goffi@goffi.org>
parents: 3901
diff changeset
190 if e.condition == "item-not-found" and autocreate:
2b2856ae5eeb plugin XEP-0470: fix autocreate in `create_attachments_node`:
Goffi <goffi@goffi.org>
parents: 3901
diff changeset
191 # we auto-create the missing node
2b2856ae5eeb plugin XEP-0470: fix autocreate in `create_attachments_node`:
Goffi <goffi@goffi.org>
parents: 3901
diff changeset
192 await self._p.createNode(
2b2856ae5eeb plugin XEP-0470: fix autocreate in `create_attachments_node`:
Goffi <goffi@goffi.org>
parents: 3901
diff changeset
193 client, service, node
2b2856ae5eeb plugin XEP-0470: fix autocreate in `create_attachments_node`:
Goffi <goffi@goffi.org>
parents: 3901
diff changeset
194 )
2b2856ae5eeb plugin XEP-0470: fix autocreate in `create_attachments_node`:
Goffi <goffi@goffi.org>
parents: 3901
diff changeset
195 node_config = await self._p.getConfiguration(client, service, node)
2b2856ae5eeb plugin XEP-0470: fix autocreate in `create_attachments_node`:
Goffi <goffi@goffi.org>
parents: 3901
diff changeset
196 else:
2b2856ae5eeb plugin XEP-0470: fix autocreate in `create_attachments_node`:
Goffi <goffi@goffi.org>
parents: 3901
diff changeset
197 raise e
3901
43024e50b701 plugin XEP-0470: add `create_attachments_node` method:
Goffi <goffi@goffi.org>
parents: 3888
diff changeset
198 try:
43024e50b701 plugin XEP-0470: add `create_attachments_node` method:
Goffi <goffi@goffi.org>
parents: 3888
diff changeset
199 # 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
200 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
201 except KeyError:
43024e50b701 plugin XEP-0470: add `create_attachments_node` method:
Goffi <goffi@goffi.org>
parents: 3888
diff changeset
202 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
203 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
204 # 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
205 try:
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
206 await self._p.createIfNewNode(
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
207 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
208 )
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
209 except Exception as e:
3901
43024e50b701 plugin XEP-0470: add `create_attachments_node` method:
Goffi <goffi@goffi.org>
parents: 3888
diff changeset
210 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
211
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
212 def items2attachmentData(
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
213 self,
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
214 client: SatXMPPEntity,
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
215 items: List[domish.Element]
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
216 ) -> List[Dict[str, Any]]:
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
217 """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
218 list_data = []
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
219 for item in items:
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
220 try:
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
221 attachments_elt = next(
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
222 item.elements(NS_PUBSUB_ATTACHMENTS, "attachments")
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
223 )
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
224 except StopIteration:
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 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
227 )
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
228 continue
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
229 item_id = item["id"]
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
230 publisher_s = item.getAttribute("publisher")
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
231 # 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
232 if publisher_s:
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
233 publisher = jid.JID(publisher_s)
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
234 if publisher.userhost() != item_id:
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
235 log.warning(
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
236 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
237 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
238 f"{item.toXml()}"
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
239 )
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
240 continue
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
241 try:
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
242 jid.JID(item_id)
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
243 except RuntimeError:
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
244 log.warning(
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
245 "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
246 f"{item.toXml}"
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
247 )
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
248 continue
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
249 data = {
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
250 "from": item_id
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
251 }
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
252 for handler in self.handlers.values():
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
253 handler["get"](client, attachments_elt, data)
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
254 if len(data) > 1:
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
255 list_data.append(data)
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
256 return list_data
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
257
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
258 def _get(
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
259 self,
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
260 service_s: str,
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
261 node: str,
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
262 item: str,
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
263 senders_s: List[str],
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
264 extra_s: str,
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
265 profile_key: str
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
266 ) -> defer.Deferred:
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
267 client = self.host.getClient(profile_key)
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
268 extra = data_format.deserialise(extra_s)
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
269 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
270 d = defer.ensureDeferred(
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
271 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
272 )
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
273 d.addCallback(
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
274 lambda ret:
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
275 (data_format.serialise(ret[0]),
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
276 data_format.serialise(ret[1]))
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
277 )
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
278 return d
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
279
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
280 async def getAttachments(
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
281 self,
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
282 client: SatXMPPEntity,
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
283 service: jid.JID,
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
284 node: str,
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
285 item: str,
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
286 senders: Optional[List[jid.JID]],
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
287 extra: Optional[dict] = None
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
288 ) -> Tuple[List[Dict[str, Any]], dict]:
3888
aa7197b67c26 component AP gateway: AP <=> XMPP reactions conversions:
Goffi <goffi@goffi.org>
parents: 3869
diff changeset
289 """Retrieve data attached to a pubsub item
aa7197b67c26 component AP gateway: AP <=> XMPP reactions conversions:
Goffi <goffi@goffi.org>
parents: 3869
diff changeset
290
aa7197b67c26 component AP gateway: AP <=> XMPP reactions conversions:
Goffi <goffi@goffi.org>
parents: 3869
diff changeset
291 @param service: pubsub service where the node is
aa7197b67c26 component AP gateway: AP <=> XMPP reactions conversions:
Goffi <goffi@goffi.org>
parents: 3869
diff changeset
292 @param node: pubsub node containing the item
aa7197b67c26 component AP gateway: AP <=> XMPP reactions conversions:
Goffi <goffi@goffi.org>
parents: 3869
diff changeset
293 @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
294 @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
295 entities will be retrieved.
aa7197b67c26 component AP gateway: AP <=> XMPP reactions conversions:
Goffi <goffi@goffi.org>
parents: 3869
diff changeset
296 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
297 @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
298 ``getItems`` call.
aa7197b67c26 component AP gateway: AP <=> XMPP reactions conversions:
Goffi <goffi@goffi.org>
parents: 3869
diff changeset
299 @return: A tuple with:
aa7197b67c26 component AP gateway: AP <=> XMPP reactions conversions:
Goffi <goffi@goffi.org>
parents: 3869
diff changeset
300 - 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
301 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
302 (contrarily to attachments data used with ``setAttachments``).
aa7197b67c26 component AP gateway: AP <=> XMPP reactions conversions:
Goffi <goffi@goffi.org>
parents: 3869
diff changeset
303 - metadata returned by the call to ``getItems``
aa7197b67c26 component AP gateway: AP <=> XMPP reactions conversions:
Goffi <goffi@goffi.org>
parents: 3869
diff changeset
304 """
3864
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
305 if extra is None:
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
306 extra = {}
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
307 attachment_node = self.getAttachmentNodeName(service, node, item)
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
308 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
309 items, metadata = await self._p.getItems(
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
310 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
311 )
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
312 list_data = self.items2attachmentData(client, items)
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
313
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
314 return list_data, metadata
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
315
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
316 def _set(
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
317 self,
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
318 attachments_s: str,
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
319 profile_key: str
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
320 ) -> None:
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
321 client = self.host.getClient(profile_key)
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
322 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
323 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
324
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
325 def applySetHandler(
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
326 self,
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
327 client: SatXMPPEntity,
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
328 attachments_data: dict,
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
329 item_elt: Optional[domish.Element],
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
330 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
331 from_jid: Optional[jid.JID] = None,
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
332 ) -> domish.Element:
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
333 """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
334
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
335 @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
336 ``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
337 @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
338 ``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
339 @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
340 will be modified in place
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
341 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
342 @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
343 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
344 @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
345 """
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
346 attachments_data.setdefault("extra", {})
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
347 if item_elt is None:
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
348 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
349 item_elt = pubsub.Item(item_id)
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
350 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
351
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
352 try:
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
353 attachments_elt = next(
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
354 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
355 )
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
356 except StopIteration:
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
357 log.warning(
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
358 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
359 )
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
360 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
361
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
362 if handlers is None:
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
363 handlers = list(self.handlers.keys())
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
364
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
365 for name, namespace in handlers:
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
366 try:
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
367 handler = self.handlers[(name, namespace)]
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
368 except KeyError:
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
369 log.error(
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
370 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
371 "ignoring"
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
372 )
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
373 continue
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
374 try:
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
375 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
376 except StopIteration:
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
377 former_elt = None
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
378 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
379 if new_elt != former_elt:
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
380 if former_elt is not None:
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
381 attachments_elt.children.remove(former_elt)
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
382 if new_elt is not None:
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
383 attachments_elt.addChild(new_elt)
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
384 return item_elt
3864
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
385
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
386 async def setAttachments(
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
387 self,
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
388 client: SatXMPPEntity,
3869
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
389 attachments_data: Dict[str, Any]
3864
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
390 ) -> None:
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
391 """Set or update attachments
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
392
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
393 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
394 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
395 "operation" key.
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
396
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
397 "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
398 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
399 but not for "noticed").
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 @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
402 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
403 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
404 ``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
405
3864
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
406 """
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
407 try:
3869
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
408 service = jid.JID(attachments_data["service"])
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
409 node = attachments_data["node"]
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
410 item = attachments_data["id"]
3864
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
411 except (KeyError, RuntimeError):
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
412 raise ValueError(
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
413 '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
414 )
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
415 attachment_node = self.getAttachmentNodeName(service, node, item)
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
416 try:
3869
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
417 items, __ = await self._p.getItems(
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
418 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
419 )
3869
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
420 except exceptions.NotFound:
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
421 item_elt = None
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
422 else:
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
423 if not items:
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
424 item_elt = None
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
425 else:
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
426 item_elt = items[0]
3864
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
427
3869
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
428 item_elt = self.applySetHandler(
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
429 client,
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
430 attachments_data,
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
431 item_elt=item_elt,
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
432 )
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
433
3864
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
434 try:
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
435 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
436 except error.StanzaError as e:
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
437 if e.condition == "item-not-found":
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
438 # 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
439 log.warning(
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
440 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
441 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
442 )
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
443 raise exceptions.NotFound("No attachment node available")
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
444 else:
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
445 raise e
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
446
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
447 async def subscribe(
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
448 self,
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
449 client: SatXMPPEntity,
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
450 service: jid.JID,
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
451 node: str,
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
452 item: str,
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
453 ) -> None:
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
454 """Subscribe to attachment node targeting the item
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
455
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
456 @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
457 @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
458 @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
459 """
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
460 attachment_node = self.getAttachmentNodeName(service, node, item)
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
461 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
462
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
463
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
464 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
465 """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
466
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
467 @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
468 @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
469 ``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
470 """
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
471 timestamp_raw = attachment_elt.getAttribute("timestamp")
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
472 if timestamp_raw:
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
473 try:
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
474 timestamp = date_utils.date_parse(timestamp_raw)
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
475 except date_utils.ParserError:
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
476 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
477 else:
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
478 data["timestamp"] = timestamp
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 noticedGet(
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 attachments_elt: domish.Element,
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
484 data: Dict[str, Any],
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
485 ) -> None:
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
486 try:
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
487 noticed_elt = next(
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
488 attachments_elt.elements(NS_PUBSUB_ATTACHMENTS, "noticed")
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
489 )
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
490 except StopIteration:
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
491 pass
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
492 else:
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
493 noticed_data = {
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
494 "noticed": True
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
495 }
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
496 self.setTimestamp(noticed_elt, noticed_data)
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
497 data["noticed"] = noticed_data
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
498
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
499 def noticedSet(
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
500 self,
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
501 client: SatXMPPEntity,
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
502 data: Dict[str, Any],
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
503 former_elt: Optional[domish.Element]
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
504 ) -> Optional[domish.Element]:
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
505 """add or remove a <noticed> attachment
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
506
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
507 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
508 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
509 """
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
510 noticed = data["extra"].get("noticed")
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
511 if noticed is None:
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
512 return former_elt
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
513 elif noticed:
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
514 return domish.Element(
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
515 (NS_PUBSUB_ATTACHMENTS, "noticed"),
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
516 attribs = {
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
517 "timestamp": xmpp_date()
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
518 }
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
519 )
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
520 else:
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
521 return None
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
522
3888
aa7197b67c26 component AP gateway: AP <=> XMPP reactions conversions:
Goffi <goffi@goffi.org>
parents: 3869
diff changeset
523 def reactionsGet(
3864
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
524 self,
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
525 client: SatXMPPEntity,
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
526 attachments_elt: domish.Element,
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 ) -> None:
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
529 try:
3888
aa7197b67c26 component AP gateway: AP <=> XMPP reactions conversions:
Goffi <goffi@goffi.org>
parents: 3869
diff changeset
530 reactions_elt = next(
aa7197b67c26 component AP gateway: AP <=> XMPP reactions conversions:
Goffi <goffi@goffi.org>
parents: 3869
diff changeset
531 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
532 )
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
533 except StopIteration:
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
534 pass
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
535 else:
3888
aa7197b67c26 component AP gateway: AP <=> XMPP reactions conversions:
Goffi <goffi@goffi.org>
parents: 3869
diff changeset
536 reactions_data = {"reactions": []}
aa7197b67c26 component AP gateway: AP <=> XMPP reactions conversions:
Goffi <goffi@goffi.org>
parents: 3869
diff changeset
537 reactions = reactions_data["reactions"]
aa7197b67c26 component AP gateway: AP <=> XMPP reactions conversions:
Goffi <goffi@goffi.org>
parents: 3869
diff changeset
538 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
539 reactions.append(str(reaction_elt))
aa7197b67c26 component AP gateway: AP <=> XMPP reactions conversions:
Goffi <goffi@goffi.org>
parents: 3869
diff changeset
540 self.setTimestamp(reactions_elt, reactions_data)
aa7197b67c26 component AP gateway: AP <=> XMPP reactions conversions:
Goffi <goffi@goffi.org>
parents: 3869
diff changeset
541 data["reactions"] = reactions_data
3864
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
542
3888
aa7197b67c26 component AP gateway: AP <=> XMPP reactions conversions:
Goffi <goffi@goffi.org>
parents: 3869
diff changeset
543 def reactionsSet(
3864
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
544 self,
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
545 client: SatXMPPEntity,
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
546 data: Dict[str, Any],
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
547 former_elt: Optional[domish.Element]
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
548 ) -> Optional[domish.Element]:
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
549 """update the <reaction> attachment"""
3888
aa7197b67c26 component AP gateway: AP <=> XMPP reactions conversions:
Goffi <goffi@goffi.org>
parents: 3869
diff changeset
550 reactions_data = data["extra"].get("reactions")
aa7197b67c26 component AP gateway: AP <=> XMPP reactions conversions:
Goffi <goffi@goffi.org>
parents: 3869
diff changeset
551 if reactions_data is None:
3864
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
552 return former_elt
3888
aa7197b67c26 component AP gateway: AP <=> XMPP reactions conversions:
Goffi <goffi@goffi.org>
parents: 3869
diff changeset
553 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
554 if operation_type == "update":
3888
aa7197b67c26 component AP gateway: AP <=> XMPP reactions conversions:
Goffi <goffi@goffi.org>
parents: 3869
diff changeset
555 former_reactions = {
aa7197b67c26 component AP gateway: AP <=> XMPP reactions conversions:
Goffi <goffi@goffi.org>
parents: 3869
diff changeset
556 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
557 } if former_elt is not None else set()
aa7197b67c26 component AP gateway: AP <=> XMPP reactions conversions:
Goffi <goffi@goffi.org>
parents: 3869
diff changeset
558 added_reactions = set(reactions_data.get("add") or [])
aa7197b67c26 component AP gateway: AP <=> XMPP reactions conversions:
Goffi <goffi@goffi.org>
parents: 3869
diff changeset
559 removed_reactions = set(reactions_data.get("remove") or [])
aa7197b67c26 component AP gateway: AP <=> XMPP reactions conversions:
Goffi <goffi@goffi.org>
parents: 3869
diff changeset
560 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
561 elif operation_type == "replace":
3888
aa7197b67c26 component AP gateway: AP <=> XMPP reactions conversions:
Goffi <goffi@goffi.org>
parents: 3869
diff changeset
562 reactions = reactions_data.get("reactions") or []
3864
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
563 else:
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
564 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
565 if reactions:
3888
aa7197b67c26 component AP gateway: AP <=> XMPP reactions conversions:
Goffi <goffi@goffi.org>
parents: 3869
diff changeset
566 reactions_elt = domish.Element(
aa7197b67c26 component AP gateway: AP <=> XMPP reactions conversions:
Goffi <goffi@goffi.org>
parents: 3869
diff changeset
567 (NS_PUBSUB_ATTACHMENTS, "reactions"),
3864
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
568 attribs = {
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
569 "timestamp": xmpp_date()
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
570 }
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
571 )
3888
aa7197b67c26 component AP gateway: AP <=> XMPP reactions conversions:
Goffi <goffi@goffi.org>
parents: 3869
diff changeset
572 for reactions_data in reactions:
aa7197b67c26 component AP gateway: AP <=> XMPP reactions conversions:
Goffi <goffi@goffi.org>
parents: 3869
diff changeset
573 reactions_elt.addElement("reaction", content=reactions_data)
aa7197b67c26 component AP gateway: AP <=> XMPP reactions conversions:
Goffi <goffi@goffi.org>
parents: 3869
diff changeset
574 return reactions_elt
3864
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
575 else:
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
576 return None
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
577
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
578
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
579 @implementer(iwokkel.IDisco)
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
580 class PubsubAttachments_Handler(xmlstream.XMPPHandler):
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
581
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
582 def getDiscoInfo(self, requestor, service, nodeIdentifier=""):
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
583 return [disco.DiscoFeature(NS_PUBSUB_ATTACHMENTS)]
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
584
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
585 def getDiscoItems(self, requestor, service, nodeIdentifier=""):
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
586 return []