Comparison

plugins/mod_vcard_legacy.lua @ 9814:5eb4ef537e98 0.11

mod_vcard_legacy: Handle partial migration Eg in case the user already published an avatar, migrate only the vcard, and vice versa.
author Kim Alvefur <zash@zash.se>
date Sat, 26 Jan 2019 13:32:26 +0100
parent 9813:071aaaa5cb34
child 10117:8643b784626c
child 10268:210278d5f995
comparison
equal deleted inserted replaced
9813:071aaaa5cb34 9814:5eb4ef537e98
243 end 243 end
244 return vcard4, avatars; 244 return vcard4, avatars;
245 end 245 end
246 246
247 function save_to_pep(pep_service, actor, vcard4, avatars) 247 function save_to_pep(pep_service, actor, vcard4, avatars)
248 248 if avatars then
249 if pep_service:purge("urn:xmpp:avatar:metadata", actor) then 249
250 pep_service:purge("urn:xmpp:avatar:data", actor); 250 if pep_service:purge("urn:xmpp:avatar:metadata", actor) then
251 end 251 pep_service:purge("urn:xmpp:avatar:data", actor);
252 252 end
253 local avatar_defaults = node_defaults; 253
254 if #avatars > 1 then 254 local avatar_defaults = node_defaults;
255 avatar_defaults = {}; 255 if #avatars > 1 then
256 for k,v in pairs(node_defaults) do 256 avatar_defaults = {};
257 avatar_defaults[k] = v; 257 for k,v in pairs(node_defaults) do
258 end 258 avatar_defaults[k] = v;
259 avatar_defaults.max_items = #avatars; 259 end
260 end 260 avatar_defaults.max_items = #avatars;
261 for _, avatar in ipairs(avatars) do 261 end
262 local ok, err = pep_service:publish("urn:xmpp:avatar:data", actor, avatar.hash, avatar.data, avatar_defaults); 262 for _, avatar in ipairs(avatars) do
263 if ok then 263 local ok, err = pep_service:publish("urn:xmpp:avatar:data", actor, avatar.hash, avatar.data, avatar_defaults);
264 ok, err = pep_service:publish("urn:xmpp:avatar:metadata", actor, avatar.hash, avatar.meta, avatar_defaults); 264 if ok then
265 end 265 ok, err = pep_service:publish("urn:xmpp:avatar:metadata", actor, avatar.hash, avatar.meta, avatar_defaults);
266 if not ok then 266 end
267 return ok, err; 267 if not ok then
268 end 268 return ok, err;
269 end 269 end
270 270 end
271 return pep_service:publish("urn:xmpp:vcard4", actor, "current", vcard4, node_defaults); 271 end
272
273 if vcard4 then
274 return pep_service:publish("urn:xmpp:vcard4", actor, "current", vcard4, node_defaults);
275 end
276
277 return true;
272 end 278 end
273 279
274 module:hook("iq-set/self/vcard-temp:vCard", function (event) 280 module:hook("iq-set/self/vcard-temp:vCard", function (event)
275 local origin, stanza = event.origin, event.stanza; 281 local origin, stanza = event.origin, event.stanza;
276 local pep_service = mod_pep.get_pep_service(origin.username); 282 local pep_service = mod_pep.get_pep_service(origin.username);
315 if not vcard_temp then 321 if not vcard_temp then
316 session.log("debug", "No legacy vCard to migrate or already migrated"); 322 session.log("debug", "No legacy vCard to migrate or already migrated");
317 return; 323 return;
318 end 324 end
319 local pep_service = mod_pep.get_pep_service(username); 325 local pep_service = mod_pep.get_pep_service(username);
320 if pep_service:get_last_item("urn:xmpp:vcard4", true) 326 vcard_temp = st.deserialize(vcard_temp);
321 or pep_service:get_last_item("urn:xmpp:avatar:metadata", true) 327 local vcard4, avatars = vcard_to_pep(vcard_temp);
328 if pep_service:get_last_item("urn:xmpp:vcard4", true) then
329 vcard4 = nil;
330 end
331 if pep_service:get_last_item("urn:xmpp:avatar:metadata", true)
322 or pep_service:get_last_item("urn:xmpp:avatar:data", true) then 332 or pep_service:get_last_item("urn:xmpp:avatar:data", true) then
333 avatars = nil;
334 end
335 if not (vcard4 or avatars) then
323 session.log("debug", "Already PEP data, not overwriting with migrated data"); 336 session.log("debug", "Already PEP data, not overwriting with migrated data");
324 vcards:set(username, nil); 337 vcards:set(username, nil);
325 return; 338 return;
326 end 339 end
327 vcard_temp = st.deserialize(vcard_temp); 340 local ok, err = save_to_pep(pep_service, true, vcard4, avatars);
328 local ok, err = save_to_pep(pep_service, true, vcard_to_pep(vcard_temp));
329 if ok and vcards:set(username, nil) then 341 if ok and vcards:set(username, nil) then
330 session.log("info", "Migrated vCard-temp to PEP"); 342 session.log("info", "Migrated vCard-temp to PEP");
331 else 343 else
332 session.log("info", "Failed to migrate vCard-temp to PEP: %s", err or "problem emptying 'vcard' store"); 344 session.log("info", "Failed to migrate vCard-temp to PEP: %s", err or "problem emptying 'vcard' store");
333 end 345 end