util.pubsub: Modify new() to take a config, and add a default config via a metatable
[prosody.git] / util / pubsub.lua
1 module("pubsub", package.seeall);
2
3 local service = {};
4 local service_mt = { __index = service };
5
6 local default_config = {
7         broadcaster = function () end;
8         get_affiliation = function () end;
9         capabilities = {};
10 };
11
12 function new(config)
13         config = config or {};
14         return setmetatable({
15                 config = setmetatable(config, { __index = default_config });
16                 affiliations = {};
17                 nodes = {};
18         }, service_mt);
19 end
20
21 function service:add_subscription(node, actor, jid)
22         local node_obj = self.nodes[node];
23         if not node_obj then
24                 return false, "item-not-found";
25         end
26         node_obj.subscribers[jid] = true;
27         return true;
28 end
29
30 function service:remove_subscription(node, actor, jid)
31         local node_obj = self.nodes[node];
32         if not node_obj then
33                 return false, "item-not-found";
34         end
35         if not node_obj.subscribers[jid] then
36                 return false, "not-subscribed";
37         end
38         node_obj.subscribers[jid] = nil;
39         return true;
40 end
41
42 function service:get_subscription(node, actor, jid)
43         local node_obj = self.nodes[node];
44         if node_obj then
45                 return node_obj.subscribers[jid];
46         end
47 end
48
49 function service:create(node, actor)
50         if not self.nodes[node] then
51                 self.nodes[node] = { name = node, subscribers = {}, config = {}, data = {} };
52                 return true;
53         end
54         return false, "conflict";
55 end
56
57 function service:publish(node, actor, id, item)
58         local node_obj = self.nodes[node];
59         if not node_obj then
60                 node_obj = { name = node, subscribers = {}, config = {}, data = {} };
61                 self.nodes[node] = node_obj;
62         end
63         node_obj.data[id] = item;
64         self.cb.broadcaster(node, node_obj.subscribers, item);
65         return true;
66 end
67
68 function service:retract(node, actor, id, retract)
69         local node_obj = self.nodes[node];
70         if (not node_obj) or (not node_obj.data[id]) then
71                 return false, "item-not-found";
72         end
73         node_obj.data[id] = nil;
74         if retract then
75                 self.cb.broadcaster(node, node_obj.subscribers, retract);
76         end
77         return true
78 end
79
80 function service:get(node, actor, id)
81         local node_obj = self.nodes[node];
82         if node_obj then
83                 if id then
84                         return { node_obj.data[id] };
85                 else
86                         return node_obj.data;
87                 end
88         end
89 end
90
91 function service:get_nodes(actor)
92         return true, self.nodes;
93 end
94
95 return _M;