annotate sat/plugins/plugin_xep_0470.py @ 3959:da0e772881c3

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