comparison libervia/backend/plugins/plugin_exp_list_of_interest.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
97 ) 97 )
98 except jabber_error.StanzaError as e: 98 except jabber_error.StanzaError as e:
99 if e.condition == "conflict": 99 if e.condition == "conflict":
100 log.debug(_("requested node already exists")) 100 log.debug(_("requested node already exists"))
101 101
102 async def register_pubsub(self, client, namespace, service, node, item_id=None, 102 async def register_pubsub(
103 creator=False, name=None, element=None, extra=None): 103 self,
104 client,
105 namespace,
106 service,
107 node,
108 item_id=None,
109 creator=False,
110 name=None,
111 element=None,
112 extra=None,
113 ):
104 """Register an interesting element in personal list 114 """Register an interesting element in personal list
105 115
106 @param namespace(unicode): namespace of the interest 116 @param namespace(unicode): namespace of the interest
107 this is used as a cache, to avoid the need to retrieve the item only to get 117 this is used as a cache, to avoid the need to retrieve the item only to get
108 its namespace 118 its namespace
121 extra = {} 131 extra = {}
122 await self.createNode(client) 132 await self.createNode(client)
123 interest_elt = domish.Element((NS_LIST_INTEREST, "interest")) 133 interest_elt = domish.Element((NS_LIST_INTEREST, "interest"))
124 interest_elt["namespace"] = namespace 134 interest_elt["namespace"] = namespace
125 if name is not None: 135 if name is not None:
126 interest_elt['name'] = name 136 interest_elt["name"] = name
127 thumb_url = extra.get('thumb_url') 137 thumb_url = extra.get("thumb_url")
128 if thumb_url: 138 if thumb_url:
129 interest_elt['thumb_url'] = thumb_url 139 interest_elt["thumb_url"] = thumb_url
130 pubsub_elt = interest_elt.addElement("pubsub") 140 pubsub_elt = interest_elt.addElement("pubsub")
131 pubsub_elt["service"] = service.full() 141 pubsub_elt["service"] = service.full()
132 pubsub_elt["node"] = node 142 pubsub_elt["node"] = node
133 if item_id is not None: 143 if item_id is not None:
134 pubsub_elt["item"] = item_id 144 pubsub_elt["item"] = item_id
135 if creator: 145 if creator:
136 pubsub_elt["creator"] = C.BOOL_TRUE 146 pubsub_elt["creator"] = C.BOOL_TRUE
137 if element is not None: 147 if element is not None:
138 pubsub_elt.addChild(element) 148 pubsub_elt.addChild(element)
139 uri_kwargs = { 149 uri_kwargs = {"path": service.full(), "node": node}
140 "path": service.full(),
141 "node": node
142 }
143 if item_id: 150 if item_id:
144 uri_kwargs['id'] = item_id 151 uri_kwargs["id"] = item_id
145 interest_uri = uri.build_xmpp_uri("pubsub", **uri_kwargs) 152 interest_uri = uri.build_xmpp_uri("pubsub", **uri_kwargs)
146 # we use URI of the interest as item id to avoid duplicates 153 # we use URI of the interest as item id to avoid duplicates
147 item_elt = pubsub.Item(interest_uri, payload=interest_elt) 154 item_elt = pubsub.Item(interest_uri, payload=interest_elt)
148 await self._p.publish( 155 await self._p.publish(
149 client, client.jid.userhostJID(), NS_LIST_INTEREST, items=[item_elt] 156 client, client.jid.userhostJID(), NS_LIST_INTEREST, items=[item_elt]
150 ) 157 )
151 158
152 def _register_file_sharing( 159 def _register_file_sharing(
153 self, service, repos_type, namespace, path, name, extra_raw, 160 self, service, repos_type, namespace, path, name, extra_raw, profile
154 profile
155 ): 161 ):
156 client = self.host.get_client(profile) 162 client = self.host.get_client(profile)
157 extra = data_format.deserialise(extra_raw) 163 extra = data_format.deserialise(extra_raw)
158 164
159 return defer.ensureDeferred(self.register_file_sharing( 165 return defer.ensureDeferred(
160 client, jid.JID(service), repos_type or None, namespace or None, path or None, 166 self.register_file_sharing(
161 name or None, extra 167 client,
162 )) 168 jid.JID(service),
169 repos_type or None,
170 namespace or None,
171 path or None,
172 name or None,
173 extra,
174 )
175 )
163 176
164 def normalise_file_sharing_service(self, client, service): 177 def normalise_file_sharing_service(self, client, service):
165 # FIXME: Q&D fix as the bare file sharing service JID will lead to user own 178 # FIXME: Q&D fix as the bare file sharing service JID will lead to user own
166 # repository, which thus would not be the same for the host and the guest. 179 # repository, which thus would not be the same for the host and the guest.
167 # By specifying the user part, we for the use of the host repository. 180 # By specifying the user part, we for the use of the host repository.
168 # A cleaner way should be implemented 181 # A cleaner way should be implemented
169 if service.user is None: 182 if service.user is None:
170 service.user = self.host.plugins['XEP-0106'].escape(client.jid.user) 183 service.user = self.host.plugins["XEP-0106"].escape(client.jid.user)
171 184
172 def get_file_sharing_id(self, service, namespace, path): 185 def get_file_sharing_id(self, service, namespace, path):
173 return f"{service}_{namespace or ''}_{path or ''}" 186 return f"{service}_{namespace or ''}_{path or ''}"
174 187
175 async def register_file_sharing( 188 async def register_file_sharing(
176 self, client, service, repos_type=None, namespace=None, path=None, name=None, 189 self,
177 extra=None): 190 client,
191 service,
192 repos_type=None,
193 namespace=None,
194 path=None,
195 name=None,
196 extra=None,
197 ):
178 """Register an interesting file repository in personal list 198 """Register an interesting file repository in personal list
179 199
180 @param service(jid.JID): service of the file repository 200 @param service(jid.JID): service of the file repository
181 @param repos_type(unicode): type of the repository 201 @param repos_type(unicode): type of the repository
182 @param namespace(unicode, None): namespace of the repository 202 @param namespace(unicode, None): namespace of the repository
190 await self.createNode(client) 210 await self.createNode(client)
191 item_id = self.get_file_sharing_id(service, namespace, path) 211 item_id = self.get_file_sharing_id(service, namespace, path)
192 interest_elt = domish.Element((NS_LIST_INTEREST, "interest")) 212 interest_elt = domish.Element((NS_LIST_INTEREST, "interest"))
193 interest_elt["namespace"] = self.host.get_namespace("fis") 213 interest_elt["namespace"] = self.host.get_namespace("fis")
194 if name is not None: 214 if name is not None:
195 interest_elt['name'] = name 215 interest_elt["name"] = name
196 thumb_url = extra.get('thumb_url') 216 thumb_url = extra.get("thumb_url")
197 if thumb_url: 217 if thumb_url:
198 interest_elt['thumb_url'] = thumb_url 218 interest_elt["thumb_url"] = thumb_url
199 219
200 file_sharing_elt = interest_elt.addElement("file_sharing") 220 file_sharing_elt = interest_elt.addElement("file_sharing")
201 file_sharing_elt["service"] = service.full() 221 file_sharing_elt["service"] = service.full()
202 if repos_type is not None: 222 if repos_type is not None:
203 file_sharing_elt["type"] = repos_type 223 file_sharing_elt["type"] = repos_type
211 ) 231 )
212 232
213 def _list_interests_serialise(self, interests_data): 233 def _list_interests_serialise(self, interests_data):
214 interests = [] 234 interests = []
215 for item_elt in interests_data[0]: 235 for item_elt in interests_data[0]:
216 interest_data = {"id": item_elt['id']} 236 interest_data = {"id": item_elt["id"]}
217 interest_elt = item_elt.interest 237 interest_elt = item_elt.interest
218 if interest_elt.hasAttribute('namespace'): 238 if interest_elt.hasAttribute("namespace"):
219 interest_data['namespace'] = interest_elt.getAttribute('namespace') 239 interest_data["namespace"] = interest_elt.getAttribute("namespace")
220 if interest_elt.hasAttribute('name'): 240 if interest_elt.hasAttribute("name"):
221 interest_data['name'] = interest_elt.getAttribute('name') 241 interest_data["name"] = interest_elt.getAttribute("name")
222 if interest_elt.hasAttribute('thumb_url'): 242 if interest_elt.hasAttribute("thumb_url"):
223 interest_data['thumb_url'] = interest_elt.getAttribute('thumb_url') 243 interest_data["thumb_url"] = interest_elt.getAttribute("thumb_url")
224 elt = interest_elt.firstChildElement() 244 elt = interest_elt.firstChildElement()
225 if elt.uri != NS_LIST_INTEREST: 245 if elt.uri != NS_LIST_INTEREST:
226 log.warning("unexpected child element, ignoring: {xml}".format( 246 log.warning(
227 xml = elt.toXml())) 247 "unexpected child element, ignoring: {xml}".format(xml=elt.toXml())
248 )
228 continue 249 continue
229 if elt.name == 'pubsub': 250 if elt.name == "pubsub":
230 interest_data.update({ 251 interest_data.update(
231 "type": "pubsub", 252 {
232 "service": elt['service'], 253 "type": "pubsub",
233 "node": elt['node'], 254 "service": elt["service"],
234 }) 255 "node": elt["node"],
235 for attr in ('item', 'creator'): 256 }
257 )
258 for attr in ("item", "creator"):
236 if elt.hasAttribute(attr): 259 if elt.hasAttribute(attr):
237 interest_data[attr] = elt[attr] 260 interest_data[attr] = elt[attr]
238 elif elt.name == 'file_sharing': 261 elif elt.name == "file_sharing":
239 interest_data.update({ 262 interest_data.update(
240 "type": "file_sharing", 263 {
241 "service": elt['service'], 264 "type": "file_sharing",
242 }) 265 "service": elt["service"],
243 if elt.hasAttribute('type'): 266 }
244 interest_data['subtype'] = elt['type'] 267 )
245 for attr in ('files_namespace', 'path'): 268 if elt.hasAttribute("type"):
269 interest_data["subtype"] = elt["type"]
270 for attr in ("files_namespace", "path"):
246 if elt.hasAttribute(attr): 271 if elt.hasAttribute(attr):
247 interest_data[attr] = elt[attr] 272 interest_data[attr] = elt[attr]
248 else: 273 else:
249 log.warning("unknown element, ignoring: {xml}".format(xml=elt.toXml())) 274 log.warning("unknown element, ignoring: {xml}".format(xml=elt.toXml()))
250 continue 275 continue
280 filtered_items = [] 305 filtered_items = []
281 for item in items: 306 for item in items:
282 try: 307 try:
283 interest_elt = next(item.elements(NS_LIST_INTEREST, "interest")) 308 interest_elt = next(item.elements(NS_LIST_INTEREST, "interest"))
284 except StopIteration: 309 except StopIteration:
285 log.warning(_("Missing interest element: {xml}").format( 310 log.warning(
286 xml=item.toXml())) 311 _("Missing interest element: {xml}").format(xml=item.toXml())
312 )
287 continue 313 continue
288 if interest_elt.getAttribute("namespace") == namespace: 314 if interest_elt.getAttribute("namespace") == namespace:
289 filtered_items.append(item) 315 filtered_items.append(item)
290 items = filtered_items 316 items = filtered_items
291 317
292 return (items, metadata) 318 return (items, metadata)
293 319
294 def _interest_retract(self, service_s, item_id, profile_key): 320 def _interest_retract(self, service_s, item_id, profile_key):
295 d = self._p._retract_item( 321 d = self._p._retract_item(service_s, NS_LIST_INTEREST, item_id, True, profile_key)
296 service_s, NS_LIST_INTEREST, item_id, True, profile_key)
297 d.addCallback(lambda __: None) 322 d.addCallback(lambda __: None)
298 return d 323 return d
299 324
300 async def get(self, client: SatXMPPEntity, item_id: str) -> dict: 325 async def get(self, client: SatXMPPEntity, item_id: str) -> dict:
301 """Retrieve a specific interest in profile's list""" 326 """Retrieve a specific interest in profile's list"""
302 items_data = await self._p.get_items(client, None, NS_LIST_INTEREST, item_ids=[item_id]) 327 items_data = await self._p.get_items(
328 client, None, NS_LIST_INTEREST, item_ids=[item_id]
329 )
303 try: 330 try:
304 return self._list_interests_serialise(items_data)[0] 331 return self._list_interests_serialise(items_data)[0]
305 except IndexError: 332 except IndexError:
306 raise exceptions.NotFound 333 raise exceptions.NotFound
307 334