Software /
code /
prosody
Comparison
util/pubsub.lua @ 3938:a3ecaf46bd82
util.pubsub: Add service-wide subscription tracking, and add :get_subscriptions()
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Wed, 22 Dec 2010 03:46:32 +0000 |
parent | 3937:843ee23cc91a |
child | 3942:0323beb7183c |
comparison
equal
deleted
inserted
replaced
3937:843ee23cc91a | 3938:a3ecaf46bd82 |
---|---|
12 function new(config) | 12 function new(config) |
13 config = config or {}; | 13 config = config or {}; |
14 return setmetatable({ | 14 return setmetatable({ |
15 config = setmetatable(config, { __index = default_config }); | 15 config = setmetatable(config, { __index = default_config }); |
16 affiliations = {}; | 16 affiliations = {}; |
17 subscriptions = {}; | |
17 nodes = {}; | 18 nodes = {}; |
18 }, service_mt); | 19 }, service_mt); |
19 end | 20 end |
20 | 21 |
21 function service:jids_equal(jid1, jid2) | 22 function service:jids_equal(jid1, jid2) |
110 end | 111 end |
111 node_obj = self.nodes[node]; | 112 node_obj = self.nodes[node]; |
112 end | 113 end |
113 end | 114 end |
114 node_obj.subscribers[jid] = options or true; | 115 node_obj.subscribers[jid] = options or true; |
116 local normal_jid = self.config.normalize_jid(jid); | |
117 local subs = self.subscriptions[normal_jid]; | |
118 if subs then | |
119 if not subs[jid] then | |
120 subs[jid] = { [node] = true }; | |
121 else | |
122 subs[jid][node] = true; | |
123 end | |
124 else | |
125 self.subscriptions[normal_jid] = { [jid] = { [node] = true } }; | |
126 end | |
115 return true; | 127 return true; |
116 end | 128 end |
117 | 129 |
118 function service:remove_subscription(node, actor, jid) | 130 function service:remove_subscription(node, actor, jid) |
119 -- Access checking | 131 -- Access checking |
136 end | 148 end |
137 if not node_obj.subscribers[jid] then | 149 if not node_obj.subscribers[jid] then |
138 return false, "not-subscribed"; | 150 return false, "not-subscribed"; |
139 end | 151 end |
140 node_obj.subscribers[jid] = nil; | 152 node_obj.subscribers[jid] = nil; |
153 local normal_jid = self.config.normalize_jid(jid); | |
154 local subs = self.subscriptions[normal_jid]; | |
155 if subs then | |
156 local jid_subs = subs[jid]; | |
157 if jid_subs then | |
158 jid_subs[node] = nil; | |
159 if next(jid_subs) == nil then | |
160 subs[jid] = nil; | |
161 end | |
162 end | |
163 if next(subs) == nil then | |
164 self.subscriptions[normal_jid] = nil; | |
165 end | |
166 end | |
141 return true; | 167 return true; |
142 end | 168 end |
143 | 169 |
144 function service:get_subscription(node, actor, jid) | 170 function service:get_subscription(node, actor, jid) |
145 -- Access checking | 171 -- Access checking |
247 end | 273 end |
248 -- | 274 -- |
249 return true, self.nodes; | 275 return true, self.nodes; |
250 end | 276 end |
251 | 277 |
278 function service:get_subscriptions(node, actor, jid) | |
279 -- Access checking | |
280 local cap; | |
281 if jid == actor or self:jids_equal(actor, jid) then | |
282 cap = "get_subscriptions"; | |
283 else | |
284 cap = "get_subscriptions_other"; | |
285 end | |
286 if not self:may(node, actor, cap) then | |
287 return false, "forbidden"; | |
288 end | |
289 -- | |
290 local node_obj; | |
291 if node then | |
292 node_obj = self.nodes[node]; | |
293 if not node_obj then | |
294 return false, "item-not-found"; | |
295 end | |
296 end | |
297 local normal_jid = self.config.normalize_jid(jid); | |
298 local subs = self.subscriptions[normal_jid]; | |
299 -- We return the subscription object from the node to save | |
300 -- a get_subscription() call for each node. | |
301 local ret = {}; | |
302 if subs then | |
303 for jid, subscribed_nodes in pairs(subs) do | |
304 if node then -- Return only subscriptions to this node | |
305 if subscribed_nodes[node] then | |
306 ret[#ret+1] = { | |
307 node = node; | |
308 jid = jid; | |
309 subscription = node_obj.subscribers[jid]; | |
310 }; | |
311 end | |
312 else -- Return subscriptions to all nodes | |
313 local nodes = self.nodes; | |
314 for subscribed_node in pairs(subscribed_nodes) do | |
315 ret[#ret+1] = { | |
316 node = node; | |
317 jid = jid; | |
318 subscription = nodes[subscribed_node].subscribers[jid]; | |
319 }; | |
320 end | |
321 end | |
322 end | |
323 end | |
324 return true, ret; | |
325 end | |
326 | |
252 -- Access models only affect 'none' affiliation caps, service/default access level... | 327 -- Access models only affect 'none' affiliation caps, service/default access level... |
253 function service:set_node_capabilities(node, actor, capabilities) | 328 function service:set_node_capabilities(node, actor, capabilities) |
254 -- Access checking | 329 -- Access checking |
255 if not self:may(node, actor, "configure") then | 330 if not self:may(node, actor, "configure") then |
256 return false, "forbidden"; | 331 return false, "forbidden"; |