aboutsummaryrefslogtreecommitdiff
path: root/xmpp-vala/src
diff options
context:
space:
mode:
authorfiaxh <git@lightrise.org>2020-06-10 19:53:56 +0200
committerfiaxh <git@lightrise.org>2020-06-10 19:53:56 +0200
commit50c55c7f55aff6622d242bdcf2b58d5f7956f28e (patch)
treeb8c26336bb3003defafe987980c17fd2fdc07bd4 /xmpp-vala/src
parentdaf803e7730555639517de18c89da42e73557679 (diff)
downloaddino-50c55c7f55aff6622d242bdcf2b58d5f7956f28e.tar.gz
dino-50c55c7f55aff6622d242bdcf2b58d5f7956f28e.zip
Fetch avatars only when they are used
Diffstat (limited to 'xmpp-vala/src')
-rw-r--r--xmpp-vala/src/module/xep/0054_vcard/module.vala48
-rw-r--r--xmpp-vala/src/module/xep/0060_pubsub.vala20
-rw-r--r--xmpp-vala/src/module/xep/0084_user_avatars.vala73
3 files changed, 57 insertions, 84 deletions
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<Module> IDENTITY = new ModuleIdentity<Module>(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);
}
}
}
diff --git a/xmpp-vala/src/module/xep/0060_pubsub.vala b/xmpp-vala/src/module/xep/0060_pubsub.vala
index 9e5d8651..c8472576 100644
--- a/xmpp-vala/src/module/xep/0060_pubsub.vala
+++ b/xmpp-vala/src/module/xep/0060_pubsub.vala
@@ -1,7 +1,7 @@
using Gee;
namespace Xmpp.Xep.Pubsub {
- private const string NS_URI = "http://jabber.org/protocol/pubsub";
+ public const string NS_URI = "http://jabber.org/protocol/pubsub";
private const string NS_URI_EVENT = NS_URI + "#event";
private const string NS_URI_OWNER = NS_URI + "#owner";
@@ -39,18 +39,14 @@ namespace Xmpp.Xep.Pubsub {
Iq.Stanza request_iq = new Iq.Stanza.get(new StanzaNode.build("pubsub", NS_URI).add_self_xmlns().put_node(new StanzaNode.build("items", NS_URI).put_attribute("node", node)));
request_iq.to = jid;
- Gee.List<StanzaNode>? ret = null;
- stream.get_module(Iq.Module.IDENTITY).send_iq(stream, request_iq, (stream, iq) => {
- StanzaNode event_node = iq.stanza.get_subnode("pubsub", NS_URI);
- if (event_node == null) return;
- StanzaNode items_node = event_node.get_subnode("items", NS_URI);
- if (items_node == null) return;
- ret = items_node.get_subnodes("item", NS_URI);
- Idle.add(request_all.callback);
- });
- yield;
+ Iq.Stanza iq_res = yield stream.get_module(Iq.Module.IDENTITY).send_iq_async(stream, request_iq);
+
+ StanzaNode event_node = iq_res.stanza.get_subnode("pubsub", NS_URI);
+ if (event_node == null) return null;
+ StanzaNode items_node = event_node.get_subnode("items", NS_URI);
+ if (items_node == null) return null;
- return ret;
+ return items_node.get_subnodes("item", NS_URI);
}
public delegate void OnResult(XmppStream stream, Jid jid, string? id, StanzaNode? node);
diff --git a/xmpp-vala/src/module/xep/0084_user_avatars.vala b/xmpp-vala/src/module/xep/0084_user_avatars.vala
index 9b21eeaf..4f3b6c4f 100644
--- a/xmpp-vala/src/module/xep/0084_user_avatars.vala
+++ b/xmpp-vala/src/module/xep/0084_user_avatars.vala
@@ -3,33 +3,44 @@ namespace Xmpp.Xep.UserAvatars {
private const string NS_URI_DATA = NS_URI + ":data";
private const string NS_URI_METADATA = NS_URI + ":metadata";
- public class Module : XmppStreamModule {
- public static ModuleIdentity<Module> IDENTITY = new ModuleIdentity<Module>(NS_URI, "0084_user_avatars");
-
- public signal void received_avatar(XmppStream stream, Jid jid, string id);
+ public void publish_png(XmppStream stream, uint8[] image, int width, int height) {
+ string sha1 = Checksum.compute_for_data(ChecksumType.SHA1, image);
+ StanzaNode data_node = new StanzaNode.build("data", NS_URI_DATA).add_self_xmlns()
+ .put_node(new StanzaNode.text(Base64.encode(image)));
+ stream.get_module(Pubsub.Module.IDENTITY).publish.begin(stream, null, NS_URI_DATA, sha1, data_node);
- private PixbufStorage storage;
-
- public Module(PixbufStorage storage) {
- this.storage = storage;
- }
-
- public void publish_png(XmppStream stream, uint8[] image, int width, int height) {
- string sha1 = Checksum.compute_for_data(ChecksumType.SHA1, image);
- StanzaNode data_node = new StanzaNode.build("data", NS_URI_DATA).add_self_xmlns()
- .put_node(new StanzaNode.text(Base64.encode(image)));
- stream.get_module(Pubsub.Module.IDENTITY).publish.begin(stream, null, NS_URI_DATA, sha1, data_node);
-
- StanzaNode metadata_node = new StanzaNode.build("metadata", NS_URI_METADATA).add_self_xmlns();
- StanzaNode info_node = new StanzaNode.build("info", NS_URI_METADATA)
+ StanzaNode metadata_node = new StanzaNode.build("metadata", NS_URI_METADATA).add_self_xmlns();
+ StanzaNode info_node = new StanzaNode.build("info", NS_URI_METADATA)
.put_attribute("bytes", image.length.to_string())
.put_attribute("id", sha1)
.put_attribute("width", width.to_string())
.put_attribute("height", height.to_string())
.put_attribute("type", "image/png");
- metadata_node.put_node(info_node);
- stream.get_module(Pubsub.Module.IDENTITY).publish.begin(stream, null, NS_URI_METADATA, sha1, metadata_node);
+ metadata_node.put_node(info_node);
+ stream.get_module(Pubsub.Module.IDENTITY).publish.begin(stream, null, NS_URI_METADATA, sha1, metadata_node);
+ }
+
+ public async Bytes? fetch_image(XmppStream stream, Jid jid, string hash) {
+ Gee.List<StanzaNode>? items = yield stream.get_module(Pubsub.Module.IDENTITY).request_all(stream, jid, NS_URI_DATA);
+ if (items == null || items.size == 0 || items[0].sub_nodes.size == 0) return null;
+
+ StanzaNode node = items[0].sub_nodes[0];
+ string? id = items[0].get_attribute("id", Pubsub.NS_URI);
+ if (id == null) return null;
+
+ Bytes image = new Bytes.take(Base64.decode(node.get_string_content()));
+ string sha1 = Checksum.compute_for_bytes(ChecksumType.SHA1, image);
+ if (sha1 != id) {
+ warning("sha sum did not match for avatar from %s expected=%s actual=%s", jid.to_string(), id, sha1);
+ return null;
}
+ return image;
+ }
+
+ public class Module : XmppStreamModule {
+ public static ModuleIdentity<Module> IDENTITY = new ModuleIdentity<Module>(NS_URI, "0084_user_avatars");
+
+ public signal void received_avatar_hash(XmppStream stream, Jid jid, string id);
public override void attach(XmppStream stream) {
stream.get_module(Pubsub.Module.IDENTITY).add_filtered_notification(stream, NS_URI_METADATA, on_pupsub_event, null);
@@ -40,32 +51,14 @@ namespace Xmpp.Xep.UserAvatars {
}
- public void on_pupsub_event(XmppStream stream, Jid jid, string id, StanzaNode? node) {
+ public void on_pupsub_event(XmppStream stream, Jid jid, string hash, StanzaNode? node) {
StanzaNode? info_node = node.get_subnode("info", NS_URI_METADATA);
string? type = info_node == null ? null : info_node.get_attribute("type");
if (type != "image/png" && type != "image/jpeg") return;
- if (storage.has_image(id)) {
- stream.get_module(Module.IDENTITY).received_avatar(stream, jid, id);
- } else {
- stream.get_module(Pubsub.Module.IDENTITY).request(stream, jid, NS_URI_DATA, on_pubsub_data_response);
- }
+ received_avatar_hash(stream, jid, hash);
}
public override string get_ns() { return NS_URI; }
public override string get_id() { return IDENTITY.id; }
-
- private void on_pubsub_data_response(XmppStream stream, Jid jid, string? id, StanzaNode? node) {
- if (node == null || id == null) {
- return;
- }
- Bytes image = new Bytes.take(Base64.decode(node.get_string_content()));
- string sha1 = Checksum.compute_for_bytes(ChecksumType.SHA1, image);
- if (sha1 != id) {
- warning("sha sum did not match for avatar from %s expected=%s actual=%s", jid.to_string(), id, sha1);
- return;
- }
- storage.store(id, image);
- stream.get_module(Module.IDENTITY).received_avatar(stream, jid, id);
- }
}
}