Software /
code /
prosody
Comparison
util/pubsub.lua @ 4365:6704b3cd032e
util.pubsub: Support for events (currently subscription-added and subscription-removed)
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Tue, 30 Aug 2011 15:03:27 -0400 |
parent | 4364:af40cf682eba |
child | 4366:b6c18cadd3ec |
comparison
equal
deleted
inserted
replaced
4364:af40cf682eba | 4365:6704b3cd032e |
---|---|
1 local events = require "util.events"; | |
2 | |
1 module("pubsub", package.seeall); | 3 module("pubsub", package.seeall); |
2 | 4 |
3 local service = {}; | 5 local service = {}; |
4 local service_mt = { __index = service }; | 6 local service_mt = { __index = service }; |
5 | 7 |
14 return setmetatable({ | 16 return setmetatable({ |
15 config = setmetatable(config, { __index = default_config }); | 17 config = setmetatable(config, { __index = default_config }); |
16 affiliations = {}; | 18 affiliations = {}; |
17 subscriptions = {}; | 19 subscriptions = {}; |
18 nodes = {}; | 20 nodes = {}; |
21 events = events.new(); | |
19 }, service_mt); | 22 }, service_mt); |
20 end | 23 end |
21 | 24 |
22 function service:jids_equal(jid1, jid2) | 25 function service:jids_equal(jid1, jid2) |
23 local normalize = self.config.normalize_jid; | 26 local normalize = self.config.normalize_jid; |
121 subs[jid][node] = true; | 124 subs[jid][node] = true; |
122 end | 125 end |
123 else | 126 else |
124 self.subscriptions[normal_jid] = { [jid] = { [node] = true } }; | 127 self.subscriptions[normal_jid] = { [jid] = { [node] = true } }; |
125 end | 128 end |
129 self.events.fire_event("subscription-added", { node = node, jid = jid, normalized_jid = normal_jid, options = options }); | |
126 return true; | 130 return true; |
127 end | 131 end |
128 | 132 |
129 function service:remove_subscription(node, actor, jid) | 133 function service:remove_subscription(node, actor, jid) |
130 -- Access checking | 134 -- Access checking |
161 end | 165 end |
162 if next(subs) == nil then | 166 if next(subs) == nil then |
163 self.subscriptions[normal_jid] = nil; | 167 self.subscriptions[normal_jid] = nil; |
164 end | 168 end |
165 end | 169 end |
170 self.events.fire_event("subscription-removed", { node = node, jid = jid, normalized_jid = normal_jid }); | |
166 return true; | 171 return true; |
167 end | 172 end |
168 | 173 |
169 function service:get_subscription(node, actor, jid) | 174 function service:get_subscription(node, actor, jid) |
170 -- Access checking | 175 -- Access checking |