annotate libervia/backend/plugins/plugin_xep_0470.py @ 4305:4cd4922de876

tests: reformat tests using black.
author Goffi <goffi@goffi.org>
date Thu, 26 Sep 2024 16:11:56 +0200
parents 0d7bb4df2343
children
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
4071
4b842c1fb686 refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents: 4037
diff changeset
27 from libervia.backend.core.constants import Const as C
4b842c1fb686 refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents: 4037
diff changeset
28 from libervia.backend.core.i18n import _
4b842c1fb686 refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents: 4037
diff changeset
29 from libervia.backend.core.log import getLogger
4b842c1fb686 refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents: 4037
diff changeset
30 from libervia.backend.core.core_types import SatXMPPEntity
4b842c1fb686 refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents: 4037
diff changeset
31 from libervia.backend.core import exceptions
4b842c1fb686 refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents: 4037
diff changeset
32 from libervia.backend.tools.common import uri, data_format, date_utils
4b842c1fb686 refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents: 4037
diff changeset
33 from libervia.backend.tools.utils import as_deferred, 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"))
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3993
diff changeset
61 host.register_namespace("pubsub-attachments", NS_PUBSUB_ATTACHMENTS)
3864
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]] = {}
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3993
diff changeset
65 host.trigger.add("XEP-0277_send", self.on_mb_send)
3956
3cb9ade2ab84 plugin pubsub signing: pubsub items signature implementation:
Goffi <goffi@goffi.org>
parents: 3945
diff changeset
66 self.register_attachment_handler(
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3993
diff changeset
67 "noticed", NS_PUBSUB_ATTACHMENTS, self.noticed_get, self.noticed_set
3864
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(
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3993
diff changeset
70 "reactions", NS_PUBSUB_ATTACHMENTS, self.reactions_get, self.reactions_set
3864
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
71 )
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3993
diff changeset
72 host.bridge.add_method(
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3993
diff changeset
73 "ps_attachments_get",
3864
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 )
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3993
diff changeset
80 host.bridge.add_method(
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3993
diff changeset
81 "ps_attachments_set",
3864
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
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3993
diff changeset
89 def get_handler(self, client):
3864
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,
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4263
diff changeset
96 get_cb: Callable[[SatXMPPEntity, domish.Element, Dict[str, Any]], None],
3864
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
97 set_cb: Callable[
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
98 [SatXMPPEntity, Dict[str, Any], Optional[domish.Element]],
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4263
diff changeset
99 Optional[domish.Element],
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4263
diff changeset
100 ],
3864
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
101 ) -> None:
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
102 """Register callbacks to handle an attachment
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
103
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
104 @param name: name of the element
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
105 @param namespace: namespace of the element
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
106 (name, namespace) couple must be unique
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
107 @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
108 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
109 <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
110 parsed data
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
111 @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
112 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
113 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
114 request an ``update`` or a ``replace``.
3956
3cb9ade2ab84 plugin pubsub signing: pubsub items signature implementation:
Goffi <goffi@goffi.org>
parents: 3945
diff changeset
115 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
116 """
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
117 key = (name, namespace)
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
118 if key in self.handlers:
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
119 raise exceptions.ConflictError(
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
120 f"({name}, {namespace}) attachment handlers are already registered"
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
121 )
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4263
diff changeset
122 self.handlers[(name, namespace)] = {"get": get_cb, "set": set_cb}
3864
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
123
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3993
diff changeset
124 def get_attachment_node_name(self, service: jid.JID, node: str, item: str) -> str:
3864
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
125 """Generate name to use for attachment node"""
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3993
diff changeset
126 target_item_uri = uri.build_xmpp_uri(
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4263
diff changeset
127 "pubsub", path=service.userhost(), node=node, item=item
3864
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
128 )
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
129 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
130
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3993
diff changeset
131 def is_attachment_node(self, node: str) -> bool:
3864
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
132 """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
133 return node.startswith(f"{NS_PUBSUB_ATTACHMENTS}/")
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
134
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3993
diff changeset
135 def attachment_node_2_item(self, node: str) -> Tuple[jid.JID, str, str]:
3864
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
136 """Retrieve service, node and item from attachement node's name"""
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3993
diff changeset
137 if not self.is_attachment_node(node):
3864
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
138 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
139 prefix_len = len(f"{NS_PUBSUB_ATTACHMENTS}/")
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
140 item_uri = node[prefix_len:]
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3993
diff changeset
141 parsed_uri = uri.parse_xmpp_uri(item_uri)
3864
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
142 if parsed_uri["type"] != "pubsub":
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
143 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
144 try:
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
145 service = jid.JID(parsed_uri["path"])
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
146 except RuntimeError:
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
147 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
148 node = parsed_uri["node"]
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
149 item = parsed_uri["item"]
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
150 return (service, node, item)
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
151
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3993
diff changeset
152 async def on_mb_send(
3864
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
153 self,
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
154 client: SatXMPPEntity,
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
155 service: jid.JID,
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
156 node: str,
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
157 item: domish.Element,
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4263
diff changeset
158 data: dict,
3864
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
159 ) -> bool:
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
160 """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
161 await self.create_attachments_node(
2b2856ae5eeb plugin XEP-0470: fix autocreate in `create_attachments_node`:
Goffi <goffi@goffi.org>
parents: 3901
diff changeset
162 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
163 )
3901
43024e50b701 plugin XEP-0470: add `create_attachments_node` method:
Goffi <goffi@goffi.org>
parents: 3888
diff changeset
164 return True
43024e50b701 plugin XEP-0470: add `create_attachments_node` method:
Goffi <goffi@goffi.org>
parents: 3888
diff changeset
165
43024e50b701 plugin XEP-0470: add `create_attachments_node` method:
Goffi <goffi@goffi.org>
parents: 3888
diff changeset
166 async def create_attachments_node(
43024e50b701 plugin XEP-0470: add `create_attachments_node` method:
Goffi <goffi@goffi.org>
parents: 3888
diff changeset
167 self,
43024e50b701 plugin XEP-0470: add `create_attachments_node` method:
Goffi <goffi@goffi.org>
parents: 3888
diff changeset
168 client: SatXMPPEntity,
43024e50b701 plugin XEP-0470: add `create_attachments_node` method:
Goffi <goffi@goffi.org>
parents: 3888
diff changeset
169 service: jid.JID,
43024e50b701 plugin XEP-0470: add `create_attachments_node` method:
Goffi <goffi@goffi.org>
parents: 3888
diff changeset
170 node: str,
3945
2b2856ae5eeb plugin XEP-0470: fix autocreate in `create_attachments_node`:
Goffi <goffi@goffi.org>
parents: 3901
diff changeset
171 item_id: str,
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4263
diff changeset
172 autocreate: bool = False,
3901
43024e50b701 plugin XEP-0470: add `create_attachments_node` method:
Goffi <goffi@goffi.org>
parents: 3888
diff changeset
173 ):
3945
2b2856ae5eeb plugin XEP-0470: fix autocreate in `create_attachments_node`:
Goffi <goffi@goffi.org>
parents: 3901
diff changeset
174 """Create node for attachements if necessary
2b2856ae5eeb plugin XEP-0470: fix autocreate in `create_attachments_node`:
Goffi <goffi@goffi.org>
parents: 3901
diff changeset
175
2b2856ae5eeb plugin XEP-0470: fix autocreate in `create_attachments_node`:
Goffi <goffi@goffi.org>
parents: 3901
diff changeset
176 @param service: service of target node
2b2856ae5eeb plugin XEP-0470: fix autocreate in `create_attachments_node`:
Goffi <goffi@goffi.org>
parents: 3901
diff changeset
177 @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
178 @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
179 @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
180 """
2b2856ae5eeb plugin XEP-0470: fix autocreate in `create_attachments_node`:
Goffi <goffi@goffi.org>
parents: 3901
diff changeset
181 try:
2b2856ae5eeb plugin XEP-0470: fix autocreate in `create_attachments_node`:
Goffi <goffi@goffi.org>
parents: 3901
diff changeset
182 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
183 except error.StanzaError as e:
2b2856ae5eeb plugin XEP-0470: fix autocreate in `create_attachments_node`:
Goffi <goffi@goffi.org>
parents: 3901
diff changeset
184 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
185 # we auto-create the missing node
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4263
diff changeset
186 await self._p.createNode(client, service, node)
3945
2b2856ae5eeb plugin XEP-0470: fix autocreate in `create_attachments_node`:
Goffi <goffi@goffi.org>
parents: 3901
diff changeset
187 node_config = await self._p.getConfiguration(client, service, node)
4263
2109d864a3e7 plugin XEP-470: Don't file configuration on `feature-not-implemented`.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
188 elif e.condition in ("forbidden", "feature-not-implemented"):
3993
722c25818778 plugin XEP-0470: create a default configuration if node config can't be accessed
Goffi <goffi@goffi.org>
parents: 3956
diff changeset
189 node_config = self._p.make_configuration_form({})
3945
2b2856ae5eeb plugin XEP-0470: fix autocreate in `create_attachments_node`:
Goffi <goffi@goffi.org>
parents: 3901
diff changeset
190 else:
2b2856ae5eeb plugin XEP-0470: fix autocreate in `create_attachments_node`:
Goffi <goffi@goffi.org>
parents: 3901
diff changeset
191 raise e
3901
43024e50b701 plugin XEP-0470: add `create_attachments_node` method:
Goffi <goffi@goffi.org>
parents: 3888
diff changeset
192 try:
43024e50b701 plugin XEP-0470: add `create_attachments_node` method:
Goffi <goffi@goffi.org>
parents: 3888
diff changeset
193 # 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
194 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
195 except KeyError:
43024e50b701 plugin XEP-0470: add `create_attachments_node` method:
Goffi <goffi@goffi.org>
parents: 3888
diff changeset
196 log.warning("pubsub#publish_model field is missing")
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3993
diff changeset
197 attachment_node = self.get_attachment_node_name(service, node, item_id)
3864
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
198 # 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
199 try:
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3993
diff changeset
200 await self._p.create_if_new_node(
3864
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
201 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
202 )
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
203 except Exception as e:
3901
43024e50b701 plugin XEP-0470: add `create_attachments_node` method:
Goffi <goffi@goffi.org>
parents: 3888
diff changeset
204 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
205
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3993
diff changeset
206 def items_2_attachment_data(
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4263
diff changeset
207 self, client: SatXMPPEntity, items: List[domish.Element]
3864
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
208 ) -> List[Dict[str, Any]]:
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
209 """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
210 list_data = []
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
211 for item in items:
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
212 try:
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
213 attachments_elt = next(
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
214 item.elements(NS_PUBSUB_ATTACHMENTS, "attachments")
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
215 )
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
216 except StopIteration:
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
217 log.warning(
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
218 "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
219 )
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
220 continue
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
221 item_id = item["id"]
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
222 publisher_s = item.getAttribute("publisher")
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
223 # 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
224 if publisher_s:
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
225 publisher = jid.JID(publisher_s)
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
226 if publisher.userhost() != item_id:
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
227 log.warning(
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
228 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
229 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
230 f"{item.toXml()}"
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
231 )
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
232 continue
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
233 try:
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
234 jid.JID(item_id)
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
235 except RuntimeError:
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 "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
238 f"{item.toXml}"
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
239 )
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
240 continue
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4263
diff changeset
241 data = {"from": item_id}
3864
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
242 for handler in self.handlers.values():
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
243 handler["get"](client, attachments_elt, data)
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
244 if len(data) > 1:
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
245 list_data.append(data)
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
246 return list_data
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
247
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
248 def _get(
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
249 self,
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
250 service_s: str,
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
251 node: str,
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
252 item: str,
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
253 senders_s: List[str],
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
254 extra_s: str,
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4263
diff changeset
255 profile_key: str,
3864
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
256 ) -> defer.Deferred:
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3993
diff changeset
257 client = self.host.get_client(profile_key)
3864
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
258 extra = data_format.deserialise(extra_s)
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
259 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
260 d = defer.ensureDeferred(
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3993
diff changeset
261 self.get_attachments(client, jid.JID(service_s), node, item, senders)
3864
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
262 )
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
263 d.addCallback(
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4263
diff changeset
264 lambda ret: (data_format.serialise(ret[0]), data_format.serialise(ret[1]))
3864
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
265 )
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
266 return d
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
267
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3993
diff changeset
268 async def get_attachments(
3864
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
269 self,
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
270 client: SatXMPPEntity,
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
271 service: jid.JID,
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
272 node: str,
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
273 item: str,
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
274 senders: Optional[List[jid.JID]],
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4263
diff changeset
275 extra: Optional[dict] = None,
3864
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
276 ) -> Tuple[List[Dict[str, Any]], dict]:
3888
aa7197b67c26 component AP gateway: AP <=> XMPP reactions conversions:
Goffi <goffi@goffi.org>
parents: 3869
diff changeset
277 """Retrieve data attached to a pubsub item
aa7197b67c26 component AP gateway: AP <=> XMPP reactions conversions:
Goffi <goffi@goffi.org>
parents: 3869
diff changeset
278
aa7197b67c26 component AP gateway: AP <=> XMPP reactions conversions:
Goffi <goffi@goffi.org>
parents: 3869
diff changeset
279 @param service: pubsub service where the node is
aa7197b67c26 component AP gateway: AP <=> XMPP reactions conversions:
Goffi <goffi@goffi.org>
parents: 3869
diff changeset
280 @param node: pubsub node containing the item
aa7197b67c26 component AP gateway: AP <=> XMPP reactions conversions:
Goffi <goffi@goffi.org>
parents: 3869
diff changeset
281 @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
282 @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
283 entities will be retrieved.
aa7197b67c26 component AP gateway: AP <=> XMPP reactions conversions:
Goffi <goffi@goffi.org>
parents: 3869
diff changeset
284 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
285 @param extra: extra data, will be used as ``extra`` argument when doing
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3993
diff changeset
286 ``get_items`` call.
3888
aa7197b67c26 component AP gateway: AP <=> XMPP reactions conversions:
Goffi <goffi@goffi.org>
parents: 3869
diff changeset
287 @return: A tuple with:
aa7197b67c26 component AP gateway: AP <=> XMPP reactions conversions:
Goffi <goffi@goffi.org>
parents: 3869
diff changeset
288 - 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
289 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
290 (contrarily to attachments data used with ``set_attachements``).
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3993
diff changeset
291 - metadata returned by the call to ``get_items``
3888
aa7197b67c26 component AP gateway: AP <=> XMPP reactions conversions:
Goffi <goffi@goffi.org>
parents: 3869
diff changeset
292 """
3864
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
293 if extra is None:
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
294 extra = {}
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3993
diff changeset
295 attachment_node = self.get_attachment_node_name(service, node, item)
3864
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
296 item_ids = [e.userhost() for e in senders] if senders else None
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3993
diff changeset
297 items, metadata = await self._p.get_items(
3864
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
298 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
299 )
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3993
diff changeset
300 list_data = self.items_2_attachment_data(client, items)
3864
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
301
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
302 return list_data, metadata
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
303
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4263
diff changeset
304 def _set(self, attachments_s: str, profile_key: str) -> None:
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3993
diff changeset
305 client = self.host.get_client(profile_key)
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4263
diff changeset
306 attachments = data_format.deserialise(attachments_s) or {}
3956
3cb9ade2ab84 plugin pubsub signing: pubsub items signature implementation:
Goffi <goffi@goffi.org>
parents: 3945
diff changeset
307 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
308
3956
3cb9ade2ab84 plugin pubsub signing: pubsub items signature implementation:
Goffi <goffi@goffi.org>
parents: 3945
diff changeset
309 async def apply_set_handler(
3869
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
310 self,
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
311 client: SatXMPPEntity,
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
312 attachments_data: dict,
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
313 item_elt: Optional[domish.Element],
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
314 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
315 from_jid: Optional[jid.JID] = None,
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
316 ) -> domish.Element:
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
317 """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
318
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
319 @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
320 ``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
321 @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
322 ``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
323 @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
324 will be modified in place
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
325 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
326 @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
327 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
328 @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
329 """
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
330 attachments_data.setdefault("extra", {})
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
331 if item_elt is None:
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
332 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
333 item_elt = pubsub.Item(item_id)
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
334 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
335
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
336 try:
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
337 attachments_elt = next(
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
338 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
339 )
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
340 except StopIteration:
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
341 log.warning(
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
342 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
343 )
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
344 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
345
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
346 if handlers is None:
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
347 handlers = list(self.handlers.keys())
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
348
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
349 for name, namespace in handlers:
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
350 try:
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
351 handler = self.handlers[(name, namespace)]
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
352 except KeyError:
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
353 log.error(
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
354 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
355 "ignoring"
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 continue
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
358 try:
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
359 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
360 except StopIteration:
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
361 former_elt = None
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3993
diff changeset
362 new_elt = await as_deferred(
3956
3cb9ade2ab84 plugin pubsub signing: pubsub items signature implementation:
Goffi <goffi@goffi.org>
parents: 3945
diff changeset
363 handler["set"], client, attachments_data, former_elt
3cb9ade2ab84 plugin pubsub signing: pubsub items signature implementation:
Goffi <goffi@goffi.org>
parents: 3945
diff changeset
364 )
3869
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
365 if new_elt != former_elt:
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
366 if former_elt is not None:
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
367 attachments_elt.children.remove(former_elt)
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
368 if new_elt is not None:
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
369 attachments_elt.addChild(new_elt)
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
370 return item_elt
3864
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
371
3956
3cb9ade2ab84 plugin pubsub signing: pubsub items signature implementation:
Goffi <goffi@goffi.org>
parents: 3945
diff changeset
372 async def set_attachements(
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4263
diff changeset
373 self, client: SatXMPPEntity, attachments_data: Dict[str, Any]
3864
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
374 ) -> None:
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
375 """Set or update attachments
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
376
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
377 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
378 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
379 "operation" key.
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
380
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
381 "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
382 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
383 but not for "noticed").
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
384
3869
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
385 @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
386 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
387 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
388 ``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
389
3864
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
390 """
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
391 try:
3869
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
392 service = jid.JID(attachments_data["service"])
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
393 node = attachments_data["node"]
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
394 item = attachments_data["id"]
3864
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
395 except (KeyError, RuntimeError):
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4263
diff changeset
396 raise ValueError('data must have "service", "node" and "id" set')
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3993
diff changeset
397 attachment_node = self.get_attachment_node_name(service, node, item)
3864
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
398 try:
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3993
diff changeset
399 items, __ = await self._p.get_items(
3869
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
400 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
401 )
3869
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
402 except exceptions.NotFound:
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
403 item_elt = None
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
404 else:
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
405 if not items:
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
406 item_elt = None
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
407 else:
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
408 item_elt = items[0]
3864
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
409
3956
3cb9ade2ab84 plugin pubsub signing: pubsub items signature implementation:
Goffi <goffi@goffi.org>
parents: 3945
diff changeset
410 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
411 client,
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
412 attachments_data,
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
413 item_elt=item_elt,
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
414 )
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
415
3864
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
416 try:
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3993
diff changeset
417 await self._p.send_items(client, service, attachment_node, [item_elt])
3864
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
418 except error.StanzaError as e:
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
419 if e.condition == "item-not-found":
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
420 # 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
421 log.warning(
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
422 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
423 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
424 )
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
425 raise exceptions.NotFound("No attachment node available")
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
426 else:
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
427 raise e
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
428
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
429 async def subscribe(
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
430 self,
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
431 client: SatXMPPEntity,
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
432 service: jid.JID,
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
433 node: str,
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
434 item: str,
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
435 ) -> None:
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
436 """Subscribe to attachment node targeting the item
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
437
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
438 @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
439 @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
440 @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
441 """
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3993
diff changeset
442 attachment_node = self.get_attachment_node_name(service, node, item)
3864
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
443 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
444
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3993
diff changeset
445 def set_timestamp(self, attachment_elt: domish.Element, data: dict) -> None:
3864
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
446 """Check if a ``timestamp`` attribute is set, parse it, and fill data
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
447
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
448 @param attachments_elt: element where the ``timestamp`` attribute may be set
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
449 @param data: data specific to the attachment (i.e. not the whole microblog data)
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
450 ``timestamp`` field will be set there if timestamp exists and is parsable
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
451 """
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
452 timestamp_raw = attachment_elt.getAttribute("timestamp")
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
453 if timestamp_raw:
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
454 try:
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
455 timestamp = date_utils.date_parse(timestamp_raw)
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
456 except date_utils.ParserError:
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
457 log.warning(f"can't parse timestamp: {timestamp_raw}")
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
458 else:
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
459 data["timestamp"] = timestamp
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
460
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3993
diff changeset
461 def noticed_get(
3864
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
462 self,
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
463 client: SatXMPPEntity,
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
464 attachments_elt: domish.Element,
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
465 data: Dict[str, Any],
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
466 ) -> None:
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
467 try:
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4263
diff changeset
468 noticed_elt = next(attachments_elt.elements(NS_PUBSUB_ATTACHMENTS, "noticed"))
3864
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
469 except StopIteration:
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
470 pass
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
471 else:
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4263
diff changeset
472 noticed_data = {"noticed": True}
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3993
diff changeset
473 self.set_timestamp(noticed_elt, noticed_data)
3864
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
474 data["noticed"] = noticed_data
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
475
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3993
diff changeset
476 def noticed_set(
3864
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
477 self,
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
478 client: SatXMPPEntity,
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
479 data: Dict[str, Any],
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4263
diff changeset
480 former_elt: Optional[domish.Element],
3864
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
481 ) -> Optional[domish.Element]:
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
482 """add or remove a <noticed> attachment
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
483
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
484 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
485 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
486 """
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
487 noticed = data["extra"].get("noticed")
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
488 if noticed is None:
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
489 return former_elt
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
490 elif noticed:
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
491 return domish.Element(
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4263
diff changeset
492 (NS_PUBSUB_ATTACHMENTS, "noticed"), attribs={"timestamp": xmpp_date()}
3864
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
493 )
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
494 else:
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
495 return None
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
496
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3993
diff changeset
497 def reactions_get(
3864
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
498 self,
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
499 client: SatXMPPEntity,
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
500 attachments_elt: domish.Element,
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
501 data: Dict[str, Any],
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
502 ) -> None:
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
503 try:
3888
aa7197b67c26 component AP gateway: AP <=> XMPP reactions conversions:
Goffi <goffi@goffi.org>
parents: 3869
diff changeset
504 reactions_elt = next(
aa7197b67c26 component AP gateway: AP <=> XMPP reactions conversions:
Goffi <goffi@goffi.org>
parents: 3869
diff changeset
505 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
506 )
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
507 except StopIteration:
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
508 pass
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
509 else:
3888
aa7197b67c26 component AP gateway: AP <=> XMPP reactions conversions:
Goffi <goffi@goffi.org>
parents: 3869
diff changeset
510 reactions_data = {"reactions": []}
aa7197b67c26 component AP gateway: AP <=> XMPP reactions conversions:
Goffi <goffi@goffi.org>
parents: 3869
diff changeset
511 reactions = reactions_data["reactions"]
aa7197b67c26 component AP gateway: AP <=> XMPP reactions conversions:
Goffi <goffi@goffi.org>
parents: 3869
diff changeset
512 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
513 reactions.append(str(reaction_elt))
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3993
diff changeset
514 self.set_timestamp(reactions_elt, reactions_data)
3888
aa7197b67c26 component AP gateway: AP <=> XMPP reactions conversions:
Goffi <goffi@goffi.org>
parents: 3869
diff changeset
515 data["reactions"] = reactions_data
3864
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
516
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3993
diff changeset
517 def reactions_set(
3864
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
518 self,
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
519 client: SatXMPPEntity,
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
520 data: Dict[str, Any],
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4263
diff changeset
521 former_elt: Optional[domish.Element],
3864
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
522 ) -> Optional[domish.Element]:
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
523 """update the <reaction> attachment"""
3888
aa7197b67c26 component AP gateway: AP <=> XMPP reactions conversions:
Goffi <goffi@goffi.org>
parents: 3869
diff changeset
524 reactions_data = data["extra"].get("reactions")
aa7197b67c26 component AP gateway: AP <=> XMPP reactions conversions:
Goffi <goffi@goffi.org>
parents: 3869
diff changeset
525 if reactions_data is None:
3864
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
526 return former_elt
3888
aa7197b67c26 component AP gateway: AP <=> XMPP reactions conversions:
Goffi <goffi@goffi.org>
parents: 3869
diff changeset
527 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
528 if operation_type == "update":
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4263
diff changeset
529 former_reactions = (
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4263
diff changeset
530 {str(r) for r in former_elt.elements(NS_PUBSUB_ATTACHMENTS, "reaction")}
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4263
diff changeset
531 if former_elt is not None
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4263
diff changeset
532 else set()
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4263
diff changeset
533 )
3888
aa7197b67c26 component AP gateway: AP <=> XMPP reactions conversions:
Goffi <goffi@goffi.org>
parents: 3869
diff changeset
534 added_reactions = set(reactions_data.get("add") or [])
aa7197b67c26 component AP gateway: AP <=> XMPP reactions conversions:
Goffi <goffi@goffi.org>
parents: 3869
diff changeset
535 removed_reactions = set(reactions_data.get("remove") or [])
aa7197b67c26 component AP gateway: AP <=> XMPP reactions conversions:
Goffi <goffi@goffi.org>
parents: 3869
diff changeset
536 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
537 elif operation_type == "replace":
3888
aa7197b67c26 component AP gateway: AP <=> XMPP reactions conversions:
Goffi <goffi@goffi.org>
parents: 3869
diff changeset
538 reactions = reactions_data.get("reactions") or []
3864
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
539 else:
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
540 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
541 if reactions:
3888
aa7197b67c26 component AP gateway: AP <=> XMPP reactions conversions:
Goffi <goffi@goffi.org>
parents: 3869
diff changeset
542 reactions_elt = domish.Element(
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4263
diff changeset
543 (NS_PUBSUB_ATTACHMENTS, "reactions"), attribs={"timestamp": xmpp_date()}
3864
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
544 )
3888
aa7197b67c26 component AP gateway: AP <=> XMPP reactions conversions:
Goffi <goffi@goffi.org>
parents: 3869
diff changeset
545 for reactions_data in reactions:
aa7197b67c26 component AP gateway: AP <=> XMPP reactions conversions:
Goffi <goffi@goffi.org>
parents: 3869
diff changeset
546 reactions_elt.addElement("reaction", content=reactions_data)
aa7197b67c26 component AP gateway: AP <=> XMPP reactions conversions:
Goffi <goffi@goffi.org>
parents: 3869
diff changeset
547 return reactions_elt
3864
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
548 else:
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
549 return None
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
550
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
551
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
552 @implementer(iwokkel.IDisco)
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
553 class PubsubAttachments_Handler(xmlstream.XMPPHandler):
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
554
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
555 def getDiscoInfo(self, requestor, service, nodeIdentifier=""):
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
556 return [disco.DiscoFeature(NS_PUBSUB_ATTACHMENTS)]
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
557
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
558 def getDiscoItems(self, requestor, service, nodeIdentifier=""):
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
559 return []