comparison idavoll/pubsub.py @ 4:ea195dc1732d

Allow publication of more than 1 item. Use mapping between backend exceptions and jabber error replies.
author Ralph Meijer <ralphm@ik.nu>
date Sun, 27 Jun 2004 14:07:12 +0000
parents 9701df89c534
children a8cfb31dc50c
comparison
equal deleted inserted replaced
3:32e1561cd68e 4:ea195dc1732d
14 PUBSUB_GET = IQ_GET + PUBSUB_ELEMENT 14 PUBSUB_GET = IQ_GET + PUBSUB_ELEMENT
15 PUBSUB_SET = IQ_SET + PUBSUB_ELEMENT 15 PUBSUB_SET = IQ_SET + PUBSUB_ELEMENT
16 PUBSUB_CREATE = PUBSUB_SET + '/create' 16 PUBSUB_CREATE = PUBSUB_SET + '/create'
17 PUBSUB_PUBLISH = PUBSUB_SET + '/publish' 17 PUBSUB_PUBLISH = PUBSUB_SET + '/publish'
18 18
19 error_map = {
20 backend.NotAuthorized: 'not-authorized',
21 backend.NodeNotFound: 'item-not-found',
22 backend.NoPayloadAllowed: 'bad-request',
23 backend.EmptyPayloadExpected: 'bad-request',
24 backend.PayloadExpected: 'bad-request',
25 }
26
19 class ComponentServiceFromBackend(component.Service, utility.EventDispatcher): 27 class ComponentServiceFromBackend(component.Service, utility.EventDispatcher):
20 28
21 def __init__(self, backend): 29 def __init__(self, backend):
22 utility.EventDispatcher.__init__(self) 30 utility.EventDispatcher.__init__(self)
23 self.backend = backend 31 self.backend = backend
27 def componentConnected(self, xmlstream): 35 def componentConnected(self, xmlstream):
28 xmlstream.addObserver(PUBSUB_SET, self.onPubSub) 36 xmlstream.addObserver(PUBSUB_SET, self.onPubSub)
29 xmlstream.addObserver(PUBSUB_GET, self.onPubSub) 37 xmlstream.addObserver(PUBSUB_GET, self.onPubSub)
30 38
31 def error(self, failure, iq): 39 def error(self, failure, iq):
32 r = failure.trap(backend.NotAuthorized, backend.NodeNotFound) 40 r = failure.trap(*error_map.keys())
33 41
34 if r == backend.NotAuthorized: 42 xmpp_error.error_from_iq(iq, error_map[r], failure.value.msg)
35 xmpp_error.error_from_iq(iq, 'not-authorized', failure.value.msg)
36
37 if r == backend.NodeNotFound:
38 xmpp_error.error_from_iq(iq, 'item-not-found', failure.value.msg)
39 43
40 return iq 44 return iq
41 45
42 def success(self, result, iq): 46 def success(self, result, iq):
43 iq.swapAttributeValues("to", "from") 47 iq.swapAttributeValues("to", "from")
50 iq.handled = True 54 iq.handled = True
51 55
52 def onPublish(self, iq): 56 def onPublish(self, iq):
53 node = iq.pubsub.publish["node"] 57 node = iq.pubsub.publish["node"]
54 58
55 d = self.backend.do_publish(node, jid.JID(iq["from"]).userhost(), iq.pubsub.publish.item) 59 items = []
60 for child in iq.pubsub.publish.children:
61 if child.__class__ == domish.Element and child.name == 'item':
62 items.append(child)
63
64 print items
65
66 d = self.backend.do_publish(node, jid.JID(iq["from"]).userhost(), items)
56 d.addCallback(self.success, iq) 67 d.addCallback(self.success, iq)
57 d.addErrback(self.error, iq) 68 d.addErrback(self.error, iq)
58 d.addCallback(self.send) 69 d.addCallback(self.send)
59 70
60 def do_notification(self, recipients, node, item): 71 def do_notification(self, list, node):
61 72
62 for recipient in recipients: 73 for recipient, items in list.items():
63 self.notify(node, item, recipient) 74 self.notify(node, items, recipient)
64 75
65 def notify(self, node, item, recipient): 76 def notify(self, node, itemlist, recipient):
66 message = domish.Element((NS_COMPONENT, "message")) 77 message = domish.Element((NS_COMPONENT, "message"))
67 message["from"] = self.parent.jabberId 78 message["from"] = self.parent.jabberId
68 message["to"] = recipient 79 message["to"] = recipient
69 x = message.addElement((NS_PUBSUB_EVENT, "x"), NS_PUBSUB_EVENT) 80 x = message.addElement((NS_PUBSUB_EVENT, "x"), NS_PUBSUB_EVENT)
70 items = x.addElement("items") 81 items = x.addElement("items")
71 items["node"] = node 82 items["node"] = node
72 items.children.append(item) 83 items.children.extend(itemlist)
73 self.send(message) 84 self.send(message)
74 85
75 """ 86 """
76 def onCreateSet(self, iq): 87 def onCreateSet(self, iq):
77 node = iq.pubsub.create["node"] 88 node = iq.pubsub.create["node"]