Software /
code /
prosody
Annotate
util/pubsub.lua @ 3796:405231b1cb88
mod_pubsub, util.pubsub: Support node creation
author | Florian Zeitz <florob@babelmonkeys.de> |
---|---|
date | Wed, 01 Dec 2010 23:38:47 +0100 |
parent | 3641:3603aeb325de |
child | 3698:77171fd1dc3c |
rev | line source |
---|---|
3619 | 1 module("pubsub", package.seeall); |
2 | |
3 local service = {}; | |
4 local service_mt = { __index = service }; | |
5 | |
6 function new(cb) | |
7 return setmetatable({ cb = cb or {}, nodes = {} }, service_mt); | |
8 end | |
9 | |
10 function service:add_subscription(node, actor, jid) | |
11 local node_obj = self.nodes[node]; | |
12 if not node_obj then | |
13 return false, "item-not-found"; | |
14 end | |
15 node_obj.subscribers[jid] = true; | |
16 return true; | |
17 end | |
18 | |
19 function service:remove_subscription(node, actor, jid) | |
20 self.nodes[node].subscribers[jid] = nil; | |
21 return true; | |
22 end | |
23 | |
3626
444f965baed8
util.pubsub: Add :get_subscription() to return the current subscription for a JID, if any
Matthew Wild <mwild1@gmail.com>
parents:
3619
diff
changeset
|
24 function service:get_subscription(node, actor, jid) |
444f965baed8
util.pubsub: Add :get_subscription() to return the current subscription for a JID, if any
Matthew Wild <mwild1@gmail.com>
parents:
3619
diff
changeset
|
25 local node_obj = self.nodes[node]; |
444f965baed8
util.pubsub: Add :get_subscription() to return the current subscription for a JID, if any
Matthew Wild <mwild1@gmail.com>
parents:
3619
diff
changeset
|
26 if node_obj then |
444f965baed8
util.pubsub: Add :get_subscription() to return the current subscription for a JID, if any
Matthew Wild <mwild1@gmail.com>
parents:
3619
diff
changeset
|
27 return node_obj.subscribers[jid]; |
444f965baed8
util.pubsub: Add :get_subscription() to return the current subscription for a JID, if any
Matthew Wild <mwild1@gmail.com>
parents:
3619
diff
changeset
|
28 end |
444f965baed8
util.pubsub: Add :get_subscription() to return the current subscription for a JID, if any
Matthew Wild <mwild1@gmail.com>
parents:
3619
diff
changeset
|
29 end |
444f965baed8
util.pubsub: Add :get_subscription() to return the current subscription for a JID, if any
Matthew Wild <mwild1@gmail.com>
parents:
3619
diff
changeset
|
30 |
3796
405231b1cb88
mod_pubsub, util.pubsub: Support node creation
Florian Zeitz <florob@babelmonkeys.de>
parents:
3641
diff
changeset
|
31 function service:create(node, actor) |
405231b1cb88
mod_pubsub, util.pubsub: Support node creation
Florian Zeitz <florob@babelmonkeys.de>
parents:
3641
diff
changeset
|
32 if not self.nodes[node] then |
405231b1cb88
mod_pubsub, util.pubsub: Support node creation
Florian Zeitz <florob@babelmonkeys.de>
parents:
3641
diff
changeset
|
33 self.nodes[node] = { name = node, subscribers = {}, config = {}, data = {} }; |
405231b1cb88
mod_pubsub, util.pubsub: Support node creation
Florian Zeitz <florob@babelmonkeys.de>
parents:
3641
diff
changeset
|
34 return true; |
405231b1cb88
mod_pubsub, util.pubsub: Support node creation
Florian Zeitz <florob@babelmonkeys.de>
parents:
3641
diff
changeset
|
35 end |
405231b1cb88
mod_pubsub, util.pubsub: Support node creation
Florian Zeitz <florob@babelmonkeys.de>
parents:
3641
diff
changeset
|
36 return false, "conflict"; |
405231b1cb88
mod_pubsub, util.pubsub: Support node creation
Florian Zeitz <florob@babelmonkeys.de>
parents:
3641
diff
changeset
|
37 end |
405231b1cb88
mod_pubsub, util.pubsub: Support node creation
Florian Zeitz <florob@babelmonkeys.de>
parents:
3641
diff
changeset
|
38 |
3619 | 39 function service:publish(node, actor, id, item) |
40 local node_obj = self.nodes[node]; | |
41 if not node_obj then | |
3641
3603aeb325de
mod_pubsub, util.pubsub: Support for fetching items
Florian Zeitz <florob@babelmonkeys.de>
parents:
3626
diff
changeset
|
42 node_obj = { name = node, subscribers = {}, config = {}, data = {} }; |
3619 | 43 self.nodes[node] = node_obj; |
44 end | |
3641
3603aeb325de
mod_pubsub, util.pubsub: Support for fetching items
Florian Zeitz <florob@babelmonkeys.de>
parents:
3626
diff
changeset
|
45 node_obj.data[id] = item; |
3619 | 46 self.cb.broadcaster(node, node_obj.subscribers, item); |
47 return true; | |
48 end | |
49 | |
3641
3603aeb325de
mod_pubsub, util.pubsub: Support for fetching items
Florian Zeitz <florob@babelmonkeys.de>
parents:
3626
diff
changeset
|
50 function service:get(node, actor, id) |
3603aeb325de
mod_pubsub, util.pubsub: Support for fetching items
Florian Zeitz <florob@babelmonkeys.de>
parents:
3626
diff
changeset
|
51 local node_obj = self.nodes[node]; |
3603aeb325de
mod_pubsub, util.pubsub: Support for fetching items
Florian Zeitz <florob@babelmonkeys.de>
parents:
3626
diff
changeset
|
52 if node_obj then |
3603aeb325de
mod_pubsub, util.pubsub: Support for fetching items
Florian Zeitz <florob@babelmonkeys.de>
parents:
3626
diff
changeset
|
53 if id then |
3603aeb325de
mod_pubsub, util.pubsub: Support for fetching items
Florian Zeitz <florob@babelmonkeys.de>
parents:
3626
diff
changeset
|
54 return { node_obj.data[id] }; |
3603aeb325de
mod_pubsub, util.pubsub: Support for fetching items
Florian Zeitz <florob@babelmonkeys.de>
parents:
3626
diff
changeset
|
55 else |
3603aeb325de
mod_pubsub, util.pubsub: Support for fetching items
Florian Zeitz <florob@babelmonkeys.de>
parents:
3626
diff
changeset
|
56 return node_obj.data; |
3603aeb325de
mod_pubsub, util.pubsub: Support for fetching items
Florian Zeitz <florob@babelmonkeys.de>
parents:
3626
diff
changeset
|
57 end |
3603aeb325de
mod_pubsub, util.pubsub: Support for fetching items
Florian Zeitz <florob@babelmonkeys.de>
parents:
3626
diff
changeset
|
58 end |
3603aeb325de
mod_pubsub, util.pubsub: Support for fetching items
Florian Zeitz <florob@babelmonkeys.de>
parents:
3626
diff
changeset
|
59 end |
3603aeb325de
mod_pubsub, util.pubsub: Support for fetching items
Florian Zeitz <florob@babelmonkeys.de>
parents:
3626
diff
changeset
|
60 |
3619 | 61 return _M; |