RichLabel: Use search API to resolve links

This commit is contained in:
bleakgrey 2019-03-17 13:17:47 +03:00
parent 5c32508eb5
commit 130f8e3e51
3 changed files with 53 additions and 16 deletions

View File

@ -4,7 +4,7 @@ using Gdk;
public class Tootle.Dialogs.MainWindow: Gtk.Window, ISavedWindow {
private Overlay overlay;
private Granite.Widgets.Toast toast;
public Granite.Widgets.Toast toast;
private Grid grid;
private Stack view_stack;
private Stack timeline_stack;

View File

@ -70,7 +70,6 @@ public class Tootle.Views.Search : Views.Abstract {
var url = "%s/api/v1/search?q=%s".printf (accounts.formal.instance, query_encoded);
var msg = new Soup.Message("GET", url);
network.queue (msg, (sess, mess) => {
try {
var root = network.parse (mess);
var accounts = root.get_array_member ("accounts");
var statuses = root.get_array_member ("statuses");
@ -104,11 +103,6 @@ public class Tootle.Views.Search : Views.Abstract {
}
empty_state ();
}
catch (GLib.Error e) {
warning ("Can't update feed");
warning (e.message);
}
});
}

View File

@ -55,18 +55,61 @@ public class Tootle.Widgets.RichLabel : Label {
return true;
}
if ("/@" in url) {
var uri = new Soup.URI (url);
var username = url.split("/@")[1];
if ("@" in url || "tags" in url) {
var query = Soup.URI.encode (url, null);
var msg_url = "%s/api/v1/search?q=%s".printf (accounts.formal.instance, query);
var msg = new Soup.Message("GET", msg_url);
msg.priority = Soup.MessagePriority.HIGH;
network.inject (msg, Network.INJECT_TOKEN);
network.queue (msg, (sess, mess) => {
var root = network.parse (mess);
var accounts = root.get_array_member ("accounts");
var statuses = root.get_array_member ("statuses");
var hashtags = root.get_array_member ("hashtags");
if ("/" in username)
Views.ExpandedStatus.open_from_link (url);
else
Views.Profile.open_from_name ("@" + username + "@" + uri.get_host ());
return true;
if (accounts.get_length () > 0) {
var item = accounts.get_object_element (0);
var obj = API.Account.parse (item);
window.open_view (new Views.Profile (obj));
}
else if (statuses.get_length () > 0) {
var item = accounts.get_object_element (0);
var obj = API.Status.parse (item);
window.open_view (new Views.ExpandedStatus (obj));
}
else if (hashtags.get_length () > 0) {
var item = accounts.get_object_element (0);
var obj = API.Tag.parse (item);
window.open_view (new Views.Hashtag (obj.name));
}
else {
Desktop.open_uri (url);
}
}, (status, reason) => {
open_link_fallback (url, reason);
});
}
else {
Desktop.open_uri (url);
}
return true;
}
return Desktop.open_uri (url);
public bool open_link_fallback (string url, string reason) {
warning ("Can't resolve url: " + url);
warning ("Reason: " + reason);
var toast = window.toast;
toast.title = reason;
toast.set_default_action (_("Open in Browser"));
ulong signal_id = 0;
signal_id = toast.default_action.connect (() => {
Desktop.open_uri (url);
toast.disconnect (signal_id);
});
toast.send_notification ();
return true;
}
}