annotate idavoll/backend.py @ 106:dc36882d2620

Also move from twisted.python.components to zope.interface.Interface
author Ralph Meijer <ralphm@ik.nu>
date Fri, 08 Apr 2005 10:08:41 +0000
parents 8d8946e67fcb
children 1c18759d2afb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
105
8d8946e67fcb Move to Twisted 2.0.
Ralph Meijer <ralphm@ik.nu>
parents: 101
diff changeset
1 from twisted.words.protocols.jabber import jid
29
d4fc29bb5381 Define interfaces of blocks of functionality.
Ralph Meijer <ralphm@ik.nu>
parents: 24
diff changeset
2 from twisted.application import service
d4fc29bb5381 Define interfaces of blocks of functionality.
Ralph Meijer <ralphm@ik.nu>
parents: 24
diff changeset
3 from twisted.xish import utility
43
9685b7e291ef Moved common stuff out of pgsql_backend.py to backend.py.
Ralph Meijer <ralphm@ik.nu>
parents: 39
diff changeset
4 from twisted.internet import defer
106
dc36882d2620 Also move from twisted.python.components to zope.interface.Interface
Ralph Meijer <ralphm@ik.nu>
parents: 105
diff changeset
5 from zope.interface import Interface, implements
84
34be83a0bd2e Implement instant nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 80
diff changeset
6 import sha
34be83a0bd2e Implement instant nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 80
diff changeset
7 import time
2
9701df89c534 First take at notifications
Ralph Meijer <ralphm@ik.nu>
parents: 1
diff changeset
8
24
4f3bbefc6fad Moved memory backend to its own file.
Ralph Meijer <ralphm@ik.nu>
parents: 23
diff changeset
9 class Error(Exception):
29
d4fc29bb5381 Define interfaces of blocks of functionality.
Ralph Meijer <ralphm@ik.nu>
parents: 24
diff changeset
10 msg = ''
1
4cc41776b7d7 Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
11
29
d4fc29bb5381 Define interfaces of blocks of functionality.
Ralph Meijer <ralphm@ik.nu>
parents: 24
diff changeset
12 def __str__(self):
d4fc29bb5381 Define interfaces of blocks of functionality.
Ralph Meijer <ralphm@ik.nu>
parents: 24
diff changeset
13 return self.msg
d4fc29bb5381 Define interfaces of blocks of functionality.
Ralph Meijer <ralphm@ik.nu>
parents: 24
diff changeset
14
24
4f3bbefc6fad Moved memory backend to its own file.
Ralph Meijer <ralphm@ik.nu>
parents: 23
diff changeset
15 class NodeNotFound(Error):
29
d4fc29bb5381 Define interfaces of blocks of functionality.
Ralph Meijer <ralphm@ik.nu>
parents: 24
diff changeset
16 msg = 'Node not found'
1
4cc41776b7d7 Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
17
24
4f3bbefc6fad Moved memory backend to its own file.
Ralph Meijer <ralphm@ik.nu>
parents: 23
diff changeset
18 class NotAuthorized(Error):
29
d4fc29bb5381 Define interfaces of blocks of functionality.
Ralph Meijer <ralphm@ik.nu>
parents: 24
diff changeset
19 pass
20
eddea65d1032 Added two exceptions: NoInstantNodes and NodeExists.
Ralph Meijer <ralphm@ik.nu>
parents: 18
diff changeset
20
24
4f3bbefc6fad Moved memory backend to its own file.
Ralph Meijer <ralphm@ik.nu>
parents: 23
diff changeset
21 class PayloadExpected(Error):
29
d4fc29bb5381 Define interfaces of blocks of functionality.
Ralph Meijer <ralphm@ik.nu>
parents: 24
diff changeset
22 msg = 'Payload expected'
15
46cd13c68ac0 Redone memory storage of nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 5
diff changeset
23
24
4f3bbefc6fad Moved memory backend to its own file.
Ralph Meijer <ralphm@ik.nu>
parents: 23
diff changeset
24 class NoPayloadAllowed(Error):
29
d4fc29bb5381 Define interfaces of blocks of functionality.
Ralph Meijer <ralphm@ik.nu>
parents: 24
diff changeset
25 msg = 'No payload allowed'
2
9701df89c534 First take at notifications
Ralph Meijer <ralphm@ik.nu>
parents: 1
diff changeset
26
24
4f3bbefc6fad Moved memory backend to its own file.
Ralph Meijer <ralphm@ik.nu>
parents: 23
diff changeset
27 class NoInstantNodes(Error):
29
d4fc29bb5381 Define interfaces of blocks of functionality.
Ralph Meijer <ralphm@ik.nu>
parents: 24
diff changeset
28 pass
23
884268687229 Simplify call chain by mapping incoming requests directly to method
Ralph Meijer <ralphm@ik.nu>
parents: 20
diff changeset
29
24
4f3bbefc6fad Moved memory backend to its own file.
Ralph Meijer <ralphm@ik.nu>
parents: 23
diff changeset
30 class NodeExists(Error):
29
d4fc29bb5381 Define interfaces of blocks of functionality.
Ralph Meijer <ralphm@ik.nu>
parents: 24
diff changeset
31 pass
d4fc29bb5381 Define interfaces of blocks of functionality.
Ralph Meijer <ralphm@ik.nu>
parents: 24
diff changeset
32
d4fc29bb5381 Define interfaces of blocks of functionality.
Ralph Meijer <ralphm@ik.nu>
parents: 24
diff changeset
33 class NotImplemented(Error):
34
b65b7ea5c992 Remove nonsense message.
Ralph Meijer <ralphm@ik.nu>
parents: 29
diff changeset
34 pass
b65b7ea5c992 Remove nonsense message.
Ralph Meijer <ralphm@ik.nu>
parents: 29
diff changeset
35
b65b7ea5c992 Remove nonsense message.
Ralph Meijer <ralphm@ik.nu>
parents: 29
diff changeset
36 class NotSubscribed(Error):
b65b7ea5c992 Remove nonsense message.
Ralph Meijer <ralphm@ik.nu>
parents: 29
diff changeset
37 pass
29
d4fc29bb5381 Define interfaces of blocks of functionality.
Ralph Meijer <ralphm@ik.nu>
parents: 24
diff changeset
38
106
dc36882d2620 Also move from twisted.python.components to zope.interface.Interface
Ralph Meijer <ralphm@ik.nu>
parents: 105
diff changeset
39 class IBackendService(Interface):
29
d4fc29bb5381 Define interfaces of blocks of functionality.
Ralph Meijer <ralphm@ik.nu>
parents: 24
diff changeset
40 """ Interface to a backend service of a pubsub service. """
d4fc29bb5381 Define interfaces of blocks of functionality.
Ralph Meijer <ralphm@ik.nu>
parents: 24
diff changeset
41
d4fc29bb5381 Define interfaces of blocks of functionality.
Ralph Meijer <ralphm@ik.nu>
parents: 24
diff changeset
42 def get_supported_affiliations(self):
d4fc29bb5381 Define interfaces of blocks of functionality.
Ralph Meijer <ralphm@ik.nu>
parents: 24
diff changeset
43 """ Reports the list of supported affiliation types.
d4fc29bb5381 Define interfaces of blocks of functionality.
Ralph Meijer <ralphm@ik.nu>
parents: 24
diff changeset
44
d4fc29bb5381 Define interfaces of blocks of functionality.
Ralph Meijer <ralphm@ik.nu>
parents: 24
diff changeset
45 @return: a list of supported affiliation types.
d4fc29bb5381 Define interfaces of blocks of functionality.
Ralph Meijer <ralphm@ik.nu>
parents: 24
diff changeset
46 """
d4fc29bb5381 Define interfaces of blocks of functionality.
Ralph Meijer <ralphm@ik.nu>
parents: 24
diff changeset
47
106
dc36882d2620 Also move from twisted.python.components to zope.interface.Interface
Ralph Meijer <ralphm@ik.nu>
parents: 105
diff changeset
48 class INodeCreationService(Interface):
29
d4fc29bb5381 Define interfaces of blocks of functionality.
Ralph Meijer <ralphm@ik.nu>
parents: 24
diff changeset
49 """ A service for creating nodes """
d4fc29bb5381 Define interfaces of blocks of functionality.
Ralph Meijer <ralphm@ik.nu>
parents: 24
diff changeset
50
d4fc29bb5381 Define interfaces of blocks of functionality.
Ralph Meijer <ralphm@ik.nu>
parents: 24
diff changeset
51 def create_node(self, node_id, requestor):
d4fc29bb5381 Define interfaces of blocks of functionality.
Ralph Meijer <ralphm@ik.nu>
parents: 24
diff changeset
52 """ Create a node.
d4fc29bb5381 Define interfaces of blocks of functionality.
Ralph Meijer <ralphm@ik.nu>
parents: 24
diff changeset
53
d4fc29bb5381 Define interfaces of blocks of functionality.
Ralph Meijer <ralphm@ik.nu>
parents: 24
diff changeset
54 @return: a deferred that fires when the node has been created.
d4fc29bb5381 Define interfaces of blocks of functionality.
Ralph Meijer <ralphm@ik.nu>
parents: 24
diff changeset
55 """
d4fc29bb5381 Define interfaces of blocks of functionality.
Ralph Meijer <ralphm@ik.nu>
parents: 24
diff changeset
56
106
dc36882d2620 Also move from twisted.python.components to zope.interface.Interface
Ralph Meijer <ralphm@ik.nu>
parents: 105
diff changeset
57 class INodeDeletionService(Interface):
90
59378610b16e Implement node purging and node deletion.
Ralph Meijer <ralphm@ik.nu>
parents: 85
diff changeset
58 """ A service for deleting nodes. """
59378610b16e Implement node purging and node deletion.
Ralph Meijer <ralphm@ik.nu>
parents: 85
diff changeset
59
59378610b16e Implement node purging and node deletion.
Ralph Meijer <ralphm@ik.nu>
parents: 85
diff changeset
60 def register_pre_delete(self, pre_delete_fn):
59378610b16e Implement node purging and node deletion.
Ralph Meijer <ralphm@ik.nu>
parents: 85
diff changeset
61 """ Register a callback that is called just before a node deletion.
59378610b16e Implement node purging and node deletion.
Ralph Meijer <ralphm@ik.nu>
parents: 85
diff changeset
62
59378610b16e Implement node purging and node deletion.
Ralph Meijer <ralphm@ik.nu>
parents: 85
diff changeset
63 The function C{pre_deleted_fn} is added to a list of functions
59378610b16e Implement node purging and node deletion.
Ralph Meijer <ralphm@ik.nu>
parents: 85
diff changeset
64 to be called just before deletion of a node. The callback
59378610b16e Implement node purging and node deletion.
Ralph Meijer <ralphm@ik.nu>
parents: 85
diff changeset
65 C{pre_delete_fn} is called with the C{node_id} that is about to be
59378610b16e Implement node purging and node deletion.
Ralph Meijer <ralphm@ik.nu>
parents: 85
diff changeset
66 deleted and should return a deferred that returns a list of deferreds
59378610b16e Implement node purging and node deletion.
Ralph Meijer <ralphm@ik.nu>
parents: 85
diff changeset
67 that are to be fired after deletion. The backend collects the lists
59378610b16e Implement node purging and node deletion.
Ralph Meijer <ralphm@ik.nu>
parents: 85
diff changeset
68 from all these callbacks before actually deleting the node in question.
59378610b16e Implement node purging and node deletion.
Ralph Meijer <ralphm@ik.nu>
parents: 85
diff changeset
69 After deletion all collected deferreds are fired to do post-processing.
59378610b16e Implement node purging and node deletion.
Ralph Meijer <ralphm@ik.nu>
parents: 85
diff changeset
70
59378610b16e Implement node purging and node deletion.
Ralph Meijer <ralphm@ik.nu>
parents: 85
diff changeset
71 The idea is that you want to be able to collect data from the
59378610b16e Implement node purging and node deletion.
Ralph Meijer <ralphm@ik.nu>
parents: 85
diff changeset
72 node before deleting it, for example to get a list of subscribers
59378610b16e Implement node purging and node deletion.
Ralph Meijer <ralphm@ik.nu>
parents: 85
diff changeset
73 that have to be notified after the node has been deleted. To do this,
59378610b16e Implement node purging and node deletion.
Ralph Meijer <ralphm@ik.nu>
parents: 85
diff changeset
74 C{pre_delete_fn} fetches the subscriber list and passes this
59378610b16e Implement node purging and node deletion.
Ralph Meijer <ralphm@ik.nu>
parents: 85
diff changeset
75 list to a callback attached to a deferred that it sets up. This
59378610b16e Implement node purging and node deletion.
Ralph Meijer <ralphm@ik.nu>
parents: 85
diff changeset
76 deferred is returned in the list of deferreds.
59378610b16e Implement node purging and node deletion.
Ralph Meijer <ralphm@ik.nu>
parents: 85
diff changeset
77 """
59378610b16e Implement node purging and node deletion.
Ralph Meijer <ralphm@ik.nu>
parents: 85
diff changeset
78
59378610b16e Implement node purging and node deletion.
Ralph Meijer <ralphm@ik.nu>
parents: 85
diff changeset
79 def get_subscribers(self, node_id):
59378610b16e Implement node purging and node deletion.
Ralph Meijer <ralphm@ik.nu>
parents: 85
diff changeset
80 """ Get node subscriber list.
59378610b16e Implement node purging and node deletion.
Ralph Meijer <ralphm@ik.nu>
parents: 85
diff changeset
81
59378610b16e Implement node purging and node deletion.
Ralph Meijer <ralphm@ik.nu>
parents: 85
diff changeset
82 @return: a deferred that fires with the list of subscribers.
59378610b16e Implement node purging and node deletion.
Ralph Meijer <ralphm@ik.nu>
parents: 85
diff changeset
83 """
59378610b16e Implement node purging and node deletion.
Ralph Meijer <ralphm@ik.nu>
parents: 85
diff changeset
84
59378610b16e Implement node purging and node deletion.
Ralph Meijer <ralphm@ik.nu>
parents: 85
diff changeset
85 def delete_node(self, node_id, requestor):
59378610b16e Implement node purging and node deletion.
Ralph Meijer <ralphm@ik.nu>
parents: 85
diff changeset
86 """ Delete a node.
59378610b16e Implement node purging and node deletion.
Ralph Meijer <ralphm@ik.nu>
parents: 85
diff changeset
87
59378610b16e Implement node purging and node deletion.
Ralph Meijer <ralphm@ik.nu>
parents: 85
diff changeset
88 @return: a deferred that fires when the node has been deleted.
59378610b16e Implement node purging and node deletion.
Ralph Meijer <ralphm@ik.nu>
parents: 85
diff changeset
89 """
59378610b16e Implement node purging and node deletion.
Ralph Meijer <ralphm@ik.nu>
parents: 85
diff changeset
90
106
dc36882d2620 Also move from twisted.python.components to zope.interface.Interface
Ralph Meijer <ralphm@ik.nu>
parents: 105
diff changeset
91 class IPublishService(Interface):
29
d4fc29bb5381 Define interfaces of blocks of functionality.
Ralph Meijer <ralphm@ik.nu>
parents: 24
diff changeset
92 """ A service for publishing items to a node. """
d4fc29bb5381 Define interfaces of blocks of functionality.
Ralph Meijer <ralphm@ik.nu>
parents: 24
diff changeset
93
d4fc29bb5381 Define interfaces of blocks of functionality.
Ralph Meijer <ralphm@ik.nu>
parents: 24
diff changeset
94 def publish(self, node_id, items, requestor):
d4fc29bb5381 Define interfaces of blocks of functionality.
Ralph Meijer <ralphm@ik.nu>
parents: 24
diff changeset
95 """ Publish items to a pubsub node.
d4fc29bb5381 Define interfaces of blocks of functionality.
Ralph Meijer <ralphm@ik.nu>
parents: 24
diff changeset
96
d4fc29bb5381 Define interfaces of blocks of functionality.
Ralph Meijer <ralphm@ik.nu>
parents: 24
diff changeset
97 @return: a deferred that fires when the items have been published.
d4fc29bb5381 Define interfaces of blocks of functionality.
Ralph Meijer <ralphm@ik.nu>
parents: 24
diff changeset
98 """
106
dc36882d2620 Also move from twisted.python.components to zope.interface.Interface
Ralph Meijer <ralphm@ik.nu>
parents: 105
diff changeset
99 class INotificationService(Interface):
29
d4fc29bb5381 Define interfaces of blocks of functionality.
Ralph Meijer <ralphm@ik.nu>
parents: 24
diff changeset
100 """ A service for notification of published items. """
d4fc29bb5381 Define interfaces of blocks of functionality.
Ralph Meijer <ralphm@ik.nu>
parents: 24
diff changeset
101
d4fc29bb5381 Define interfaces of blocks of functionality.
Ralph Meijer <ralphm@ik.nu>
parents: 24
diff changeset
102 def register_notifier(self, observerfn, *args, **kwargs):
d4fc29bb5381 Define interfaces of blocks of functionality.
Ralph Meijer <ralphm@ik.nu>
parents: 24
diff changeset
103 """ Register callback which is called for notification. """
d4fc29bb5381 Define interfaces of blocks of functionality.
Ralph Meijer <ralphm@ik.nu>
parents: 24
diff changeset
104
d4fc29bb5381 Define interfaces of blocks of functionality.
Ralph Meijer <ralphm@ik.nu>
parents: 24
diff changeset
105 def get_notification_list(self, node_id, items):
d4fc29bb5381 Define interfaces of blocks of functionality.
Ralph Meijer <ralphm@ik.nu>
parents: 24
diff changeset
106 pass
d4fc29bb5381 Define interfaces of blocks of functionality.
Ralph Meijer <ralphm@ik.nu>
parents: 24
diff changeset
107
106
dc36882d2620 Also move from twisted.python.components to zope.interface.Interface
Ralph Meijer <ralphm@ik.nu>
parents: 105
diff changeset
108 class ISubscriptionService(Interface):
29
d4fc29bb5381 Define interfaces of blocks of functionality.
Ralph Meijer <ralphm@ik.nu>
parents: 24
diff changeset
109 """ A service for managing subscriptions. """
d4fc29bb5381 Define interfaces of blocks of functionality.
Ralph Meijer <ralphm@ik.nu>
parents: 24
diff changeset
110
d4fc29bb5381 Define interfaces of blocks of functionality.
Ralph Meijer <ralphm@ik.nu>
parents: 24
diff changeset
111 def subscribe(self, node_id, subscriber, requestor):
d4fc29bb5381 Define interfaces of blocks of functionality.
Ralph Meijer <ralphm@ik.nu>
parents: 24
diff changeset
112 """ Request the subscription of an entity to a pubsub node.
d4fc29bb5381 Define interfaces of blocks of functionality.
Ralph Meijer <ralphm@ik.nu>
parents: 24
diff changeset
113
d4fc29bb5381 Define interfaces of blocks of functionality.
Ralph Meijer <ralphm@ik.nu>
parents: 24
diff changeset
114 Depending on the node's configuration and possible business rules, the
d4fc29bb5381 Define interfaces of blocks of functionality.
Ralph Meijer <ralphm@ik.nu>
parents: 24
diff changeset
115 C{subscriber} is added to the list of subscriptions of the node with id
d4fc29bb5381 Define interfaces of blocks of functionality.
Ralph Meijer <ralphm@ik.nu>
parents: 24
diff changeset
116 C{node_id}. The C{subscriber} might be different from the C{requestor},
d4fc29bb5381 Define interfaces of blocks of functionality.
Ralph Meijer <ralphm@ik.nu>
parents: 24
diff changeset
117 and if the C{requestor} is not allowed to subscribe this entity an
d4fc29bb5381 Define interfaces of blocks of functionality.
Ralph Meijer <ralphm@ik.nu>
parents: 24
diff changeset
118 exception should be raised.
59
0fa161c00ed9 Use jid.JIDs everywhere in the backend.
Ralph Meijer <ralphm@ik.nu>
parents: 53
diff changeset
119
29
d4fc29bb5381 Define interfaces of blocks of functionality.
Ralph Meijer <ralphm@ik.nu>
parents: 24
diff changeset
120 @return: a deferred that returns the subscription state
d4fc29bb5381 Define interfaces of blocks of functionality.
Ralph Meijer <ralphm@ik.nu>
parents: 24
diff changeset
121 """
d4fc29bb5381 Define interfaces of blocks of functionality.
Ralph Meijer <ralphm@ik.nu>
parents: 24
diff changeset
122
d4fc29bb5381 Define interfaces of blocks of functionality.
Ralph Meijer <ralphm@ik.nu>
parents: 24
diff changeset
123 def unsubscribe(self, node_id, subscriber, requestor):
d4fc29bb5381 Define interfaces of blocks of functionality.
Ralph Meijer <ralphm@ik.nu>
parents: 24
diff changeset
124 """ Cancel the subscription of an entity to a pubsub node.
d4fc29bb5381 Define interfaces of blocks of functionality.
Ralph Meijer <ralphm@ik.nu>
parents: 24
diff changeset
125
d4fc29bb5381 Define interfaces of blocks of functionality.
Ralph Meijer <ralphm@ik.nu>
parents: 24
diff changeset
126 The subscription of C{subscriber} is removed from the list of
d4fc29bb5381 Define interfaces of blocks of functionality.
Ralph Meijer <ralphm@ik.nu>
parents: 24
diff changeset
127 subscriptions of the node with id C{node_id}. If the C{requestor}
d4fc29bb5381 Define interfaces of blocks of functionality.
Ralph Meijer <ralphm@ik.nu>
parents: 24
diff changeset
128 is not allowed to unsubscribe C{subscriber}, an an exception should
d4fc29bb5381 Define interfaces of blocks of functionality.
Ralph Meijer <ralphm@ik.nu>
parents: 24
diff changeset
129 be raised.
d4fc29bb5381 Define interfaces of blocks of functionality.
Ralph Meijer <ralphm@ik.nu>
parents: 24
diff changeset
130
d4fc29bb5381 Define interfaces of blocks of functionality.
Ralph Meijer <ralphm@ik.nu>
parents: 24
diff changeset
131 @return: a deferred that fires when unsubscription is complete.
d4fc29bb5381 Define interfaces of blocks of functionality.
Ralph Meijer <ralphm@ik.nu>
parents: 24
diff changeset
132 """
d4fc29bb5381 Define interfaces of blocks of functionality.
Ralph Meijer <ralphm@ik.nu>
parents: 24
diff changeset
133
106
dc36882d2620 Also move from twisted.python.components to zope.interface.Interface
Ralph Meijer <ralphm@ik.nu>
parents: 105
diff changeset
134 class IAffiliationsService(Interface):
29
d4fc29bb5381 Define interfaces of blocks of functionality.
Ralph Meijer <ralphm@ik.nu>
parents: 24
diff changeset
135 """ A service for retrieving the affiliations with this pubsub service. """
d4fc29bb5381 Define interfaces of blocks of functionality.
Ralph Meijer <ralphm@ik.nu>
parents: 24
diff changeset
136
d4fc29bb5381 Define interfaces of blocks of functionality.
Ralph Meijer <ralphm@ik.nu>
parents: 24
diff changeset
137 def get_affiliations(self, entity):
d4fc29bb5381 Define interfaces of blocks of functionality.
Ralph Meijer <ralphm@ik.nu>
parents: 24
diff changeset
138 """ Report the list of current affiliations with this pubsub service.
d4fc29bb5381 Define interfaces of blocks of functionality.
Ralph Meijer <ralphm@ik.nu>
parents: 24
diff changeset
139
d4fc29bb5381 Define interfaces of blocks of functionality.
Ralph Meijer <ralphm@ik.nu>
parents: 24
diff changeset
140 Report the list of the current affiliations with all nodes within this
d4fc29bb5381 Define interfaces of blocks of functionality.
Ralph Meijer <ralphm@ik.nu>
parents: 24
diff changeset
141 pubsub service, along with subscriptions to such nodes, for the
d4fc29bb5381 Define interfaces of blocks of functionality.
Ralph Meijer <ralphm@ik.nu>
parents: 24
diff changeset
142 C{entity}.
d4fc29bb5381 Define interfaces of blocks of functionality.
Ralph Meijer <ralphm@ik.nu>
parents: 24
diff changeset
143
d4fc29bb5381 Define interfaces of blocks of functionality.
Ralph Meijer <ralphm@ik.nu>
parents: 24
diff changeset
144 @return: a deferred that returns the list of all current affiliations
d4fc29bb5381 Define interfaces of blocks of functionality.
Ralph Meijer <ralphm@ik.nu>
parents: 24
diff changeset
145 and subscriptions.
d4fc29bb5381 Define interfaces of blocks of functionality.
Ralph Meijer <ralphm@ik.nu>
parents: 24
diff changeset
146 """
d4fc29bb5381 Define interfaces of blocks of functionality.
Ralph Meijer <ralphm@ik.nu>
parents: 24
diff changeset
147
106
dc36882d2620 Also move from twisted.python.components to zope.interface.Interface
Ralph Meijer <ralphm@ik.nu>
parents: 105
diff changeset
148 class IRetractionService(Interface):
29
d4fc29bb5381 Define interfaces of blocks of functionality.
Ralph Meijer <ralphm@ik.nu>
parents: 24
diff changeset
149 """ A service for retracting published items """
d4fc29bb5381 Define interfaces of blocks of functionality.
Ralph Meijer <ralphm@ik.nu>
parents: 24
diff changeset
150
d4fc29bb5381 Define interfaces of blocks of functionality.
Ralph Meijer <ralphm@ik.nu>
parents: 24
diff changeset
151 def retract_item(self, node_id, item_id, requestor):
d4fc29bb5381 Define interfaces of blocks of functionality.
Ralph Meijer <ralphm@ik.nu>
parents: 24
diff changeset
152 """ Removes item in node from persistent storage """
d4fc29bb5381 Define interfaces of blocks of functionality.
Ralph Meijer <ralphm@ik.nu>
parents: 24
diff changeset
153
d4fc29bb5381 Define interfaces of blocks of functionality.
Ralph Meijer <ralphm@ik.nu>
parents: 24
diff changeset
154 def purge_node(self, node_id, requestor):
d4fc29bb5381 Define interfaces of blocks of functionality.
Ralph Meijer <ralphm@ik.nu>
parents: 24
diff changeset
155 """ Removes all items in node from persistent storage """
d4fc29bb5381 Define interfaces of blocks of functionality.
Ralph Meijer <ralphm@ik.nu>
parents: 24
diff changeset
156
106
dc36882d2620 Also move from twisted.python.components to zope.interface.Interface
Ralph Meijer <ralphm@ik.nu>
parents: 105
diff changeset
157 class IItemRetrievalService(Interface):
29
d4fc29bb5381 Define interfaces of blocks of functionality.
Ralph Meijer <ralphm@ik.nu>
parents: 24
diff changeset
158 """ A service for retrieving previously published items. """
d4fc29bb5381 Define interfaces of blocks of functionality.
Ralph Meijer <ralphm@ik.nu>
parents: 24
diff changeset
159
80
ec354aab3949 Implemented ItemRetrievalService.
Ralph Meijer <ralphm@ik.nu>
parents: 72
diff changeset
160 def get_items(self, node_id, requestor, max_items=None, item_ids=[]):
29
d4fc29bb5381 Define interfaces of blocks of functionality.
Ralph Meijer <ralphm@ik.nu>
parents: 24
diff changeset
161 """ Retrieve items from persistent storage
d4fc29bb5381 Define interfaces of blocks of functionality.
Ralph Meijer <ralphm@ik.nu>
parents: 24
diff changeset
162
d4fc29bb5381 Define interfaces of blocks of functionality.
Ralph Meijer <ralphm@ik.nu>
parents: 24
diff changeset
163 If C{max_items} is given, return the C{max_items} last published
d4fc29bb5381 Define interfaces of blocks of functionality.
Ralph Meijer <ralphm@ik.nu>
parents: 24
diff changeset
164 items, else if C{item_ids} is not empty, return the items requested.
d4fc29bb5381 Define interfaces of blocks of functionality.
Ralph Meijer <ralphm@ik.nu>
parents: 24
diff changeset
165 If neither is given, return all items.
d4fc29bb5381 Define interfaces of blocks of functionality.
Ralph Meijer <ralphm@ik.nu>
parents: 24
diff changeset
166
d4fc29bb5381 Define interfaces of blocks of functionality.
Ralph Meijer <ralphm@ik.nu>
parents: 24
diff changeset
167 @return: a deferred that returns the requested items
d4fc29bb5381 Define interfaces of blocks of functionality.
Ralph Meijer <ralphm@ik.nu>
parents: 24
diff changeset
168 """
d4fc29bb5381 Define interfaces of blocks of functionality.
Ralph Meijer <ralphm@ik.nu>
parents: 24
diff changeset
169
d4fc29bb5381 Define interfaces of blocks of functionality.
Ralph Meijer <ralphm@ik.nu>
parents: 24
diff changeset
170 class BackendService(service.MultiService, utility.EventDispatcher):
d4fc29bb5381 Define interfaces of blocks of functionality.
Ralph Meijer <ralphm@ik.nu>
parents: 24
diff changeset
171
105
8d8946e67fcb Move to Twisted 2.0.
Ralph Meijer <ralphm@ik.nu>
parents: 101
diff changeset
172 implements(IBackendService)
29
d4fc29bb5381 Define interfaces of blocks of functionality.
Ralph Meijer <ralphm@ik.nu>
parents: 24
diff changeset
173
101
b75fcc554358 Added support for disco info meta data.
Ralph Meijer <ralphm@ik.nu>
parents: 98
diff changeset
174 options = {"pubsub#persist_items":
b75fcc554358 Added support for disco info meta data.
Ralph Meijer <ralphm@ik.nu>
parents: 98
diff changeset
175 {"type": "boolean",
b75fcc554358 Added support for disco info meta data.
Ralph Meijer <ralphm@ik.nu>
parents: 98
diff changeset
176 "label": "Persist items to storage"},
b75fcc554358 Added support for disco info meta data.
Ralph Meijer <ralphm@ik.nu>
parents: 98
diff changeset
177 "pubsub#deliver_payloads":
b75fcc554358 Added support for disco info meta data.
Ralph Meijer <ralphm@ik.nu>
parents: 98
diff changeset
178 {"type": "boolean",
b75fcc554358 Added support for disco info meta data.
Ralph Meijer <ralphm@ik.nu>
parents: 98
diff changeset
179 "label": "Deliver payloads with event notifications"},
b75fcc554358 Added support for disco info meta data.
Ralph Meijer <ralphm@ik.nu>
parents: 98
diff changeset
180 }
b75fcc554358 Added support for disco info meta data.
Ralph Meijer <ralphm@ik.nu>
parents: 98
diff changeset
181
b75fcc554358 Added support for disco info meta data.
Ralph Meijer <ralphm@ik.nu>
parents: 98
diff changeset
182 default_config = {"pubsub#persist_items": True,
b75fcc554358 Added support for disco info meta data.
Ralph Meijer <ralphm@ik.nu>
parents: 98
diff changeset
183 "pubsub#deliver_payloads": True,
b75fcc554358 Added support for disco info meta data.
Ralph Meijer <ralphm@ik.nu>
parents: 98
diff changeset
184 }
b75fcc554358 Added support for disco info meta data.
Ralph Meijer <ralphm@ik.nu>
parents: 98
diff changeset
185
43
9685b7e291ef Moved common stuff out of pgsql_backend.py to backend.py.
Ralph Meijer <ralphm@ik.nu>
parents: 39
diff changeset
186 def __init__(self, storage):
29
d4fc29bb5381 Define interfaces of blocks of functionality.
Ralph Meijer <ralphm@ik.nu>
parents: 24
diff changeset
187 service.MultiService.__init__(self)
d4fc29bb5381 Define interfaces of blocks of functionality.
Ralph Meijer <ralphm@ik.nu>
parents: 24
diff changeset
188 utility.EventDispatcher.__init__(self)
43
9685b7e291ef Moved common stuff out of pgsql_backend.py to backend.py.
Ralph Meijer <ralphm@ik.nu>
parents: 39
diff changeset
189 self.storage = storage
29
d4fc29bb5381 Define interfaces of blocks of functionality.
Ralph Meijer <ralphm@ik.nu>
parents: 24
diff changeset
190
53
e602ddda2d6e Implement NodeCreationService, using the create_node() method of Storage.
Ralph Meijer <ralphm@ik.nu>
parents: 49
diff changeset
191 def supports_publisher_affiliation(self):
e602ddda2d6e Implement NodeCreationService, using the create_node() method of Storage.
Ralph Meijer <ralphm@ik.nu>
parents: 49
diff changeset
192 return True
e602ddda2d6e Implement NodeCreationService, using the create_node() method of Storage.
Ralph Meijer <ralphm@ik.nu>
parents: 49
diff changeset
193
e602ddda2d6e Implement NodeCreationService, using the create_node() method of Storage.
Ralph Meijer <ralphm@ik.nu>
parents: 49
diff changeset
194 def supports_outcast_affiliation(self):
e602ddda2d6e Implement NodeCreationService, using the create_node() method of Storage.
Ralph Meijer <ralphm@ik.nu>
parents: 49
diff changeset
195 return True
e602ddda2d6e Implement NodeCreationService, using the create_node() method of Storage.
Ralph Meijer <ralphm@ik.nu>
parents: 49
diff changeset
196
e602ddda2d6e Implement NodeCreationService, using the create_node() method of Storage.
Ralph Meijer <ralphm@ik.nu>
parents: 49
diff changeset
197 def supports_persistent_items(self):
e602ddda2d6e Implement NodeCreationService, using the create_node() method of Storage.
Ralph Meijer <ralphm@ik.nu>
parents: 49
diff changeset
198 return True
39
788114f9b5bc Added get_supported_affiliations() to BackendService.
Ralph Meijer <ralphm@ik.nu>
parents: 34
diff changeset
199
65
d617f8d19263 Add get_node_type() to BackendService.
Ralph Meijer <ralphm@ik.nu>
parents: 61
diff changeset
200 def get_node_type(self, node_id):
72
a914cad421c5 Added get_nodes() to BackendService.
Ralph Meijer <ralphm@ik.nu>
parents: 65
diff changeset
201 return self.storage.get_node_type(node_id)
a914cad421c5 Added get_nodes() to BackendService.
Ralph Meijer <ralphm@ik.nu>
parents: 65
diff changeset
202
a914cad421c5 Added get_nodes() to BackendService.
Ralph Meijer <ralphm@ik.nu>
parents: 65
diff changeset
203 def get_nodes(self):
a914cad421c5 Added get_nodes() to BackendService.
Ralph Meijer <ralphm@ik.nu>
parents: 65
diff changeset
204 return self.storage.get_nodes()
65
d617f8d19263 Add get_node_type() to BackendService.
Ralph Meijer <ralphm@ik.nu>
parents: 61
diff changeset
205
101
b75fcc554358 Added support for disco info meta data.
Ralph Meijer <ralphm@ik.nu>
parents: 98
diff changeset
206 def get_node_meta_data(self, node_id):
b75fcc554358 Added support for disco info meta data.
Ralph Meijer <ralphm@ik.nu>
parents: 98
diff changeset
207 d = self.storage.get_node_configuration(node_id)
b75fcc554358 Added support for disco info meta data.
Ralph Meijer <ralphm@ik.nu>
parents: 98
diff changeset
208
b75fcc554358 Added support for disco info meta data.
Ralph Meijer <ralphm@ik.nu>
parents: 98
diff changeset
209 d.addCallback(self._make_meta_data)
b75fcc554358 Added support for disco info meta data.
Ralph Meijer <ralphm@ik.nu>
parents: 98
diff changeset
210 return d
b75fcc554358 Added support for disco info meta data.
Ralph Meijer <ralphm@ik.nu>
parents: 98
diff changeset
211
b75fcc554358 Added support for disco info meta data.
Ralph Meijer <ralphm@ik.nu>
parents: 98
diff changeset
212 def _make_meta_data(self, meta_data):
b75fcc554358 Added support for disco info meta data.
Ralph Meijer <ralphm@ik.nu>
parents: 98
diff changeset
213 options = []
b75fcc554358 Added support for disco info meta data.
Ralph Meijer <ralphm@ik.nu>
parents: 98
diff changeset
214 for key, value in meta_data.iteritems():
b75fcc554358 Added support for disco info meta data.
Ralph Meijer <ralphm@ik.nu>
parents: 98
diff changeset
215 if self.options.has_key(key):
b75fcc554358 Added support for disco info meta data.
Ralph Meijer <ralphm@ik.nu>
parents: 98
diff changeset
216 option = {"var": key}
b75fcc554358 Added support for disco info meta data.
Ralph Meijer <ralphm@ik.nu>
parents: 98
diff changeset
217 option.update(self.options[key])
b75fcc554358 Added support for disco info meta data.
Ralph Meijer <ralphm@ik.nu>
parents: 98
diff changeset
218 option["value"] = value
b75fcc554358 Added support for disco info meta data.
Ralph Meijer <ralphm@ik.nu>
parents: 98
diff changeset
219 options.append(option)
b75fcc554358 Added support for disco info meta data.
Ralph Meijer <ralphm@ik.nu>
parents: 98
diff changeset
220
b75fcc554358 Added support for disco info meta data.
Ralph Meijer <ralphm@ik.nu>
parents: 98
diff changeset
221 return options
b75fcc554358 Added support for disco info meta data.
Ralph Meijer <ralphm@ik.nu>
parents: 98
diff changeset
222
44
bc7438476a67 Removed IPersistenceService, this is a feature of the storage facility.
Ralph Meijer <ralphm@ik.nu>
parents: 43
diff changeset
223 class PublishService(service.Service):
bc7438476a67 Removed IPersistenceService, this is a feature of the storage facility.
Ralph Meijer <ralphm@ik.nu>
parents: 43
diff changeset
224
105
8d8946e67fcb Move to Twisted 2.0.
Ralph Meijer <ralphm@ik.nu>
parents: 101
diff changeset
225 implements(IPublishService)
44
bc7438476a67 Removed IPersistenceService, this is a feature of the storage facility.
Ralph Meijer <ralphm@ik.nu>
parents: 43
diff changeset
226
43
9685b7e291ef Moved common stuff out of pgsql_backend.py to backend.py.
Ralph Meijer <ralphm@ik.nu>
parents: 39
diff changeset
227 def publish(self, node_id, items, requestor):
44
bc7438476a67 Removed IPersistenceService, this is a feature of the storage facility.
Ralph Meijer <ralphm@ik.nu>
parents: 43
diff changeset
228 d1 = self.parent.storage.get_node_configuration(node_id)
59
0fa161c00ed9 Use jid.JIDs everywhere in the backend.
Ralph Meijer <ralphm@ik.nu>
parents: 53
diff changeset
229 d2 = self.parent.storage.get_affiliation(node_id, requestor)
92
878a5b7697f2 Consume errors in DeferredLists.
Ralph Meijer <ralphm@ik.nu>
parents: 90
diff changeset
230 d = defer.DeferredList([d1, d2], fireOnOneErrback=1, consumeErrors=1)
43
9685b7e291ef Moved common stuff out of pgsql_backend.py to backend.py.
Ralph Meijer <ralphm@ik.nu>
parents: 39
diff changeset
231 d.addErrback(lambda x: x.value[0])
9685b7e291ef Moved common stuff out of pgsql_backend.py to backend.py.
Ralph Meijer <ralphm@ik.nu>
parents: 39
diff changeset
232 d.addCallback(self._do_publish, node_id, items, requestor)
9685b7e291ef Moved common stuff out of pgsql_backend.py to backend.py.
Ralph Meijer <ralphm@ik.nu>
parents: 39
diff changeset
233 return d
9685b7e291ef Moved common stuff out of pgsql_backend.py to backend.py.
Ralph Meijer <ralphm@ik.nu>
parents: 39
diff changeset
234
9685b7e291ef Moved common stuff out of pgsql_backend.py to backend.py.
Ralph Meijer <ralphm@ik.nu>
parents: 39
diff changeset
235 def _do_publish(self, result, node_id, items, requestor):
9685b7e291ef Moved common stuff out of pgsql_backend.py to backend.py.
Ralph Meijer <ralphm@ik.nu>
parents: 39
diff changeset
236 configuration = result[0][1]
95
3ad74552bbc7 Merge from RELENG_0: Implemented node configuration.
Ralph Meijer <ralphm@ik.nu>
parents: 92
diff changeset
237 persist_items = configuration["pubsub#persist_items"]
3ad74552bbc7 Merge from RELENG_0: Implemented node configuration.
Ralph Meijer <ralphm@ik.nu>
parents: 92
diff changeset
238 deliver_payloads = configuration["pubsub#deliver_payloads"]
43
9685b7e291ef Moved common stuff out of pgsql_backend.py to backend.py.
Ralph Meijer <ralphm@ik.nu>
parents: 39
diff changeset
239 affiliation = result[1][1]
9685b7e291ef Moved common stuff out of pgsql_backend.py to backend.py.
Ralph Meijer <ralphm@ik.nu>
parents: 39
diff changeset
240
9685b7e291ef Moved common stuff out of pgsql_backend.py to backend.py.
Ralph Meijer <ralphm@ik.nu>
parents: 39
diff changeset
241 if affiliation not in ['owner', 'publisher']:
9685b7e291ef Moved common stuff out of pgsql_backend.py to backend.py.
Ralph Meijer <ralphm@ik.nu>
parents: 39
diff changeset
242 raise NotAuthorized
9685b7e291ef Moved common stuff out of pgsql_backend.py to backend.py.
Ralph Meijer <ralphm@ik.nu>
parents: 39
diff changeset
243
9685b7e291ef Moved common stuff out of pgsql_backend.py to backend.py.
Ralph Meijer <ralphm@ik.nu>
parents: 39
diff changeset
244 if items and not persist_items and not deliver_payloads:
9685b7e291ef Moved common stuff out of pgsql_backend.py to backend.py.
Ralph Meijer <ralphm@ik.nu>
parents: 39
diff changeset
245 raise NoPayloadAllowed
9685b7e291ef Moved common stuff out of pgsql_backend.py to backend.py.
Ralph Meijer <ralphm@ik.nu>
parents: 39
diff changeset
246 elif not items and (persist_items or deliver_payloads):
9685b7e291ef Moved common stuff out of pgsql_backend.py to backend.py.
Ralph Meijer <ralphm@ik.nu>
parents: 39
diff changeset
247 raise PayloadExpected
9685b7e291ef Moved common stuff out of pgsql_backend.py to backend.py.
Ralph Meijer <ralphm@ik.nu>
parents: 39
diff changeset
248
9685b7e291ef Moved common stuff out of pgsql_backend.py to backend.py.
Ralph Meijer <ralphm@ik.nu>
parents: 39
diff changeset
249 if persist_items or deliver_payloads:
9685b7e291ef Moved common stuff out of pgsql_backend.py to backend.py.
Ralph Meijer <ralphm@ik.nu>
parents: 39
diff changeset
250 for item in items:
84
34be83a0bd2e Implement instant nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 80
diff changeset
251 if not item.getAttribute("id"):
34be83a0bd2e Implement instant nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 80
diff changeset
252 item["id"] = sha.new(str(time.time()) +
34be83a0bd2e Implement instant nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 80
diff changeset
253 requestor.full()).hexdigest()
43
9685b7e291ef Moved common stuff out of pgsql_backend.py to backend.py.
Ralph Meijer <ralphm@ik.nu>
parents: 39
diff changeset
254
9685b7e291ef Moved common stuff out of pgsql_backend.py to backend.py.
Ralph Meijer <ralphm@ik.nu>
parents: 39
diff changeset
255 if persist_items:
44
bc7438476a67 Removed IPersistenceService, this is a feature of the storage facility.
Ralph Meijer <ralphm@ik.nu>
parents: 43
diff changeset
256 d = self.parent.storage.store_items(node_id, items,
59
0fa161c00ed9 Use jid.JIDs everywhere in the backend.
Ralph Meijer <ralphm@ik.nu>
parents: 53
diff changeset
257 requestor)
43
9685b7e291ef Moved common stuff out of pgsql_backend.py to backend.py.
Ralph Meijer <ralphm@ik.nu>
parents: 39
diff changeset
258 else:
9685b7e291ef Moved common stuff out of pgsql_backend.py to backend.py.
Ralph Meijer <ralphm@ik.nu>
parents: 39
diff changeset
259 d = defer.succeed(None)
9685b7e291ef Moved common stuff out of pgsql_backend.py to backend.py.
Ralph Meijer <ralphm@ik.nu>
parents: 39
diff changeset
260
9685b7e291ef Moved common stuff out of pgsql_backend.py to backend.py.
Ralph Meijer <ralphm@ik.nu>
parents: 39
diff changeset
261 d.addCallback(self._do_notify, node_id, items, deliver_payloads)
9685b7e291ef Moved common stuff out of pgsql_backend.py to backend.py.
Ralph Meijer <ralphm@ik.nu>
parents: 39
diff changeset
262
9685b7e291ef Moved common stuff out of pgsql_backend.py to backend.py.
Ralph Meijer <ralphm@ik.nu>
parents: 39
diff changeset
263 def _do_notify(self, result, node_id, items, deliver_payloads):
9685b7e291ef Moved common stuff out of pgsql_backend.py to backend.py.
Ralph Meijer <ralphm@ik.nu>
parents: 39
diff changeset
264 if items and not deliver_payloads:
9685b7e291ef Moved common stuff out of pgsql_backend.py to backend.py.
Ralph Meijer <ralphm@ik.nu>
parents: 39
diff changeset
265 for item in items:
9685b7e291ef Moved common stuff out of pgsql_backend.py to backend.py.
Ralph Meijer <ralphm@ik.nu>
parents: 39
diff changeset
266 item.children = []
9685b7e291ef Moved common stuff out of pgsql_backend.py to backend.py.
Ralph Meijer <ralphm@ik.nu>
parents: 39
diff changeset
267
44
bc7438476a67 Removed IPersistenceService, this is a feature of the storage facility.
Ralph Meijer <ralphm@ik.nu>
parents: 43
diff changeset
268 self.parent.dispatch({ 'items': items, 'node_id': node_id },
bc7438476a67 Removed IPersistenceService, this is a feature of the storage facility.
Ralph Meijer <ralphm@ik.nu>
parents: 43
diff changeset
269 '//event/pubsub/notify')
bc7438476a67 Removed IPersistenceService, this is a feature of the storage facility.
Ralph Meijer <ralphm@ik.nu>
parents: 43
diff changeset
270
bc7438476a67 Removed IPersistenceService, this is a feature of the storage facility.
Ralph Meijer <ralphm@ik.nu>
parents: 43
diff changeset
271 class NotificationService(service.Service):
bc7438476a67 Removed IPersistenceService, this is a feature of the storage facility.
Ralph Meijer <ralphm@ik.nu>
parents: 43
diff changeset
272
105
8d8946e67fcb Move to Twisted 2.0.
Ralph Meijer <ralphm@ik.nu>
parents: 101
diff changeset
273 implements(INotificationService)
43
9685b7e291ef Moved common stuff out of pgsql_backend.py to backend.py.
Ralph Meijer <ralphm@ik.nu>
parents: 39
diff changeset
274
9685b7e291ef Moved common stuff out of pgsql_backend.py to backend.py.
Ralph Meijer <ralphm@ik.nu>
parents: 39
diff changeset
275 def get_notification_list(self, node_id, items):
44
bc7438476a67 Removed IPersistenceService, this is a feature of the storage facility.
Ralph Meijer <ralphm@ik.nu>
parents: 43
diff changeset
276 d = self.parent.storage.get_subscribers(node_id)
43
9685b7e291ef Moved common stuff out of pgsql_backend.py to backend.py.
Ralph Meijer <ralphm@ik.nu>
parents: 39
diff changeset
277 d.addCallback(self._magic_filter, node_id, items)
9685b7e291ef Moved common stuff out of pgsql_backend.py to backend.py.
Ralph Meijer <ralphm@ik.nu>
parents: 39
diff changeset
278 return d
9685b7e291ef Moved common stuff out of pgsql_backend.py to backend.py.
Ralph Meijer <ralphm@ik.nu>
parents: 39
diff changeset
279
9685b7e291ef Moved common stuff out of pgsql_backend.py to backend.py.
Ralph Meijer <ralphm@ik.nu>
parents: 39
diff changeset
280 def _magic_filter(self, subscribers, node_id, items):
9685b7e291ef Moved common stuff out of pgsql_backend.py to backend.py.
Ralph Meijer <ralphm@ik.nu>
parents: 39
diff changeset
281 list = {}
9685b7e291ef Moved common stuff out of pgsql_backend.py to backend.py.
Ralph Meijer <ralphm@ik.nu>
parents: 39
diff changeset
282 for subscriber in subscribers:
9685b7e291ef Moved common stuff out of pgsql_backend.py to backend.py.
Ralph Meijer <ralphm@ik.nu>
parents: 39
diff changeset
283 list[subscriber] = items
9685b7e291ef Moved common stuff out of pgsql_backend.py to backend.py.
Ralph Meijer <ralphm@ik.nu>
parents: 39
diff changeset
284 return list
9685b7e291ef Moved common stuff out of pgsql_backend.py to backend.py.
Ralph Meijer <ralphm@ik.nu>
parents: 39
diff changeset
285
29
d4fc29bb5381 Define interfaces of blocks of functionality.
Ralph Meijer <ralphm@ik.nu>
parents: 24
diff changeset
286 def register_notifier(self, observerfn, *args, **kwargs):
d4fc29bb5381 Define interfaces of blocks of functionality.
Ralph Meijer <ralphm@ik.nu>
parents: 24
diff changeset
287 self.parent.addObserver('//event/pubsub/notify', observerfn,
d4fc29bb5381 Define interfaces of blocks of functionality.
Ralph Meijer <ralphm@ik.nu>
parents: 24
diff changeset
288 *args, **kwargs)
49
94e4ede2a357 Implement SubscriptionService with separate storage.
Ralph Meijer <ralphm@ik.nu>
parents: 44
diff changeset
289
94e4ede2a357 Implement SubscriptionService with separate storage.
Ralph Meijer <ralphm@ik.nu>
parents: 44
diff changeset
290 class SubscriptionService(service.Service):
94e4ede2a357 Implement SubscriptionService with separate storage.
Ralph Meijer <ralphm@ik.nu>
parents: 44
diff changeset
291
105
8d8946e67fcb Move to Twisted 2.0.
Ralph Meijer <ralphm@ik.nu>
parents: 101
diff changeset
292 implements(ISubscriptionService)
49
94e4ede2a357 Implement SubscriptionService with separate storage.
Ralph Meijer <ralphm@ik.nu>
parents: 44
diff changeset
293
94e4ede2a357 Implement SubscriptionService with separate storage.
Ralph Meijer <ralphm@ik.nu>
parents: 44
diff changeset
294 def subscribe(self, node_id, subscriber, requestor):
94e4ede2a357 Implement SubscriptionService with separate storage.
Ralph Meijer <ralphm@ik.nu>
parents: 44
diff changeset
295 if subscriber.userhostJID() != requestor:
94e4ede2a357 Implement SubscriptionService with separate storage.
Ralph Meijer <ralphm@ik.nu>
parents: 44
diff changeset
296 raise NotAuthorized
94e4ede2a357 Implement SubscriptionService with separate storage.
Ralph Meijer <ralphm@ik.nu>
parents: 44
diff changeset
297
94e4ede2a357 Implement SubscriptionService with separate storage.
Ralph Meijer <ralphm@ik.nu>
parents: 44
diff changeset
298 d1 = self.parent.storage.get_node_configuration(node_id)
59
0fa161c00ed9 Use jid.JIDs everywhere in the backend.
Ralph Meijer <ralphm@ik.nu>
parents: 53
diff changeset
299 d2 = self.parent.storage.get_affiliation(node_id, subscriber)
92
878a5b7697f2 Consume errors in DeferredLists.
Ralph Meijer <ralphm@ik.nu>
parents: 90
diff changeset
300 d = defer.DeferredList([d1, d2], fireOnOneErrback=1, consumeErrors=1)
49
94e4ede2a357 Implement SubscriptionService with separate storage.
Ralph Meijer <ralphm@ik.nu>
parents: 44
diff changeset
301 d.addErrback(lambda x: x.value[0])
94e4ede2a357 Implement SubscriptionService with separate storage.
Ralph Meijer <ralphm@ik.nu>
parents: 44
diff changeset
302 d.addCallback(self._do_subscribe, node_id, subscriber)
94e4ede2a357 Implement SubscriptionService with separate storage.
Ralph Meijer <ralphm@ik.nu>
parents: 44
diff changeset
303 return d
94e4ede2a357 Implement SubscriptionService with separate storage.
Ralph Meijer <ralphm@ik.nu>
parents: 44
diff changeset
304
94e4ede2a357 Implement SubscriptionService with separate storage.
Ralph Meijer <ralphm@ik.nu>
parents: 44
diff changeset
305 def _do_subscribe(self, result, node_id, subscriber):
94e4ede2a357 Implement SubscriptionService with separate storage.
Ralph Meijer <ralphm@ik.nu>
parents: 44
diff changeset
306 configuration = result[0][1]
94e4ede2a357 Implement SubscriptionService with separate storage.
Ralph Meijer <ralphm@ik.nu>
parents: 44
diff changeset
307 affiliation = result[1][1]
94e4ede2a357 Implement SubscriptionService with separate storage.
Ralph Meijer <ralphm@ik.nu>
parents: 44
diff changeset
308
94e4ede2a357 Implement SubscriptionService with separate storage.
Ralph Meijer <ralphm@ik.nu>
parents: 44
diff changeset
309 if affiliation == 'outcast':
94e4ede2a357 Implement SubscriptionService with separate storage.
Ralph Meijer <ralphm@ik.nu>
parents: 44
diff changeset
310 raise NotAuthorized
94e4ede2a357 Implement SubscriptionService with separate storage.
Ralph Meijer <ralphm@ik.nu>
parents: 44
diff changeset
311
59
0fa161c00ed9 Use jid.JIDs everywhere in the backend.
Ralph Meijer <ralphm@ik.nu>
parents: 53
diff changeset
312 d = self.parent.storage.add_subscription(node_id, subscriber,
49
94e4ede2a357 Implement SubscriptionService with separate storage.
Ralph Meijer <ralphm@ik.nu>
parents: 44
diff changeset
313 'subscribed')
94e4ede2a357 Implement SubscriptionService with separate storage.
Ralph Meijer <ralphm@ik.nu>
parents: 44
diff changeset
314 d.addCallback(self._return_subscription, affiliation)
94e4ede2a357 Implement SubscriptionService with separate storage.
Ralph Meijer <ralphm@ik.nu>
parents: 44
diff changeset
315 return d
94e4ede2a357 Implement SubscriptionService with separate storage.
Ralph Meijer <ralphm@ik.nu>
parents: 44
diff changeset
316
94e4ede2a357 Implement SubscriptionService with separate storage.
Ralph Meijer <ralphm@ik.nu>
parents: 44
diff changeset
317 def _return_subscription(self, result, affiliation):
94e4ede2a357 Implement SubscriptionService with separate storage.
Ralph Meijer <ralphm@ik.nu>
parents: 44
diff changeset
318 result['affiliation'] = affiliation
94e4ede2a357 Implement SubscriptionService with separate storage.
Ralph Meijer <ralphm@ik.nu>
parents: 44
diff changeset
319 return result
94e4ede2a357 Implement SubscriptionService with separate storage.
Ralph Meijer <ralphm@ik.nu>
parents: 44
diff changeset
320
94e4ede2a357 Implement SubscriptionService with separate storage.
Ralph Meijer <ralphm@ik.nu>
parents: 44
diff changeset
321 def unsubscribe(self, node_id, subscriber, requestor):
94e4ede2a357 Implement SubscriptionService with separate storage.
Ralph Meijer <ralphm@ik.nu>
parents: 44
diff changeset
322 if subscriber.userhostJID() != requestor:
94e4ede2a357 Implement SubscriptionService with separate storage.
Ralph Meijer <ralphm@ik.nu>
parents: 44
diff changeset
323 raise NotAuthorized
94e4ede2a357 Implement SubscriptionService with separate storage.
Ralph Meijer <ralphm@ik.nu>
parents: 44
diff changeset
324
94e4ede2a357 Implement SubscriptionService with separate storage.
Ralph Meijer <ralphm@ik.nu>
parents: 44
diff changeset
325 d = self.parent.storage.get_node_configuration(node_id)
94e4ede2a357 Implement SubscriptionService with separate storage.
Ralph Meijer <ralphm@ik.nu>
parents: 44
diff changeset
326 d.addCallback(self._do_unsubscribe, node_id, subscriber)
94e4ede2a357 Implement SubscriptionService with separate storage.
Ralph Meijer <ralphm@ik.nu>
parents: 44
diff changeset
327 return d
94e4ede2a357 Implement SubscriptionService with separate storage.
Ralph Meijer <ralphm@ik.nu>
parents: 44
diff changeset
328
94e4ede2a357 Implement SubscriptionService with separate storage.
Ralph Meijer <ralphm@ik.nu>
parents: 44
diff changeset
329 def _do_unsubscribe(self, result, node_id, subscriber):
94e4ede2a357 Implement SubscriptionService with separate storage.
Ralph Meijer <ralphm@ik.nu>
parents: 44
diff changeset
330 return self.parent.storage.remove_subscription(node_id,
59
0fa161c00ed9 Use jid.JIDs everywhere in the backend.
Ralph Meijer <ralphm@ik.nu>
parents: 53
diff changeset
331 subscriber)
53
e602ddda2d6e Implement NodeCreationService, using the create_node() method of Storage.
Ralph Meijer <ralphm@ik.nu>
parents: 49
diff changeset
332
e602ddda2d6e Implement NodeCreationService, using the create_node() method of Storage.
Ralph Meijer <ralphm@ik.nu>
parents: 49
diff changeset
333 class NodeCreationService(service.Service):
e602ddda2d6e Implement NodeCreationService, using the create_node() method of Storage.
Ralph Meijer <ralphm@ik.nu>
parents: 49
diff changeset
334
105
8d8946e67fcb Move to Twisted 2.0.
Ralph Meijer <ralphm@ik.nu>
parents: 101
diff changeset
335 implements(INodeCreationService)
53
e602ddda2d6e Implement NodeCreationService, using the create_node() method of Storage.
Ralph Meijer <ralphm@ik.nu>
parents: 49
diff changeset
336
e602ddda2d6e Implement NodeCreationService, using the create_node() method of Storage.
Ralph Meijer <ralphm@ik.nu>
parents: 49
diff changeset
337 def supports_instant_nodes(self):
84
34be83a0bd2e Implement instant nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 80
diff changeset
338 return True
53
e602ddda2d6e Implement NodeCreationService, using the create_node() method of Storage.
Ralph Meijer <ralphm@ik.nu>
parents: 49
diff changeset
339
e602ddda2d6e Implement NodeCreationService, using the create_node() method of Storage.
Ralph Meijer <ralphm@ik.nu>
parents: 49
diff changeset
340 def create_node(self, node_id, requestor):
e602ddda2d6e Implement NodeCreationService, using the create_node() method of Storage.
Ralph Meijer <ralphm@ik.nu>
parents: 49
diff changeset
341 if not node_id:
84
34be83a0bd2e Implement instant nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 80
diff changeset
342 node_id = 'generic/%s' % sha.new(str(time.time()) +
34be83a0bd2e Implement instant nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 80
diff changeset
343 requestor.full()).hexdigest()
53
e602ddda2d6e Implement NodeCreationService, using the create_node() method of Storage.
Ralph Meijer <ralphm@ik.nu>
parents: 49
diff changeset
344
59
0fa161c00ed9 Use jid.JIDs everywhere in the backend.
Ralph Meijer <ralphm@ik.nu>
parents: 53
diff changeset
345 d = self.parent.storage.create_node(node_id, requestor)
53
e602ddda2d6e Implement NodeCreationService, using the create_node() method of Storage.
Ralph Meijer <ralphm@ik.nu>
parents: 49
diff changeset
346 d.addCallback(lambda _: node_id)
e602ddda2d6e Implement NodeCreationService, using the create_node() method of Storage.
Ralph Meijer <ralphm@ik.nu>
parents: 49
diff changeset
347 return d
61
ad08e021b4d5 Implement AffiliationsService.
Ralph Meijer <ralphm@ik.nu>
parents: 59
diff changeset
348
95
3ad74552bbc7 Merge from RELENG_0: Implemented node configuration.
Ralph Meijer <ralphm@ik.nu>
parents: 92
diff changeset
349 def get_node_configuration(self, node_id):
3ad74552bbc7 Merge from RELENG_0: Implemented node configuration.
Ralph Meijer <ralphm@ik.nu>
parents: 92
diff changeset
350 if node_id:
3ad74552bbc7 Merge from RELENG_0: Implemented node configuration.
Ralph Meijer <ralphm@ik.nu>
parents: 92
diff changeset
351 d = self.parent.storage.get_node_configuration(node_id)
3ad74552bbc7 Merge from RELENG_0: Implemented node configuration.
Ralph Meijer <ralphm@ik.nu>
parents: 92
diff changeset
352 else:
101
b75fcc554358 Added support for disco info meta data.
Ralph Meijer <ralphm@ik.nu>
parents: 98
diff changeset
353 # XXX: this is disabled in pubsub.py
b75fcc554358 Added support for disco info meta data.
Ralph Meijer <ralphm@ik.nu>
parents: 98
diff changeset
354 d = defer.succeed(self.parent.default_config)
95
3ad74552bbc7 Merge from RELENG_0: Implemented node configuration.
Ralph Meijer <ralphm@ik.nu>
parents: 92
diff changeset
355
3ad74552bbc7 Merge from RELENG_0: Implemented node configuration.
Ralph Meijer <ralphm@ik.nu>
parents: 92
diff changeset
356 d.addCallback(self._make_config)
3ad74552bbc7 Merge from RELENG_0: Implemented node configuration.
Ralph Meijer <ralphm@ik.nu>
parents: 92
diff changeset
357 return d
3ad74552bbc7 Merge from RELENG_0: Implemented node configuration.
Ralph Meijer <ralphm@ik.nu>
parents: 92
diff changeset
358
3ad74552bbc7 Merge from RELENG_0: Implemented node configuration.
Ralph Meijer <ralphm@ik.nu>
parents: 92
diff changeset
359 def _make_config(self, config):
3ad74552bbc7 Merge from RELENG_0: Implemented node configuration.
Ralph Meijer <ralphm@ik.nu>
parents: 92
diff changeset
360 options = []
101
b75fcc554358 Added support for disco info meta data.
Ralph Meijer <ralphm@ik.nu>
parents: 98
diff changeset
361 for key, value in self.parent.options.iteritems():
95
3ad74552bbc7 Merge from RELENG_0: Implemented node configuration.
Ralph Meijer <ralphm@ik.nu>
parents: 92
diff changeset
362 option = {"var": key}
101
b75fcc554358 Added support for disco info meta data.
Ralph Meijer <ralphm@ik.nu>
parents: 98
diff changeset
363 option.update(value)
b75fcc554358 Added support for disco info meta data.
Ralph Meijer <ralphm@ik.nu>
parents: 98
diff changeset
364 if config.has_key(key):
b75fcc554358 Added support for disco info meta data.
Ralph Meijer <ralphm@ik.nu>
parents: 98
diff changeset
365 option["value"] = config[key]
95
3ad74552bbc7 Merge from RELENG_0: Implemented node configuration.
Ralph Meijer <ralphm@ik.nu>
parents: 92
diff changeset
366 options.append(option)
3ad74552bbc7 Merge from RELENG_0: Implemented node configuration.
Ralph Meijer <ralphm@ik.nu>
parents: 92
diff changeset
367
3ad74552bbc7 Merge from RELENG_0: Implemented node configuration.
Ralph Meijer <ralphm@ik.nu>
parents: 92
diff changeset
368 return options
3ad74552bbc7 Merge from RELENG_0: Implemented node configuration.
Ralph Meijer <ralphm@ik.nu>
parents: 92
diff changeset
369
3ad74552bbc7 Merge from RELENG_0: Implemented node configuration.
Ralph Meijer <ralphm@ik.nu>
parents: 92
diff changeset
370 def set_node_configuration(self, node_id, options, requestor):
3ad74552bbc7 Merge from RELENG_0: Implemented node configuration.
Ralph Meijer <ralphm@ik.nu>
parents: 92
diff changeset
371 for key in options.iterkeys():
101
b75fcc554358 Added support for disco info meta data.
Ralph Meijer <ralphm@ik.nu>
parents: 98
diff changeset
372 if not self.parent.options.has_key(key):
95
3ad74552bbc7 Merge from RELENG_0: Implemented node configuration.
Ralph Meijer <ralphm@ik.nu>
parents: 92
diff changeset
373 raise InvalidConfigurationOption
3ad74552bbc7 Merge from RELENG_0: Implemented node configuration.
Ralph Meijer <ralphm@ik.nu>
parents: 92
diff changeset
374
3ad74552bbc7 Merge from RELENG_0: Implemented node configuration.
Ralph Meijer <ralphm@ik.nu>
parents: 92
diff changeset
375 d = self.parent.storage.get_affiliation(node_id, requestor)
3ad74552bbc7 Merge from RELENG_0: Implemented node configuration.
Ralph Meijer <ralphm@ik.nu>
parents: 92
diff changeset
376 d.addCallback(self._do_set_node_configuration, node_id, options)
3ad74552bbc7 Merge from RELENG_0: Implemented node configuration.
Ralph Meijer <ralphm@ik.nu>
parents: 92
diff changeset
377 return d
3ad74552bbc7 Merge from RELENG_0: Implemented node configuration.
Ralph Meijer <ralphm@ik.nu>
parents: 92
diff changeset
378
3ad74552bbc7 Merge from RELENG_0: Implemented node configuration.
Ralph Meijer <ralphm@ik.nu>
parents: 92
diff changeset
379 def _do_set_node_configuration(self, affiliation, node_id, options):
3ad74552bbc7 Merge from RELENG_0: Implemented node configuration.
Ralph Meijer <ralphm@ik.nu>
parents: 92
diff changeset
380 if affiliation != 'owner':
3ad74552bbc7 Merge from RELENG_0: Implemented node configuration.
Ralph Meijer <ralphm@ik.nu>
parents: 92
diff changeset
381 raise NotAuthorized
3ad74552bbc7 Merge from RELENG_0: Implemented node configuration.
Ralph Meijer <ralphm@ik.nu>
parents: 92
diff changeset
382
3ad74552bbc7 Merge from RELENG_0: Implemented node configuration.
Ralph Meijer <ralphm@ik.nu>
parents: 92
diff changeset
383 return self.parent.storage.set_node_configuration(node_id, options)
3ad74552bbc7 Merge from RELENG_0: Implemented node configuration.
Ralph Meijer <ralphm@ik.nu>
parents: 92
diff changeset
384
61
ad08e021b4d5 Implement AffiliationsService.
Ralph Meijer <ralphm@ik.nu>
parents: 59
diff changeset
385 class AffiliationsService(service.Service):
ad08e021b4d5 Implement AffiliationsService.
Ralph Meijer <ralphm@ik.nu>
parents: 59
diff changeset
386
105
8d8946e67fcb Move to Twisted 2.0.
Ralph Meijer <ralphm@ik.nu>
parents: 101
diff changeset
387 implements(IAffiliationsService)
61
ad08e021b4d5 Implement AffiliationsService.
Ralph Meijer <ralphm@ik.nu>
parents: 59
diff changeset
388
ad08e021b4d5 Implement AffiliationsService.
Ralph Meijer <ralphm@ik.nu>
parents: 59
diff changeset
389 def get_affiliations(self, entity):
ad08e021b4d5 Implement AffiliationsService.
Ralph Meijer <ralphm@ik.nu>
parents: 59
diff changeset
390 d1 = self.parent.storage.get_affiliations(entity)
ad08e021b4d5 Implement AffiliationsService.
Ralph Meijer <ralphm@ik.nu>
parents: 59
diff changeset
391 d2 = self.parent.storage.get_subscriptions(entity)
92
878a5b7697f2 Consume errors in DeferredLists.
Ralph Meijer <ralphm@ik.nu>
parents: 90
diff changeset
392 d = defer.DeferredList([d1, d2], fireOnOneErrback=1, consumeErrors=1)
61
ad08e021b4d5 Implement AffiliationsService.
Ralph Meijer <ralphm@ik.nu>
parents: 59
diff changeset
393 d.addErrback(lambda x: x.value[0])
ad08e021b4d5 Implement AffiliationsService.
Ralph Meijer <ralphm@ik.nu>
parents: 59
diff changeset
394 d.addCallback(self._affiliations_result, entity)
ad08e021b4d5 Implement AffiliationsService.
Ralph Meijer <ralphm@ik.nu>
parents: 59
diff changeset
395 return d
ad08e021b4d5 Implement AffiliationsService.
Ralph Meijer <ralphm@ik.nu>
parents: 59
diff changeset
396
ad08e021b4d5 Implement AffiliationsService.
Ralph Meijer <ralphm@ik.nu>
parents: 59
diff changeset
397 def _affiliations_result(self, result, entity):
ad08e021b4d5 Implement AffiliationsService.
Ralph Meijer <ralphm@ik.nu>
parents: 59
diff changeset
398 affiliations = result[0][1]
ad08e021b4d5 Implement AffiliationsService.
Ralph Meijer <ralphm@ik.nu>
parents: 59
diff changeset
399 subscriptions = result[1][1]
ad08e021b4d5 Implement AffiliationsService.
Ralph Meijer <ralphm@ik.nu>
parents: 59
diff changeset
400
ad08e021b4d5 Implement AffiliationsService.
Ralph Meijer <ralphm@ik.nu>
parents: 59
diff changeset
401 new_affiliations = {}
ad08e021b4d5 Implement AffiliationsService.
Ralph Meijer <ralphm@ik.nu>
parents: 59
diff changeset
402
ad08e021b4d5 Implement AffiliationsService.
Ralph Meijer <ralphm@ik.nu>
parents: 59
diff changeset
403 for node, affiliation in affiliations:
ad08e021b4d5 Implement AffiliationsService.
Ralph Meijer <ralphm@ik.nu>
parents: 59
diff changeset
404 new_affiliations[(node, entity.full())] = {'node': node,
ad08e021b4d5 Implement AffiliationsService.
Ralph Meijer <ralphm@ik.nu>
parents: 59
diff changeset
405 'jid': entity,
ad08e021b4d5 Implement AffiliationsService.
Ralph Meijer <ralphm@ik.nu>
parents: 59
diff changeset
406 'affiliation': affiliation,
ad08e021b4d5 Implement AffiliationsService.
Ralph Meijer <ralphm@ik.nu>
parents: 59
diff changeset
407 'subscription': None
ad08e021b4d5 Implement AffiliationsService.
Ralph Meijer <ralphm@ik.nu>
parents: 59
diff changeset
408 }
ad08e021b4d5 Implement AffiliationsService.
Ralph Meijer <ralphm@ik.nu>
parents: 59
diff changeset
409
ad08e021b4d5 Implement AffiliationsService.
Ralph Meijer <ralphm@ik.nu>
parents: 59
diff changeset
410 for node, subscriber, subscription in subscriptions:
ad08e021b4d5 Implement AffiliationsService.
Ralph Meijer <ralphm@ik.nu>
parents: 59
diff changeset
411 key = node, subscriber.full()
ad08e021b4d5 Implement AffiliationsService.
Ralph Meijer <ralphm@ik.nu>
parents: 59
diff changeset
412 if new_affiliations.has_key(key):
ad08e021b4d5 Implement AffiliationsService.
Ralph Meijer <ralphm@ik.nu>
parents: 59
diff changeset
413 new_affiliations[key]['subscription'] = subscription
ad08e021b4d5 Implement AffiliationsService.
Ralph Meijer <ralphm@ik.nu>
parents: 59
diff changeset
414 else:
ad08e021b4d5 Implement AffiliationsService.
Ralph Meijer <ralphm@ik.nu>
parents: 59
diff changeset
415 new_affiliations[key] = {'node': node,
ad08e021b4d5 Implement AffiliationsService.
Ralph Meijer <ralphm@ik.nu>
parents: 59
diff changeset
416 'jid': subscriber,
ad08e021b4d5 Implement AffiliationsService.
Ralph Meijer <ralphm@ik.nu>
parents: 59
diff changeset
417 'affiliation': None,
ad08e021b4d5 Implement AffiliationsService.
Ralph Meijer <ralphm@ik.nu>
parents: 59
diff changeset
418 'subscription': subscription}
ad08e021b4d5 Implement AffiliationsService.
Ralph Meijer <ralphm@ik.nu>
parents: 59
diff changeset
419
ad08e021b4d5 Implement AffiliationsService.
Ralph Meijer <ralphm@ik.nu>
parents: 59
diff changeset
420 return new_affiliations.values()
80
ec354aab3949 Implemented ItemRetrievalService.
Ralph Meijer <ralphm@ik.nu>
parents: 72
diff changeset
421
ec354aab3949 Implemented ItemRetrievalService.
Ralph Meijer <ralphm@ik.nu>
parents: 72
diff changeset
422 class ItemRetrievalService(service.Service):
ec354aab3949 Implemented ItemRetrievalService.
Ralph Meijer <ralphm@ik.nu>
parents: 72
diff changeset
423
105
8d8946e67fcb Move to Twisted 2.0.
Ralph Meijer <ralphm@ik.nu>
parents: 101
diff changeset
424 implements(IItemRetrievalService)
80
ec354aab3949 Implemented ItemRetrievalService.
Ralph Meijer <ralphm@ik.nu>
parents: 72
diff changeset
425
ec354aab3949 Implemented ItemRetrievalService.
Ralph Meijer <ralphm@ik.nu>
parents: 72
diff changeset
426 def get_items(self, node_id, requestor, max_items=None, item_ids=[]):
ec354aab3949 Implemented ItemRetrievalService.
Ralph Meijer <ralphm@ik.nu>
parents: 72
diff changeset
427 d = self.parent.storage.is_subscribed(node_id, requestor)
ec354aab3949 Implemented ItemRetrievalService.
Ralph Meijer <ralphm@ik.nu>
parents: 72
diff changeset
428 d.addCallback(self._do_get_items, node_id, max_items, item_ids)
ec354aab3949 Implemented ItemRetrievalService.
Ralph Meijer <ralphm@ik.nu>
parents: 72
diff changeset
429 return d
ec354aab3949 Implemented ItemRetrievalService.
Ralph Meijer <ralphm@ik.nu>
parents: 72
diff changeset
430
ec354aab3949 Implemented ItemRetrievalService.
Ralph Meijer <ralphm@ik.nu>
parents: 72
diff changeset
431 def _do_get_items(self, result, node_id, max_items, item_ids):
ec354aab3949 Implemented ItemRetrievalService.
Ralph Meijer <ralphm@ik.nu>
parents: 72
diff changeset
432 if not result:
ec354aab3949 Implemented ItemRetrievalService.
Ralph Meijer <ralphm@ik.nu>
parents: 72
diff changeset
433 raise NotAuthorized
ec354aab3949 Implemented ItemRetrievalService.
Ralph Meijer <ralphm@ik.nu>
parents: 72
diff changeset
434
ec354aab3949 Implemented ItemRetrievalService.
Ralph Meijer <ralphm@ik.nu>
parents: 72
diff changeset
435 if item_ids:
98
b9c449f4c167 Removed debugging junk
Ralph Meijer <ralphm@ik.nu>
parents: 95
diff changeset
436 return self.parent.storage.get_items_by_ids(node_id, item_ids)
80
ec354aab3949 Implemented ItemRetrievalService.
Ralph Meijer <ralphm@ik.nu>
parents: 72
diff changeset
437 else:
ec354aab3949 Implemented ItemRetrievalService.
Ralph Meijer <ralphm@ik.nu>
parents: 72
diff changeset
438 return self.parent.storage.get_items(node_id, max_items)
85
ec557449d1aa Implement node retraction, with storage support for pgsql.
Ralph Meijer <ralphm@ik.nu>
parents: 84
diff changeset
439
ec557449d1aa Implement node retraction, with storage support for pgsql.
Ralph Meijer <ralphm@ik.nu>
parents: 84
diff changeset
440 class RetractionService(service.Service):
ec557449d1aa Implement node retraction, with storage support for pgsql.
Ralph Meijer <ralphm@ik.nu>
parents: 84
diff changeset
441
105
8d8946e67fcb Move to Twisted 2.0.
Ralph Meijer <ralphm@ik.nu>
parents: 101
diff changeset
442 implements(IRetractionService)
85
ec557449d1aa Implement node retraction, with storage support for pgsql.
Ralph Meijer <ralphm@ik.nu>
parents: 84
diff changeset
443
ec557449d1aa Implement node retraction, with storage support for pgsql.
Ralph Meijer <ralphm@ik.nu>
parents: 84
diff changeset
444 def retract_item(self, node_id, item_ids, requestor):
ec557449d1aa Implement node retraction, with storage support for pgsql.
Ralph Meijer <ralphm@ik.nu>
parents: 84
diff changeset
445 d1 = self.parent.storage.get_node_configuration(node_id)
ec557449d1aa Implement node retraction, with storage support for pgsql.
Ralph Meijer <ralphm@ik.nu>
parents: 84
diff changeset
446 d2 = self.parent.storage.get_affiliation(node_id, requestor)
ec557449d1aa Implement node retraction, with storage support for pgsql.
Ralph Meijer <ralphm@ik.nu>
parents: 84
diff changeset
447 d = defer.DeferredList([d1, d2], fireOnOneErrback=1)
ec557449d1aa Implement node retraction, with storage support for pgsql.
Ralph Meijer <ralphm@ik.nu>
parents: 84
diff changeset
448 d.addErrback(lambda x: x.value[0])
ec557449d1aa Implement node retraction, with storage support for pgsql.
Ralph Meijer <ralphm@ik.nu>
parents: 84
diff changeset
449 d.addCallback(self._do_retract, node_id, item_ids)
ec557449d1aa Implement node retraction, with storage support for pgsql.
Ralph Meijer <ralphm@ik.nu>
parents: 84
diff changeset
450 return d
ec557449d1aa Implement node retraction, with storage support for pgsql.
Ralph Meijer <ralphm@ik.nu>
parents: 84
diff changeset
451
ec557449d1aa Implement node retraction, with storage support for pgsql.
Ralph Meijer <ralphm@ik.nu>
parents: 84
diff changeset
452 def _do_retract(self, result, node_id, item_ids):
ec557449d1aa Implement node retraction, with storage support for pgsql.
Ralph Meijer <ralphm@ik.nu>
parents: 84
diff changeset
453 configuration = result[0][1]
ec557449d1aa Implement node retraction, with storage support for pgsql.
Ralph Meijer <ralphm@ik.nu>
parents: 84
diff changeset
454 persist_items = configuration["persist_items"]
ec557449d1aa Implement node retraction, with storage support for pgsql.
Ralph Meijer <ralphm@ik.nu>
parents: 84
diff changeset
455 affiliation = result[1][1]
ec557449d1aa Implement node retraction, with storage support for pgsql.
Ralph Meijer <ralphm@ik.nu>
parents: 84
diff changeset
456
ec557449d1aa Implement node retraction, with storage support for pgsql.
Ralph Meijer <ralphm@ik.nu>
parents: 84
diff changeset
457 if affiliation not in ['owner', 'publisher']:
ec557449d1aa Implement node retraction, with storage support for pgsql.
Ralph Meijer <ralphm@ik.nu>
parents: 84
diff changeset
458 raise NotAuthorized
ec557449d1aa Implement node retraction, with storage support for pgsql.
Ralph Meijer <ralphm@ik.nu>
parents: 84
diff changeset
459
ec557449d1aa Implement node retraction, with storage support for pgsql.
Ralph Meijer <ralphm@ik.nu>
parents: 84
diff changeset
460 if not persist_items:
ec557449d1aa Implement node retraction, with storage support for pgsql.
Ralph Meijer <ralphm@ik.nu>
parents: 84
diff changeset
461 raise NodeNotPersistent
ec557449d1aa Implement node retraction, with storage support for pgsql.
Ralph Meijer <ralphm@ik.nu>
parents: 84
diff changeset
462
ec557449d1aa Implement node retraction, with storage support for pgsql.
Ralph Meijer <ralphm@ik.nu>
parents: 84
diff changeset
463 d = self.parent.storage.remove_items(node_id, item_ids)
ec557449d1aa Implement node retraction, with storage support for pgsql.
Ralph Meijer <ralphm@ik.nu>
parents: 84
diff changeset
464 d.addCallback(self._do_notify_retraction, node_id)
ec557449d1aa Implement node retraction, with storage support for pgsql.
Ralph Meijer <ralphm@ik.nu>
parents: 84
diff changeset
465 return d
ec557449d1aa Implement node retraction, with storage support for pgsql.
Ralph Meijer <ralphm@ik.nu>
parents: 84
diff changeset
466
ec557449d1aa Implement node retraction, with storage support for pgsql.
Ralph Meijer <ralphm@ik.nu>
parents: 84
diff changeset
467 def _do_notify_retraction(self, result, node_id):
ec557449d1aa Implement node retraction, with storage support for pgsql.
Ralph Meijer <ralphm@ik.nu>
parents: 84
diff changeset
468 self.parent.dispatch({ 'item_ids': result, 'node_id': node_id },
ec557449d1aa Implement node retraction, with storage support for pgsql.
Ralph Meijer <ralphm@ik.nu>
parents: 84
diff changeset
469 '//event/pubsub/retract')
ec557449d1aa Implement node retraction, with storage support for pgsql.
Ralph Meijer <ralphm@ik.nu>
parents: 84
diff changeset
470
ec557449d1aa Implement node retraction, with storage support for pgsql.
Ralph Meijer <ralphm@ik.nu>
parents: 84
diff changeset
471 def purge_node(self, node_id, requestor):
90
59378610b16e Implement node purging and node deletion.
Ralph Meijer <ralphm@ik.nu>
parents: 85
diff changeset
472 d1 = self.parent.storage.get_node_configuration(node_id)
59378610b16e Implement node purging and node deletion.
Ralph Meijer <ralphm@ik.nu>
parents: 85
diff changeset
473 d2 = self.parent.storage.get_affiliation(node_id, requestor)
59378610b16e Implement node purging and node deletion.
Ralph Meijer <ralphm@ik.nu>
parents: 85
diff changeset
474 d = defer.DeferredList([d1, d2], fireOnOneErrback=1)
59378610b16e Implement node purging and node deletion.
Ralph Meijer <ralphm@ik.nu>
parents: 85
diff changeset
475 d.addErrback(lambda x: x.value[0])
59378610b16e Implement node purging and node deletion.
Ralph Meijer <ralphm@ik.nu>
parents: 85
diff changeset
476 d.addCallback(self._do_purge, node_id)
59378610b16e Implement node purging and node deletion.
Ralph Meijer <ralphm@ik.nu>
parents: 85
diff changeset
477 return d
59378610b16e Implement node purging and node deletion.
Ralph Meijer <ralphm@ik.nu>
parents: 85
diff changeset
478
59378610b16e Implement node purging and node deletion.
Ralph Meijer <ralphm@ik.nu>
parents: 85
diff changeset
479 def _do_purge(self, result, node_id):
59378610b16e Implement node purging and node deletion.
Ralph Meijer <ralphm@ik.nu>
parents: 85
diff changeset
480 configuration = result[0][1]
59378610b16e Implement node purging and node deletion.
Ralph Meijer <ralphm@ik.nu>
parents: 85
diff changeset
481 persist_items = configuration["persist_items"]
59378610b16e Implement node purging and node deletion.
Ralph Meijer <ralphm@ik.nu>
parents: 85
diff changeset
482 affiliation = result[1][1]
59378610b16e Implement node purging and node deletion.
Ralph Meijer <ralphm@ik.nu>
parents: 85
diff changeset
483
59378610b16e Implement node purging and node deletion.
Ralph Meijer <ralphm@ik.nu>
parents: 85
diff changeset
484 if affiliation != 'owner':
59378610b16e Implement node purging and node deletion.
Ralph Meijer <ralphm@ik.nu>
parents: 85
diff changeset
485 raise NotAuthorized
59378610b16e Implement node purging and node deletion.
Ralph Meijer <ralphm@ik.nu>
parents: 85
diff changeset
486
59378610b16e Implement node purging and node deletion.
Ralph Meijer <ralphm@ik.nu>
parents: 85
diff changeset
487 if not persist_items:
59378610b16e Implement node purging and node deletion.
Ralph Meijer <ralphm@ik.nu>
parents: 85
diff changeset
488 raise NodeNotPersistent
59378610b16e Implement node purging and node deletion.
Ralph Meijer <ralphm@ik.nu>
parents: 85
diff changeset
489
59378610b16e Implement node purging and node deletion.
Ralph Meijer <ralphm@ik.nu>
parents: 85
diff changeset
490 d = self.parent.storage.purge_node(node_id)
59378610b16e Implement node purging and node deletion.
Ralph Meijer <ralphm@ik.nu>
parents: 85
diff changeset
491 d.addCallback(self._do_notify_purge, node_id)
59378610b16e Implement node purging and node deletion.
Ralph Meijer <ralphm@ik.nu>
parents: 85
diff changeset
492 return d
59378610b16e Implement node purging and node deletion.
Ralph Meijer <ralphm@ik.nu>
parents: 85
diff changeset
493
59378610b16e Implement node purging and node deletion.
Ralph Meijer <ralphm@ik.nu>
parents: 85
diff changeset
494 def _do_notify_purge(self, result, node_id):
59378610b16e Implement node purging and node deletion.
Ralph Meijer <ralphm@ik.nu>
parents: 85
diff changeset
495 self.parent.dispatch(node_id, '//event/pubsub/purge')
59378610b16e Implement node purging and node deletion.
Ralph Meijer <ralphm@ik.nu>
parents: 85
diff changeset
496
59378610b16e Implement node purging and node deletion.
Ralph Meijer <ralphm@ik.nu>
parents: 85
diff changeset
497 class NodeDeletionService(service.Service):
59378610b16e Implement node purging and node deletion.
Ralph Meijer <ralphm@ik.nu>
parents: 85
diff changeset
498
105
8d8946e67fcb Move to Twisted 2.0.
Ralph Meijer <ralphm@ik.nu>
parents: 101
diff changeset
499 implements(INodeDeletionService)
90
59378610b16e Implement node purging and node deletion.
Ralph Meijer <ralphm@ik.nu>
parents: 85
diff changeset
500
59378610b16e Implement node purging and node deletion.
Ralph Meijer <ralphm@ik.nu>
parents: 85
diff changeset
501 def __init__(self):
59378610b16e Implement node purging and node deletion.
Ralph Meijer <ralphm@ik.nu>
parents: 85
diff changeset
502 self._callback_list = []
59378610b16e Implement node purging and node deletion.
Ralph Meijer <ralphm@ik.nu>
parents: 85
diff changeset
503
59378610b16e Implement node purging and node deletion.
Ralph Meijer <ralphm@ik.nu>
parents: 85
diff changeset
504 def register_pre_delete(self, pre_delete_fn):
59378610b16e Implement node purging and node deletion.
Ralph Meijer <ralphm@ik.nu>
parents: 85
diff changeset
505 self._callback_list.append(pre_delete_fn)
59378610b16e Implement node purging and node deletion.
Ralph Meijer <ralphm@ik.nu>
parents: 85
diff changeset
506
59378610b16e Implement node purging and node deletion.
Ralph Meijer <ralphm@ik.nu>
parents: 85
diff changeset
507 def get_subscribers(self, node_id):
59378610b16e Implement node purging and node deletion.
Ralph Meijer <ralphm@ik.nu>
parents: 85
diff changeset
508 return self.parent.storage.get_subscribers(node_id)
59378610b16e Implement node purging and node deletion.
Ralph Meijer <ralphm@ik.nu>
parents: 85
diff changeset
509
59378610b16e Implement node purging and node deletion.
Ralph Meijer <ralphm@ik.nu>
parents: 85
diff changeset
510 def delete_node(self, node_id, requestor):
59378610b16e Implement node purging and node deletion.
Ralph Meijer <ralphm@ik.nu>
parents: 85
diff changeset
511 d1 = self.parent.storage.get_node_configuration(node_id)
59378610b16e Implement node purging and node deletion.
Ralph Meijer <ralphm@ik.nu>
parents: 85
diff changeset
512 d2 = self.parent.storage.get_affiliation(node_id, requestor)
59378610b16e Implement node purging and node deletion.
Ralph Meijer <ralphm@ik.nu>
parents: 85
diff changeset
513 d = defer.DeferredList([d1, d2], fireOnOneErrback=1)
59378610b16e Implement node purging and node deletion.
Ralph Meijer <ralphm@ik.nu>
parents: 85
diff changeset
514 d.addErrback(lambda x: x.value[0])
59378610b16e Implement node purging and node deletion.
Ralph Meijer <ralphm@ik.nu>
parents: 85
diff changeset
515 d.addCallback(self._do_pre_delete, node_id)
59378610b16e Implement node purging and node deletion.
Ralph Meijer <ralphm@ik.nu>
parents: 85
diff changeset
516 return d
59378610b16e Implement node purging and node deletion.
Ralph Meijer <ralphm@ik.nu>
parents: 85
diff changeset
517
59378610b16e Implement node purging and node deletion.
Ralph Meijer <ralphm@ik.nu>
parents: 85
diff changeset
518 def _do_pre_delete(self, result, node_id):
59378610b16e Implement node purging and node deletion.
Ralph Meijer <ralphm@ik.nu>
parents: 85
diff changeset
519 configuration = result[0][1]
59378610b16e Implement node purging and node deletion.
Ralph Meijer <ralphm@ik.nu>
parents: 85
diff changeset
520 persist_items = configuration["persist_items"]
59378610b16e Implement node purging and node deletion.
Ralph Meijer <ralphm@ik.nu>
parents: 85
diff changeset
521 affiliation = result[1][1]
59378610b16e Implement node purging and node deletion.
Ralph Meijer <ralphm@ik.nu>
parents: 85
diff changeset
522
59378610b16e Implement node purging and node deletion.
Ralph Meijer <ralphm@ik.nu>
parents: 85
diff changeset
523 if affiliation != 'owner':
59378610b16e Implement node purging and node deletion.
Ralph Meijer <ralphm@ik.nu>
parents: 85
diff changeset
524 raise NotAuthorized
59378610b16e Implement node purging and node deletion.
Ralph Meijer <ralphm@ik.nu>
parents: 85
diff changeset
525
59378610b16e Implement node purging and node deletion.
Ralph Meijer <ralphm@ik.nu>
parents: 85
diff changeset
526 d = defer.DeferredList([cb(node_id) for cb in self._callback_list],
59378610b16e Implement node purging and node deletion.
Ralph Meijer <ralphm@ik.nu>
parents: 85
diff changeset
527 consumeErrors=1)
59378610b16e Implement node purging and node deletion.
Ralph Meijer <ralphm@ik.nu>
parents: 85
diff changeset
528 d.addCallback(self._do_delete, node_id)
59378610b16e Implement node purging and node deletion.
Ralph Meijer <ralphm@ik.nu>
parents: 85
diff changeset
529
59378610b16e Implement node purging and node deletion.
Ralph Meijer <ralphm@ik.nu>
parents: 85
diff changeset
530 def _do_delete(self, result, node_id):
59378610b16e Implement node purging and node deletion.
Ralph Meijer <ralphm@ik.nu>
parents: 85
diff changeset
531 dl = []
59378610b16e Implement node purging and node deletion.
Ralph Meijer <ralphm@ik.nu>
parents: 85
diff changeset
532 for succeeded, r in result:
59378610b16e Implement node purging and node deletion.
Ralph Meijer <ralphm@ik.nu>
parents: 85
diff changeset
533 if succeeded and r:
59378610b16e Implement node purging and node deletion.
Ralph Meijer <ralphm@ik.nu>
parents: 85
diff changeset
534 dl.extend(r)
59378610b16e Implement node purging and node deletion.
Ralph Meijer <ralphm@ik.nu>
parents: 85
diff changeset
535
59378610b16e Implement node purging and node deletion.
Ralph Meijer <ralphm@ik.nu>
parents: 85
diff changeset
536 d = self.parent.storage.delete_node(node_id)
59378610b16e Implement node purging and node deletion.
Ralph Meijer <ralphm@ik.nu>
parents: 85
diff changeset
537 d.addCallback(self._do_notify_delete, dl)
59378610b16e Implement node purging and node deletion.
Ralph Meijer <ralphm@ik.nu>
parents: 85
diff changeset
538
59378610b16e Implement node purging and node deletion.
Ralph Meijer <ralphm@ik.nu>
parents: 85
diff changeset
539 return d
59378610b16e Implement node purging and node deletion.
Ralph Meijer <ralphm@ik.nu>
parents: 85
diff changeset
540
59378610b16e Implement node purging and node deletion.
Ralph Meijer <ralphm@ik.nu>
parents: 85
diff changeset
541 def _do_notify_delete(self, result, dl):
59378610b16e Implement node purging and node deletion.
Ralph Meijer <ralphm@ik.nu>
parents: 85
diff changeset
542 for d in dl:
59378610b16e Implement node purging and node deletion.
Ralph Meijer <ralphm@ik.nu>
parents: 85
diff changeset
543 d.callback(None)