annotate libervia/backend/plugins/plugin_xep_0470.py @ 4112:bc60875cb3b8

plugin XEP-0166, XEP-0167, XEP-0234, XEP-0353: call events management to prepare for UI: - XEP-0166: add `jingle_preflight` and `jingle_preflight_cancel` methods to prepare a jingle session, principally used by XEP-0353 to create and cancel a session - XEP-0167: preflight methods implementation, workflow split in more methods/signals to handle UI and call events (e.g.: retract or reject a call) - XEP-0234: implementation of preflight methods as they are now mandatory - XEP-0353: handle various events using the new preflight methods rel 423
author Goffi <goffi@goffi.org>
date Wed, 09 Aug 2023 00:07:37 +0200
parents 4b842c1fb686
children 2109d864a3e7
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,
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
96 get_cb: Callable[
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
97 [SatXMPPEntity, domish.Element, Dict[str, Any]],
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
98 None],
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
99 set_cb: Callable[
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
100 [SatXMPPEntity, Dict[str, Any], Optional[domish.Element]],
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
101 Optional[domish.Element]],
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
102 ) -> None:
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
103 """Register callbacks to handle an attachment
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
104
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
105 @param name: name of the element
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
106 @param namespace: namespace of the element
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
107 (name, namespace) couple must be unique
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
108 @param get: method to call when attachments are retrieved
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
109 it will be called with (client, element, data) where element is the
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
110 <attachments> element to parse, and data must be updated in place with
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
111 parsed data
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
112 @param set: method to call when the attachment need to be set or udpated
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
113 it will be called with (client, data, former_elt of None if there was no
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
114 former element). When suitable, ``operation`` should be used to check if we
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
115 request an ``update`` or a ``replace``.
3956
3cb9ade2ab84 plugin pubsub signing: pubsub items signature implementation:
Goffi <goffi@goffi.org>
parents: 3945
diff changeset
116 The callback can be either a blocking method, a Deferred or a coroutine
3864
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
117 """
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
118 key = (name, namespace)
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
119 if key in self.handlers:
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
120 raise exceptions.ConflictError(
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
121 f"({name}, {namespace}) attachment handlers are already registered"
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
122 )
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
123 self.handlers[(name, namespace)] = {
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
124 "get": get_cb,
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
125 "set": set_cb
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
126 }
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
127
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3993
diff changeset
128 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
129 """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
130 target_item_uri = uri.build_xmpp_uri(
3864
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
131 "pubsub",
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
132 path=service.userhost(),
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
133 node=node,
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
134 item=item
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
135 )
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
136 return f"{NS_PUBSUB_ATTACHMENTS}/{target_item_uri}"
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
137
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3993
diff changeset
138 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
139 """Return True if node name is an attachment node"""
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
140 return node.startswith(f"{NS_PUBSUB_ATTACHMENTS}/")
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
141
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3993
diff changeset
142 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
143 """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
144 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
145 raise ValueError("this is not an attachment node!")
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
146 prefix_len = len(f"{NS_PUBSUB_ATTACHMENTS}/")
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
147 item_uri = node[prefix_len:]
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3993
diff changeset
148 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
149 if parsed_uri["type"] != "pubsub":
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
150 raise ValueError(f"unexpected URI type, it must be a pubsub URI: {item_uri}")
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
151 try:
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
152 service = jid.JID(parsed_uri["path"])
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
153 except RuntimeError:
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
154 raise ValueError(f"invalid service in pubsub URI: {item_uri}")
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
155 node = parsed_uri["node"]
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
156 item = parsed_uri["item"]
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
157 return (service, node, item)
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
158
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3993
diff changeset
159 async def on_mb_send(
3864
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
160 self,
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
161 client: SatXMPPEntity,
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
162 service: jid.JID,
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
163 node: str,
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
164 item: domish.Element,
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
165 data: dict
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
166 ) -> bool:
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
167 """trigger to create attachment node on each publication"""
3945
2b2856ae5eeb plugin XEP-0470: fix autocreate in `create_attachments_node`:
Goffi <goffi@goffi.org>
parents: 3901
diff changeset
168 await self.create_attachments_node(
2b2856ae5eeb plugin XEP-0470: fix autocreate in `create_attachments_node`:
Goffi <goffi@goffi.org>
parents: 3901
diff changeset
169 client, service, node, item["id"], autocreate=True
2b2856ae5eeb plugin XEP-0470: fix autocreate in `create_attachments_node`:
Goffi <goffi@goffi.org>
parents: 3901
diff changeset
170 )
3901
43024e50b701 plugin XEP-0470: add `create_attachments_node` method:
Goffi <goffi@goffi.org>
parents: 3888
diff changeset
171 return True
43024e50b701 plugin XEP-0470: add `create_attachments_node` method:
Goffi <goffi@goffi.org>
parents: 3888
diff changeset
172
43024e50b701 plugin XEP-0470: add `create_attachments_node` method:
Goffi <goffi@goffi.org>
parents: 3888
diff changeset
173 async def create_attachments_node(
43024e50b701 plugin XEP-0470: add `create_attachments_node` method:
Goffi <goffi@goffi.org>
parents: 3888
diff changeset
174 self,
43024e50b701 plugin XEP-0470: add `create_attachments_node` method:
Goffi <goffi@goffi.org>
parents: 3888
diff changeset
175 client: SatXMPPEntity,
43024e50b701 plugin XEP-0470: add `create_attachments_node` method:
Goffi <goffi@goffi.org>
parents: 3888
diff changeset
176 service: jid.JID,
43024e50b701 plugin XEP-0470: add `create_attachments_node` method:
Goffi <goffi@goffi.org>
parents: 3888
diff changeset
177 node: str,
3945
2b2856ae5eeb plugin XEP-0470: fix autocreate in `create_attachments_node`:
Goffi <goffi@goffi.org>
parents: 3901
diff changeset
178 item_id: str,
2b2856ae5eeb plugin XEP-0470: fix autocreate in `create_attachments_node`:
Goffi <goffi@goffi.org>
parents: 3901
diff changeset
179 autocreate: bool = False
3901
43024e50b701 plugin XEP-0470: add `create_attachments_node` method:
Goffi <goffi@goffi.org>
parents: 3888
diff changeset
180 ):
3945
2b2856ae5eeb plugin XEP-0470: fix autocreate in `create_attachments_node`:
Goffi <goffi@goffi.org>
parents: 3901
diff changeset
181 """Create node for attachements if necessary
2b2856ae5eeb plugin XEP-0470: fix autocreate in `create_attachments_node`:
Goffi <goffi@goffi.org>
parents: 3901
diff changeset
182
2b2856ae5eeb plugin XEP-0470: fix autocreate in `create_attachments_node`:
Goffi <goffi@goffi.org>
parents: 3901
diff changeset
183 @param service: service of target node
2b2856ae5eeb plugin XEP-0470: fix autocreate in `create_attachments_node`:
Goffi <goffi@goffi.org>
parents: 3901
diff changeset
184 @param node: node where target item is published
2b2856ae5eeb plugin XEP-0470: fix autocreate in `create_attachments_node`:
Goffi <goffi@goffi.org>
parents: 3901
diff changeset
185 @param item_id: ID of target item
2b2856ae5eeb plugin XEP-0470: fix autocreate in `create_attachments_node`:
Goffi <goffi@goffi.org>
parents: 3901
diff changeset
186 @param autocrate: if True, target node is create if it doesn't exist
2b2856ae5eeb plugin XEP-0470: fix autocreate in `create_attachments_node`:
Goffi <goffi@goffi.org>
parents: 3901
diff changeset
187 """
2b2856ae5eeb plugin XEP-0470: fix autocreate in `create_attachments_node`:
Goffi <goffi@goffi.org>
parents: 3901
diff changeset
188 try:
2b2856ae5eeb plugin XEP-0470: fix autocreate in `create_attachments_node`:
Goffi <goffi@goffi.org>
parents: 3901
diff changeset
189 node_config = await self._p.getConfiguration(client, service, node)
2b2856ae5eeb plugin XEP-0470: fix autocreate in `create_attachments_node`:
Goffi <goffi@goffi.org>
parents: 3901
diff changeset
190 except error.StanzaError as e:
2b2856ae5eeb plugin XEP-0470: fix autocreate in `create_attachments_node`:
Goffi <goffi@goffi.org>
parents: 3901
diff changeset
191 if e.condition == "item-not-found" and autocreate:
2b2856ae5eeb plugin XEP-0470: fix autocreate in `create_attachments_node`:
Goffi <goffi@goffi.org>
parents: 3901
diff changeset
192 # we auto-create the missing node
2b2856ae5eeb plugin XEP-0470: fix autocreate in `create_attachments_node`:
Goffi <goffi@goffi.org>
parents: 3901
diff changeset
193 await self._p.createNode(
2b2856ae5eeb plugin XEP-0470: fix autocreate in `create_attachments_node`:
Goffi <goffi@goffi.org>
parents: 3901
diff changeset
194 client, service, node
2b2856ae5eeb plugin XEP-0470: fix autocreate in `create_attachments_node`:
Goffi <goffi@goffi.org>
parents: 3901
diff changeset
195 )
2b2856ae5eeb plugin XEP-0470: fix autocreate in `create_attachments_node`:
Goffi <goffi@goffi.org>
parents: 3901
diff changeset
196 node_config = await self._p.getConfiguration(client, service, node)
3993
722c25818778 plugin XEP-0470: create a default configuration if node config can't be accessed
Goffi <goffi@goffi.org>
parents: 3956
diff changeset
197 elif e.condition == "forbidden":
722c25818778 plugin XEP-0470: create a default configuration if node config can't be accessed
Goffi <goffi@goffi.org>
parents: 3956
diff changeset
198 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
199 else:
2b2856ae5eeb plugin XEP-0470: fix autocreate in `create_attachments_node`:
Goffi <goffi@goffi.org>
parents: 3901
diff changeset
200 raise e
3901
43024e50b701 plugin XEP-0470: add `create_attachments_node` method:
Goffi <goffi@goffi.org>
parents: 3888
diff changeset
201 try:
43024e50b701 plugin XEP-0470: add `create_attachments_node` method:
Goffi <goffi@goffi.org>
parents: 3888
diff changeset
202 # 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
203 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
204 except KeyError:
43024e50b701 plugin XEP-0470: add `create_attachments_node` method:
Goffi <goffi@goffi.org>
parents: 3888
diff changeset
205 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
206 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
207 # 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
208 try:
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3993
diff changeset
209 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
210 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
211 )
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
212 except Exception as e:
3901
43024e50b701 plugin XEP-0470: add `create_attachments_node` method:
Goffi <goffi@goffi.org>
parents: 3888
diff changeset
213 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
214
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3993
diff changeset
215 def items_2_attachment_data(
3864
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
216 self,
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
217 client: SatXMPPEntity,
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
218 items: List[domish.Element]
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
219 ) -> List[Dict[str, Any]]:
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
220 """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
221 list_data = []
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
222 for item in items:
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
223 try:
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
224 attachments_elt = next(
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
225 item.elements(NS_PUBSUB_ATTACHMENTS, "attachments")
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
226 )
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
227 except StopIteration:
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
228 log.warning(
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
229 "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
230 )
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
231 continue
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
232 item_id = item["id"]
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
233 publisher_s = item.getAttribute("publisher")
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
234 # 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
235 if publisher_s:
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
236 publisher = jid.JID(publisher_s)
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
237 if publisher.userhost() != item_id:
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
238 log.warning(
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
239 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
240 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
241 f"{item.toXml()}"
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
242 )
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
243 continue
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
244 try:
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
245 jid.JID(item_id)
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
246 except RuntimeError:
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
247 log.warning(
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
248 "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
249 f"{item.toXml}"
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
250 )
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
251 continue
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
252 data = {
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
253 "from": item_id
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
254 }
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
255 for handler in self.handlers.values():
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
256 handler["get"](client, attachments_elt, data)
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
257 if len(data) > 1:
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
258 list_data.append(data)
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
259 return list_data
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
260
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
261 def _get(
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
262 self,
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
263 service_s: str,
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
264 node: str,
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
265 item: str,
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
266 senders_s: List[str],
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
267 extra_s: str,
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
268 profile_key: str
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
269 ) -> defer.Deferred:
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3993
diff changeset
270 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
271 extra = data_format.deserialise(extra_s)
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
272 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
273 d = defer.ensureDeferred(
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3993
diff changeset
274 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
275 )
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
276 d.addCallback(
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
277 lambda ret:
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
278 (data_format.serialise(ret[0]),
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
279 data_format.serialise(ret[1]))
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
280 )
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
281 return d
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
282
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3993
diff changeset
283 async def get_attachments(
3864
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
284 self,
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
285 client: SatXMPPEntity,
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
286 service: jid.JID,
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
287 node: str,
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
288 item: str,
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
289 senders: Optional[List[jid.JID]],
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
290 extra: Optional[dict] = None
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
291 ) -> Tuple[List[Dict[str, Any]], dict]:
3888
aa7197b67c26 component AP gateway: AP <=> XMPP reactions conversions:
Goffi <goffi@goffi.org>
parents: 3869
diff changeset
292 """Retrieve data attached to a pubsub item
aa7197b67c26 component AP gateway: AP <=> XMPP reactions conversions:
Goffi <goffi@goffi.org>
parents: 3869
diff changeset
293
aa7197b67c26 component AP gateway: AP <=> XMPP reactions conversions:
Goffi <goffi@goffi.org>
parents: 3869
diff changeset
294 @param service: pubsub service where the node is
aa7197b67c26 component AP gateway: AP <=> XMPP reactions conversions:
Goffi <goffi@goffi.org>
parents: 3869
diff changeset
295 @param node: pubsub node containing the item
aa7197b67c26 component AP gateway: AP <=> XMPP reactions conversions:
Goffi <goffi@goffi.org>
parents: 3869
diff changeset
296 @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
297 @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
298 entities will be retrieved.
aa7197b67c26 component AP gateway: AP <=> XMPP reactions conversions:
Goffi <goffi@goffi.org>
parents: 3869
diff changeset
299 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
300 @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
301 ``get_items`` call.
3888
aa7197b67c26 component AP gateway: AP <=> XMPP reactions conversions:
Goffi <goffi@goffi.org>
parents: 3869
diff changeset
302 @return: A tuple with:
aa7197b67c26 component AP gateway: AP <=> XMPP reactions conversions:
Goffi <goffi@goffi.org>
parents: 3869
diff changeset
303 - 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
304 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
305 (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
306 - 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
307 """
3864
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
308 if extra is None:
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
309 extra = {}
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3993
diff changeset
310 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
311 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
312 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
313 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
314 )
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3993
diff changeset
315 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
316
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
317 return list_data, metadata
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
318
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
319 def _set(
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
320 self,
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
321 attachments_s: str,
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
322 profile_key: str
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
323 ) -> None:
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3993
diff changeset
324 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
325 attachments = data_format.deserialise(attachments_s) or {}
3956
3cb9ade2ab84 plugin pubsub signing: pubsub items signature implementation:
Goffi <goffi@goffi.org>
parents: 3945
diff changeset
326 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
327
3956
3cb9ade2ab84 plugin pubsub signing: pubsub items signature implementation:
Goffi <goffi@goffi.org>
parents: 3945
diff changeset
328 async def apply_set_handler(
3869
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
329 self,
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
330 client: SatXMPPEntity,
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
331 attachments_data: dict,
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
332 item_elt: Optional[domish.Element],
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
333 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
334 from_jid: Optional[jid.JID] = None,
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
335 ) -> domish.Element:
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
336 """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
337
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
338 @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
339 ``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
340 @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
341 ``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
342 @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
343 will be modified in place
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
344 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
345 @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
346 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
347 @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
348 """
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
349 attachments_data.setdefault("extra", {})
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
350 if item_elt is None:
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
351 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
352 item_elt = pubsub.Item(item_id)
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
353 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
354
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
355 try:
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
356 attachments_elt = next(
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
357 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
358 )
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
359 except StopIteration:
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
360 log.warning(
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
361 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
362 )
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
363 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
364
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
365 if handlers is None:
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
366 handlers = list(self.handlers.keys())
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
367
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
368 for name, namespace in handlers:
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
369 try:
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
370 handler = self.handlers[(name, namespace)]
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
371 except KeyError:
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
372 log.error(
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
373 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
374 "ignoring"
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
375 )
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
376 continue
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
377 try:
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
378 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
379 except StopIteration:
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
380 former_elt = None
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3993
diff changeset
381 new_elt = await as_deferred(
3956
3cb9ade2ab84 plugin pubsub signing: pubsub items signature implementation:
Goffi <goffi@goffi.org>
parents: 3945
diff changeset
382 handler["set"], client, attachments_data, former_elt
3cb9ade2ab84 plugin pubsub signing: pubsub items signature implementation:
Goffi <goffi@goffi.org>
parents: 3945
diff changeset
383 )
3869
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
384 if new_elt != former_elt:
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
385 if former_elt is not None:
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
386 attachments_elt.children.remove(former_elt)
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
387 if new_elt is not None:
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
388 attachments_elt.addChild(new_elt)
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
389 return item_elt
3864
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
390
3956
3cb9ade2ab84 plugin pubsub signing: pubsub items signature implementation:
Goffi <goffi@goffi.org>
parents: 3945
diff changeset
391 async def set_attachements(
3864
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
392 self,
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
393 client: SatXMPPEntity,
3869
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
394 attachments_data: Dict[str, Any]
3864
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
395 ) -> None:
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
396 """Set or update attachments
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
397
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
398 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
399 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
400 "operation" key.
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
401
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
402 "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
403 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
404 but not for "noticed").
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
405
3869
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
406 @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
407 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
408 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
409 ``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
410
3864
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
411 """
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
412 try:
3869
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
413 service = jid.JID(attachments_data["service"])
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
414 node = attachments_data["node"]
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
415 item = attachments_data["id"]
3864
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
416 except (KeyError, RuntimeError):
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
417 raise ValueError(
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
418 'data must have "service", "node" and "id" set'
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
419 )
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3993
diff changeset
420 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
421 try:
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3993
diff changeset
422 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
423 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
424 )
3869
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
425 except exceptions.NotFound:
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
426 item_elt = None
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
427 else:
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
428 if not items:
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
429 item_elt = None
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
430 else:
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
431 item_elt = items[0]
3864
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
432
3956
3cb9ade2ab84 plugin pubsub signing: pubsub items signature implementation:
Goffi <goffi@goffi.org>
parents: 3945
diff changeset
433 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
434 client,
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
435 attachments_data,
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
436 item_elt=item_elt,
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
437 )
c0bcbcf5b4b7 component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents: 3864
diff changeset
438
3864
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
439 try:
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3993
diff changeset
440 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
441 except error.StanzaError as e:
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
442 if e.condition == "item-not-found":
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
443 # 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
444 log.warning(
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
445 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
446 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
447 )
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
448 raise exceptions.NotFound("No attachment node available")
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
449 else:
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
450 raise e
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 async def subscribe(
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
453 self,
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
454 client: SatXMPPEntity,
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
455 service: jid.JID,
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
456 node: str,
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
457 item: str,
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
458 ) -> None:
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
459 """Subscribe to attachment node targeting the item
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
460
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
461 @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
462 @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
463 @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
464 """
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3993
diff changeset
465 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
466 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
467
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
468
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3993
diff changeset
469 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
470 """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
471
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
472 @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
473 @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
474 ``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
475 """
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
476 timestamp_raw = attachment_elt.getAttribute("timestamp")
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
477 if timestamp_raw:
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
478 try:
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
479 timestamp = date_utils.date_parse(timestamp_raw)
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
480 except date_utils.ParserError:
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
481 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
482 else:
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
483 data["timestamp"] = timestamp
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
484
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3993
diff changeset
485 def noticed_get(
3864
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
486 self,
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
487 client: SatXMPPEntity,
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
488 attachments_elt: domish.Element,
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
489 data: Dict[str, Any],
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
490 ) -> None:
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
491 try:
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
492 noticed_elt = next(
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
493 attachments_elt.elements(NS_PUBSUB_ATTACHMENTS, "noticed")
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
494 )
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
495 except StopIteration:
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
496 pass
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
497 else:
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
498 noticed_data = {
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
499 "noticed": True
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
500 }
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3993
diff changeset
501 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
502 data["noticed"] = noticed_data
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
503
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3993
diff changeset
504 def noticed_set(
3864
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
505 self,
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
506 client: SatXMPPEntity,
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
507 data: Dict[str, Any],
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
508 former_elt: Optional[domish.Element]
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
509 ) -> Optional[domish.Element]:
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
510 """add or remove a <noticed> attachment
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
511
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
512 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
513 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
514 """
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
515 noticed = data["extra"].get("noticed")
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
516 if noticed is None:
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
517 return former_elt
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
518 elif noticed:
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
519 return domish.Element(
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
520 (NS_PUBSUB_ATTACHMENTS, "noticed"),
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
521 attribs = {
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
522 "timestamp": xmpp_date()
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
523 }
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
524 )
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
525 else:
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
526 return None
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
527
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3993
diff changeset
528 def reactions_get(
3864
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
529 self,
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
530 client: SatXMPPEntity,
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
531 attachments_elt: domish.Element,
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
532 data: Dict[str, Any],
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
533 ) -> None:
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
534 try:
3888
aa7197b67c26 component AP gateway: AP <=> XMPP reactions conversions:
Goffi <goffi@goffi.org>
parents: 3869
diff changeset
535 reactions_elt = next(
aa7197b67c26 component AP gateway: AP <=> XMPP reactions conversions:
Goffi <goffi@goffi.org>
parents: 3869
diff changeset
536 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
537 )
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
538 except StopIteration:
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
539 pass
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
540 else:
3888
aa7197b67c26 component AP gateway: AP <=> XMPP reactions conversions:
Goffi <goffi@goffi.org>
parents: 3869
diff changeset
541 reactions_data = {"reactions": []}
aa7197b67c26 component AP gateway: AP <=> XMPP reactions conversions:
Goffi <goffi@goffi.org>
parents: 3869
diff changeset
542 reactions = reactions_data["reactions"]
aa7197b67c26 component AP gateway: AP <=> XMPP reactions conversions:
Goffi <goffi@goffi.org>
parents: 3869
diff changeset
543 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
544 reactions.append(str(reaction_elt))
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3993
diff changeset
545 self.set_timestamp(reactions_elt, reactions_data)
3888
aa7197b67c26 component AP gateway: AP <=> XMPP reactions conversions:
Goffi <goffi@goffi.org>
parents: 3869
diff changeset
546 data["reactions"] = reactions_data
3864
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
547
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3993
diff changeset
548 def reactions_set(
3864
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
549 self,
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
550 client: SatXMPPEntity,
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
551 data: Dict[str, Any],
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
552 former_elt: Optional[domish.Element]
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
553 ) -> Optional[domish.Element]:
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
554 """update the <reaction> attachment"""
3888
aa7197b67c26 component AP gateway: AP <=> XMPP reactions conversions:
Goffi <goffi@goffi.org>
parents: 3869
diff changeset
555 reactions_data = data["extra"].get("reactions")
aa7197b67c26 component AP gateway: AP <=> XMPP reactions conversions:
Goffi <goffi@goffi.org>
parents: 3869
diff changeset
556 if reactions_data is None:
3864
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
557 return former_elt
3888
aa7197b67c26 component AP gateway: AP <=> XMPP reactions conversions:
Goffi <goffi@goffi.org>
parents: 3869
diff changeset
558 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
559 if operation_type == "update":
3888
aa7197b67c26 component AP gateway: AP <=> XMPP reactions conversions:
Goffi <goffi@goffi.org>
parents: 3869
diff changeset
560 former_reactions = {
aa7197b67c26 component AP gateway: AP <=> XMPP reactions conversions:
Goffi <goffi@goffi.org>
parents: 3869
diff changeset
561 str(r) for r in former_elt.elements(NS_PUBSUB_ATTACHMENTS, "reaction")
aa7197b67c26 component AP gateway: AP <=> XMPP reactions conversions:
Goffi <goffi@goffi.org>
parents: 3869
diff changeset
562 } if former_elt is not None else set()
aa7197b67c26 component AP gateway: AP <=> XMPP reactions conversions:
Goffi <goffi@goffi.org>
parents: 3869
diff changeset
563 added_reactions = set(reactions_data.get("add") or [])
aa7197b67c26 component AP gateway: AP <=> XMPP reactions conversions:
Goffi <goffi@goffi.org>
parents: 3869
diff changeset
564 removed_reactions = set(reactions_data.get("remove") or [])
aa7197b67c26 component AP gateway: AP <=> XMPP reactions conversions:
Goffi <goffi@goffi.org>
parents: 3869
diff changeset
565 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
566 elif operation_type == "replace":
3888
aa7197b67c26 component AP gateway: AP <=> XMPP reactions conversions:
Goffi <goffi@goffi.org>
parents: 3869
diff changeset
567 reactions = reactions_data.get("reactions") or []
3864
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
568 else:
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
569 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
570 if reactions:
3888
aa7197b67c26 component AP gateway: AP <=> XMPP reactions conversions:
Goffi <goffi@goffi.org>
parents: 3869
diff changeset
571 reactions_elt = domish.Element(
aa7197b67c26 component AP gateway: AP <=> XMPP reactions conversions:
Goffi <goffi@goffi.org>
parents: 3869
diff changeset
572 (NS_PUBSUB_ATTACHMENTS, "reactions"),
3864
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
573 attribs = {
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
574 "timestamp": xmpp_date()
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
575 }
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
576 )
3888
aa7197b67c26 component AP gateway: AP <=> XMPP reactions conversions:
Goffi <goffi@goffi.org>
parents: 3869
diff changeset
577 for reactions_data in reactions:
aa7197b67c26 component AP gateway: AP <=> XMPP reactions conversions:
Goffi <goffi@goffi.org>
parents: 3869
diff changeset
578 reactions_elt.addElement("reaction", content=reactions_data)
aa7197b67c26 component AP gateway: AP <=> XMPP reactions conversions:
Goffi <goffi@goffi.org>
parents: 3869
diff changeset
579 return reactions_elt
3864
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
580 else:
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
581 return None
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
582
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
583
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
584 @implementer(iwokkel.IDisco)
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
585 class PubsubAttachments_Handler(xmlstream.XMPPHandler):
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
586
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
587 def getDiscoInfo(self, requestor, service, nodeIdentifier=""):
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
588 return [disco.DiscoFeature(NS_PUBSUB_ATTACHMENTS)]
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
589
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
590 def getDiscoItems(self, requestor, service, nodeIdentifier=""):
ac255a0fbd4c plugin pubsub attachments: partial implementation of pubsub-attachments protoXEP:
Goffi <goffi@goffi.org>
parents:
diff changeset
591 return []