comparison idavoll/memory_backend.py @ 50:64f0986d8b35

Implement add_subscription() and remove_subscription() for Storage, remove implementation from BackendService. Make SubscriptionService subclass of backend.SubscriptionService. Fix create_node() implementation so it works with the backend (still needs factoring out to benefit other storages).
author Ralph Meijer <ralphm@ik.nu>
date Wed, 03 Nov 2004 16:19:17 +0000
parents 979e53b54267
children 62fdb37234e5
comparison
equal deleted inserted replaced
49:94e4ede2a357 50:64f0986d8b35
63 return defer.succeed(subscribers) 63 return defer.succeed(subscribers)
64 64
65 def store_items(self, node_id, items, publisher): 65 def store_items(self, node_id, items, publisher):
66 for item in items: 66 for item in items:
67 self.nodes[node_id].items[item["id"]] = (item, publisher) 67 self.nodes[node_id].items[item["id"]] = (item, publisher)
68 print self.nodes[node_id].items
69 return defer.succeed(None) 68 return defer.succeed(None)
70 69
71 class BackendService(backend.BackendService): 70 def add_subscription(self, node_id, subscriber, state):
72
73 def create_node(self, node_id, requestor):
74 if not node_id:
75 raise backend.NoInstantNodes
76
77 if node_id in self.nodes:
78 raise backend.NodeExists
79
80 node = Node(node_id)
81 node.affiliations[requestor.full()] = 'owner'
82 self.nodes[node_id] = node
83
84 return defer.succeed({'node_id': node.id})
85
86 def subscribe(self, node_id, subscriber, requestor):
87 # expect subscriber and requestor to be a jid.JID
88 try: 71 try:
89 node = self.nodes[node_id] 72 node = self.nodes[node_id]
90 except KeyError: 73 except KeyError:
91 raise backend.NodeNotFound 74 raise backend.NodeNotFound
92 75
93 if subscriber.userhostJID() != requestor: 76 try:
94 raise backend.NotAuthorized 77 subscription = node.subscriptions[subscriber]
78 except:
79 subscription = Subscription(state)
80 node.subscriptions[subscriber] = subscription
95 81
96 affiliation = node.affiliations.get(subscriber.full(), 'none') 82 return defer.succeed({'node': node_id,
83 'jid': subscriber,
84 'subscription': subscription.state})
97 85
98 if affiliation == 'outcast': 86 def remove_subscription(self, node_id, subscriber):
99 raise backend.NotAuthorized
100
101 try:
102 subscription = node.subscriptions[subscriber.full()]
103 except KeyError:
104 subscription = Subscription('subscribed')
105 node.subscriptions[subscriber.full()] = subscription
106
107 print node.subscriptions
108
109 return defer.succeed({
110 'affiliation': affiliation,
111 'node': node_id,
112 'jid': subscriber,
113 'subscription': subscription.state})
114
115 def unsubscribe(self, node_id, subscriber, requestor):
116 try: 87 try:
117 node = self.nodes[node_id] 88 node = self.nodes[node_id]
118 except KeyError: 89 except KeyError:
119 raise backend.NodeNotFound 90 raise backend.NodeNotFound
120 91
121 if subscriber.userhostJID() != requestor:
122 raise backend.NotAuthorized
123
124 try: 92 try:
125 del node.subscriptions[subscriber.full()] 93 del node.subscriptions[subscriber]
126 except KeyError: 94 except KeyError:
127 raise backend.NotSubscribed 95 raise backend.NotSubscribed
128 96
129 return defer.succeed(None) 97 return defer.succeed(None)
98
99 class BackendService(backend.BackendService):
100 pass
130 101
131 class NodeCreationService(service.Service): 102 class NodeCreationService(service.Service):
132 103
133 __implements__ = backend.INodeCreationService, 104 __implements__ = backend.INodeCreationService,
134 105
135 def create_node(self, node_id, requestor): 106 def create_node(self, node_id, requestor):
136 return self.parent.create_node(node_id, requestor) 107 if not node_id:
108 raise backend.NoInstantNodes
109
110 if node_id in self.parent.storage.nodes:
111 raise backend.NodeExists
112
113 node = Node(node_id)
114 node.affiliations[requestor.full()] = 'owner'
115 self.parent.storage.nodes[node_id] = node
116
117 return defer.succeed({'node_id': node.id})
137 118
138 class PublishService(backend.PublishService): 119 class PublishService(backend.PublishService):
139 pass 120 pass
140 121
141 class NotificationService(backend.NotificationService): 122 class NotificationService(backend.NotificationService):
142 pass 123 pass
143 124
144 class SubscriptionService(service.Service): 125 class SubscriptionService(backend.SubscriptionService):
145 126 pass
146 __implements__ = backend.ISubscriptionService,
147
148 def subscribe(self, node_id, subscriber, requestor):
149 return self.parent.subscribe(node_id, subscriber, requestor)
150
151 def unsubscribe(self, node_id, subscriber, requestor):
152 return self.parent.unsubscribe(node_id, subscriber, requestor)