From 08a5088c16ae0bd69adc42ac6489adde3a9ad13f Mon Sep 17 00:00:00 2001 From: fiaxh Date: Fri, 2 Aug 2019 03:15:12 +0200 Subject: Rework encryption enabling logic + UI --- plugins/omemo/src/protocol/stream_module.vala | 38 ++++++++++++++++++++------- 1 file changed, 28 insertions(+), 10 deletions(-) (limited to 'plugins/omemo/src/protocol') diff --git a/plugins/omemo/src/protocol/stream_module.vala b/plugins/omemo/src/protocol/stream_module.vala index 0e4e962d..258ff8c0 100644 --- a/plugins/omemo/src/protocol/stream_module.vala +++ b/plugins/omemo/src/protocol/stream_module.vala @@ -17,7 +17,7 @@ public class StreamModule : XmppStreamModule { public Store store { public get; private set; } private ConcurrentSet active_bundle_requests = new ConcurrentSet(); - private ConcurrentSet active_devicelist_requests = new ConcurrentSet(); + private HashMap>> active_devicelist_requests = new HashMap>>(Jid.hash_func, Jid.equals_func); private Map> ignored_devices = new HashMap>(Jid.hash_bare_func, Jid.equals_bare_func); public signal void store_created(Store store); @@ -29,22 +29,40 @@ public class StreamModule : XmppStreamModule { this.store = Plugin.get_context().create_store(); store_created(store); - stream.get_module(Pubsub.Module.IDENTITY).add_filtered_notification(stream, NODE_DEVICELIST, (stream, jid, id, node) => on_devicelist(stream, jid, id, node)); + stream.get_module(Pubsub.Module.IDENTITY).add_filtered_notification(stream, NODE_DEVICELIST, (stream, jid, id, node) => parse_device_list(stream, jid, id, node)); } public override void detach(XmppStream stream) {} - public void request_user_devicelist(XmppStream stream, Jid jid) { - if (active_devicelist_requests.add(jid)) { - debug("requesting device list for %s", jid.to_string()); - stream.get_module(Pubsub.Module.IDENTITY).request(stream, jid, NODE_DEVICELIST, (stream, jid, id, node) => on_devicelist(stream, jid, id, node)); + public async ArrayList request_user_devicelist(XmppStream stream, Jid jid) { + var future = active_devicelist_requests[jid]; + if (future == null) { + var promise = new Promise?>(); + future = promise.future; + active_devicelist_requests[jid] = future; + + stream.get_module(Pubsub.Module.IDENTITY).request(stream, jid, NODE_DEVICELIST, (stream, jid, id, node) => { + ArrayList device_list = parse_device_list(stream, jid, id, node); + promise.set_value(device_list); + active_devicelist_requests.unset(jid); + }); + } + + try { + ArrayList device_list = yield future.wait_async(); + return device_list; + } catch (FutureError error) { + warning("Future error when waiting for device list: %s", error.message); + return new ArrayList(); } } - public void on_devicelist(XmppStream stream, Jid jid, string? id, StanzaNode? node_) { + public ArrayList parse_device_list(XmppStream stream, Jid jid, string? id, StanzaNode? node_) { + ArrayList device_list = new ArrayList(); + StanzaNode node = node_ ?? new StanzaNode.build("list", NS_URI).add_self_xmlns(); Jid? my_jid = stream.get_flag(Bind.Flag.IDENTITY).my_jid; - if (my_jid == null) return; + if (my_jid == null) return device_list; if (jid.equals_bare(my_jid) && store.local_registration_id != 0) { bool am_on_devicelist = false; foreach (StanzaNode device_node in node.get_subnodes("device")) { @@ -61,12 +79,12 @@ public class StreamModule : XmppStreamModule { publish_bundles_if_needed(stream, jid); } - ArrayList device_list = new ArrayList(); foreach (StanzaNode device_node in node.get_subnodes("device")) { device_list.add(device_node.get_attribute_int("id")); } - active_devicelist_requests.remove(jid); device_list_loaded(jid, device_list); + + return device_list; } public void fetch_bundles(XmppStream stream, Jid jid, Gee.List devices) { -- cgit v1.2.3-54-g00ecf