annotate libervia/backend/plugins/plugin_xep_0499.py @ 4401:ae26233b655f default tip

doc (components): Add message cleaning section to email gateway doc: fix 464
author Goffi <goffi@goffi.org>
date Thu, 11 Sep 2025 21:17:51 +0200
parents dcda916f16f6
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
4392
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
1 #!/usr/bin/env python3
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
2
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
3 # Libervia plugin for Pubsub Extended Discovery.
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
4 # Copyright (C) 2009-2025 Jérôme Poisson (goffi@goffi.org)
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
5
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
6 # This program is free software: you can redistribute it and/or modify
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
7 # it under the terms of the GNU Affero General Public License as published by
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
8 # the Free Software Foundation, either version 3 of the License, or
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
9 # (at your option) any later version.
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
10
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
11 # This program is distributed in the hope that it will be useful,
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
14 # GNU Affero General Public License for more details.
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
15
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
16 # You should have received a copy of the GNU Affero General Public License
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
18
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
19 from collections.abc import Callable, Coroutine, Iterable
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
20 from typing import (
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
21 TYPE_CHECKING,
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
22 ClassVar,
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
23 Final,
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
24 Literal,
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
25 NamedTuple,
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
26 Self,
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
27 cast,
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
28 )
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
29
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
30 from pydantic import BaseModel, Field, RootModel, model_validator
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
31 from sqlalchemy.exc import SQLAlchemyError
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
32 from twisted.internet import defer
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
33 from twisted.words.protocols.jabber.jid import JID
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
34 from twisted.words.protocols.jabber import xmlstream
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
35 from twisted.words.protocols.jabber.xmlstream import XMPPHandler
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
36 from twisted.words.xish import domish
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
37 from wokkel import disco, data_form, pubsub
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
38 from zope.interface import implementer
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
39
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
40 from libervia.backend.core import exceptions
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
41 from libervia.backend.core.constants import Const as C
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
42 from libervia.backend.core.core_types import SatXMPPComponent, SatXMPPEntity
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
43 from libervia.backend.core.i18n import _
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
44 from libervia.backend.core.log import getLogger
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
45 from libervia.backend.memory.sqla_mapping import PubsubItem, PubsubNode
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
46 from libervia.backend.models.types import JIDType
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
47 from libervia.backend.plugins.plugin_xep_0060 import NodeMetadata
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
48 from libervia.backend.tools import utils
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
49
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
50 if TYPE_CHECKING:
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
51 from libervia.backend.core.main import LiberviaBackend
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
52
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
53 log = getLogger(__name__)
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
54
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
55
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
56 PLUGIN_INFO = {
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
57 C.PI_NAME: "Pubsub Extended Discovery",
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
58 C.PI_IMPORT_NAME: "XEP-0499",
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
59 C.PI_TYPE: "XEP",
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
60 C.PI_MODES: C.PLUG_MODE_BOTH,
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
61 C.PI_PROTOCOLS: ["XEP-0499"],
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
62 C.PI_DEPENDENCIES: [
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
63 "XEP-0060",
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
64 ],
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
65 C.PI_RECOMMENDATIONS: [],
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
66 C.PI_MAIN: "XEP_0499",
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
67 C.PI_HANDLER: "yes",
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
68 C.PI_DESCRIPTION: _(
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
69 """Extended discovery for Pubsub nodes with linked nodes and descendants."""
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
70 ),
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
71 }
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
72
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
73 NS_PUBSUB_EXT_DISCO: Final = "urn:xmpp:pubsub-ext-disco:0"
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
74 NS_PUBSUB_RELATIONSHIPS: Final = "urn:xmpp:pubsub-relationships:0"
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
75 PARENT_VAR = f"{{{NS_PUBSUB_RELATIONSHIPS}}}parent"
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
76 LINK_VAR = f"{{{NS_PUBSUB_RELATIONSHIPS}}}link"
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
77
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
78
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
79 class ExtDiscoMetadata(NodeMetadata):
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
80
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
81 parent: str | None = None
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
82 link: str | None = None
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
83
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
84 @classmethod
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
85 def from_data_form(cls, form: data_form.Form) -> Self:
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
86 """Create a ExtDiscoMetadata instance from a data form.
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
87
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
88 @param form: Data form containing node metadata.
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
89 @return: Filled instance of this class.
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
90 @raise TypeError: Type of the form do not correspond to what is expected according
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
91 to specifications.
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
92 """
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
93 metadata = super().from_data_form(form)
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
94 metadata.parent = form.get(PARENT_VAR)
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
95 metadata.link = form.get(LINK_VAR)
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
96 return metadata
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
97
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
98 def to_data_form(self) -> data_form.Form:
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
99 """Convert this instance to a data form.
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
100
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
101 @return: Data form representation of this instance.
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
102 """
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
103 form = super().to_data_form()
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
104 if self.parent is not None:
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
105 form.addField(data_form.Field(var=PARENT_VAR, value=self.parent))
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
106 if self.link is not None:
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
107 form.addField(data_form.Field(var=LINK_VAR, value=self.link))
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
108 return form
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
109
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
110
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
111 class ExtDiscoOptions(BaseModel):
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
112 """Pydantic model for the pubsub extended discovery form fields."""
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
113
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
114 type: list[Literal["items", "nodes"]] = Field(
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
115 default_factory=lambda: ["items", "nodes"]
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
116 )
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
117 linked_nodes: bool = False
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
118 full_metadata: bool = False
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
119 depth: int = 0
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
120
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
121 _fields_defs: ClassVar[dict] = {
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
122 "type": {"type": "list-multi"},
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
123 "linked_nodes": {"type": "boolean"},
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
124 "full_metadata": {"type": "boolean"},
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
125 "depth": {"type": "text-single"},
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
126 }
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
127
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
128 @classmethod
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
129 def from_data_form(cls, form: data_form.Form) -> Self:
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
130 """Create a PubsubExtDiscoForm instance from a data form.
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
131
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
132 @param form: Extended Discovery Data Form.
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
133 @return: Filled instance of this class.
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
134 @raise TypeError: Type of the form do not correspond to what is expected according
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
135 to specifications.
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
136 """
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
137 fields = {}
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
138 form.typeCheck(cls._fields_defs)
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
139 for field in form.fields.values():
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
140 if field.var == "type":
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
141 fields["type"] = field.values
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
142 elif field.var == "linked_nodes":
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
143 fields["linked_nodes"] = field.value
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
144 elif field.var == "full_metadata":
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
145 fields["full_metadata"] = field.value
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
146 elif field.var == "depth":
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
147 try:
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
148 fields["depth"] = int(field.value)
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
149 except (ValueError, TypeError):
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
150 log.warning(f"Invalid depth found: {field.value!r}.")
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
151 fields["depth"] = 0
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
152
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
153 return cls(**fields)
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
154
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
155 def to_data_form(self) -> data_form.Form:
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
156 """Convert this instance to a data form.
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
157
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
158 @return: Data form representation of this instance.
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
159 """
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
160 form = data_form.Form(formType="submit", formNamespace=NS_PUBSUB_EXT_DISCO)
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
161
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
162 form.makeFields(
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
163 {
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
164 "type": self.type,
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
165 "linked_nodes": self.linked_nodes,
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
166 "full_metadata": self.full_metadata,
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
167 "depth": str(self.depth),
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
168 },
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
169 fieldDefs=self._fields_defs,
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
170 )
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
171
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
172 return form
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
173
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
174 def to_element(self) -> domish.Element:
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
175 """Generate the <x> element corresponding to this form."""
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
176 return self.to_data_form().toElement()
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
177
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
178
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
179 class DiscoPubsubItem(BaseModel):
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
180 type: Literal["item"] = "item"
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
181 parent_node: str
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
182 jid: JIDType
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
183 name: str
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
184
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
185 @classmethod
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
186 def from_sqlalchemy(cls, item: PubsubItem, node_name: str, service: JID) -> Self:
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
187 """Create a DiscoPubsubItem instance from a PubsubItem SQLAlchemy model.
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
188
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
189 @param item: The SQLAlchemy PubsubItem instance.
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
190 @param node_name: The name of the parent node.
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
191 @param service: The JID of the service where the item is.
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
192 @return: A new instance of this class.
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
193 """
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
194 return cls(parent_node=node_name, jid=service, name=item.name)
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
195
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
196 def to_element(self) -> domish.Element:
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
197 """Generate the element corresponding to this instance."""
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
198 item_elt = domish.Element((disco.NS_DISCO_ITEMS, "item"))
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
199 item_elt["jid"] = self.jid.full()
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
200 item_elt["node"] = self.parent_node
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
201 item_elt["name"] = self.name
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
202 return item_elt
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
203
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
204
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
205 class DiscoPubsubNode(BaseModel):
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
206 type: Literal["node"] = "node"
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
207 jid: JIDType
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
208 name: str
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
209 items: list[DiscoPubsubItem] | None = None
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
210 linking_nodes: list[Self] | None = None
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
211 children: list[Self] | None = None
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
212 metadata: ExtDiscoMetadata = Field(default_factory=ExtDiscoMetadata)
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
213
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
214 @model_validator(mode="after")
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
215 def set_link_and_parent_metadata(self) -> Self:
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
216 """We ensure that `metadata.link` and `metadata.parent` are set correctly."""
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
217 if self.linking_nodes is not None:
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
218 for linking_node in self.linking_nodes:
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
219 linking_node.metadata.link = self.name
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
220
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
221 if self.children is not None:
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
222 for child in self.children:
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
223 child.metadata.parent = self.name
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
224
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
225 return self
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
226
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
227 def add_child(self, child_node: Self) -> None:
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
228 """Add a child and set its ``parent`` metadata to current node."""
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
229 if self.children is None:
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
230 self.children = []
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
231 self.children.append(child_node)
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
232 child_node.metadata.parent = self.name
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
233
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
234 def link_to(self, linked_node: Self) -> None:
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
235 """Link this node to another one, and set the ``link`` metadata."""
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
236 if linked_node.linking_nodes is None:
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
237 linked_node.linking_nodes = []
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
238 linked_node.linking_nodes.append(self)
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
239 self.metadata.link = linked_node.name
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
240
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
241 @classmethod
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
242 def from_sqlalchemy(cls, node: PubsubNode, service: JID) -> Self:
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
243 """Create a DiscoPubsubNode instance from a PubsubNode SQLAlchemy model.
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
244
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
245 @param node: The SQLAlchemy PubsubNode instance.
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
246 @param service: The JID of the service where the node is.
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
247 @return: A new instance of this class.
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
248 """
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
249 # Create base node instance
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
250 disco_node = cls(
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
251 jid=service,
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
252 name=node.name,
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
253 metadata=ExtDiscoMetadata(
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
254 type=node.type_ if node.type_ else None,
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
255 access_model=node.access_model.value if node.access_model else None,
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
256 publish_model=node.publish_model.value if node.publish_model else None,
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
257 ),
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
258 )
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
259
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
260 # Set parent and link metadata based on relationships
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
261 if node.parent_node_id:
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
262 disco_node.metadata.parent = node.parent_node.name
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
263
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
264 if node.linked_node_id:
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
265 disco_node.metadata.link = node.linked_node.name
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
266
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
267 if node.extra is not None:
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
268 for field in ("title", "description"):
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
269 try:
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
270 setattr(disco_node.metadata, field, node.extra[field])
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
271 except KeyError:
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
272 pass
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
273
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
274 return disco_node
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
275
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
276 @classmethod
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
277 def from_sqlalchemy_full_hierarchy(cls, node: PubsubNode, service: JID) -> Self:
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
278 """Create a DiscoPubsubNode instance with children and items populated.
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
279
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
280 The whole nodes and items hierarchy will be recursively created.
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
281
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
282 @param node: The SQLAlchemy PubsubNode instance.
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
283 @param service: The JID of the service where the node is.
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
284 @return: A new instance of this class with children and items.
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
285 """
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
286 disco_node = cls.from_sqlalchemy(node, service)
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
287
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
288 try:
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
289 items = node.items
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
290 except SQLAlchemyError as e:
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
291 log.debug(f"Can't load items: {e}")
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
292 else:
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
293 disco_node.items = [
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
294 DiscoPubsubItem.from_sqlalchemy(item, node.name, service)
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
295 for item in items
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
296 ]
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
297
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
298 try:
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
299 child_nodes = node.child_nodes
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
300 except Exception as e:
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
301 log.debug(f"Can't load child nodes: {e}")
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
302 else:
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
303 try:
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
304 disco_node.children = [
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
305 cls.from_sqlalchemy_full_hierarchy(child, service)
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
306 for child in child_nodes
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
307 ]
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
308 except SQLAlchemyError as e:
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
309 log.debug(f"Can't handle children, ignoring them: {e}.")
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
310
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
311 try:
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
312 linking_nodes = node.linking_nodes
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
313 except Exception as e:
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
314 log.debug(f"Can't load linking nodes: {e}")
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
315 else:
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
316 disco_node.linking_nodes = [
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
317 cls.from_sqlalchemy_full_hierarchy(linking_node, service)
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
318 for linking_node in linking_nodes
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
319 ]
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
320
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
321 return disco_node
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
322
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
323 def to_element(self) -> domish.Element:
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
324 item_elt = domish.Element((disco.NS_DISCO_ITEMS, "item"))
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
325 item_elt["jid"] = self.jid.full()
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
326 item_elt["node"] = self.name
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
327 item_elt.addChild(self.metadata.to_element())
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
328 return item_elt
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
329
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
330 def to_elements(
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
331 self,
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
332 ) -> list[domish.Element]:
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
333 """Return this elements and all its descendants and linking nodes.
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
334
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
335 @param parent_node: Parent of this node, None if it's a root node.
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
336 @param linked_node: Node this node is linking to, if any.
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
337 @return: This node, its descendants and linking nodes.
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
338 """
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
339 elements = [self.to_element()]
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
340 if self.linking_nodes is not None:
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
341 for linking_node in self.linking_nodes:
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
342 elements.append(linking_node.to_element())
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
343 if self.children is not None:
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
344 for child in self.children:
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
345 elements.extend(child.to_elements())
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
346 return elements
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
347
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
348
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
349 class ExtDiscoResult(RootModel):
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
350 root: list[DiscoPubsubNode | DiscoPubsubItem]
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
351
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
352 def __iter__(self) -> Iterable[DiscoPubsubNode | DiscoPubsubItem]: # type: ignore
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
353 return iter(self.root)
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
354
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
355 def __getitem__(self, item) -> str:
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
356 return self.root[item]
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
357
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
358 def __len__(self) -> int:
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
359 return len(self.root)
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
360
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
361 def append(self, item: DiscoPubsubNode | DiscoPubsubItem) -> None:
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
362 self.root.append(item)
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
363
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
364 def sort(self, key=None, reverse=False) -> None:
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
365 self.root.sort(key=key, reverse=reverse) # type: ignore
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
366
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
367 @classmethod
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
368 def from_element(cls, query_elt: domish.Element) -> Self:
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
369 """Build nodes hierarchy from unordered list of disco <items>
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
370
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
371 @param query_elt: Parent <query> element from the disco result, disco <item>
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
372 elements will be retrieved in its children.
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
373 @return: An instance of this class, with nodes hierarchy reconstructed.
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
374 """
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
375 # First pass: Create all nodes and items
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
376 nodes: dict[str, DiscoPubsubNode] = {}
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
377 items: list[DiscoPubsubItem] = []
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
378
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
379 # Process all disco items
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
380 for item_elt in query_elt.elements(disco.NS_DISCO_ITEMS, "item"):
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
381 try:
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
382 item_jid = JID(item_elt["jid"])
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
383 node_name = item_elt["node"]
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
384 except KeyError:
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
385 log.warning(f"Invalid extended disco item, ignoring: {item_elt.toXml()}")
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
386 continue
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
387
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
388 # Check if this item has metadata (indicating it's a node)
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
389 metadata_form = data_form.findForm(item_elt, pubsub.NS_PUBSUB_META_DATA)
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
390
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
391 if metadata_form is not None:
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
392 # This is a PubsubNode
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
393 metadata = ExtDiscoMetadata.from_data_form(metadata_form)
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
394 node = DiscoPubsubNode(jid=item_jid, name=node_name, metadata=metadata)
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
395 nodes[node_name] = node
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
396 else:
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
397 # This is a PubsubItem
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
398 try:
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
399 name = item_elt["name"]
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
400 except KeyError:
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
401 log.warning(
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
402 "Invalid disco item, pubsub items must have a name: "
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
403 f"{item_elt.toXml()}"
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
404 )
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
405 continue
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
406 item = DiscoPubsubItem(parent_node=node_name, jid=item_jid, name=name)
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
407 items.append(item)
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
408
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
409 # Second pass: Build hierarchy using parent/link relationships
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
410 root_nodes: list[DiscoPubsubNode] = []
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
411
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
412 for node in nodes.values():
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
413 parent_name = node.metadata.parent
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
414 link_name = node.metadata.link
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
415
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
416 if parent_name is not None:
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
417 # This node has a parent
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
418 parent_node = nodes.get(parent_name)
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
419 if parent_node is not None:
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
420 parent_node.add_child(node)
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
421 else:
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
422 # Parent not found, treat as root
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
423 log.warning(f"Parent node found for {node}.")
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
424 root_nodes.append(node)
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
425 elif link_name is not None:
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
426 # This is a linking node
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
427 linked_node = nodes.get(link_name)
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
428 if linked_node is not None:
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
429 linked_node.link_to(node)
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
430 else:
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
431 # Linked node not found, treat as root
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
432 log.warning(f"Linked node found for {node}.")
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
433 root_nodes.append(node)
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
434 else:
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
435 # This is a root node (no parent, no link)
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
436 root_nodes.append(node)
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
437
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
438 # Third pass: Assign items to their corresponding nodes
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
439 root_items: list[DiscoPubsubItem] = []
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
440
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
441 for item in items:
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
442 target_node = nodes.get(item.parent_node)
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
443 if target_node is not None:
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
444 if target_node.items is None:
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
445 target_node.items = []
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
446 target_node.items.append(item)
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
447 else:
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
448 # Item's parent node not found, treat as root item
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
449 root_items.append(item)
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
450
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
451 # Combine root nodes and root items
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
452 result_items: list[DiscoPubsubNode | DiscoPubsubItem] = []
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
453 result_items.extend(root_nodes)
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
454 result_items.extend(root_items)
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
455
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
456 return cls(root=result_items)
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
457
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
458 def to_elements(self) -> list[domish.Element]:
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
459 elements = []
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
460 for item in self.root:
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
461 if isinstance(item, DiscoPubsubNode):
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
462 elements.extend(item.to_elements())
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
463 else:
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
464 elements.append(item.to_element())
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
465 return elements
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
466
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
467
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
468 class RequestHandler(NamedTuple):
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
469 callback: Callable[
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
470 [SatXMPPComponent, domish.Element, ExtDiscoOptions],
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
471 ExtDiscoResult
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
472 | Coroutine[None, None, ExtDiscoResult]
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
473 | defer.Deferred[ExtDiscoResult],
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
474 ]
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
475 priority: int
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
476
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
477
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
478 class XEP_0499:
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
479 namespace = NS_PUBSUB_EXT_DISCO
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
480
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
481 def __init__(self, host: "LiberviaBackend") -> None:
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
482 log.info(f"plugin {PLUGIN_INFO[C.PI_NAME]!r} initialization")
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
483 self.host = host
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
484 self.handlers: list[RequestHandler] = []
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
485
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
486 host.register_namespace("pubsub-ext-disco", NS_PUBSUB_EXT_DISCO)
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
487 host.register_namespace("pubsub-relationships", NS_PUBSUB_RELATIONSHIPS)
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
488 host.bridge.add_method(
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
489 "ps_disco_get",
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
490 ".plugin",
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
491 in_sign="ssss",
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
492 out_sign="s",
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
493 method=self._disco_get,
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
494 async_=True,
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
495 )
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
496
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
497 def get_handler(self, client: SatXMPPEntity) -> "PubsubExtDiscoHandler":
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
498 return PubsubExtDiscoHandler(self)
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
499
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
500 def register_handler(
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
501 self,
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
502 callback: Callable[
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
503 [SatXMPPComponent, domish.Element, ExtDiscoOptions],
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
504 ExtDiscoResult
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
505 | Coroutine[None, None, ExtDiscoResult]
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
506 | defer.Deferred[ExtDiscoResult],
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
507 ],
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
508 priority: int = 0,
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
509 ) -> None:
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
510 """Register an extended discovery request handler.
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
511
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
512 @param callack: method to call when a request is done
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
513 the callback must return an DiscoveryData.
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
514 If the callback raises a StanzaError, its condition will be used if no other
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
515 callback can handle the request.
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
516 @param priority: Handlers with higher priorities will be called first.
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
517 """
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
518 assert callback not in self.handlers
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
519 req_handler = RequestHandler(callback, priority)
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
520 self.handlers.append(req_handler)
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
521 self.handlers.sort(key=lambda handler: handler.priority, reverse=True)
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
522
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
523 def _handle_disco_items_request(
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
524 self, iq_elt: domish.Element, client: SatXMPPEntity
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
525 ) -> None:
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
526 query_elt = iq_elt.query
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
527 assert query_elt is not None
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
528 ext_disco_form = data_form.findForm(query_elt, NS_PUBSUB_EXT_DISCO)
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
529 if ext_disco_form is None:
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
530 # This is a normal disco request, we transmit to Wokkel's disco handler.
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
531 client.discoHandler.handleRequest(iq_elt)
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
532 return
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
533 # We have an extended pubsub discovery request (the form is present), we continue.
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
534 iq_elt.handled = True
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
535 ext_disco_options = ExtDiscoOptions.from_data_form(ext_disco_form)
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
536 defer.ensureDeferred(
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
537 self.handle_disco_items_request(client, iq_elt, ext_disco_options)
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
538 )
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
539
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
540 async def handle_disco_items_request(
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
541 self,
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
542 client: SatXMPPEntity,
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
543 iq_elt: domish.Element,
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
544 ext_disco_options: ExtDiscoOptions,
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
545 ) -> None:
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
546 query_elt = iq_elt.query
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
547 assert query_elt is not None
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
548 for handler in self.handlers:
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
549 try:
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
550 disco_result = await utils.as_deferred(
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
551 handler.callback, client, iq_elt, ext_disco_options
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
552 )
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
553 except Exception as e:
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
554 log.exception("Can't retrieve disco data.")
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
555 client.sendError(iq_elt, "internal-server-error", text=str(e))
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
556 raise e
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
557 else:
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
558 if disco_result is not None:
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
559 break
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
560 else:
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
561 # No handler did return a result.
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
562 disco_result = ExtDiscoResult([])
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
563 iq_result_elt = xmlstream.toResponse(iq_elt, "result")
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
564 result_query_elt = iq_result_elt.addElement((disco.NS_DISCO_ITEMS, "query"))
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
565 if query_elt.hasAttribute("node"):
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
566 result_query_elt["node"] = query_elt["node"]
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
567 for elt in disco_result.to_elements():
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
568 result_query_elt.addChild(elt)
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
569 client.send(iq_result_elt)
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
570
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
571 def _disco_get(
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
572 self, service: str, node: str, options_s: str, profile: str
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
573 ) -> defer.Deferred[str]:
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
574 client = self.host.get_client(profile)
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
575 service_jid = JID(service)
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
576 options = ExtDiscoOptions.model_validate_json(options_s)
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
577 d = defer.ensureDeferred(
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
578 self.disco_get(client, service_jid, node or None, options)
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
579 )
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
580 d.addCallback(
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
581 lambda ext_disco_result: ext_disco_result.model_dump_json(exclude_none=True)
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
582 )
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
583 d = cast(defer.Deferred[str], d)
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
584 return d
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
585
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
586 async def disco_get(
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
587 self,
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
588 client: SatXMPPEntity,
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
589 service: JID,
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
590 node: str | None,
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
591 options: ExtDiscoOptions,
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
592 ) -> ExtDiscoResult:
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
593
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
594 query_elt = domish.Element((disco.NS_DISCO_ITEMS, "query"))
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
595 if node is not None:
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
596 query_elt["node"] = node
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
597
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
598 query_elt.addChild(options.to_element())
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
599 iq_elt = client.IQ("get")
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
600 iq_elt["to"] = service.full()
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
601 iq_elt.addChild(query_elt)
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
602 iq_result_elt = await iq_elt.send()
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
603 try:
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
604 query_elt = next(iq_result_elt.elements(disco.NS_DISCO_ITEMS, "query"))
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
605 except StopIteration:
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
606 raise exceptions.DataError(
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
607 "<query> missing in disco result, this is invalid."
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
608 )
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
609 disco_result = ExtDiscoResult.from_element(query_elt)
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
610
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
611 return disco_result
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
612
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
613
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
614 @implementer(disco.IDisco)
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
615 class PubsubExtDiscoHandler(XMPPHandler):
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
616 """Handler for pubsub extended discovery requests."""
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
617
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
618 def __init__(self, plugin_parent: XEP_0499) -> None:
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
619 self.plugin_parent = plugin_parent
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
620
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
621 @property
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
622 def client(self) -> SatXMPPEntity:
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
623 return cast(SatXMPPEntity, self.parent)
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
624
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
625 def connectionInitialized(self):
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
626 assert self.xmlstream is not None
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
627 if self.client.is_component:
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
628 # We have to remove Wokkel's disco handler to avoid stanza to be replied
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
629 # twice.
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
630 self.xmlstream.removeObserver(
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
631 disco.DISCO_ITEMS, self.client.discoHandler.handleRequest
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
632 )
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
633 self.xmlstream.addObserver(
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
634 disco.DISCO_ITEMS,
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
635 self.plugin_parent._handle_disco_items_request,
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
636 client=self.client,
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
637 )
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
638
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
639 def getDiscoInfo(
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
640 self, requestor: JID, target: JID, nodeIdentifier: str = ""
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
641 ) -> list[disco.DiscoFeature]:
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
642 """Get disco info for pubsub extended discovery
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
643
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
644 @param requestor: JID of the requesting entity
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
645 @param target: JID of the target entity
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
646 @param nodeIdentifier: optional node identifier
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
647 @return: list of disco features
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
648 """
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
649 return [
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
650 disco.DiscoFeature(NS_PUBSUB_EXT_DISCO),
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
651 ]
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
652
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
653 def getDiscoItems(
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
654 self, requestor: JID, target: JID, nodeIdentifier: str = ""
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
655 ) -> list[disco.DiscoItem]:
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
656 """Get disco items with extended discovery support
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
657
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
658 @param requestor: JID of the requesting entity
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
659 @param target: JID of the target entity
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
660 @param nodeIdentifier: optional node identifier
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
661 @return: list of disco items
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
662 """
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
663 # We return empty list for Wokkel disco handling, the extended discovery is done
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
664 # with the ``handle_disco_items_request`` method above.
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
665
dcda916f16f6 plugin XEP-0499 Pubsub Extended Discovery implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
666 return []