mod_bookmarks2/mod_bookmarks2.lua
changeset 3743 1ed3d86eeaed
parent 3742 9cd6d602ad1b
child 3744 b3fb32bd1f01
equal deleted inserted replaced
3742:9cd6d602ad1b 3743:1ed3d86eeaed
    93 	        a_conference.attr.autojoin == b_conference.attr.autojoin and
    93 	        a_conference.attr.autojoin == b_conference.attr.autojoin and
    94 	        a_nick == b_nick and
    94 	        a_nick == b_nick and
    95 	        a_password == b_password);
    95 	        a_password == b_password);
    96 end
    96 end
    97 
    97 
    98 local function publish_to_pep(jid, bookmarks)
    98 local function publish_to_pep(jid, bookmarks, synchronise)
    99 	local service = mod_pep.get_pep_service(jid_split(jid));
    99 	local service = mod_pep.get_pep_service(jid_split(jid));
   100 
   100 
   101 	-- If we set zero legacy bookmarks, purge the bookmarks 2 node.
       
   102 	if #bookmarks.tags == 0 then
   101 	if #bookmarks.tags == 0 then
   103 		module:log("debug", "No bookmark in the set, purging instead.");
   102 		if synchronise then
   104 		return service:purge("urn:xmpp:bookmarks:0", jid, true);
   103 			-- If we set zero legacy bookmarks, purge the bookmarks 2 node.
       
   104 			module:log("debug", "No bookmark in the set, purging instead.");
       
   105 			return service:purge("urn:xmpp:bookmarks:0", jid, true);
       
   106 		else
       
   107 			return true;
       
   108 		end
   105 	end
   109 	end
   106 
   110 
   107 	-- Retrieve the current bookmarks2.
   111 	-- Retrieve the current bookmarks2.
   108 	module:log("debug", "Retrieving the current bookmarks 2.");
   112 	module:log("debug", "Retrieving the current bookmarks 2.");
   109 	local has_bookmarks2, ret = service:get_items("urn:xmpp:bookmarks:0", jid);
   113 	local has_bookmarks2, ret = service:get_items("urn:xmpp:bookmarks:0", jid);
   166 			end
   170 			end
   167 		end
   171 		end
   168 	end
   172 	end
   169 
   173 
   170 	-- Now handle retracting items that have been removed.
   174 	-- Now handle retracting items that have been removed.
   171 	for id in pairs(to_remove) do
   175 	if synchronise then
   172 		module:log("debug", "Item %s removed from bookmarks.", id);
   176 		for id in pairs(to_remove) do
   173 		local ok, err = service:retract("urn:xmpp:bookmarks:0", jid, id, st.stanza("retract", { id = id }));
   177 			module:log("debug", "Item %s removed from bookmarks.", id);
   174 		if not ok then
   178 			local ok, err = service:retract("urn:xmpp:bookmarks:0", jid, id, st.stanza("retract", { id = id }));
   175 			module:log("error", "Retracting item %s failed: %s", id, err);
   179 			if not ok then
   176 			return ok, err;
   180 				module:log("error", "Retracting item %s failed: %s", id, err);
       
   181 				return ok, err;
       
   182 			end
   177 		end
   183 		end
   178 	end
   184 	end
   179 	return true;
   185 	return true;
   180 end
   186 end
   181 
   187 
   192 		return;
   198 		return;
   193 	end
   199 	end
   194 
   200 
   195 	module:log("debug", "Private bookmarks set by client, publishing to pep.");
   201 	module:log("debug", "Private bookmarks set by client, publishing to pep.");
   196 
   202 
   197 	local ok, err = publish_to_pep(session.full_jid, bookmarks);
   203 	local ok, err = publish_to_pep(session.full_jid, bookmarks, true);
   198 	if not ok then
   204 	if not ok then
   199 		module:log("error", "Failed to publish to PEP bookmarks for %s@%s: %s", session.username, session.host, err);
   205 		module:log("error", "Failed to publish to PEP bookmarks for %s@%s: %s", session.username, session.host, err);
   200 		session.send(st.error_reply(stanza, "cancel", "internal-server-error", "Failed to store bookmarks to PEP"));
   206 		session.send(st.error_reply(stanza, "cancel", "internal-server-error", "Failed to store bookmarks to PEP"));
   201 		return true;
   207 		return true;
   202 	end
   208 	end
   222 		return;
   228 		return;
   223 	end
   229 	end
   224 	local bookmarks = st.deserialize(data);
   230 	local bookmarks = st.deserialize(data);
   225 	module:log("debug", "Got legacy bookmarks of %s: %s", jid, bookmarks);
   231 	module:log("debug", "Got legacy bookmarks of %s: %s", jid, bookmarks);
   226 
   232 
   227 	-- We don’t care if deleting succeeds or not, we only want to start with a non-existent node.
       
   228 	module:log("debug", "Deleting possibly existing PEP item for %s.", jid);
       
   229 	service:delete("urn:xmpp:bookmarks:0", jid);
       
   230 
       
   231 	module:log("debug", "Going to store PEP item for %s.", jid);
   233 	module:log("debug", "Going to store PEP item for %s.", jid);
   232 	local ok, err = publish_to_pep(session.full_jid, bookmarks);
   234 	local ok, err = publish_to_pep(session.full_jid, bookmarks, false);
   233 	if not ok then
   235 	if not ok then
   234 		module:log("error", "Failed to store bookmarks to PEP for %s, aborting migration: %s", jid, err);
   236 		module:log("error", "Failed to store bookmarks to PEP for %s, aborting migration: %s", jid, err);
   235 		return;
   237 		return;
   236 	end
   238 	end
   237 	module:log("debug", "Stored bookmarks to PEP for %s.", jid);
   239 	module:log("debug", "Stored bookmarks to PEP for %s.", jid);