Skip to content

Commit

Permalink
feat(onlineservices): run on startup and async
Browse files Browse the repository at this point in the history
  • Loading branch information
GeopJr committed Dec 15, 2024
1 parent fc98871 commit 306c68e
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 26 deletions.
55 changes: 31 additions & 24 deletions src/Application.vala
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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;
Expand All @@ -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)");
}

}
}

Expand Down
4 changes: 2 additions & 2 deletions src/Dialogs/Preferences.vala
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 306c68e

Please sign in to comment.