Mercurial > libervia-pubsub
annotate idavoll/memory_backend.py @ 59:0fa161c00ed9
Use jid.JIDs everywhere in the backend.
Fix subscription list retrieval in pgsql_backend, thereby actually making
notifications work.
author | Ralph Meijer <ralphm@ik.nu> |
---|---|
date | Sat, 06 Nov 2004 17:07:47 +0000 |
parents | 62fdb37234e5 |
children | 0e642e5b4fb2 |
rev | line source |
---|---|
27 | 1 from twisted.application import service |
2 from twisted.internet import defer | |
3 from twisted.protocols.jabber import jid | |
4 import backend | |
5 | |
6 class Subscription: | |
30
ff7c73b253bf
Recode backend to match interaces: one big Service that handles everything
Ralph Meijer <ralphm@ik.nu>
parents:
27
diff
changeset
|
7 def __init__(self, state): |
ff7c73b253bf
Recode backend to match interaces: one big Service that handles everything
Ralph Meijer <ralphm@ik.nu>
parents:
27
diff
changeset
|
8 self.state = state |
27 | 9 |
10 class NodeConfiguration: | |
30
ff7c73b253bf
Recode backend to match interaces: one big Service that handles everything
Ralph Meijer <ralphm@ik.nu>
parents:
27
diff
changeset
|
11 def __init__(self): |
ff7c73b253bf
Recode backend to match interaces: one big Service that handles everything
Ralph Meijer <ralphm@ik.nu>
parents:
27
diff
changeset
|
12 self.persist_items = False |
ff7c73b253bf
Recode backend to match interaces: one big Service that handles everything
Ralph Meijer <ralphm@ik.nu>
parents:
27
diff
changeset
|
13 self.deliver_payloads = False |
27 | 14 |
15 class Node: | |
30
ff7c73b253bf
Recode backend to match interaces: one big Service that handles everything
Ralph Meijer <ralphm@ik.nu>
parents:
27
diff
changeset
|
16 def __init__(self, id): |
ff7c73b253bf
Recode backend to match interaces: one big Service that handles everything
Ralph Meijer <ralphm@ik.nu>
parents:
27
diff
changeset
|
17 self.id = id |
ff7c73b253bf
Recode backend to match interaces: one big Service that handles everything
Ralph Meijer <ralphm@ik.nu>
parents:
27
diff
changeset
|
18 self.configuration = NodeConfiguration() |
ff7c73b253bf
Recode backend to match interaces: one big Service that handles everything
Ralph Meijer <ralphm@ik.nu>
parents:
27
diff
changeset
|
19 self.subscriptions = {} |
ff7c73b253bf
Recode backend to match interaces: one big Service that handles everything
Ralph Meijer <ralphm@ik.nu>
parents:
27
diff
changeset
|
20 self.affiliations = {} |
ff7c73b253bf
Recode backend to match interaces: one big Service that handles everything
Ralph Meijer <ralphm@ik.nu>
parents:
27
diff
changeset
|
21 self.items = {} |
27 | 22 |
43
9685b7e291ef
Moved common stuff out of pgsql_backend.py to backend.py.
Ralph Meijer <ralphm@ik.nu>
parents:
40
diff
changeset
|
23 class Storage: |
30
ff7c73b253bf
Recode backend to match interaces: one big Service that handles everything
Ralph Meijer <ralphm@ik.nu>
parents:
27
diff
changeset
|
24 def __init__(self): |
ff7c73b253bf
Recode backend to match interaces: one big Service that handles everything
Ralph Meijer <ralphm@ik.nu>
parents:
27
diff
changeset
|
25 self.nodes = {} |
27 | 26 |
30
ff7c73b253bf
Recode backend to match interaces: one big Service that handles everything
Ralph Meijer <ralphm@ik.nu>
parents:
27
diff
changeset
|
27 node = Node("ralphm/mood/ralphm@ik.nu") |
ff7c73b253bf
Recode backend to match interaces: one big Service that handles everything
Ralph Meijer <ralphm@ik.nu>
parents:
27
diff
changeset
|
28 node.subscriptions["ralphm@doe.ik.nu"] = Subscription("subscribed") |
ff7c73b253bf
Recode backend to match interaces: one big Service that handles everything
Ralph Meijer <ralphm@ik.nu>
parents:
27
diff
changeset
|
29 node.subscriptions["notify@ik.nu/mood_monitor"] = Subscription("subscribed") |
ff7c73b253bf
Recode backend to match interaces: one big Service that handles everything
Ralph Meijer <ralphm@ik.nu>
parents:
27
diff
changeset
|
30 node.affiliations["ralphm@ik.nu"] = "owner" |
ff7c73b253bf
Recode backend to match interaces: one big Service that handles everything
Ralph Meijer <ralphm@ik.nu>
parents:
27
diff
changeset
|
31 node.affiliations["ralphm@doe.ik.nu"] = "publisher" |
ff7c73b253bf
Recode backend to match interaces: one big Service that handles everything
Ralph Meijer <ralphm@ik.nu>
parents:
27
diff
changeset
|
32 node.configuration.persist_items = True |
ff7c73b253bf
Recode backend to match interaces: one big Service that handles everything
Ralph Meijer <ralphm@ik.nu>
parents:
27
diff
changeset
|
33 node.configuration.deliver_payloads = True |
ff7c73b253bf
Recode backend to match interaces: one big Service that handles everything
Ralph Meijer <ralphm@ik.nu>
parents:
27
diff
changeset
|
34 self.nodes[node.id] = node |
43
9685b7e291ef
Moved common stuff out of pgsql_backend.py to backend.py.
Ralph Meijer <ralphm@ik.nu>
parents:
40
diff
changeset
|
35 |
9685b7e291ef
Moved common stuff out of pgsql_backend.py to backend.py.
Ralph Meijer <ralphm@ik.nu>
parents:
40
diff
changeset
|
36 def get_node_configuration(self, node_id): |
9685b7e291ef
Moved common stuff out of pgsql_backend.py to backend.py.
Ralph Meijer <ralphm@ik.nu>
parents:
40
diff
changeset
|
37 try: |
9685b7e291ef
Moved common stuff out of pgsql_backend.py to backend.py.
Ralph Meijer <ralphm@ik.nu>
parents:
40
diff
changeset
|
38 node = self.nodes[node_id] |
9685b7e291ef
Moved common stuff out of pgsql_backend.py to backend.py.
Ralph Meijer <ralphm@ik.nu>
parents:
40
diff
changeset
|
39 except KeyError: |
9685b7e291ef
Moved common stuff out of pgsql_backend.py to backend.py.
Ralph Meijer <ralphm@ik.nu>
parents:
40
diff
changeset
|
40 raise backend.NodeNotFound |
9685b7e291ef
Moved common stuff out of pgsql_backend.py to backend.py.
Ralph Meijer <ralphm@ik.nu>
parents:
40
diff
changeset
|
41 else: |
9685b7e291ef
Moved common stuff out of pgsql_backend.py to backend.py.
Ralph Meijer <ralphm@ik.nu>
parents:
40
diff
changeset
|
42 c = self.nodes[node_id].configuration |
9685b7e291ef
Moved common stuff out of pgsql_backend.py to backend.py.
Ralph Meijer <ralphm@ik.nu>
parents:
40
diff
changeset
|
43 return defer.succeed({'persist_items': c.persist_items, |
9685b7e291ef
Moved common stuff out of pgsql_backend.py to backend.py.
Ralph Meijer <ralphm@ik.nu>
parents:
40
diff
changeset
|
44 'deliver_payloads': c.deliver_payloads}) |
9685b7e291ef
Moved common stuff out of pgsql_backend.py to backend.py.
Ralph Meijer <ralphm@ik.nu>
parents:
40
diff
changeset
|
45 |
9685b7e291ef
Moved common stuff out of pgsql_backend.py to backend.py.
Ralph Meijer <ralphm@ik.nu>
parents:
40
diff
changeset
|
46 def get_affiliation(self, node_id, entity): |
9685b7e291ef
Moved common stuff out of pgsql_backend.py to backend.py.
Ralph Meijer <ralphm@ik.nu>
parents:
40
diff
changeset
|
47 try: |
9685b7e291ef
Moved common stuff out of pgsql_backend.py to backend.py.
Ralph Meijer <ralphm@ik.nu>
parents:
40
diff
changeset
|
48 node = self.nodes[node_id] |
9685b7e291ef
Moved common stuff out of pgsql_backend.py to backend.py.
Ralph Meijer <ralphm@ik.nu>
parents:
40
diff
changeset
|
49 except KeyError: |
9685b7e291ef
Moved common stuff out of pgsql_backend.py to backend.py.
Ralph Meijer <ralphm@ik.nu>
parents:
40
diff
changeset
|
50 raise backend.NodeNotFound |
9685b7e291ef
Moved common stuff out of pgsql_backend.py to backend.py.
Ralph Meijer <ralphm@ik.nu>
parents:
40
diff
changeset
|
51 else: |
59
0fa161c00ed9
Use jid.JIDs everywhere in the backend.
Ralph Meijer <ralphm@ik.nu>
parents:
54
diff
changeset
|
52 return defer.succeed(node.affiliations.get(entity.full(), None)) |
43
9685b7e291ef
Moved common stuff out of pgsql_backend.py to backend.py.
Ralph Meijer <ralphm@ik.nu>
parents:
40
diff
changeset
|
53 |
9685b7e291ef
Moved common stuff out of pgsql_backend.py to backend.py.
Ralph Meijer <ralphm@ik.nu>
parents:
40
diff
changeset
|
54 def get_subscribers(self, node_id): |
9685b7e291ef
Moved common stuff out of pgsql_backend.py to backend.py.
Ralph Meijer <ralphm@ik.nu>
parents:
40
diff
changeset
|
55 try: |
9685b7e291ef
Moved common stuff out of pgsql_backend.py to backend.py.
Ralph Meijer <ralphm@ik.nu>
parents:
40
diff
changeset
|
56 node = self.nodes[node_id] |
9685b7e291ef
Moved common stuff out of pgsql_backend.py to backend.py.
Ralph Meijer <ralphm@ik.nu>
parents:
40
diff
changeset
|
57 except KeyError: |
9685b7e291ef
Moved common stuff out of pgsql_backend.py to backend.py.
Ralph Meijer <ralphm@ik.nu>
parents:
40
diff
changeset
|
58 raise backend.NodeNotFound |
9685b7e291ef
Moved common stuff out of pgsql_backend.py to backend.py.
Ralph Meijer <ralphm@ik.nu>
parents:
40
diff
changeset
|
59 else: |
9685b7e291ef
Moved common stuff out of pgsql_backend.py to backend.py.
Ralph Meijer <ralphm@ik.nu>
parents:
40
diff
changeset
|
60 subscriptions = self.nodes[node_id].subscriptions |
59
0fa161c00ed9
Use jid.JIDs everywhere in the backend.
Ralph Meijer <ralphm@ik.nu>
parents:
54
diff
changeset
|
61 subscribers = [jid.JID(s) for s in subscriptions |
43
9685b7e291ef
Moved common stuff out of pgsql_backend.py to backend.py.
Ralph Meijer <ralphm@ik.nu>
parents:
40
diff
changeset
|
62 if subscriptions[s].state == 'subscribed'] |
9685b7e291ef
Moved common stuff out of pgsql_backend.py to backend.py.
Ralph Meijer <ralphm@ik.nu>
parents:
40
diff
changeset
|
63 return defer.succeed(subscribers) |
9685b7e291ef
Moved common stuff out of pgsql_backend.py to backend.py.
Ralph Meijer <ralphm@ik.nu>
parents:
40
diff
changeset
|
64 |
9685b7e291ef
Moved common stuff out of pgsql_backend.py to backend.py.
Ralph Meijer <ralphm@ik.nu>
parents:
40
diff
changeset
|
65 def store_items(self, node_id, items, publisher): |
9685b7e291ef
Moved common stuff out of pgsql_backend.py to backend.py.
Ralph Meijer <ralphm@ik.nu>
parents:
40
diff
changeset
|
66 for item in items: |
9685b7e291ef
Moved common stuff out of pgsql_backend.py to backend.py.
Ralph Meijer <ralphm@ik.nu>
parents:
40
diff
changeset
|
67 self.nodes[node_id].items[item["id"]] = (item, publisher) |
9685b7e291ef
Moved common stuff out of pgsql_backend.py to backend.py.
Ralph Meijer <ralphm@ik.nu>
parents:
40
diff
changeset
|
68 return defer.succeed(None) |
9685b7e291ef
Moved common stuff out of pgsql_backend.py to backend.py.
Ralph Meijer <ralphm@ik.nu>
parents:
40
diff
changeset
|
69 |
50
64f0986d8b35
Implement add_subscription() and remove_subscription() for Storage, remove
Ralph Meijer <ralphm@ik.nu>
parents:
46
diff
changeset
|
70 def add_subscription(self, node_id, subscriber, state): |
30
ff7c73b253bf
Recode backend to match interaces: one big Service that handles everything
Ralph Meijer <ralphm@ik.nu>
parents:
27
diff
changeset
|
71 try: |
ff7c73b253bf
Recode backend to match interaces: one big Service that handles everything
Ralph Meijer <ralphm@ik.nu>
parents:
27
diff
changeset
|
72 node = self.nodes[node_id] |
ff7c73b253bf
Recode backend to match interaces: one big Service that handles everything
Ralph Meijer <ralphm@ik.nu>
parents:
27
diff
changeset
|
73 except KeyError: |
ff7c73b253bf
Recode backend to match interaces: one big Service that handles everything
Ralph Meijer <ralphm@ik.nu>
parents:
27
diff
changeset
|
74 raise backend.NodeNotFound |
27 | 75 |
50
64f0986d8b35
Implement add_subscription() and remove_subscription() for Storage, remove
Ralph Meijer <ralphm@ik.nu>
parents:
46
diff
changeset
|
76 try: |
59
0fa161c00ed9
Use jid.JIDs everywhere in the backend.
Ralph Meijer <ralphm@ik.nu>
parents:
54
diff
changeset
|
77 subscription = node.subscriptions[subscriber.full()] |
50
64f0986d8b35
Implement add_subscription() and remove_subscription() for Storage, remove
Ralph Meijer <ralphm@ik.nu>
parents:
46
diff
changeset
|
78 except: |
64f0986d8b35
Implement add_subscription() and remove_subscription() for Storage, remove
Ralph Meijer <ralphm@ik.nu>
parents:
46
diff
changeset
|
79 subscription = Subscription(state) |
59
0fa161c00ed9
Use jid.JIDs everywhere in the backend.
Ralph Meijer <ralphm@ik.nu>
parents:
54
diff
changeset
|
80 node.subscriptions[subscriber.full()] = subscription |
27 | 81 |
50
64f0986d8b35
Implement add_subscription() and remove_subscription() for Storage, remove
Ralph Meijer <ralphm@ik.nu>
parents:
46
diff
changeset
|
82 return defer.succeed({'node': node_id, |
64f0986d8b35
Implement add_subscription() and remove_subscription() for Storage, remove
Ralph Meijer <ralphm@ik.nu>
parents:
46
diff
changeset
|
83 'jid': subscriber, |
64f0986d8b35
Implement add_subscription() and remove_subscription() for Storage, remove
Ralph Meijer <ralphm@ik.nu>
parents:
46
diff
changeset
|
84 'subscription': subscription.state}) |
27 | 85 |
50
64f0986d8b35
Implement add_subscription() and remove_subscription() for Storage, remove
Ralph Meijer <ralphm@ik.nu>
parents:
46
diff
changeset
|
86 def remove_subscription(self, node_id, subscriber): |
37
9aa20efac203
Fix get_notification_list() to only return JIDs for subscriptions in the
Ralph Meijer <ralphm@ik.nu>
parents:
30
diff
changeset
|
87 try: |
9aa20efac203
Fix get_notification_list() to only return JIDs for subscriptions in the
Ralph Meijer <ralphm@ik.nu>
parents:
30
diff
changeset
|
88 node = self.nodes[node_id] |
9aa20efac203
Fix get_notification_list() to only return JIDs for subscriptions in the
Ralph Meijer <ralphm@ik.nu>
parents:
30
diff
changeset
|
89 except KeyError: |
9aa20efac203
Fix get_notification_list() to only return JIDs for subscriptions in the
Ralph Meijer <ralphm@ik.nu>
parents:
30
diff
changeset
|
90 raise backend.NodeNotFound |
9aa20efac203
Fix get_notification_list() to only return JIDs for subscriptions in the
Ralph Meijer <ralphm@ik.nu>
parents:
30
diff
changeset
|
91 |
9aa20efac203
Fix get_notification_list() to only return JIDs for subscriptions in the
Ralph Meijer <ralphm@ik.nu>
parents:
30
diff
changeset
|
92 try: |
59
0fa161c00ed9
Use jid.JIDs everywhere in the backend.
Ralph Meijer <ralphm@ik.nu>
parents:
54
diff
changeset
|
93 del node.subscriptions[subscriber.full()] |
37
9aa20efac203
Fix get_notification_list() to only return JIDs for subscriptions in the
Ralph Meijer <ralphm@ik.nu>
parents:
30
diff
changeset
|
94 except KeyError: |
9aa20efac203
Fix get_notification_list() to only return JIDs for subscriptions in the
Ralph Meijer <ralphm@ik.nu>
parents:
30
diff
changeset
|
95 raise backend.NotSubscribed |
9aa20efac203
Fix get_notification_list() to only return JIDs for subscriptions in the
Ralph Meijer <ralphm@ik.nu>
parents:
30
diff
changeset
|
96 |
9aa20efac203
Fix get_notification_list() to only return JIDs for subscriptions in the
Ralph Meijer <ralphm@ik.nu>
parents:
30
diff
changeset
|
97 return defer.succeed(None) |
9aa20efac203
Fix get_notification_list() to only return JIDs for subscriptions in the
Ralph Meijer <ralphm@ik.nu>
parents:
30
diff
changeset
|
98 |
54
62fdb37234e5
Make NodeCreationService a subclass of backend.NodeCreationService.
Ralph Meijer <ralphm@ik.nu>
parents:
50
diff
changeset
|
99 def create_node(self, node_id, owner): |
62fdb37234e5
Make NodeCreationService a subclass of backend.NodeCreationService.
Ralph Meijer <ralphm@ik.nu>
parents:
50
diff
changeset
|
100 if node_id in self.nodes: |
62fdb37234e5
Make NodeCreationService a subclass of backend.NodeCreationService.
Ralph Meijer <ralphm@ik.nu>
parents:
50
diff
changeset
|
101 raise backend.NodeExists |
62fdb37234e5
Make NodeCreationService a subclass of backend.NodeCreationService.
Ralph Meijer <ralphm@ik.nu>
parents:
50
diff
changeset
|
102 |
62fdb37234e5
Make NodeCreationService a subclass of backend.NodeCreationService.
Ralph Meijer <ralphm@ik.nu>
parents:
50
diff
changeset
|
103 node = Node(node_id) |
59
0fa161c00ed9
Use jid.JIDs everywhere in the backend.
Ralph Meijer <ralphm@ik.nu>
parents:
54
diff
changeset
|
104 node.affiliations[owner.full()] = 'owner' |
54
62fdb37234e5
Make NodeCreationService a subclass of backend.NodeCreationService.
Ralph Meijer <ralphm@ik.nu>
parents:
50
diff
changeset
|
105 self.nodes[node_id] = node |
62fdb37234e5
Make NodeCreationService a subclass of backend.NodeCreationService.
Ralph Meijer <ralphm@ik.nu>
parents:
50
diff
changeset
|
106 |
62fdb37234e5
Make NodeCreationService a subclass of backend.NodeCreationService.
Ralph Meijer <ralphm@ik.nu>
parents:
50
diff
changeset
|
107 return defer.succeed(None) |
62fdb37234e5
Make NodeCreationService a subclass of backend.NodeCreationService.
Ralph Meijer <ralphm@ik.nu>
parents:
50
diff
changeset
|
108 |
50
64f0986d8b35
Implement add_subscription() and remove_subscription() for Storage, remove
Ralph Meijer <ralphm@ik.nu>
parents:
46
diff
changeset
|
109 class BackendService(backend.BackendService): |
64f0986d8b35
Implement add_subscription() and remove_subscription() for Storage, remove
Ralph Meijer <ralphm@ik.nu>
parents:
46
diff
changeset
|
110 pass |
64f0986d8b35
Implement add_subscription() and remove_subscription() for Storage, remove
Ralph Meijer <ralphm@ik.nu>
parents:
46
diff
changeset
|
111 |
54
62fdb37234e5
Make NodeCreationService a subclass of backend.NodeCreationService.
Ralph Meijer <ralphm@ik.nu>
parents:
50
diff
changeset
|
112 class NodeCreationService(backend.NodeCreationService): |
62fdb37234e5
Make NodeCreationService a subclass of backend.NodeCreationService.
Ralph Meijer <ralphm@ik.nu>
parents:
50
diff
changeset
|
113 pass |
30
ff7c73b253bf
Recode backend to match interaces: one big Service that handles everything
Ralph Meijer <ralphm@ik.nu>
parents:
27
diff
changeset
|
114 |
46
979e53b54267
Redefined several classes as subclasses of the implementations in backend.py.
Ralph Meijer <ralphm@ik.nu>
parents:
43
diff
changeset
|
115 class PublishService(backend.PublishService): |
979e53b54267
Redefined several classes as subclasses of the implementations in backend.py.
Ralph Meijer <ralphm@ik.nu>
parents:
43
diff
changeset
|
116 pass |
27 | 117 |
30
ff7c73b253bf
Recode backend to match interaces: one big Service that handles everything
Ralph Meijer <ralphm@ik.nu>
parents:
27
diff
changeset
|
118 class NotificationService(backend.NotificationService): |
46
979e53b54267
Redefined several classes as subclasses of the implementations in backend.py.
Ralph Meijer <ralphm@ik.nu>
parents:
43
diff
changeset
|
119 pass |
30
ff7c73b253bf
Recode backend to match interaces: one big Service that handles everything
Ralph Meijer <ralphm@ik.nu>
parents:
27
diff
changeset
|
120 |
50
64f0986d8b35
Implement add_subscription() and remove_subscription() for Storage, remove
Ralph Meijer <ralphm@ik.nu>
parents:
46
diff
changeset
|
121 class SubscriptionService(backend.SubscriptionService): |
64f0986d8b35
Implement add_subscription() and remove_subscription() for Storage, remove
Ralph Meijer <ralphm@ik.nu>
parents:
46
diff
changeset
|
122 pass |