Software /
code /
prosody
Comparison
util/pubsub.lua @ 3619:291ae816d800
mod_pubsub: It's aliiiive!
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Sat, 13 Nov 2010 03:16:58 +0000 |
child | 3626:444f965baed8 |
comparison
equal
deleted
inserted
replaced
3618:321767e78029 | 3619:291ae816d800 |
---|---|
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 | |
24 function service:publish(node, actor, id, item) | |
25 local node_obj = self.nodes[node]; | |
26 if not node_obj then | |
27 node_obj = { name = node, subscribers = {}, config = {} }; | |
28 self.nodes[node] = node_obj; | |
29 end | |
30 node_obj.data = item; | |
31 self.cb.broadcaster(node, node_obj.subscribers, item); | |
32 return true; | |
33 end | |
34 | |
35 return _M; |