Diff

mod_profile/mod_profile.lua @ 1515:f367d7cbfaa6

mod_profile: Use module:handle_items() for PEP node bootstrapping (see trunk 388786af0dd2)
author Kim Alvefur <zash@zash.se>
date Sat, 27 Sep 2014 19:11:04 +0200
parent 1503:c4c9ccc6e6c9
child 1516:48d95ab404c7
line wrap: on
line diff
--- a/mod_profile/mod_profile.lua	Thu Sep 25 10:30:51 2014 +0200
+++ b/mod_profile/mod_profile.lua	Sat Sep 27 19:11:04 2014 +0200
@@ -13,7 +13,6 @@
 	pep_plus = module:depends"pep_plus";
 end
 
-local loaded_pep_for = module:shared"loaded-pep-for";
 local storage = module:open_store();
 local legacy_storage = module:open_store("vcard");
 
@@ -43,8 +42,8 @@
 	return "application/octet-stream";
 end
 
-local function update_pep(username, data)
-	local pep = pep_plus.get_pep_service(username.."@"..module.host);
+local function update_pep(username, data, pep)
+	pep = pep or pep_plus.get_pep_service(username.."@"..module.host);
 	local photo, p = get_item(data, "PHOTO");
 	if vcard.to_vcard4 then
 		if p then t_remove(data, p); end
@@ -119,7 +118,6 @@
 
 	if pep_plus and username then
 		update_pep(username, data);
-		loaded_pep_for[username] = true;
 	end
 
 	return origin.send(st.reply(stanza));
@@ -131,16 +129,22 @@
 module:hook("iq-set/bare/vcard-temp:vCard", handle_set);
 module:hook("iq-set/host/vcard-temp:vCard", handle_set);
 
-module:hook("presence/initial", function (event)
-	local username = event.origin.username
-	if not loaded_pep_for[username] then
-		local data = storage:get(username);
-		if data then
-			update_pep(username, data);
-		end
-		loaded_pep_for[username] = true;
+local function pep_service_added(event)
+	local item = event.item;
+	local service, username = item.service, jid_split(item.jid);
+	local data = storage:get(username);
+	if data then
+		update_pep(username, data, service);
 	end
-end);
+end
+
+local function pep_service_removed()
+	-- This would happen when mod_pep_plus gets unloaded, but this module gets unloaded before that
+end
+
+function module.load()
+	module:handle_items("pep-service", pep_service_added, pep_service_removed, true);
+end
 
 -- The vCard4 part
 if vcard.to_vcard4 then