MUC: Separate force-save parameter from save-entire-state flag
[prosody.git] / plugins / muc / muc.lib.lua
index 8efa295e31cc241926933422eb1f05f810802f9f..eeb531a90c02c3783718d9a9bf2e8455d706a7eb 100644 (file)
@@ -435,6 +435,12 @@ function room_mt:handle_first_presence(origin, stanza)
                origin = origin;
                jid = real_jid;
        });
+       module:fire_event("muc-room-created", {
+               room = self;
+               creator = dest_occupant;
+               stanza = stanza;
+               origin = origin;
+       });
        return true;
 end
 
@@ -763,7 +769,7 @@ function room_mt:process_form(origin, stanza)
                end
                event.field, event.value = nil, nil;
 
-               self:save();
+               self:save(true);
                origin.send(st.reply(stanza));
 
                if next(event.status_codes) then
@@ -846,7 +852,7 @@ function room_mt:handle_admin_query_set_command(origin, stanza)
        else
                success, errtype, err = nil, "cancel", "bad-request";
        end
-       self:save();
+       self:save(true);
        if not success then
                origin.send(st.error_reply(stanza, errtype, err));
        else
@@ -1213,7 +1219,7 @@ function room_mt:set_affiliation(actor, jid, affiliation, reason)
                end
        end
 
-       self:save();
+       self:save(true);
 
        module:fire_event("muc-set-affiliation", {
                room = self;
@@ -1316,6 +1322,11 @@ function room_mt:freeze(live)
                                frozen[jid] = st.preserialize(presence);
                        end
                end
+               local history = self._history;
+               if history then
+                       frozen._last_message = st.preserialize(history[#history].stanza);
+                       frozen._last_message_at = history[#history].timestamp;
+               end
        end
        return frozen;
 end
@@ -1331,6 +1342,13 @@ function _M.restore_room(frozen)
        local room_jid = frozen._jid;
        local room = _M.new_room(room_jid, frozen._data);
 
+       if frozen._last_message and frozen._last_message_at then
+               room._history = {
+                       { stanza = st.deserialize(frozen._last_message),
+                         timestamp = frozen._last_message_at, },
+               };
+       end
+
        local occupants = {};
        local occupant_sessions = {};
        local room_name, room_host = jid_split(room_jid);
@@ -1340,7 +1358,7 @@ function _M.restore_room(frozen)
                        if not resource and type(data) == "string" then
                                -- bare jid: affiliation
                                room._affiliations[jid] = data;
-                       elseif host == room_host and node == room_name and resource then
+                       elseif host == room_host and node == room_name and resource and type(data) == "table" then
                                -- full room jid: bare real jid and role
                                local bare_jid = data.bare_jid;
                                local   occupant = occupant_lib.new(bare_jid, jid);
@@ -1354,7 +1372,7 @@ function _M.restore_room(frozen)
                                        end
                                end
                                occupant_sessions[bare_jid] = nil;
-                       else
+                       elseif type(data) == "table" and data.name then
                                -- full user jid: presence
                                local presence = st.deserialize(data);
                                local bare_jid = jid_bare(jid);