aboutsummaryrefslogtreecommitdiffstats
path: root/util/pubsub.lua
blob: 34859fdb629b119bf3564c745ed00b79aa258987 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
module("pubsub", package.seeall);

local service = {};
local service_mt = { __index = service };

function new(cb)
	return setmetatable({ cb = cb or {}, nodes = {} }, service_mt);
end

function service:add_subscription(node, actor, jid)
	local node_obj = self.nodes[node];
	if not node_obj then
		return false, "item-not-found";
	end
	node_obj.subscribers[jid] = true;
	return true;
end

function service:remove_subscription(node, actor, jid)
	self.nodes[node].subscribers[jid] = nil;
	return true;
end

function service:publish(node, actor, id, item)
	local node_obj = self.nodes[node];
	if not node_obj then
		node_obj = { name = node, subscribers = {}, config = {} };
		self.nodes[node] = node_obj;
	end
	node_obj.data = item;
	self.cb.broadcaster(node, node_obj.subscribers, item);
	return true;
end

return _M;