annotate libervia/backend/plugins/plugin_xep_0059.py @ 4370:0eaa50f21efb

plugin XEP-0461: Message Replies implementation: Implement message replies. Thread ID are always added when a reply is initiated from Libervia, so a thread can continue the reply. rel 457
author Goffi <goffi@goffi.org>
date Tue, 06 May 2025 00:34:01 +0200
parents c9626f46b63e
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
1 #!/usr/bin/env python3
3137
559a625a236b fixed shebangs
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
2
3592
ef144aaea2bb plugin XEP-0059: new `getNextRequest` method to generate request to retrieve next page
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
3 # Result Set Management (XEP-0059)
3479
be6d91572633 date update
Goffi <goffi@goffi.org>
parents: 3308
diff changeset
4 # Copyright (C) 2009-2021 Jérôme Poisson (goffi@goffi.org)
1766
d17772b0fe22 copyright update
Goffi <goffi@goffi.org>
parents: 1465
diff changeset
5 # Copyright (C) 2013-2016 Adrien Cossa (souliane@mailoo.org)
1219
16484ebb695b plugin XEP-0059: first draft, pubsub and jabber search do not exploit it yet
souliane <souliane@mailoo.org>
parents:
diff changeset
6
16484ebb695b plugin XEP-0059: first draft, pubsub and jabber search do not exploit it yet
souliane <souliane@mailoo.org>
parents:
diff changeset
7 # This program is free software: you can redistribute it and/or modify
16484ebb695b plugin XEP-0059: first draft, pubsub and jabber search do not exploit it yet
souliane <souliane@mailoo.org>
parents:
diff changeset
8 # it under the terms of the GNU Affero General Public License as published by
16484ebb695b plugin XEP-0059: first draft, pubsub and jabber search do not exploit it yet
souliane <souliane@mailoo.org>
parents:
diff changeset
9 # the Free Software Foundation, either version 3 of the License, or
16484ebb695b plugin XEP-0059: first draft, pubsub and jabber search do not exploit it yet
souliane <souliane@mailoo.org>
parents:
diff changeset
10 # (at your option) any later version.
16484ebb695b plugin XEP-0059: first draft, pubsub and jabber search do not exploit it yet
souliane <souliane@mailoo.org>
parents:
diff changeset
11
16484ebb695b plugin XEP-0059: first draft, pubsub and jabber search do not exploit it yet
souliane <souliane@mailoo.org>
parents:
diff changeset
12 # This program is distributed in the hope that it will be useful,
16484ebb695b plugin XEP-0059: first draft, pubsub and jabber search do not exploit it yet
souliane <souliane@mailoo.org>
parents:
diff changeset
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16484ebb695b plugin XEP-0059: first draft, pubsub and jabber search do not exploit it yet
souliane <souliane@mailoo.org>
parents:
diff changeset
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16484ebb695b plugin XEP-0059: first draft, pubsub and jabber search do not exploit it yet
souliane <souliane@mailoo.org>
parents:
diff changeset
15 # GNU Affero General Public License for more details.
16484ebb695b plugin XEP-0059: first draft, pubsub and jabber search do not exploit it yet
souliane <souliane@mailoo.org>
parents:
diff changeset
16
16484ebb695b plugin XEP-0059: first draft, pubsub and jabber search do not exploit it yet
souliane <souliane@mailoo.org>
parents:
diff changeset
17 # You should have received a copy of the GNU Affero General Public License
16484ebb695b plugin XEP-0059: first draft, pubsub and jabber search do not exploit it yet
souliane <souliane@mailoo.org>
parents:
diff changeset
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
16484ebb695b plugin XEP-0059: first draft, pubsub and jabber search do not exploit it yet
souliane <souliane@mailoo.org>
parents:
diff changeset
19
4356
c9626f46b63e plugin XEP-0059: Use Pydantic models for RSM.
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
20 from typing import Self
c9626f46b63e plugin XEP-0059: Use Pydantic models for RSM.
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
21 from pydantic import BaseModel, Field, field_validator
3592
ef144aaea2bb plugin XEP-0059: new `getNextRequest` method to generate request to retrieve next page
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
22 from zope.interface import implementer
ef144aaea2bb plugin XEP-0059: new `getNextRequest` method to generate request to retrieve next page
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
23 from twisted.words.protocols.jabber import xmlstream
4356
c9626f46b63e plugin XEP-0059: Use Pydantic models for RSM.
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
24 from twisted.words.xish import domish
c9626f46b63e plugin XEP-0059: Use Pydantic models for RSM.
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
25 from wokkel import disco, iwokkel, rsm
4071
4b842c1fb686 refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents: 4037
diff changeset
26 from libervia.backend.core.i18n import _
4b842c1fb686 refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents: 4037
diff changeset
27 from libervia.backend.core.constants import Const as C
4b842c1fb686 refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents: 4037
diff changeset
28 from libervia.backend.core.log import getLogger
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
29
3592
ef144aaea2bb plugin XEP-0059: new `getNextRequest` method to generate request to retrieve next page
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
30
1219
16484ebb695b plugin XEP-0059: first draft, pubsub and jabber search do not exploit it yet
souliane <souliane@mailoo.org>
parents:
diff changeset
31 log = getLogger(__name__)
16484ebb695b plugin XEP-0059: first draft, pubsub and jabber search do not exploit it yet
souliane <souliane@mailoo.org>
parents:
diff changeset
32
16484ebb695b plugin XEP-0059: first draft, pubsub and jabber search do not exploit it yet
souliane <souliane@mailoo.org>
parents:
diff changeset
33
16484ebb695b plugin XEP-0059: first draft, pubsub and jabber search do not exploit it yet
souliane <souliane@mailoo.org>
parents:
diff changeset
34 PLUGIN_INFO = {
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
35 C.PI_NAME: "Result Set Management",
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
36 C.PI_IMPORT_NAME: "XEP-0059",
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
37 C.PI_TYPE: "XEP",
3738
ffa8c8c78115 plugin XEP-0059, cache: allow those plugins to work in component mode
Goffi <goffi@goffi.org>
parents: 3617
diff changeset
38 C.PI_MODES: C.PLUG_MODE_BOTH,
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
39 C.PI_PROTOCOLS: ["XEP-0059"],
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
40 C.PI_MAIN: "XEP_0059",
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
41 C.PI_HANDLER: "yes",
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
42 C.PI_DESCRIPTION: _("""Implementation of Result Set Management"""),
1219
16484ebb695b plugin XEP-0059: first draft, pubsub and jabber search do not exploit it yet
souliane <souliane@mailoo.org>
parents:
diff changeset
43 }
16484ebb695b plugin XEP-0059: first draft, pubsub and jabber search do not exploit it yet
souliane <souliane@mailoo.org>
parents:
diff changeset
44
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
45 RSM_PREFIX = "rsm_"
2700
035901dc946d plugin XEP-0059: added "parseExtra" method to easily handle RSM argument coming from bridge.
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
46
1219
16484ebb695b plugin XEP-0059: first draft, pubsub and jabber search do not exploit it yet
souliane <souliane@mailoo.org>
parents:
diff changeset
47
4356
c9626f46b63e plugin XEP-0059: Use Pydantic models for RSM.
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
48 class RSMRequest(BaseModel):
c9626f46b63e plugin XEP-0059: Use Pydantic models for RSM.
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
49 """Pydantic model for RSM request parameters"""
c9626f46b63e plugin XEP-0059: Use Pydantic models for RSM.
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
50 max: int = Field(default=10, gt=0)
c9626f46b63e plugin XEP-0059: Use Pydantic models for RSM.
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
51 after: str | None = None
c9626f46b63e plugin XEP-0059: Use Pydantic models for RSM.
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
52 before: str | None = None
c9626f46b63e plugin XEP-0059: Use Pydantic models for RSM.
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
53 index: int | None = None
c9626f46b63e plugin XEP-0059: Use Pydantic models for RSM.
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
54
c9626f46b63e plugin XEP-0059: Use Pydantic models for RSM.
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
55 @field_validator('after')
c9626f46b63e plugin XEP-0059: Use Pydantic models for RSM.
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
56 def check_after_not_empty(cls, v: str | None) -> str | None:
c9626f46b63e plugin XEP-0059: Use Pydantic models for RSM.
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
57 """Validate that after value isn't empty string
c9626f46b63e plugin XEP-0059: Use Pydantic models for RSM.
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
58
c9626f46b63e plugin XEP-0059: Use Pydantic models for RSM.
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
59 Note: Empty before is allowed (means "last page") but empty after is not
c9626f46b63e plugin XEP-0059: Use Pydantic models for RSM.
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
60 @param v: value to validate
c9626f46b63e plugin XEP-0059: Use Pydantic models for RSM.
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
61 @return: validated value
c9626f46b63e plugin XEP-0059: Use Pydantic models for RSM.
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
62 @raise ValueError: if value is an empty string
c9626f46b63e plugin XEP-0059: Use Pydantic models for RSM.
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
63 """
c9626f46b63e plugin XEP-0059: Use Pydantic models for RSM.
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
64 if v == "":
c9626f46b63e plugin XEP-0059: Use Pydantic models for RSM.
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
65 raise ValueError("RSM \"after\" can't be empty")
c9626f46b63e plugin XEP-0059: Use Pydantic models for RSM.
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
66 return v
c9626f46b63e plugin XEP-0059: Use Pydantic models for RSM.
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
67
c9626f46b63e plugin XEP-0059: Use Pydantic models for RSM.
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
68 def to_wokkel_request(self) -> rsm.RSMRequest:
c9626f46b63e plugin XEP-0059: Use Pydantic models for RSM.
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
69 """Convert to wokkel RSMRequest
c9626f46b63e plugin XEP-0059: Use Pydantic models for RSM.
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
70
c9626f46b63e plugin XEP-0059: Use Pydantic models for RSM.
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
71 @return: wokkel RSMRequest instance
c9626f46b63e plugin XEP-0059: Use Pydantic models for RSM.
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
72 """
c9626f46b63e plugin XEP-0059: Use Pydantic models for RSM.
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
73 return rsm.RSMRequest(
c9626f46b63e plugin XEP-0059: Use Pydantic models for RSM.
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
74 max_=self.max,
c9626f46b63e plugin XEP-0059: Use Pydantic models for RSM.
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
75 after=self.after,
c9626f46b63e plugin XEP-0059: Use Pydantic models for RSM.
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
76 before=self.before,
c9626f46b63e plugin XEP-0059: Use Pydantic models for RSM.
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
77 index=self.index
c9626f46b63e plugin XEP-0059: Use Pydantic models for RSM.
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
78 )
c9626f46b63e plugin XEP-0059: Use Pydantic models for RSM.
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
79
c9626f46b63e plugin XEP-0059: Use Pydantic models for RSM.
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
80 @classmethod
c9626f46b63e plugin XEP-0059: Use Pydantic models for RSM.
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
81 def from_wokkel_request(cls, request: rsm.RSMRequest) -> Self:
c9626f46b63e plugin XEP-0059: Use Pydantic models for RSM.
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
82 """Create from wokkel RSMRequest
c9626f46b63e plugin XEP-0059: Use Pydantic models for RSM.
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
83
c9626f46b63e plugin XEP-0059: Use Pydantic models for RSM.
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
84 @param request: wokkel RSMRequest to convert
c9626f46b63e plugin XEP-0059: Use Pydantic models for RSM.
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
85 @return: RSMRequestModel instance
c9626f46b63e plugin XEP-0059: Use Pydantic models for RSM.
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
86 """
c9626f46b63e plugin XEP-0059: Use Pydantic models for RSM.
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
87 return cls(
c9626f46b63e plugin XEP-0059: Use Pydantic models for RSM.
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
88 max=request.max,
c9626f46b63e plugin XEP-0059: Use Pydantic models for RSM.
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
89 after=request.after,
c9626f46b63e plugin XEP-0059: Use Pydantic models for RSM.
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
90 before=request.before,
c9626f46b63e plugin XEP-0059: Use Pydantic models for RSM.
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
91 index=request.index
c9626f46b63e plugin XEP-0059: Use Pydantic models for RSM.
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
92 )
c9626f46b63e plugin XEP-0059: Use Pydantic models for RSM.
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
93
c9626f46b63e plugin XEP-0059: Use Pydantic models for RSM.
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
94 def to_element(self) -> domish.Element:
c9626f46b63e plugin XEP-0059: Use Pydantic models for RSM.
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
95 """Convert to domish.Element
c9626f46b63e plugin XEP-0059: Use Pydantic models for RSM.
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
96
c9626f46b63e plugin XEP-0059: Use Pydantic models for RSM.
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
97 @return: XML element representing the RSM request
c9626f46b63e plugin XEP-0059: Use Pydantic models for RSM.
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
98 """
c9626f46b63e plugin XEP-0059: Use Pydantic models for RSM.
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
99 return self.to_wokkel_request().toElement()
c9626f46b63e plugin XEP-0059: Use Pydantic models for RSM.
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
100
c9626f46b63e plugin XEP-0059: Use Pydantic models for RSM.
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
101 @classmethod
c9626f46b63e plugin XEP-0059: Use Pydantic models for RSM.
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
102 def from_element(cls, element: domish.Element) -> Self:
c9626f46b63e plugin XEP-0059: Use Pydantic models for RSM.
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
103 """Create from domish.Element
c9626f46b63e plugin XEP-0059: Use Pydantic models for RSM.
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
104
c9626f46b63e plugin XEP-0059: Use Pydantic models for RSM.
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
105 @param element: XML element to parse
c9626f46b63e plugin XEP-0059: Use Pydantic models for RSM.
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
106 @return: RSMRequestModel instance
c9626f46b63e plugin XEP-0059: Use Pydantic models for RSM.
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
107 @raise ValueError: if the element is invalid
c9626f46b63e plugin XEP-0059: Use Pydantic models for RSM.
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
108 """
c9626f46b63e plugin XEP-0059: Use Pydantic models for RSM.
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
109 try:
c9626f46b63e plugin XEP-0059: Use Pydantic models for RSM.
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
110 wokkel_req = rsm.RSMRequest.fromElement(element)
c9626f46b63e plugin XEP-0059: Use Pydantic models for RSM.
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
111 except rsm.RSMNotFoundError:
c9626f46b63e plugin XEP-0059: Use Pydantic models for RSM.
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
112 raise ValueError("No RSM set element found")
c9626f46b63e plugin XEP-0059: Use Pydantic models for RSM.
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
113 except rsm.RSMError as e:
c9626f46b63e plugin XEP-0059: Use Pydantic models for RSM.
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
114 raise ValueError(str(e))
c9626f46b63e plugin XEP-0059: Use Pydantic models for RSM.
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
115 return cls.from_wokkel_request(wokkel_req)
c9626f46b63e plugin XEP-0059: Use Pydantic models for RSM.
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
116
1219
16484ebb695b plugin XEP-0059: first draft, pubsub and jabber search do not exploit it yet
souliane <souliane@mailoo.org>
parents:
diff changeset
117
4356
c9626f46b63e plugin XEP-0059: Use Pydantic models for RSM.
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
118 class RSMResponse(BaseModel):
c9626f46b63e plugin XEP-0059: Use Pydantic models for RSM.
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
119 """Pydantic model for RSM response parameters"""
c9626f46b63e plugin XEP-0059: Use Pydantic models for RSM.
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
120 first: str | None = None
c9626f46b63e plugin XEP-0059: Use Pydantic models for RSM.
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
121 last: str | None = None
c9626f46b63e plugin XEP-0059: Use Pydantic models for RSM.
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
122 index: int | None = None
c9626f46b63e plugin XEP-0059: Use Pydantic models for RSM.
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
123 count: int | None = None
c9626f46b63e plugin XEP-0059: Use Pydantic models for RSM.
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
124
c9626f46b63e plugin XEP-0059: Use Pydantic models for RSM.
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
125 def to_wokkel_response(self) -> rsm.RSMResponse:
c9626f46b63e plugin XEP-0059: Use Pydantic models for RSM.
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
126 """Convert to wokkel RSMResponse
c9626f46b63e plugin XEP-0059: Use Pydantic models for RSM.
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
127
c9626f46b63e plugin XEP-0059: Use Pydantic models for RSM.
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
128 @return: wokkel RSMResponse instance
c9626f46b63e plugin XEP-0059: Use Pydantic models for RSM.
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
129 """
c9626f46b63e plugin XEP-0059: Use Pydantic models for RSM.
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
130 return rsm.RSMResponse(
c9626f46b63e plugin XEP-0059: Use Pydantic models for RSM.
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
131 first=self.first,
c9626f46b63e plugin XEP-0059: Use Pydantic models for RSM.
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
132 last=self.last,
c9626f46b63e plugin XEP-0059: Use Pydantic models for RSM.
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
133 index=self.index,
c9626f46b63e plugin XEP-0059: Use Pydantic models for RSM.
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
134 count=self.count
c9626f46b63e plugin XEP-0059: Use Pydantic models for RSM.
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
135 )
c9626f46b63e plugin XEP-0059: Use Pydantic models for RSM.
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
136
c9626f46b63e plugin XEP-0059: Use Pydantic models for RSM.
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
137 @classmethod
c9626f46b63e plugin XEP-0059: Use Pydantic models for RSM.
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
138 def from_wokkel_response(cls, response: rsm.RSMResponse) -> Self:
c9626f46b63e plugin XEP-0059: Use Pydantic models for RSM.
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
139 """Create from wokkel RSMResponse
c9626f46b63e plugin XEP-0059: Use Pydantic models for RSM.
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
140
c9626f46b63e plugin XEP-0059: Use Pydantic models for RSM.
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
141 @param response: wokkel RSMResponse to convert
c9626f46b63e plugin XEP-0059: Use Pydantic models for RSM.
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
142 @return: RSMResponseModel instance
c9626f46b63e plugin XEP-0059: Use Pydantic models for RSM.
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
143 """
c9626f46b63e plugin XEP-0059: Use Pydantic models for RSM.
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
144 return cls(
c9626f46b63e plugin XEP-0059: Use Pydantic models for RSM.
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
145 first=response.first,
c9626f46b63e plugin XEP-0059: Use Pydantic models for RSM.
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
146 last=response.last,
c9626f46b63e plugin XEP-0059: Use Pydantic models for RSM.
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
147 index=response.index,
c9626f46b63e plugin XEP-0059: Use Pydantic models for RSM.
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
148 count=response.count
c9626f46b63e plugin XEP-0059: Use Pydantic models for RSM.
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
149 )
c9626f46b63e plugin XEP-0059: Use Pydantic models for RSM.
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
150
c9626f46b63e plugin XEP-0059: Use Pydantic models for RSM.
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
151 def to_element(self) -> domish.Element:
c9626f46b63e plugin XEP-0059: Use Pydantic models for RSM.
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
152 """Convert to domish.Element
1219
16484ebb695b plugin XEP-0059: first draft, pubsub and jabber search do not exploit it yet
souliane <souliane@mailoo.org>
parents:
diff changeset
153
4356
c9626f46b63e plugin XEP-0059: Use Pydantic models for RSM.
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
154 @return: XML element representing the RSM response
c9626f46b63e plugin XEP-0059: Use Pydantic models for RSM.
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
155 """
c9626f46b63e plugin XEP-0059: Use Pydantic models for RSM.
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
156 return self.to_wokkel_response().toElement()
c9626f46b63e plugin XEP-0059: Use Pydantic models for RSM.
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
157
c9626f46b63e plugin XEP-0059: Use Pydantic models for RSM.
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
158 @classmethod
c9626f46b63e plugin XEP-0059: Use Pydantic models for RSM.
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
159 def from_element(cls, element: domish.Element) -> Self:
c9626f46b63e plugin XEP-0059: Use Pydantic models for RSM.
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
160 """Create from domish.Element
c9626f46b63e plugin XEP-0059: Use Pydantic models for RSM.
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
161
c9626f46b63e plugin XEP-0059: Use Pydantic models for RSM.
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
162 @param element: XML element to parse
c9626f46b63e plugin XEP-0059: Use Pydantic models for RSM.
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
163 @return: RSMResponseModel instance
c9626f46b63e plugin XEP-0059: Use Pydantic models for RSM.
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
164 @raise ValueError: if the element is invalid
c9626f46b63e plugin XEP-0059: Use Pydantic models for RSM.
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
165 """
c9626f46b63e plugin XEP-0059: Use Pydantic models for RSM.
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
166 try:
c9626f46b63e plugin XEP-0059: Use Pydantic models for RSM.
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
167 wokkel_resp = rsm.RSMResponse.fromElement(element)
c9626f46b63e plugin XEP-0059: Use Pydantic models for RSM.
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
168 except rsm.RSMNotFoundError:
c9626f46b63e plugin XEP-0059: Use Pydantic models for RSM.
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
169 raise ValueError("No RSM set element found")
c9626f46b63e plugin XEP-0059: Use Pydantic models for RSM.
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
170 except rsm.RSMError as e:
c9626f46b63e plugin XEP-0059: Use Pydantic models for RSM.
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
171 raise ValueError(str(e))
c9626f46b63e plugin XEP-0059: Use Pydantic models for RSM.
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
172 return cls.from_wokkel_response(wokkel_resp)
c9626f46b63e plugin XEP-0059: Use Pydantic models for RSM.
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
173
c9626f46b63e plugin XEP-0059: Use Pydantic models for RSM.
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
174
c9626f46b63e plugin XEP-0059: Use Pydantic models for RSM.
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
175 class XEP_0059:
c9626f46b63e plugin XEP-0059: Use Pydantic models for RSM.
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
176 def __init__(self, host: str) -> None:
c9626f46b63e plugin XEP-0059: Use Pydantic models for RSM.
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
177 """Initialize the RSM plugin
c9626f46b63e plugin XEP-0059: Use Pydantic models for RSM.
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
178
c9626f46b63e plugin XEP-0059: Use Pydantic models for RSM.
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
179 @param host: host instance
c9626f46b63e plugin XEP-0059: Use Pydantic models for RSM.
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
180 """
c9626f46b63e plugin XEP-0059: Use Pydantic models for RSM.
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
181 log.info(f"Plugin {PLUGIN_INFO[C.PI_NAME]!r} initialization.")
c9626f46b63e plugin XEP-0059: Use Pydantic models for RSM.
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
182
c9626f46b63e plugin XEP-0059: Use Pydantic models for RSM.
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
183 def get_handler(self, client) -> 'XEP_0059_handler':
c9626f46b63e plugin XEP-0059: Use Pydantic models for RSM.
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
184 """Get the XMPP handler for this plugin
c9626f46b63e plugin XEP-0059: Use Pydantic models for RSM.
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
185
c9626f46b63e plugin XEP-0059: Use Pydantic models for RSM.
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
186 @param client: client instance
c9626f46b63e plugin XEP-0059: Use Pydantic models for RSM.
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
187 @return: XEP_0059_handler instance
c9626f46b63e plugin XEP-0059: Use Pydantic models for RSM.
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
188 """
1465
cbf38047ca60 plugin XEP-0059: fixed bad disco handling
Goffi <goffi@goffi.org>
parents: 1396
diff changeset
189 return XEP_0059_handler()
cbf38047ca60 plugin XEP-0059: fixed bad disco handling
Goffi <goffi@goffi.org>
parents: 1396
diff changeset
190
4356
c9626f46b63e plugin XEP-0059: Use Pydantic models for RSM.
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
191 def parse_extra(self, extra: dict[str, str]) -> rsm.RSMRequest | None:
2700
035901dc946d plugin XEP-0059: added "parseExtra" method to easily handle RSM argument coming from bridge.
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
192 """Parse extra dictionnary to retrieve RSM arguments
035901dc946d plugin XEP-0059: added "parseExtra" method to easily handle RSM argument coming from bridge.
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
193
4356
c9626f46b63e plugin XEP-0059: Use Pydantic models for RSM.
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
194 @param extra: data to parse
c9626f46b63e plugin XEP-0059: Use Pydantic models for RSM.
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
195 @return: request with parsed arguments or None if no RSM arguments found
c9626f46b63e plugin XEP-0059: Use Pydantic models for RSM.
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
196 @raise ValueError: if rsm_max is negative
2700
035901dc946d plugin XEP-0059: added "parseExtra" method to easily handle RSM argument coming from bridge.
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
197 """
4356
c9626f46b63e plugin XEP-0059: Use Pydantic models for RSM.
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
198 if int(extra.get(f"{RSM_PREFIX}max", 0)) < 0:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
199 raise ValueError(_("rsm_max can't be negative"))
2763
c4190d5340ab XEP-0059: max value check:
Goffi <goffi@goffi.org>
parents: 2717
diff changeset
200
2700
035901dc946d plugin XEP-0059: added "parseExtra" method to easily handle RSM argument coming from bridge.
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
201 rsm_args = {}
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
202 for arg in ("max", "after", "before", "index"):
2700
035901dc946d plugin XEP-0059: added "parseExtra" method to easily handle RSM argument coming from bridge.
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
203 try:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
204 argname = "max_" if arg == "max" else arg
4356
c9626f46b63e plugin XEP-0059: Use Pydantic models for RSM.
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
205 rsm_args[argname] = extra.pop(f"{RSM_PREFIX}{arg}")
2700
035901dc946d plugin XEP-0059: added "parseExtra" method to easily handle RSM argument coming from bridge.
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
206 except KeyError:
035901dc946d plugin XEP-0059: added "parseExtra" method to easily handle RSM argument coming from bridge.
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
207 continue
035901dc946d plugin XEP-0059: added "parseExtra" method to easily handle RSM argument coming from bridge.
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
208
4356
c9626f46b63e plugin XEP-0059: Use Pydantic models for RSM.
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
209 return RSMRequest(**rsm_args).to_wokkel_request() if rsm_args else None
2700
035901dc946d plugin XEP-0059: added "parseExtra" method to easily handle RSM argument coming from bridge.
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
210
4356
c9626f46b63e plugin XEP-0059: Use Pydantic models for RSM.
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
211 def response2dict(
c9626f46b63e plugin XEP-0059: Use Pydantic models for RSM.
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
212 self,
c9626f46b63e plugin XEP-0059: Use Pydantic models for RSM.
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
213 rsm_response: rsm.RSMResponse,
c9626f46b63e plugin XEP-0059: Use Pydantic models for RSM.
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
214 data: dict[str, str] | None = None
c9626f46b63e plugin XEP-0059: Use Pydantic models for RSM.
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
215 ) -> dict[str, str]:
c9626f46b63e plugin XEP-0059: Use Pydantic models for RSM.
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
216 """Return a dict with RSM response data
2717
e3f6de6ce320 plugin XEP-0059: added serialise method
Goffi <goffi@goffi.org>
parents: 2700
diff changeset
217
e3f6de6ce320 plugin XEP-0059: added serialise method
Goffi <goffi@goffi.org>
parents: 2700
diff changeset
218 Key set in data can be:
4356
c9626f46b63e plugin XEP-0059: Use Pydantic models for RSM.
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
219 - first: first item id in the page
c9626f46b63e plugin XEP-0059: Use Pydantic models for RSM.
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
220 - last: last item id in the page
c9626f46b63e plugin XEP-0059: Use Pydantic models for RSM.
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
221 - index: position of the first item in the full set
c9626f46b63e plugin XEP-0059: Use Pydantic models for RSM.
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
222 - count: total number of items in the full set
c9626f46b63e plugin XEP-0059: Use Pydantic models for RSM.
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
223 If a value doesn't exist, it's not set.
2717
e3f6de6ce320 plugin XEP-0059: added serialise method
Goffi <goffi@goffi.org>
parents: 2700
diff changeset
224 All values are set as strings.
4356
c9626f46b63e plugin XEP-0059: Use Pydantic models for RSM.
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
225
c9626f46b63e plugin XEP-0059: Use Pydantic models for RSM.
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
226 @param rsm_response: response to parse
c9626f46b63e plugin XEP-0059: Use Pydantic models for RSM.
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
227 @param data: dict to update with rsm data. If None, a new dict is created
c9626f46b63e plugin XEP-0059: Use Pydantic models for RSM.
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
228 @return: data dict with RSM values
2717
e3f6de6ce320 plugin XEP-0059: added serialise method
Goffi <goffi@goffi.org>
parents: 2700
diff changeset
229 """
4356
c9626f46b63e plugin XEP-0059: Use Pydantic models for RSM.
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
230 # FIXME: This method should not be used anymore, and removed once replace in
c9626f46b63e plugin XEP-0059: Use Pydantic models for RSM.
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
231 # XEP-0313 plugin.
2717
e3f6de6ce320 plugin XEP-0059: added serialise method
Goffi <goffi@goffi.org>
parents: 2700
diff changeset
232 if data is None:
e3f6de6ce320 plugin XEP-0059: added serialise method
Goffi <goffi@goffi.org>
parents: 2700
diff changeset
233 data = {}
4356
c9626f46b63e plugin XEP-0059: Use Pydantic models for RSM.
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
234 model = RSMResponse.from_wokkel_response(rsm_response)
c9626f46b63e plugin XEP-0059: Use Pydantic models for RSM.
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
235
c9626f46b63e plugin XEP-0059: Use Pydantic models for RSM.
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
236 if model.first is not None:
c9626f46b63e plugin XEP-0059: Use Pydantic models for RSM.
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
237 data["first"] = model.first
c9626f46b63e plugin XEP-0059: Use Pydantic models for RSM.
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
238 if model.last is not None:
c9626f46b63e plugin XEP-0059: Use Pydantic models for RSM.
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
239 data["last"] = model.last
c9626f46b63e plugin XEP-0059: Use Pydantic models for RSM.
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
240 if model.index is not None:
c9626f46b63e plugin XEP-0059: Use Pydantic models for RSM.
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
241 data["index"] = str(model.index)
c9626f46b63e plugin XEP-0059: Use Pydantic models for RSM.
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
242 if model.count is not None:
c9626f46b63e plugin XEP-0059: Use Pydantic models for RSM.
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
243 data["count"] = str(model.count)
c9626f46b63e plugin XEP-0059: Use Pydantic models for RSM.
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
244
2717
e3f6de6ce320 plugin XEP-0059: added serialise method
Goffi <goffi@goffi.org>
parents: 2700
diff changeset
245 return data
e3f6de6ce320 plugin XEP-0059: added serialise method
Goffi <goffi@goffi.org>
parents: 2700
diff changeset
246
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3738
diff changeset
247 def get_next_request(
3592
ef144aaea2bb plugin XEP-0059: new `getNextRequest` method to generate request to retrieve next page
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
248 self,
4356
c9626f46b63e plugin XEP-0059: Use Pydantic models for RSM.
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
249 rsm_request: RSMRequest,
c9626f46b63e plugin XEP-0059: Use Pydantic models for RSM.
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
250 rsm_response: RSMResponse,
3592
ef144aaea2bb plugin XEP-0059: new `getNextRequest` method to generate request to retrieve next page
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
251 log_progress: bool = True,
4356
c9626f46b63e plugin XEP-0059: Use Pydantic models for RSM.
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
252 ) -> RSMRequest | None:
c9626f46b63e plugin XEP-0059: Use Pydantic models for RSM.
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
253 """Generate the request for the next page of items.
3592
ef144aaea2bb plugin XEP-0059: new `getNextRequest` method to generate request to retrieve next page
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
254
4356
c9626f46b63e plugin XEP-0059: Use Pydantic models for RSM.
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
255 Page will be retrieved forward.
3592
ef144aaea2bb plugin XEP-0059: new `getNextRequest` method to generate request to retrieve next page
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
256 @param rsm_request: last request used
ef144aaea2bb plugin XEP-0059: new `getNextRequest` method to generate request to retrieve next page
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
257 @param rsm_response: response from the last request
4356
c9626f46b63e plugin XEP-0059: Use Pydantic models for RSM.
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
258 @param log_progress: whether to log progress information
c9626f46b63e plugin XEP-0059: Use Pydantic models for RSM.
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
259 @return: request to retrieve next page, or None if we are at the end
c9626f46b63e plugin XEP-0059: Use Pydantic models for RSM.
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
260 or if pagination is not possible.
3592
ef144aaea2bb plugin XEP-0059: new `getNextRequest` method to generate request to retrieve next page
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
261 """
ef144aaea2bb plugin XEP-0059: new `getNextRequest` method to generate request to retrieve next page
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
262 if rsm_request.max == 0:
ef144aaea2bb plugin XEP-0059: new `getNextRequest` method to generate request to retrieve next page
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
263 log.warning("Can't do pagination if max is 0")
ef144aaea2bb plugin XEP-0059: new `getNextRequest` method to generate request to retrieve next page
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
264 return None
4356
c9626f46b63e plugin XEP-0059: Use Pydantic models for RSM.
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
265
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
266 if rsm_response.count is not None and rsm_response.index is not None:
3592
ef144aaea2bb plugin XEP-0059: new `getNextRequest` method to generate request to retrieve next page
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
267 next_index = rsm_response.index + rsm_request.max
ef144aaea2bb plugin XEP-0059: new `getNextRequest` method to generate request to retrieve next page
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
268 if next_index >= rsm_response.count:
4356
c9626f46b63e plugin XEP-0059: Use Pydantic models for RSM.
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
269 # We have reached the last page.
3592
ef144aaea2bb plugin XEP-0059: new `getNextRequest` method to generate request to retrieve next page
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
270 return None
ef144aaea2bb plugin XEP-0059: new `getNextRequest` method to generate request to retrieve next page
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
271
ef144aaea2bb plugin XEP-0059: new `getNextRequest` method to generate request to retrieve next page
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
272 if log_progress:
ef144aaea2bb plugin XEP-0059: new `getNextRequest` method to generate request to retrieve next page
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
273 log.debug(
4356
c9626f46b63e plugin XEP-0059: Use Pydantic models for RSM.
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
274 f"Retrieving items {next_index} to "
c9626f46b63e plugin XEP-0059: Use Pydantic models for RSM.
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
275 f"{min(next_index + rsm_request.max, rsm_response.count)} on "
3592
ef144aaea2bb plugin XEP-0059: new `getNextRequest` method to generate request to retrieve next page
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
276 f"{rsm_response.count} ({next_index/rsm_response.count*100:.2f}%)"
ef144aaea2bb plugin XEP-0059: new `getNextRequest` method to generate request to retrieve next page
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
277 )
ef144aaea2bb plugin XEP-0059: new `getNextRequest` method to generate request to retrieve next page
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
278
ef144aaea2bb plugin XEP-0059: new `getNextRequest` method to generate request to retrieve next page
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
279 if rsm_response.last is None:
3617
766729ac4146 plugin XEP-0059: don't show warning message when <last> is missing if there is not message
Goffi <goffi@goffi.org>
parents: 3592
diff changeset
280 if rsm_response.count:
4356
c9626f46b63e plugin XEP-0059: Use Pydantic models for RSM.
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
281 log.warning('Can\'t do pagination, no "last" received.')
3592
ef144aaea2bb plugin XEP-0059: new `getNextRequest` method to generate request to retrieve next page
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
282 return None
ef144aaea2bb plugin XEP-0059: new `getNextRequest` method to generate request to retrieve next page
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
283
4356
c9626f46b63e plugin XEP-0059: Use Pydantic models for RSM.
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
284 return RSMRequest(
c9626f46b63e plugin XEP-0059: Use Pydantic models for RSM.
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
285 max=rsm_request.max,
c9626f46b63e plugin XEP-0059: Use Pydantic models for RSM.
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
286 after=rsm_response.last
c9626f46b63e plugin XEP-0059: Use Pydantic models for RSM.
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
287 )
3592
ef144aaea2bb plugin XEP-0059: new `getNextRequest` method to generate request to retrieve next page
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
288
1219
16484ebb695b plugin XEP-0059: first draft, pubsub and jabber search do not exploit it yet
souliane <souliane@mailoo.org>
parents:
diff changeset
289
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
290 @implementer(iwokkel.IDisco)
1769
1fc6a380f4db plugin xep-0059: minor cleaning
Goffi <goffi@goffi.org>
parents: 1766
diff changeset
291 class XEP_0059_handler(xmlstream.XMPPHandler):
4356
c9626f46b63e plugin XEP-0059: Use Pydantic models for RSM.
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
292 def getDiscoInfo(
c9626f46b63e plugin XEP-0059: Use Pydantic models for RSM.
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
293 self,
c9626f46b63e plugin XEP-0059: Use Pydantic models for RSM.
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
294 requestor: str,
c9626f46b63e plugin XEP-0059: Use Pydantic models for RSM.
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
295 target: str,
c9626f46b63e plugin XEP-0059: Use Pydantic models for RSM.
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
296 nodeIdentifier: str = ""
c9626f46b63e plugin XEP-0059: Use Pydantic models for RSM.
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
297 ) -> list[disco.DiscoFeature]:
c9626f46b63e plugin XEP-0059: Use Pydantic models for RSM.
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
298 """Get disco info for RSM
1219
16484ebb695b plugin XEP-0059: first draft, pubsub and jabber search do not exploit it yet
souliane <souliane@mailoo.org>
parents:
diff changeset
299
4356
c9626f46b63e plugin XEP-0059: Use Pydantic models for RSM.
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
300 @param requestor: JID of the requesting entity
c9626f46b63e plugin XEP-0059: Use Pydantic models for RSM.
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
301 @param target: JID of the target entity
c9626f46b63e plugin XEP-0059: Use Pydantic models for RSM.
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
302 @param nodeIdentifier: optional node identifier
c9626f46b63e plugin XEP-0059: Use Pydantic models for RSM.
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
303 @return: list of disco features
c9626f46b63e plugin XEP-0059: Use Pydantic models for RSM.
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
304 """
1769
1fc6a380f4db plugin xep-0059: minor cleaning
Goffi <goffi@goffi.org>
parents: 1766
diff changeset
305 return [disco.DiscoFeature(rsm.NS_RSM)]
1219
16484ebb695b plugin XEP-0059: first draft, pubsub and jabber search do not exploit it yet
souliane <souliane@mailoo.org>
parents:
diff changeset
306
4356
c9626f46b63e plugin XEP-0059: Use Pydantic models for RSM.
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
307 def getDiscoItems(
c9626f46b63e plugin XEP-0059: Use Pydantic models for RSM.
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
308 self,
c9626f46b63e plugin XEP-0059: Use Pydantic models for RSM.
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
309 requestor: str,
c9626f46b63e plugin XEP-0059: Use Pydantic models for RSM.
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
310 target: str,
c9626f46b63e plugin XEP-0059: Use Pydantic models for RSM.
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
311 nodeIdentifier: str = ""
c9626f46b63e plugin XEP-0059: Use Pydantic models for RSM.
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
312 ) -> list:
c9626f46b63e plugin XEP-0059: Use Pydantic models for RSM.
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
313 """Get disco items for RSM
c9626f46b63e plugin XEP-0059: Use Pydantic models for RSM.
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
314
c9626f46b63e plugin XEP-0059: Use Pydantic models for RSM.
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
315 @param requestor: JID of the requesting entity
c9626f46b63e plugin XEP-0059: Use Pydantic models for RSM.
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
316 @param target: JID of the target entity
c9626f46b63e plugin XEP-0059: Use Pydantic models for RSM.
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
317 @param nodeIdentifier: optional node identifier
c9626f46b63e plugin XEP-0059: Use Pydantic models for RSM.
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
318 @return: empty list (RSM doesn't have items)
c9626f46b63e plugin XEP-0059: Use Pydantic models for RSM.
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
319 """
1219
16484ebb695b plugin XEP-0059: first draft, pubsub and jabber search do not exploit it yet
souliane <souliane@mailoo.org>
parents:
diff changeset
320 return []