From 306c68e1b0d2ac441e6791c075b9eaa3daaa97c9 Mon Sep 17 00:00:00 2001 From: Evan Paterakis Date: Sun, 15 Dec 2024 16:55:58 +0200 Subject: [PATCH] feat(onlineservices): run on startup and async --- src/Application.vala | 55 ++++++++++++++++++++---------------- src/Dialogs/Preferences.vala | 4 +-- 2 files changed, 33 insertions(+), 26 deletions(-) diff --git a/src/Application.vala b/src/Application.vala index 7eada0f3..efee7196 100644 --- a/src/Application.vala +++ b/src/Application.vala @@ -335,6 +335,9 @@ namespace Tuba { if (settings.proxy != "") on_proxy_change (); settings.notify ["proxy"].connect (on_proxy_notify); + + if (settings.analytics) app.update_analytics.begin (); + app.update_contributors.begin (); } private void on_proxy_change (bool recover = false) { @@ -382,9 +385,6 @@ namespace Tuba { } protected override void shutdown () { - if (settings.analytics) app.update_analytics (); - app.update_contributors (); - #if !DEV_MODE settings.apply_all (); #endif @@ -757,7 +757,7 @@ namespace Tuba { return generator.to_data (null); } - public void update_contributors () { + public async void update_contributors () { if (!settings.update_contributors) { // if updating contributors from the API is not enabled // but it has been enabled at some point in the past, @@ -788,24 +788,27 @@ namespace Tuba { } if (!can_update) return; - new Request.GET ("https://api.tuba.geopjr.dev/v1/supporters") - .then ((in_stream) => { - var parser = Network.get_parser_from_inputstream (in_stream); + var msg = new Request.GET ("https://api.tuba.geopjr.dev/v1/supporters"); + + try { + yield msg.await (); + var parser = Network.get_parser_from_inputstream (msg.response_body); - string[] new_contributors = {}; - Network.parse_array (parser, node => { - if (node != null) { - new_contributors += node.get_string (); - } - }); + string[] new_contributors = {}; + Network.parse_array (parser, node => { + if (node != null) { + new_contributors += node.get_string (); + } + }); - settings.contributors = new_contributors; - settings.last_contributors_update = now_utc.format_iso8601 (); - }) - .exec (); + settings.contributors = new_contributors; + settings.last_contributors_update = now_utc.format_iso8601 (); + } catch (Error e) { + warning (@"Couldn't update contributors: $(e.code) $(e.message)"); + } } - public void update_analytics () { + public async void update_analytics () { if (!settings.analytics) return; bool can_update = false; @@ -823,12 +826,16 @@ namespace Tuba { } if (!can_update) return; - new Request.POST ("https://api.tuba.geopjr.dev/v1/analytics") - .body ("application/json", new Bytes.take (generate_analytics_object ().data)) - .then ((in_stream) => { - settings.last_analytics_update = now_utc.format_iso8601 (); - }) - .exec (); + var msg = new Request.POST ("https://api.tuba.geopjr.dev/v1/analytics") + .body ("application/json", new Bytes.take (generate_analytics_object ().data)); + + try { + yield msg.await (); + settings.last_analytics_update = now_utc.format_iso8601 (); + } catch (Error e) { + warning (@"Couldn't update analytics: $(e.code) $(e.message)"); + } + } } diff --git a/src/Dialogs/Preferences.vala b/src/Dialogs/Preferences.vala index 2615139f..b756b4f9 100644 --- a/src/Dialogs/Preferences.vala +++ b/src/Dialogs/Preferences.vala @@ -390,8 +390,8 @@ public class Tuba.Dialogs.Preferences : Adw.PreferencesDialog { settings.proxy = ""; } - if (settings.analytics) app.update_analytics (); - app.update_contributors (); + if (settings.analytics) app.update_analytics.begin (); + app.update_contributors.begin (); } protected class AnalyticsDialog : Adw.Dialog {