comparison src/plugins/plugin_xep_0060.py @ 1268:bb30bf3ae932

plugins XEP-0060, XEP-0277, groupblog: make use of RSM (XEP-0059)
author souliane <souliane@mailoo.org>
date Mon, 15 Dec 2014 14:04:19 +0100
parents ea692d51a0ee
children 74d558e6c9fd
comparison
equal deleted inserted replaced
1267:ea692d51a0ee 1268:bb30bf3ae932
24 from sat.memory.memory import Sessions 24 from sat.memory.memory import Sessions
25 25
26 from wokkel import disco, pubsub, rsm 26 from wokkel import disco, pubsub, rsm
27 from zope.interface import implements 27 from zope.interface import implements
28 from twisted.internet import defer 28 from twisted.internet import defer
29 import uuid
29 30
30 31
31 PLUGIN_INFO = { 32 PLUGIN_INFO = {
32 "name": "Publish-Subscribe", 33 "name": "Publish-Subscribe",
33 "import_name": "XEP-0060", 34 "import_name": "XEP-0060",
152 153
153 def publish(self, service, nodeIdentifier, items=None, profile_key=C.PROF_KEY_NONE): 154 def publish(self, service, nodeIdentifier, items=None, profile_key=C.PROF_KEY_NONE):
154 profile, client = self.__getClientNProfile(profile_key, 'publish item') 155 profile, client = self.__getClientNProfile(profile_key, 'publish item')
155 return client.publish(service, nodeIdentifier, items, client.parent.jid) 156 return client.publish(service, nodeIdentifier, items, client.parent.jid)
156 157
157 def getItems(self, service, node, max_items=None, item_ids=None, sub_id=None, profile_key=C.PROF_KEY_NONE): 158 def getItems(self, service, node, max_items=None, item_ids=None, sub_id=None, rsm=None, profile_key=C.PROF_KEY_NONE):
159 """Retrieve pubsub items from a node.
160
161 @param service (JID): target service.
162 @param node (str): node id.
163 @param max_items (int): optional limit on the number of retrieved items.
164 @param item_ids (list[str]): identifiers of the items to be retrieved (should not be used).
165 @param sub_id (str): optional subscription identifier.
166 @param rsm (dict): RSM request data
167 @param profile_key (str): %(doc_profile_key)s
168 @return: a deferred couple (list[dict], dict) containing:
169 - list of items
170 - RSM response data
171 """
158 profile, client = self.__getClientNProfile(profile_key, 'get items') 172 profile, client = self.__getClientNProfile(profile_key, 'get items')
159 return client.items(service, node, max_items, item_ids, sub_id, client.parent.jid) 173 ext_data = {'id': unicode(uuid.uuid4()), 'rsm': rsm} if rsm else None
174 d = client.items(service, node, max_items, item_ids, sub_id, client.parent.jid, ext_data)
175 d.addCallback(lambda items: (items, client.getRSMResponse(ext_data['id']) if rsm else {}))
176 return d
160 177
161 @defer.inlineCallbacks 178 @defer.inlineCallbacks
162 def getItemsFromMany(self, service, data, max_items=None, item_ids=None, sub_id=None, profile_key=C.PROF_KEY_NONE): 179 def getItemsFromMany(self, service, data, max_items=None, sub_id=None, rsm=None, profile_key=C.PROF_KEY_NONE):
163 """Massively retrieve pubsub items from many nodes. 180 """Massively retrieve pubsub items from many nodes.
164 181
165 @param service (JID): target service. 182 @param service (JID): target service.
166 @param data (dict): dictionnary binding some arbitrary keys to the node identifiers. 183 @param data (dict): dictionnary binding some arbitrary keys to the node identifiers.
167 @param max_items (int): optional limit on the number of retrieved items *per node*. 184 @param max_items (int): optional limit on the number of retrieved items *per node*.
168 @param item_ids (list[str]): identifiers of the items to be retrieved (should not be used).
169 @param sub_id (str): optional subscription identifier. 185 @param sub_id (str): optional subscription identifier.
186 @param rsm (dict): RSM request data
170 @param profile_key (str): %(doc_profile_key)s 187 @param profile_key (str): %(doc_profile_key)s
171 @return: dict binding a subset of the keys of data to Deferred instances. 188 @return: a deferred dict with:
189 - key: a value in (a subset of) data.keys()
190 - couple (list[dict], dict) containing:
191 - list of items
192 - RSM response data
172 """ 193 """
173 profile, client = self.__getClientNProfile(profile_key, 'get items') 194 profile, client = self.__getClientNProfile(profile_key, 'get items')
174 found_nodes = yield self.listNodes(service, profile=profile) 195 found_nodes = yield self.listNodes(service, profile=profile)
175 d_dict = {} 196 d_dict = {}
176 for publisher, node in data.items(): 197 for publisher, node in data.items():
177 if node not in found_nodes: 198 if node not in found_nodes:
178 log.debug("Skip the items retrieval for [{node}]: node doesn't exist".format(node=node)) 199 log.debug("Skip the items retrieval for [{node}]: node doesn't exist".format(node=node))
179 continue # avoid pubsub "item-not-found" error 200 continue # avoid pubsub "item-not-found" error
180 d_dict[publisher] = client.items(service, node, max_items, item_ids, sub_id, client.parent.jid) 201 d_dict[publisher] = self.getItems(service, node, max_items, None, sub_id, rsm, profile)
181 defer.returnValue(d_dict) 202 defer.returnValue(d_dict)
182 203
183 def getOptions(self, service, nodeIdentifier, subscriber, subscriptionIdentifier=None, profile_key=C.PROF_KEY_NONE): 204 def getOptions(self, service, nodeIdentifier, subscriber, subscriptionIdentifier=None, profile_key=C.PROF_KEY_NONE):
184 profile, client = self.__getClientNProfile(profile_key, 'get options') 205 profile, client = self.__getClientNProfile(profile_key, 'get options')
185 return client.getOptions(service, nodeIdentifier, subscriber, subscriptionIdentifier) 206 return client.getOptions(service, nodeIdentifier, subscriber, subscriptionIdentifier)