comparison idavoll/backend.py @ 49:94e4ede2a357

Implement SubscriptionService with separate storage.
author Ralph Meijer <ralphm@ik.nu>
date Wed, 03 Nov 2004 16:16:27 +0000
parents bc7438476a67
children e602ddda2d6e
comparison
equal deleted inserted replaced
48:671ead538758 49:94e4ede2a357
1 from twisted.protocols.jabber import jid
1 from twisted.python import components 2 from twisted.python import components
2 from twisted.application import service 3 from twisted.application import service
3 from twisted.xish import utility 4 from twisted.xish import utility
4 from twisted.internet import defer 5 from twisted.internet import defer
5 6
154 d.addErrback(lambda x: x.value[0]) 155 d.addErrback(lambda x: x.value[0])
155 d.addCallback(self._do_publish, node_id, items, requestor) 156 d.addCallback(self._do_publish, node_id, items, requestor)
156 return d 157 return d
157 158
158 def _do_publish(self, result, node_id, items, requestor): 159 def _do_publish(self, result, node_id, items, requestor):
159 print result
160 configuration = result[0][1] 160 configuration = result[0][1]
161 persist_items = configuration["persist_items"] 161 persist_items = configuration["persist_items"]
162 deliver_payloads = configuration["deliver_payloads"] 162 deliver_payloads = configuration["deliver_payloads"]
163 affiliation = result[1][1] 163 affiliation = result[1][1]
164 164
210 return list 210 return list
211 211
212 def register_notifier(self, observerfn, *args, **kwargs): 212 def register_notifier(self, observerfn, *args, **kwargs):
213 self.parent.addObserver('//event/pubsub/notify', observerfn, 213 self.parent.addObserver('//event/pubsub/notify', observerfn,
214 *args, **kwargs) 214 *args, **kwargs)
215
216 class SubscriptionService(service.Service):
217
218 __implements__ = ISubscriptionService,
219
220 def subscribe(self, node_id, subscriber, requestor):
221 if subscriber.userhostJID() != requestor:
222 raise NotAuthorized
223
224 d1 = self.parent.storage.get_node_configuration(node_id)
225 d2 = self.parent.storage.get_affiliation(node_id, subscriber.full())
226 d = defer.DeferredList([d1, d2], fireOnOneErrback=1)
227 d.addErrback(lambda x: x.value[0])
228 d.addCallback(self._do_subscribe, node_id, subscriber)
229 return d
230
231 def _do_subscribe(self, result, node_id, subscriber):
232 configuration = result[0][1]
233 affiliation = result[1][1]
234
235 if affiliation == 'outcast':
236 raise NotAuthorized
237
238 d = self.parent.storage.add_subscription(node_id, subscriber.full(),
239 'subscribed')
240 d.addCallback(self._return_subscription, affiliation)
241 return d
242
243 def _return_subscription(self, result, affiliation):
244 result['affiliation'] = affiliation
245 result['jid'] = jid.JID(result['jid'])
246 return result
247
248 def unsubscribe(self, node_id, subscriber, requestor):
249 if subscriber.userhostJID() != requestor:
250 raise NotAuthorized
251
252 d = self.parent.storage.get_node_configuration(node_id)
253 d.addCallback(self._do_unsubscribe, node_id, subscriber)
254 return d
255
256 def _do_unsubscribe(self, result, node_id, subscriber):
257 return self.parent.storage.remove_subscription(node_id,
258 subscriber.full())