Comparison

plugins/muc/mod_muc.lua @ 9050:f5c43e829d93

MUC: Add new iteration methods, all_rooms/live_rooms to eventually replace each_room
author Matthew Wild <mwild1@gmail.com>
date Tue, 17 Jul 2018 10:29:16 +0100
parent 9049:5870a0a6d4b6
child 9051:68386f7b9fc9
comparison
equal deleted inserted replaced
9049:5870a0a6d4b6 9050:f5c43e829d93
11 -- create_room(jid) -> room 11 -- create_room(jid) -> room
12 -- track_room(room) 12 -- track_room(room)
13 -- delete_room(room) 13 -- delete_room(room)
14 -- forget_room(room) 14 -- forget_room(room)
15 -- get_room_from_jid(jid) -> room 15 -- get_room_from_jid(jid) -> room
16 -- each_room(live_only) -> () -> room 16 -- each_room(live_only) -> () -> room [DEPRECATED]
17 -- all_rooms() -> room
18 -- live_rooms() -> room
17 -- shutdown_component() 19 -- shutdown_component()
18 20
19 if module:get_host_type() ~= "component" then 21 if module:get_host_type() ~= "component" then
20 error("MUC should be loaded as a component, please see https://prosody.im/doc/components", 0); 22 error("MUC should be loaded as a component, please see https://prosody.im/doc/components", 0);
21 end 23 end
218 persistent_rooms:set(nil, room.jid, nil); 220 persistent_rooms:set(nil, room.jid, nil);
219 room_items_cache[room.jid] = nil; 221 room_items_cache[room.jid] = nil;
220 end 222 end
221 223
222 function module.unload() 224 function module.unload()
223 for room in rooms:values() do 225 for room in live_rooms() do
224 room:save(nil, true); 226 room:save(nil, true);
225 forget_room(room); 227 forget_room(room);
226 end 228 end
227 end 229 end
228 230
247 room = room; 249 room = room;
248 }); 250 });
249 return track_room(room); 251 return track_room(room);
250 end 252 end
251 253
252 function each_room(live_only) 254 function all_rooms()
253 if live_only then
254 return rooms:values();
255 end
256 return coroutine.wrap(function () 255 return coroutine.wrap(function ()
257 local seen = {}; -- Don't iterate over persistent rooms twice 256 local seen = {}; -- Don't iterate over persistent rooms twice
258 for room in rooms:values() do 257 for room in live_rooms() do
259 coroutine.yield(room); 258 coroutine.yield(room);
260 seen[room.jid] = true; 259 seen[room.jid] = true;
261 end 260 end
262 local all_persistent_rooms, err = persistent_rooms_storage:get(nil); 261 local all_persistent_rooms, err = persistent_rooms_storage:get(nil);
263 if not all_persistent_rooms then 262 if not all_persistent_rooms then
278 end 277 end
279 end 278 end
280 end); 279 end);
281 end 280 end
282 281
282 function live_rooms()
283 return rooms:values();
284 end
285
286 function each_room(live_only)
287 if live_only then
288 return live_rooms();
289 end
290 return all_rooms();
291 end
292
283 module:hook("host-disco-items", function(event) 293 module:hook("host-disco-items", function(event)
284 local reply = event.reply; 294 local reply = event.reply;
285 module:log("debug", "host-disco-items called"); 295 module:log("debug", "host-disco-items called");
286 if next(room_items_cache) ~= nil then 296 if next(room_items_cache) ~= nil then
287 for jid, room_name in pairs(room_items_cache) do 297 for jid, room_name in pairs(room_items_cache) do
288 reply:tag("item", { jid = jid, name = room_name }):up(); 298 reply:tag("item", { jid = jid, name = room_name }):up();
289 end 299 end
290 else 300 else
291 for room in each_room() do 301 for room in all_rooms() do
292 if not room:get_hidden() then 302 if not room:get_hidden() then
293 local jid, room_name = room.jid, room:get_name(); 303 local jid, room_name = room.jid, room:get_name();
294 room_items_cache[jid] = room_name; 304 room_items_cache[jid] = room_name;
295 reply:tag("item", { jid = jid, name = room_name }):up(); 305 reply:tag("item", { jid = jid, name = room_name }):up();
296 end 306 end
431 return room[method](room, origin, stanza); 441 return room[method](room, origin, stanza);
432 end, -2) 442 end, -2)
433 end 443 end
434 444
435 function shutdown_component() 445 function shutdown_component()
436 for room in each_room(true) do 446 for room in live_rooms() do
437 room:save(nil, true); 447 room:save(nil, true);
438 end 448 end
439 end 449 end
440 module:hook_global("server-stopping", shutdown_component, -300); 450 module:hook_global("server-stopping", shutdown_component, -300);
441 451
454 { name = "FORM_TYPE", type = "hidden", value = "http://prosody.im/protocol/muc#destroy" }; 464 { name = "FORM_TYPE", type = "hidden", value = "http://prosody.im/protocol/muc#destroy" };
455 { name = "rooms", type = "list-multi", required = true, label = "Rooms to destroy:"}; 465 { name = "rooms", type = "list-multi", required = true, label = "Rooms to destroy:"};
456 }; 466 };
457 467
458 local destroy_rooms_handler = adhoc_initial(destroy_rooms_layout, function() 468 local destroy_rooms_handler = adhoc_initial(destroy_rooms_layout, function()
459 return { rooms = array.collect(each_room()):pluck("jid"):sort(); }; 469 return { rooms = array.collect(all_rooms()):pluck("jid"):sort(); };
460 end, function(fields, errors) 470 end, function(fields, errors)
461 if errors then 471 if errors then
462 local errmsg = {}; 472 local errmsg = {};
463 for field, err in pairs(errors) do 473 for field, err in pairs(errors) do
464 errmsg[#errmsg + 1] = field .. ": " .. err; 474 errmsg[#errmsg + 1] = field .. ": " .. err;