comparison sat/plugins/plugin_exp_list_of_interest.py @ 3584:edc79cefe968

plugin XEP-0060: `getItem(s)`, `publish` and `(un)subscribe` are now coroutines
author Goffi <goffi@goffi.org>
date Wed, 30 Jun 2021 16:19:14 +0200
parents be6d91572633
children 524856bd7b19
comparison
equal deleted inserted replaced
3583:16ade4ad63f3 3584:edc79cefe968
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 @defer.inlineCallbacks 102 async def registerPubsub(self, client, namespace, service, node, item_id=None,
103 def registerPubsub(self, client, namespace, service, node, item_id=None,
104 creator=False, name=None, element=None, extra=None): 103 creator=False, name=None, element=None, extra=None):
105 """Register an interesting element in personal list 104 """Register an interesting element in personal list
106 105
107 @param namespace(unicode): namespace of the interest 106 @param namespace(unicode): namespace of the interest
108 this is used as a cache, to avoid the need to retrieve the item only to get 107 this is used as a cache, to avoid the need to retrieve the item only to get
118 @param extra(dict, None): extra data, key can be: 117 @param extra(dict, None): extra data, key can be:
119 - thumb_url: http(s) URL of a thumbnail 118 - thumb_url: http(s) URL of a thumbnail
120 """ 119 """
121 if extra is None: 120 if extra is None:
122 extra = {} 121 extra = {}
123 yield self.createNode(client) 122 await self.createNode(client)
124 interest_elt = domish.Element((NS_LIST_INTEREST, "interest")) 123 interest_elt = domish.Element((NS_LIST_INTEREST, "interest"))
125 interest_elt["namespace"] = namespace 124 interest_elt["namespace"] = namespace
126 if name is not None: 125 if name is not None:
127 interest_elt['name'] = name 126 interest_elt['name'] = name
128 thumb_url = extra.get('thumb_url') 127 thumb_url = extra.get('thumb_url')
144 if item_id: 143 if item_id:
145 uri_kwargs['id'] = item_id 144 uri_kwargs['id'] = item_id
146 interest_uri = uri.buildXMPPUri("pubsub", **uri_kwargs) 145 interest_uri = uri.buildXMPPUri("pubsub", **uri_kwargs)
147 # we use URI of the interest as item id to avoid duplicates 146 # we use URI of the interest as item id to avoid duplicates
148 item_elt = pubsub.Item(interest_uri, payload=interest_elt) 147 item_elt = pubsub.Item(interest_uri, payload=interest_elt)
149 yield self._p.publish( 148 await self._p.publish(
150 client, client.jid.userhostJID(), NS_LIST_INTEREST, items=[item_elt] 149 client, client.jid.userhostJID(), NS_LIST_INTEREST, items=[item_elt]
151 ) 150 )
152 151
153 def _registerFileSharing( 152 def _registerFileSharing(
154 self, service, repos_type, namespace, path, name, extra_raw, 153 self, service, repos_type, namespace, path, name, extra_raw,
256 def _listInterests(self, service, node, namespace, profile): 255 def _listInterests(self, service, node, namespace, profile):
257 service = jid.JID(service) if service else None 256 service = jid.JID(service) if service else None
258 node = node or None 257 node = node or None
259 namespace = namespace or None 258 namespace = namespace or None
260 client = self.host.getClient(profile) 259 client = self.host.getClient(profile)
261 d = self.listInterests(client, service, node, namespace) 260 d = defer.ensureDeferred(self.listInterests(client, service, node, namespace))
262 d.addCallback(self._listInterestsSerialise) 261 d.addCallback(self._listInterestsSerialise)
263 return d 262 return d
264 263
265 @defer.inlineCallbacks 264 async def listInterests(self, client, service=None, node=None, namespace=None):
266 def listInterests(self, client, service=None, node=None, namespace=None):
267 """Retrieve list of interests 265 """Retrieve list of interests
268 266
269 @param service(jid.JID, None): service to use 267 @param service(jid.JID, None): service to use
270 None to use own PEP 268 None to use own PEP
271 @param node(unicode, None): node to use 269 @param node(unicode, None): node to use
275 @return: same as [XEP_0060.getItems] 273 @return: same as [XEP_0060.getItems]
276 """ 274 """
277 # TODO: if a MAM filter were available, it would improve performances 275 # TODO: if a MAM filter were available, it would improve performances
278 if not node: 276 if not node:
279 node = NS_LIST_INTEREST 277 node = NS_LIST_INTEREST
280 items, metadata = yield self._p.getItems(client, service, node) 278 items, metadata = await self._p.getItems(client, service, node)
281 if namespace is not None: 279 if namespace is not None:
282 filtered_items = [] 280 filtered_items = []
283 for item in items: 281 for item in items:
284 try: 282 try:
285 interest_elt = next(item.elements(NS_LIST_INTEREST, "interest")) 283 interest_elt = next(item.elements(NS_LIST_INTEREST, "interest"))
289 continue 287 continue
290 if interest_elt.getAttribute("namespace") == namespace: 288 if interest_elt.getAttribute("namespace") == namespace:
291 filtered_items.append(item) 289 filtered_items.append(item)
292 items = filtered_items 290 items = filtered_items
293 291
294 defer.returnValue((items, metadata)) 292 return (items, metadata)
295 293
296 def _interestRetract(self, service_s, item_id, profile_key): 294 def _interestRetract(self, service_s, item_id, profile_key):
297 d = self._p._retractItem( 295 d = self._p._retractItem(
298 service_s, NS_LIST_INTEREST, item_id, True, profile_key) 296 service_s, NS_LIST_INTEREST, item_id, True, profile_key)
299 d.addCallback(lambda __: None) 297 d.addCallback(lambda __: None)