Comparison

plugins/muc/muc.lib.lua @ 7369:c5cae59831d7

MUC: Add support for serializing live rooms, including occupants and their presence
author Kim Alvefur <zash@zash.se>
date Mon, 18 Apr 2016 19:30:15 +0200
parent 7368:4e24aff1e4df
child 7386:5a9af30d5737
comparison
equal deleted inserted replaced
7368:4e24aff1e4df 7369:c5cae59831d7
1242 _data = config or {}; 1242 _data = config or {};
1243 _affiliations = {}; 1243 _affiliations = {};
1244 }, room_mt); 1244 }, room_mt);
1245 end 1245 end
1246 1246
1247 function room_mt:freeze() 1247 function room_mt:freeze(live)
1248 local frozen = { 1248 local frozen = {
1249 _jid = self.jid; 1249 _jid = self.jid;
1250 _data = self._data; 1250 _data = self._data;
1251 }; 1251 };
1252 for user, affiliation in pairs(self._affiliations) do 1252 for user, affiliation in pairs(self._affiliations) do
1253 frozen[user] = affiliation; 1253 frozen[user] = affiliation;
1254 end 1254 end
1255 if live then
1256 for nick, occupant in self:each_occupant() do
1257 frozen[nick] = {
1258 bare_jid = occupant.bare_jid;
1259 role = occupant.role;
1260 jid = occupant.jid;
1261 }
1262 for jid, presence in occupant:each_session() do
1263 frozen[jid] = st.preserialize(presence);
1264 end
1265 end
1266 end
1255 return frozen; 1267 return frozen;
1256 end 1268 end
1257 1269
1258 function _M.restore_room(frozen) 1270 function _M.restore_room(frozen)
1259 -- COMPAT 1271 -- COMPAT
1264 end 1276 end
1265 1277
1266 local room_jid = frozen._jid; 1278 local room_jid = frozen._jid;
1267 local room = _M.new_room(room_jid, frozen._data); 1279 local room = _M.new_room(room_jid, frozen._data);
1268 1280
1281 local occupants = {};
1282 local occupant_sessions = {};
1283 local room_name, room_host = jid_split(room_jid);
1269 for jid, data in pairs(frozen) do 1284 for jid, data in pairs(frozen) do
1270 local node, host = jid_split(jid); 1285 local node, host, resource = jid_split(jid);
1271 if node or host:sub(1,1) ~= "_" then 1286 if node or host:sub(1,1) ~= "_" then
1272 room._affiliations[jid] = data; 1287 if not resource then
1273 end 1288 -- bare jid: affiliation
1274 end 1289 room._affiliations[jid] = data;
1290 elseif host == room_host and node == room_name then
1291 -- full room jid: bare real jid and role
1292 local bare_jid = data.bare_jid;
1293 local occupant = occupant_lib.new(bare_jid, jid);
1294 occupant.jid = data.jid;
1295 occupant.role = data.role;
1296 occupants[bare_jid] = occupant;
1297 local sessions = occupant_sessions[bare_jid];
1298 if sessions then
1299 for full_jid, presence in pairs(sessions) do
1300 occupant:set_session(full_jid, presence);
1301 end
1302 end
1303 occupant_sessions[bare_jid] = nil;
1304 else
1305 -- full user jid: presence
1306 local presence = st.deserialize(data);
1307 local bare_jid = jid_bare(jid);
1308 local occupant = occupants[bare_jid];
1309 local sessions = occupant_sessions[bare_jid];
1310 if occupant then
1311 occupant:set_session(jid, presence);
1312 elseif sessions then
1313 sessions[jid] = presence;
1314 else
1315 occupant_sessions[bare_jid] = {
1316 [jid] = presence;
1317 };
1318 end
1319 end
1320 end
1321 end
1322
1323 for _, occupant in pairs(occupants) do
1324 room:save_occupant(occupant);
1325 end
1326
1275 return room; 1327 return room;
1276 end 1328 end
1277 1329
1278 _M.room_mt = room_mt; 1330 _M.room_mt = room_mt;
1279 1331