Mercurial > libervia-backend
comparison libervia/backend/plugins/plugin_xep_0372.py @ 4270:0d7bb4df2343
Reformatted code base using black.
author | Goffi <goffi@goffi.org> |
---|---|
date | Wed, 19 Jun 2024 18:44:57 +0200 |
parents | 4b842c1fb686 |
children |
comparison
equal
deleted
inserted
replaced
4269:64a85ce8be70 | 4270:0d7bb4df2343 |
---|---|
44 C.PI_MODES: C.PLUG_MODE_BOTH, | 44 C.PI_MODES: C.PLUG_MODE_BOTH, |
45 C.PI_PROTOCOLS: ["XEP-0372"], | 45 C.PI_PROTOCOLS: ["XEP-0372"], |
46 C.PI_DEPENDENCIES: ["XEP-0334"], | 46 C.PI_DEPENDENCIES: ["XEP-0334"], |
47 C.PI_MAIN: "XEP_0372", | 47 C.PI_MAIN: "XEP_0372", |
48 C.PI_HANDLER: "yes", | 48 C.PI_HANDLER: "yes", |
49 C.PI_DESCRIPTION: _(dedent("""\ | 49 C.PI_DESCRIPTION: _( |
50 dedent( | |
51 """\ | |
50 XEP-0372 (References) implementation | 52 XEP-0372 (References) implementation |
51 | 53 |
52 This plugin implement generic references and mentions. | 54 This plugin implement generic references and mentions. |
53 """)), | 55 """ |
56 ) | |
57 ), | |
54 } | 58 } |
55 | 59 |
56 NS_REFS = "urn:xmpp:reference:0" | 60 NS_REFS = "urn:xmpp:reference:0" |
57 ALLOWED_TYPES = ("mention", "data") | 61 ALLOWED_TYPES = ("mention", "data") |
58 | 62 |
77 | 81 |
78 def get_handler(self, client): | 82 def get_handler(self, client): |
79 return XEP_0372_Handler() | 83 return XEP_0372_Handler() |
80 | 84 |
81 def ref_element_to_ref_data( | 85 def ref_element_to_ref_data( |
82 self, | 86 self, reference_elt: domish.Element |
83 reference_elt: domish.Element | |
84 ) -> Dict[str, Union[str, int, dict]]: | 87 ) -> Dict[str, Union[str, int, dict]]: |
85 ref_data: Dict[str, Union[str, int, dict]] = { | 88 ref_data: Dict[str, Union[str, int, dict]] = { |
86 "uri": reference_elt["uri"], | 89 "uri": reference_elt["uri"], |
87 "type": reference_elt["type"] | 90 "type": reference_elt["type"], |
88 } | 91 } |
89 | 92 |
90 if ref_data["uri"].startswith("xmpp:"): | 93 if ref_data["uri"].startswith("xmpp:"): |
91 ref_data["parsed_uri"] = xmpp_uri.parse_xmpp_uri(ref_data["uri"]) | 94 ref_data["parsed_uri"] = xmpp_uri.parse_xmpp_uri(ref_data["uri"]) |
92 | 95 |
105 | 108 |
106 async def _message_received_trigger( | 109 async def _message_received_trigger( |
107 self, | 110 self, |
108 client: SatXMPPEntity, | 111 client: SatXMPPEntity, |
109 message_elt: domish.Element, | 112 message_elt: domish.Element, |
110 post_treat: defer.Deferred | 113 post_treat: defer.Deferred, |
111 ) -> bool: | 114 ) -> bool: |
112 """Check if a direct invitation is in the message, and handle it""" | 115 """Check if a direct invitation is in the message, and handle it""" |
113 reference_elt = next(message_elt.elements(NS_REFS, "reference"), None) | 116 reference_elt = next(message_elt.elements(NS_REFS, "reference"), None) |
114 if reference_elt is None: | 117 if reference_elt is None: |
115 return True | 118 return True |
135 ) -> domish.Element: | 138 ) -> domish.Element: |
136 """Build and return the <reference> element""" | 139 """Build and return the <reference> element""" |
137 if type_ not in ALLOWED_TYPES: | 140 if type_ not in ALLOWED_TYPES: |
138 raise ValueError(f"Unknown type: {type_!r}") | 141 raise ValueError(f"Unknown type: {type_!r}") |
139 reference_elt = domish.Element( | 142 reference_elt = domish.Element( |
140 (NS_REFS, "reference"), | 143 (NS_REFS, "reference"), attribs={"uri": uri, "type": type_} |
141 attribs={"uri": uri, "type": type_} | |
142 ) | 144 ) |
143 if begin is not None: | 145 if begin is not None: |
144 reference_elt["begin"] = str(begin) | 146 reference_elt["begin"] = str(begin) |
145 if end is not None: | 147 if end is not None: |
146 reference_elt["end"] = str(end) | 148 reference_elt["end"] = str(end) |
147 if anchor is not None: | 149 if anchor is not None: |
148 reference_elt["anchor"] = anchor | 150 reference_elt["anchor"] = anchor |
149 return reference_elt | 151 return reference_elt |
150 | 152 |
151 def _send_reference( | 153 def _send_reference( |
152 self, | 154 self, recipient: str, anchor: str, type_: str, extra_s: str, profile_key: str |
153 recipient: str, | |
154 anchor: str, | |
155 type_: str, | |
156 extra_s: str, | |
157 profile_key: str | |
158 ) -> defer.Deferred: | 155 ) -> defer.Deferred: |
159 recipient_jid = jid.JID(recipient) | 156 recipient_jid = jid.JID(recipient) |
160 client = self.host.get_client(profile_key) | 157 client = self.host.get_client(profile_key) |
161 extra: dict = data_format.deserialise(extra_s, default={}) | 158 extra: dict = data_format.deserialise(extra_s, default={}) |
162 self.send_reference( | 159 self.send_reference( |
163 client, | 160 client, uri=extra.get("uri"), type_=type_, anchor=anchor, to_jid=recipient_jid |
164 uri=extra.get("uri"), | |
165 type_=type_, | |
166 anchor=anchor, | |
167 to_jid=recipient_jid | |
168 ) | 161 ) |
169 | 162 |
170 def send_reference( | 163 def send_reference( |
171 self, | 164 self, |
172 client: "SatXMPPEntity", | 165 client: "SatXMPPEntity", |
174 type_: str = "mention", | 167 type_: str = "mention", |
175 begin: Optional[int] = None, | 168 begin: Optional[int] = None, |
176 end: Optional[int] = None, | 169 end: Optional[int] = None, |
177 anchor: Optional[str] = None, | 170 anchor: Optional[str] = None, |
178 message_elt: Optional[domish.Element] = None, | 171 message_elt: Optional[domish.Element] = None, |
179 to_jid: Optional[jid.JID] = None | 172 to_jid: Optional[jid.JID] = None, |
180 ) -> None: | 173 ) -> None: |
181 """Build and send a reference_elt | 174 """Build and send a reference_elt |
182 | 175 |
183 @param uri: URI pointing to referenced object (XMPP entity, Pubsub Item, etc) | 176 @param uri: URI pointing to referenced object (XMPP entity, Pubsub Item, etc) |
184 if not set, "to_jid" will be used to build an URI to the entity | 177 if not set, "to_jid" will be used to build an URI to the entity |
195 message_elt will be used. | 188 message_elt will be used. |
196 | 189 |
197 """ | 190 """ |
198 if uri is None: | 191 if uri is None: |
199 if to_jid is None: | 192 if to_jid is None: |
200 raise exceptions.InternalError( | 193 raise exceptions.InternalError('"to_jid" must be set if "uri is None"') |
201 '"to_jid" must be set if "uri is None"' | |
202 ) | |
203 uri = xmpp_uri.build_xmpp_uri(path=to_jid.full()) | 194 uri = xmpp_uri.build_xmpp_uri(path=to_jid.full()) |
204 if message_elt is None: | 195 if message_elt is None: |
205 message_elt = domish.Element((None, "message")) | 196 message_elt = domish.Element((None, "message")) |
206 | 197 |
207 if to_jid is not None: | 198 if to_jid is not None: |
210 try: | 201 try: |
211 to_jid = jid.JID(message_elt["to"]) | 202 to_jid = jid.JID(message_elt["to"]) |
212 except (KeyError, RuntimeError): | 203 except (KeyError, RuntimeError): |
213 raise exceptions.InternalError( | 204 raise exceptions.InternalError( |
214 'invalid "to" attribute in given message element: ' | 205 'invalid "to" attribute in given message element: ' |
215 '{message_elt.toXml()}' | 206 "{message_elt.toXml()}" |
216 ) | 207 ) |
217 | 208 |
218 message_elt.addChild(self.build_ref_element(uri, type_, begin, end, anchor)) | 209 message_elt.addChild(self.build_ref_element(uri, type_, begin, end, anchor)) |
219 self._h.add_hint_elements(message_elt, [self._h.HINT_STORE]) | 210 self._h.add_hint_elements(message_elt, [self._h.HINT_STORE]) |
220 client.send(message_elt) | 211 client.send(message_elt) |