comparison src/plugins/plugin_misc_groupblog.py @ 467:47af60767013

plugin group blog: getMassiveGroupBlog first draft
author Goffi <goffi@goffi.org>
date Thu, 29 Mar 2012 00:04:31 +0200
parents 78e67a59d51d
children 5c916b99d0f6
comparison
equal deleted inserted replaced
466:448ce3c9e2ac 467:47af60767013
85 'param_0':'jid: publisher of wanted microblog', 85 'param_0':'jid: publisher of wanted microblog',
86 'param_1':'max_items: see XEP-0060 #6.5.7', 86 'param_1':'max_items: see XEP-0060 #6.5.7',
87 'param_2':'%(doc_profile)s', 87 'param_2':'%(doc_profile)s',
88 'return':'list of microblog data (dict)' 88 'return':'list of microblog data (dict)'
89 }) 89 })
90
91 host.bridge.addMethod("getMassiveLastGroupBlogs", ".plugin",
92 in_sign='sasis', out_sign='a{saa{ss}}',
93 method=self.getMassiveLastGroupBlogs,
94 async = True)
95
90 96
91 @defer.inlineCallbacks 97 @defer.inlineCallbacks
92 def initialise(self, profile_key): 98 def initialise(self, profile_key):
93 """Check that this data for this profile are initialised, and do it else 99 """Check that this data for this profile are initialised, and do it else
94 @param client: client of the profile 100 @param client: client of the profile
115 if set([NS_PUBSUB_ITEM_ACCESS, NS_PUBSUB_AUTO_CREATE, NS_PUBSUB_CREATOR_JID_CHECK]).issubset(disco.features): 121 if set([NS_PUBSUB_ITEM_ACCESS, NS_PUBSUB_AUTO_CREATE, NS_PUBSUB_CREATOR_JID_CHECK]).issubset(disco.features):
116 info(_("item-access powered pubsub service found: [%s]") % entity.full()) 122 info(_("item-access powered pubsub service found: [%s]") % entity.full())
117 client.item_access_pubsub = entity 123 client.item_access_pubsub = entity
118 124
119 if not client.item_access_pubsub: 125 if not client.item_access_pubsub:
120 error(_("No item-access powered pubsub server found, can't post group blog")) 126 error(_("No item-access powered pubsub server found, can't use group blog"))
121 raise NoCompatiblePubSubServerFound 127 raise NoCompatiblePubSubServerFound
122 128
123 defer.returnValue((profile, client)) 129 defer.returnValue((profile, client))
124 130
125 131
178 184
179 assert(callback) 185 assert(callback)
180 self.initialise(profile_key).addCallback(initialised) 186 self.initialise(profile_key).addCallback(initialised)
181 #TODO: we need to use the server corresponding the the host of the jid 187 #TODO: we need to use the server corresponding the the host of the jid
182 188
189 def getMassiveLastGroupBlogs(self, publishers_type, publishers, max_items=10, profile_key='@DEFAULT@', callback=None, errback=None):
190 """Get the last published microblogs for a list of groups or jids
191 @param publishers_type: type of the list of publishers (one of "GROUP" or "JID" or "ALL")
192 @param publishers: list of publishers, according to "publishers_type" (list of groups or list of jids)
193 @param max_items: how many microblogs we want to get
194 @param profile_key: profile key
195 @param callback: used for the async answer
196 @param errback: used for the async answer
197 """
198 def sendResult(result):
199 """send result of DeferredList (list of microblogs to the calling method"""
200
201 ret = {}
202
203 for (success, value) in result:
204 if success:
205 source_jid, data = value
206 ret[source_jid] = data
207
208 callback(ret)
209
210 def initialised(result):
211 profile, client = result
212
213 if publishers_type == "ALL":
214 contacts = client.roster.getItems()
215 print "contacts:", contacts
216 jids = [contact.jid.userhost() for contact in contacts]
217 print "jids:", jids
218 mblogs = []
219 for _jid in jids:
220 d = self.host.plugins["XEP-0060"].getItems(client.item_access_pubsub, jid.JID(_jid).userhost(),
221 max_items=max_items, profile_key=profile_key)
222 #TODO: check empty result (nothing published so far)
223 d.addCallback(lambda items, source_jid: (source_jid, map(self.host.plugins["XEP-0277"].item2mbdata, items)), _jid)
224 mblogs.append(d)
225 dlist = defer.DeferredList(mblogs)
226 dlist.addCallback(sendResult)
227
228 #d = self.host.plugins["XEP-0060"].getItems(client.item_access_pubsub, jid.JID(pub_jid).userhost(),
229 # max_items=max_items, profile_key=profile_key)
230 #d.addCallbacks(lambda items: callback(map(self.host.plugins["XEP-0277"].item2mbdata, items)), errback)
231
232 assert(callback)
233 #TODO: custom exception
234 if publishers_type not in ["GROUP", "JID", "ALL"]:
235 raise Exception("Bad call, unknown publishers_type")
236 if publishers_type=="ALL" and publishers:
237 raise Exception("Publishers list must be empty when getting microblogs for all contacts")
238 return self.initialise(profile_key).addCallback(initialised)
239 #TODO: we need to use the server corresponding the the host of the jid
240