Comparison

plugins/mod_vcard_legacy.lua @ 9808:00d210deea28 0.11

mod_vcard_legacy: Factor out conversion from vcard-temp to 4
author Kim Alvefur <zash@zash.se>
date Fri, 11 Jan 2019 09:20:45 +0100
parent 9672:e71484c210fb
child 9809:5648e8733569
comparison
equal deleted inserted replaced
9805:7bfc4269dc36 9808:00d210deea28
141 local node_defaults = { 141 local node_defaults = {
142 access_model = "open"; 142 access_model = "open";
143 _defaults_only = true; 143 _defaults_only = true;
144 }; 144 };
145 145
146 module:hook("iq-set/self/vcard-temp:vCard", function (event) 146 function vcard_to_pep(vcard_temp)
147 local origin, stanza = event.origin, event.stanza; 147 local avatars = {};
148 local pep_service = mod_pep.get_pep_service(origin.username);
149
150 local vcard_temp = stanza.tags[1];
151 148
152 local vcard4 = st.stanza("item", { xmlns = "http://jabber.org/protocol/pubsub", id = "current" }) 149 local vcard4 = st.stanza("item", { xmlns = "http://jabber.org/protocol/pubsub", id = "current" })
153 :tag("vcard", { xmlns = 'urn:ietf:params:xml:ns:vcard-4.0' }); 150 :tag("vcard", { xmlns = 'urn:ietf:params:xml:ns:vcard-4.0' });
154
155 if pep_service:purge("urn:xmpp:avatar:metadata", origin.full_jid) then
156 pep_service:purge("urn:xmpp:avatar:data", origin.full_jid);
157 end
158 151
159 vcard4:tag("fn"):text_tag("text", vcard_temp:get_child_text("FN")):up(); 152 vcard4:tag("fn"):text_tag("text", vcard_temp:get_child_text("FN")):up();
160 153
161 local N = vcard_temp:get_child("N"); 154 local N = vcard_temp:get_child("N");
162 155
246 239
247 local avatar_data = st.stanza("item", { id = avatar_hash, xmlns = "http://jabber.org/protocol/pubsub" }) 240 local avatar_data = st.stanza("item", { id = avatar_hash, xmlns = "http://jabber.org/protocol/pubsub" })
248 :tag("data", { xmlns="urn:xmpp:avatar:data" }) 241 :tag("data", { xmlns="urn:xmpp:avatar:data" })
249 :text(avatar_payload); 242 :text(avatar_payload);
250 243
251 local ok, err = pep_service:publish("urn:xmpp:avatar:data", origin.full_jid, avatar_hash, avatar_data, node_defaults) 244 table.insert(avatars, { hash = avatar_hash, meta = avatar_meta, data = avatar_data });
252 if ok then 245 end
253 ok, err = pep_service:publish("urn:xmpp:avatar:metadata", origin.full_jid, avatar_hash, avatar_meta, node_defaults); 246 end
254 end 247 end
255 if not ok then 248 return vcard4, avatars;
256 handle_error(origin, stanza, err); 249 end
257 return true; 250
258 end 251 function save_to_pep(pep_service, actor, vcard4, avatars)
259 end 252
260 end 253 if pep_service:purge("urn:xmpp:avatar:metadata", actor) then
261 end 254 pep_service:purge("urn:xmpp:avatar:data", actor);
262 255 end
263 local ok, err = pep_service:publish("urn:xmpp:vcard4", origin.full_jid, "current", vcard4, node_defaults); 256
257 for _, avatar in ipairs(avatars) do
258 local ok, err = pep_service:publish("urn:xmpp:avatar:data", actor, avatar.hash, avatar.data, node_defaults)
259 if ok then
260 ok, err = pep_service:publish("urn:xmpp:avatar:metadata", actor, avatar.hash, avatar.meta, node_defaults);
261 end
262 if not ok then
263 return ok, err;
264 end
265 end
266
267 return pep_service:publish("urn:xmpp:vcard4", actor, "current", vcard4, node_defaults);
268 end
269
270 module:hook("iq-set/self/vcard-temp:vCard", function (event)
271 local origin, stanza = event.origin, event.stanza;
272 local pep_service = mod_pep.get_pep_service(origin.username);
273
274 local vcard_temp = stanza.tags[1];
275
276 local ok, err = save_to_pep(pep_service, origin.full_jid, vcard_to_pep(vcard_temp));
264 if ok then 277 if ok then
265 origin.send(st.reply(stanza)); 278 origin.send(st.reply(stanza));
266 else 279 else
267 handle_error(origin, stanza, err); 280 handle_error(origin, stanza, err);
268 end 281 end