From 50c55c7f55aff6622d242bdcf2b58d5f7956f28e Mon Sep 17 00:00:00 2001 From: fiaxh Date: Wed, 10 Jun 2020 19:53:56 +0200 Subject: Fetch avatars only when they are used --- xmpp-vala/src/module/xep/0054_vcard/module.vala | 48 +++++++++---------------- 1 file changed, 16 insertions(+), 32 deletions(-) (limited to 'xmpp-vala/src/module/xep/0054_vcard') diff --git a/xmpp-vala/src/module/xep/0054_vcard/module.vala b/xmpp-vala/src/module/xep/0054_vcard/module.vala index 2cebea2f..4df1d9a5 100644 --- a/xmpp-vala/src/module/xep/0054_vcard/module.vala +++ b/xmpp-vala/src/module/xep/0054_vcard/module.vala @@ -2,16 +2,24 @@ namespace Xmpp.Xep.VCard { private const string NS_URI = "vcard-temp"; private const string NS_URI_UPDATE = NS_URI + ":x:update"; +public async Bytes? fetch_image(XmppStream stream, Jid jid, string hash) { + Iq.Stanza iq = new Iq.Stanza.get(new StanzaNode.build("vCard", NS_URI).add_self_xmlns()) { to=jid }; + Iq.Stanza iq_res = yield stream.get_module(Iq.Module.IDENTITY).send_iq_async(stream, iq); + + if (iq_res.is_error()) return null; + string? res = iq_res.stanza.get_deep_string_content(@"$NS_URI:vCard", "PHOTO", "BINVAL"); + if (res == null) return null; + Bytes content = new Bytes.take(Base64.decode(res)); + string sha1 = Checksum.compute_for_bytes(ChecksumType.SHA1, content); + if (sha1 != hash) return null; + + return content; +} + public class Module : XmppStreamModule { public static ModuleIdentity IDENTITY = new ModuleIdentity(NS_URI, "0153_vcard_based_avatars"); - public signal void received_avatar(XmppStream stream, Jid jid, string id); - - private PixbufStorage storage; - - public Module(PixbufStorage storage) { - this.storage = storage; - } + public signal void received_avatar_hash(XmppStream stream, Jid jid, string hash); public override void attach(XmppStream stream) { stream.get_module(Presence.Module.IDENTITY).received_presence.connect(on_received_presence); @@ -34,31 +42,7 @@ public class Module : XmppStreamModule { if (photo_node == null) return; string? sha1 = photo_node.get_string_content(); if (sha1 == null) return; - if (storage.has_image(sha1)) { - if (stream.get_flag(Muc.Flag.IDENTITY).is_occupant(presence.from)) { - received_avatar(stream, presence.from, sha1); - } else { - received_avatar(stream, presence.from.bare_jid, sha1); - } - } else { - Iq.Stanza iq = new Iq.Stanza.get(new StanzaNode.build("vCard", NS_URI).add_self_xmlns()); - if (stream.get_flag(Muc.Flag.IDENTITY).is_occupant(presence.from)) { - iq.to = presence.from; - } else { - iq.to = presence.from.bare_jid; - } - stream.get_module(Iq.Module.IDENTITY).send_iq(stream, iq, on_received_vcard); - } - } - - private void on_received_vcard(XmppStream stream, Iq.Stanza iq) { - if (iq.is_error()) return; - string? res = iq.stanza.get_deep_string_content(@"$NS_URI:vCard", "PHOTO", "BINVAL"); - if (res == null) return; - Bytes content = new Bytes.take(Base64.decode(res)); - string sha1 = Checksum.compute_for_bytes(ChecksumType.SHA1, content); - storage.store(sha1, content); - stream.get_module(IDENTITY).received_avatar(stream, iq.from, sha1); + received_avatar_hash(stream, presence.from, sha1); } } } -- cgit v1.2.3-54-g00ecf