Skip to content

Commit

Permalink
feat: web+ap support (GeopJr#561)
Browse files Browse the repository at this point in the history
* feat: web+ap support

* feat: tests
  • Loading branch information
GeopJr authored Sep 29, 2023
1 parent f767cdd commit bf3d14c
Show file tree
Hide file tree
Showing 7 changed files with 105 additions and 10 deletions.
2 changes: 1 addition & 1 deletion data/dev.geopjr.Tuba.desktop.in
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Categories=GNOME;GTK;Network;
Keywords=toot;mastodon;fediverse;gotosocial;pleroma;akkoma;
X-GNOME-Gettext-Domain=dev.geopjr.Tuba
X-GNOME-UsesNotifications=true
MimeType=x-scheme-handler/tuba;
MimeType=x-scheme-handler/tuba;x-scheme-handler/web+ap;
# Translators: Do NOT translate or transliterate this text (these are enum types)!
X-Purism-FormFactor=Workstation;Mobile;
StartupNotify=true
58 changes: 52 additions & 6 deletions src/Application.vala
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,22 @@ namespace Tuba {
);
}

private void handle_web_ap (Uri uri) {
if (accounts.active == null) return;

accounts.active.resolve.begin (WebApHandler.from_uri (uri), (obj, res) => {
try {
accounts.active.resolve.end (res).open ();
} catch (Error e) {
string msg = @"Failed to resolve URL \"$uri\": $(e.message)";
warning (msg);

var dlg = inform (_("Error"), msg);
dlg.present ();
}
});
}

construct {
application_id = Build.DOMAIN;
flags = ApplicationFlags.HANDLES_OPEN;
Expand Down Expand Up @@ -233,7 +249,9 @@ namespace Tuba {
add_action_entries (APP_ENTRIES, this);
}

private bool activated = false;
protected override void activate () {
activated = true;
present_window ();

if (start_hidden) {
Expand All @@ -252,13 +270,41 @@ namespace Tuba {
}

public override void open (File[] files, string hint) {
if (!activated) activate ();

foreach (File file in files) {
string uri = file.get_uri ();
if (add_account_window != null)
add_account_window.redirect (uri);
else
warning (@"Received an unexpected uri to open: $uri");
return;
string unparsed_uri = file.get_uri ();

try {
Uri uri = Uri.parse (unparsed_uri, UriFlags.NONE);
string scheme = uri.get_scheme ();

switch (scheme) {
case "tuba":
// translators: the variable is a uri scheme like 'https'
if (add_account_window == null)
throw new Error.literal (-1, 1, _("'%s://' may only be used when adding a new account").printf (scheme));
add_account_window.redirect (uri);

break;
case "web+ap":
// translators: the variable is a uri scheme like 'https'
if (add_account_window != null)
throw new Error.literal (-1, 2, _("'%s://' may not be used when adding a new account").printf (scheme));
handle_web_ap (uri);

break;
default:
// translators: the first variable is the app name ('Tuba'),
// the second one is a uri scheme like 'https'
throw new Error.literal (-1, 3, _("%s does not accept '%s://'").printf (Build.NAME, scheme));
}
} catch (GLib.Error e) {
string msg = @"Couldn't open $unparsed_uri: $(e.message)";
warning (msg);
var dlg = inform (_("Error"), msg);
dlg.present ();
}
}
}

Expand Down
5 changes: 2 additions & 3 deletions src/Dialogs/NewAccount.vala
Original file line number Diff line number Diff line change
Expand Up @@ -189,13 +189,12 @@ public class Tuba.Dialogs.NewAccount: Adw.Window {
accounts.activate (account);
}

public void redirect (string t_uri) {
public void redirect (Uri uri) {
present ();
debug (@"Received uri: $t_uri");
debug (@"Received uri: $uri");

string code_from_params = "";
try {
var uri = Uri.parse (t_uri, UriFlags.NONE);
var uri_params = Uri.parse_params (uri.get_query ());
if (uri_params.contains ("code"))
code_from_params = uri_params.get ("code");
Expand Down
15 changes: 15 additions & 0 deletions src/Utils/WebApHandler.vala
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// https://fedilinks.org/spec/en/6-The-web-ap-URI
public class Tuba.WebApHandler {
public static string from_uri (Uri uri) {
return Uri.join (
uri.get_flags (),
"https",
uri.get_userinfo (),
uri.get_host (),
uri.get_port (),
uri.get_path (),
uri.get_query (),
uri.get_fragment ()
);
}
}
1 change: 1 addition & 0 deletions src/Utils/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ sources += files(
'Locale.vala',
'Tracking.vala',
'Units.vala',
'WebApHandler.vala',
)
31 changes: 31 additions & 0 deletions tests/WebApHandler.test.vala
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
struct TestUrl {
public string original;
public string result;
}

const TestUrl[] URLS = {
{ "web+ap://www.gnome.org/", "https://www.gnome.org/" },
{ "web+ap://www.gnome.org/test", "https://www.gnome.org/test" },
{ "web+ap://www.gnome.org/test?foo=bar", "https://www.gnome.org/test?foo=bar" },
{ "web+ap://www.gnome.org/test?foo=bar&fizz=buzz", "https://www.gnome.org/test?foo=bar&fizz=buzz" },
{ "web+ap://www.gnome.org/test?foo=bar&fizz=buzz#main", "https://www.gnome.org/test?foo=bar&fizz=buzz#main" }
};

public void test_web_ap_handler () {
foreach (var test_url in URLS) {
try {
var uri = Uri.parse (test_url.original, UriFlags.NONE);

assert_cmpstr (Tuba.WebApHandler.from_uri (uri), CompareOperator.EQ, test_url.result);
} catch (Error e) {
critical (e.message);
}
}
}

public int main (string[] args) {
Test.init (ref args);

Test.add_func ("/test_web_ap_handler", test_web_ap_handler);
return Test.run ();
}
3 changes: 3 additions & 0 deletions tests/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ unit_tests = {
),
'Units': files (
meson.project_source_root() + '/src/Utils/Units.vala'
),
'WebApHandler': files (
meson.project_source_root() + '/src/Utils/WebApHandler.vala'
)
}

Expand Down

0 comments on commit bf3d14c

Please sign in to comment.