Software /
code /
prosody
Comparison
util/pubsub.lua @ 6442:0f4025abbe8f
Merge 0.10->trunk
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Sun, 28 Sep 2014 01:56:22 +0200 |
parent | 5973:905b4fd863b4 |
parent | 6439:d58ad8bd244b |
child | 6516:ecd8d6437053 |
comparison
equal
deleted
inserted
replaced
6432:388786af0dd2 | 6442:0f4025abbe8f |
---|---|
4 module("pubsub", package.seeall); | 4 module("pubsub", package.seeall); |
5 | 5 |
6 local service = {}; | 6 local service = {}; |
7 local service_mt = { __index = service }; | 7 local service_mt = { __index = service }; |
8 | 8 |
9 local default_config = { | 9 local default_config = { __index = { |
10 broadcaster = function () end; | 10 broadcaster = function () end; |
11 get_affiliation = function () end; | 11 get_affiliation = function () end; |
12 capabilities = {}; | 12 capabilities = {}; |
13 }; | 13 } }; |
14 local default_node_config = { __index = { | |
15 ["pubsub#max_items"] = "20"; | |
16 } }; | |
14 | 17 |
15 function new(config) | 18 function new(config) |
16 config = config or {}; | 19 config = config or {}; |
17 return setmetatable({ | 20 return setmetatable({ |
18 config = setmetatable(config, { __index = default_config }); | 21 config = setmetatable(config, default_config); |
22 node_defaults = setmetatable(config.node_defaults or {}, default_node_config); | |
19 affiliations = {}; | 23 affiliations = {}; |
20 subscriptions = {}; | 24 subscriptions = {}; |
21 nodes = {}; | 25 nodes = {}; |
22 data = {}; | 26 data = {}; |
23 events = events.new(); | 27 events = events.new(); |
202 return false, "item-not-found"; | 206 return false, "item-not-found"; |
203 end | 207 end |
204 return true, node_obj.subscribers[jid]; | 208 return true, node_obj.subscribers[jid]; |
205 end | 209 end |
206 | 210 |
207 function service:create(node, actor) | 211 function service:create(node, actor, options) |
208 -- Access checking | 212 -- Access checking |
209 if not self:may(node, actor, "create") then | 213 if not self:may(node, actor, "create") then |
210 return false, "forbidden"; | 214 return false, "forbidden"; |
211 end | 215 end |
212 -- | 216 -- |
216 | 220 |
217 self.data[node] = {}; | 221 self.data[node] = {}; |
218 self.nodes[node] = { | 222 self.nodes[node] = { |
219 name = node; | 223 name = node; |
220 subscribers = {}; | 224 subscribers = {}; |
221 config = {}; | 225 config = setmetatable(options or {}, {__index=self.node_defaults}); |
222 affiliations = {}; | 226 affiliations = {}; |
223 }; | 227 }; |
224 setmetatable(self.nodes[node], { __index = { data = self.data[node] } }); -- COMPAT | 228 setmetatable(self.nodes[node], { __index = { data = self.data[node] } }); -- COMPAT |
225 self.events.fire_event("node-created", { node = node, actor = actor }); | 229 self.events.fire_event("node-created", { node = node, actor = actor }); |
226 local ok, err = self:set_affiliation(node, true, actor, "owner"); | 230 local ok, err = self:set_affiliation(node, true, actor, "owner"); |
257 return i; | 261 return i; |
258 end | 262 end |
259 end | 263 end |
260 end | 264 end |
261 | 265 |
266 local function trim_items(data, max) | |
267 max = tonumber(max); | |
268 if not max or #data <= max then return end | |
269 repeat | |
270 data[t_remove(data, 1)] = nil; | |
271 until #data <= max | |
272 end | |
273 | |
262 function service:publish(node, actor, id, item) | 274 function service:publish(node, actor, id, item) |
263 -- Access checking | 275 -- Access checking |
264 if not self:may(node, actor, "publish") then | 276 if not self:may(node, actor, "publish") then |
265 return false, "forbidden"; | 277 return false, "forbidden"; |
266 end | 278 end |
276 end | 288 end |
277 node_obj = self.nodes[node]; | 289 node_obj = self.nodes[node]; |
278 end | 290 end |
279 local node_data = self.data[node]; | 291 local node_data = self.data[node]; |
280 remove_item_by_id(node_data, id); | 292 remove_item_by_id(node_data, id); |
281 node_data[#self.data[node] + 1] = id; | 293 node_data[#node_data + 1] = id; |
282 node_data[id] = item; | 294 node_data[id] = item; |
295 trim_items(node_data, node_obj.config["pubsub#max_items"]); | |
283 self.events.fire_event("item-published", { node = node, actor = actor, id = id, item = item }); | 296 self.events.fire_event("item-published", { node = node, actor = actor, id = id, item = item }); |
284 self.config.broadcaster("items", node, node_obj.subscribers, item); | 297 self.config.broadcaster("items", node, node_obj.subscribers, item); |
285 return true; | 298 return true; |
286 end | 299 end |
287 | 300 |
409 end | 422 end |
410 node_obj.capabilities = capabilities; | 423 node_obj.capabilities = capabilities; |
411 return true; | 424 return true; |
412 end | 425 end |
413 | 426 |
427 function service:set_node_config(node, actor, new_config) | |
428 if not self:may(node, actor, "configure") then | |
429 return false, "forbidden"; | |
430 end | |
431 | |
432 local node_obj = self.nodes[node]; | |
433 if not node_obj then | |
434 return false, "item-not-found"; | |
435 end | |
436 | |
437 for k,v in pairs(new_config) do | |
438 node_obj.config[k] = v; | |
439 end | |
440 trim_items(self.data[node], node_obj.config["pubsub#max_items"]); | |
441 | |
442 return true; | |
443 end | |
444 | |
414 return _M; | 445 return _M; |