comparison mod_bookmarks2/mod_bookmarks2.lua @ 3741:33b3e2387a10

mod_bookmarks2: Also migrate legacy PEP bookmarks (current XEP-0048).
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Wed, 13 Nov 2019 16:40:09 +0100
parents b3fb32bd1f01
children 05bca7a771d6
comparison
equal deleted inserted replaced
3740:b3fb32bd1f01 3741:33b3e2387a10
215 local session = event.session; 215 local session = event.session;
216 local username = session.username; 216 local username = session.username;
217 local service = mod_pep.get_pep_service(username); 217 local service = mod_pep.get_pep_service(username);
218 local jid = username.."@"..session.host; 218 local jid = username.."@"..session.host;
219 219
220 local ok, ret = service:get_items("storage:bookmarks", session.full_jid);
221 if ok then
222 module:log("debug", "Legacy PEP bookmarks found for %s, migrating.", jid);
223 local failed = false;
224 for _, item_id in ipairs(ret) do
225 local item = ret[item_id];
226 if item.attr.id ~= "current" then
227 module:log("warn", "Legacy PEP bookmarks for %s isn’t using 'current' as its id: %s", jid, item.attr.id);
228 end
229 local bookmarks = item:get_child("storage", "storage:bookmarks");
230 module:log("debug", "Got legacy PEP bookmarks of %s: %s", jid, bookmarks);
231
232 local ok, err = publish_to_pep(session.full_jid, bookmarks, false);
233 if not ok then
234 module:log("error", "Failed to store legacy PEP bookmarks to bookmarks 2 for %s, aborting migration: %s", jid, err);
235 failed = true;
236 break;
237 end
238 end
239 if not failed then
240 module:log("debug", "Successfully migrated legacy PEP bookmarks of %s to bookmarks 2, attempting deletion of the node.", jid);
241 local ok, err = service:delete("storage:bookmarks", jid);
242 if not ok then
243 module:log("error", "Failed to delete legacy PEP bookmarks for %s: %s", jid, err);
244 end
245 end
246 end
247
220 local data, err = private_storage:get(username, "storage:storage:bookmarks"); 248 local data, err = private_storage:get(username, "storage:storage:bookmarks");
221 if not data then 249 if not data then
222 module:log("debug", "No existing legacy bookmarks for %s, migration already done: %s", jid, err); 250 module:log("debug", "No existing legacy bookmarks for %s, migration already done: %s", jid, err);
223 local ok, ret = service:get_items("urn:xmpp:bookmarks:0", session.full_jid); 251 local ok, ret = service:get_items("urn:xmpp:bookmarks:0", session.full_jid);
224 if not ok or not ret then 252 if not ok or not ret then
228 return; 256 return;
229 end 257 end
230 local bookmarks = st.deserialize(data); 258 local bookmarks = st.deserialize(data);
231 module:log("debug", "Got legacy bookmarks of %s: %s", jid, bookmarks); 259 module:log("debug", "Got legacy bookmarks of %s: %s", jid, bookmarks);
232 260
233 module:log("debug", "Going to store PEP item for %s.", jid); 261 module:log("debug", "Going to store legacy bookmarks to bookmarks 2 %s.", jid);
234 local ok, err = publish_to_pep(session.full_jid, bookmarks, false); 262 local ok, err = publish_to_pep(session.full_jid, bookmarks, false);
235 if not ok then 263 if not ok then
236 module:log("error", "Failed to store bookmarks to PEP for %s, aborting migration: %s", jid, err); 264 module:log("error", "Failed to store legacy bookmarks to bookmarks 2 for %s, aborting migration: %s", jid, err);
237 return; 265 return;
238 end 266 end
239 module:log("debug", "Stored bookmarks to PEP for %s.", jid); 267 module:log("debug", "Stored legacy bookmarks to bookmarks 2 for %s.", jid);
240 268
241 local ok, err = private_storage:set(username, "storage:storage:bookmarks", nil); 269 local ok, err = private_storage:set(username, "storage:storage:bookmarks", nil);
242 if not ok then 270 if not ok then
243 module:log("error", "Failed to remove private bookmarks of %s: %s", jid, err); 271 module:log("error", "Failed to remove legacy bookmarks of %s: %s", jid, err);
244 return; 272 return;
245 end 273 end
246 module:log("debug", "Removed private bookmarks of %s, migration done!", jid); 274 module:log("debug", "Removed legacy bookmarks of %s, migration done!", jid);
247 end 275 end
248 276
249 local function on_node_created(event) 277 local function on_node_created(event)
250 local service, node, actor = event.service, event.node, event.actor; 278 local service, node, actor = event.service, event.node, event.actor;
251 if node ~= "storage:bookmarks" then 279 if node ~= "storage:bookmarks" then