comparison sat/plugins/plugin_misc_forums.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 3fd60beb9b92
children 6c5f0fbc519b
comparison
equal deleted inserted replaced
3583:16ade4ad63f3 3584:edc79cefe968
187 service = jid.JID(service) 187 service = jid.JID(service)
188 else: 188 else:
189 service = None 189 service = None
190 if not node.strip(): 190 if not node.strip():
191 node = None 191 node = None
192 d=self.get(client, service, node, forums_key or None) 192 d = defer.ensureDeferred(self.get(client, service, node, forums_key or None))
193 d.addCallback(lambda data: json.dumps(data)) 193 d.addCallback(lambda data: json.dumps(data))
194 return d 194 return d
195 195
196 @defer.inlineCallbacks 196 async def get(self, client, service=None, node=None, forums_key=None):
197 def get(self, client, service=None, node=None, forums_key=None):
198 if service is None: 197 if service is None:
199 service = client.pubsub_service 198 service = client.pubsub_service
200 if node is None: 199 if node is None:
201 node = NS_FORUMS 200 node = NS_FORUMS
202 if forums_key is None: 201 if forums_key is None:
203 forums_key = 'default' 202 forums_key = 'default'
204 items_data = yield self._p.getItems(client, service, node, item_ids=[forums_key]) 203 items_data = await self._p.getItems(client, service, node, item_ids=[forums_key])
205 item = items_data[0][0] 204 item = items_data[0][0]
206 # we have the item and need to convert it to json 205 # we have the item and need to convert it to json
207 forums = self._parseForums(item) 206 forums = self._parseForums(item)
208 defer.returnValue(forums) 207 return forums
209 208
210 def _set(self, forums, service=None, node=None, forums_key=None, profile_key=C.PROF_KEY_NONE): 209 def _set(self, forums, service=None, node=None, forums_key=None, profile_key=C.PROF_KEY_NONE):
211 client = self.host.getClient(profile_key) 210 client = self.host.getClient(profile_key)
212 forums = json.loads(forums) 211 forums = json.loads(forums)
213 if service.strip(): 212 if service.strip():
214 service = jid.JID(service) 213 service = jid.JID(service)
215 else: 214 else:
216 service = None 215 service = None
217 if not node.strip(): 216 if not node.strip():
218 node = None 217 node = None
219 return self.set(client, forums, service, node, forums_key or None) 218 return defer.ensureDeferred(
220 219 self.set(client, forums, service, node, forums_key or None)
221 @defer.inlineCallbacks 220 )
222 def set(self, client, forums, service=None, node=None, forums_key=None): 221
222 async def set(self, client, forums, service=None, node=None, forums_key=None):
223 """Create or replace forums structure 223 """Create or replace forums structure
224 224
225 @param forums(list): list of dictionary as follow: 225 @param forums(list): list of dictionary as follow:
226 a dictionary represent a forum metadata, with the following keys: 226 a dictionary represent a forum metadata, with the following keys:
227 - title: title of the forum 227 - title: title of the forum
240 service = client.pubsub_service 240 service = client.pubsub_service
241 if node is None: 241 if node is None:
242 node = NS_FORUMS 242 node = NS_FORUMS
243 if forums_key is None: 243 if forums_key is None:
244 forums_key = 'default' 244 forums_key = 'default'
245 forums_elt = yield self._createForums(client, forums, service, node) 245 forums_elt = await self._createForums(client, forums, service, node)
246 yield self._p.sendItem(client, service, node, forums_elt, item_id=forums_key) 246 return await self._p.sendItem(
247 client, service, node, forums_elt, item_id=forums_key
248 )
247 249
248 def _getTopics(self, service, node, extra=None, profile_key=C.PROF_KEY_NONE): 250 def _getTopics(self, service, node, extra=None, profile_key=C.PROF_KEY_NONE):
249 client = self.host.getClient(profile_key) 251 client = self.host.getClient(profile_key)
250 extra = self._p.parseExtra(extra) 252 extra = self._p.parseExtra(extra)
251 d = self.getTopics(client, jid.JID(service), node, rsm_request=extra.rsm_request, extra=extra.extra) 253 d = defer.ensureDeferred(
254 self.getTopics(
255 client, jid.JID(service), node, rsm_request=extra.rsm_request,
256 extra=extra.extra
257 )
258 )
252 d.addCallback( 259 d.addCallback(
253 lambda topics_data: (topics_data[0], data_format.serialise(topics_data[1])) 260 lambda topics_data: (topics_data[0], data_format.serialise(topics_data[1]))
254 ) 261 )
255 return d 262 return d
256 263
257 @defer.inlineCallbacks 264 async def getTopics(self, client, service, node, rsm_request=None, extra=None):
258 def getTopics(self, client, service, node, rsm_request=None, extra=None):
259 """Retrieve topics data 265 """Retrieve topics data
260 266
261 Topics are simple microblog URIs with some metadata duplicated from first post 267 Topics are simple microblog URIs with some metadata duplicated from first post
262 """ 268 """
263 topics_data = yield self._p.getItems(client, service, node, rsm_request=rsm_request, extra=extra) 269 topics_data = await self._p.getItems(
270 client, service, node, rsm_request=rsm_request, extra=extra
271 )
264 topics = [] 272 topics = []
265 item_elts, metadata = topics_data 273 item_elts, metadata = topics_data
266 for item_elt in item_elts: 274 for item_elt in item_elts:
267 topic_elt = next(item_elt.elements(NS_FORUMS, 'topic')) 275 topic_elt = next(item_elt.elements(NS_FORUMS, 'topic'))
268 title_elt = next(topic_elt.elements(NS_FORUMS, 'title')) 276 title_elt = next(topic_elt.elements(NS_FORUMS, 'title'))
269 topic = {'uri': topic_elt['uri'], 277 topic = {'uri': topic_elt['uri'],
270 'author': topic_elt['author'], 278 'author': topic_elt['author'],
271 'title': str(title_elt)} 279 'title': str(title_elt)}
272 topics.append(topic) 280 topics.append(topic)
273 defer.returnValue((topics, metadata)) 281 return (topics, metadata)
274 282
275 def _createTopic(self, service, node, mb_data, profile_key): 283 def _createTopic(self, service, node, mb_data, profile_key):
276 client = self.host.getClient(profile_key) 284 client = self.host.getClient(profile_key)
277 return defer.ensureDeferred( 285 return defer.ensureDeferred(
278 self.createTopic(client, jid.JID(service), node, mb_data) 286 self.createTopic(client, jid.JID(service), node, mb_data)