From a332b3a2f03793934ed2b8734100cbe4b8c41357 Mon Sep 17 00:00:00 2001 From: Evangelos Paterakis Date: Wed, 24 May 2023 02:22:51 +0300 Subject: [PATCH] feat(status): doas --- data/style-dark.css | 4 ++ data/style.css | 8 ++++ src/API/Status.vala | 5 ++- src/Widgets/Status.vala | 95 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 111 insertions(+), 1 deletion(-) diff --git a/data/style-dark.css b/data/style-dark.css index d63da1683..ac2301cd6 100644 --- a/data/style-dark.css +++ b/data/style-dark.css @@ -45,3 +45,7 @@ .ttl-post.direct:hover { background-color: lighter(alpha(@warning_bg_color, .1)); } + +.ttl-post.doas:hover { + background-color: lighter(alpha(@purple_2, .1)); +} \ No newline at end of file diff --git a/data/style.css b/data/style.css index 87acf994d..266adb87c 100644 --- a/data/style.css +++ b/data/style.css @@ -265,6 +265,14 @@ flowboxchild { background-color: darker(alpha(@warning_bg_color, .1)); } +.ttl-post.doas { + background-color: alpha(@purple_2, .1); +} + +.ttl-post.doas:hover { + background-color: darker(alpha(@purple_2, .1)); +} + .ttl-post-actions { margin-bottom: -12px; } diff --git a/src/API/Status.vala b/src/API/Status.vala index 6dae8cc81..5971a0430 100644 --- a/src/API/Status.vala +++ b/src/API/Status.vala @@ -6,6 +6,9 @@ public class Tuba.API.Status : Entity, Widgetizable { message ("[OBJ] Destroyed "+uri); } + public InstanceAccount? doas { get; set; default=null; } + public string? doas_id { get; set; default=null; } + public string id { get; set; } public API.Account account { get; set; } public string uri { get; set; } @@ -163,7 +166,7 @@ public class Tuba.API.Status : Entity, Widgetizable { } public Request action (string action) { - var req = new Request.POST (@"/api/v1/statuses/$(formal.id)/$action").with_account (accounts.active); + var req = new Request.POST (@"/api/v1/statuses/$(doas != null ? doas_id : formal.id)/$action").with_account (doas ?? accounts.active); req.priority = Soup.MessagePriority.HIGH; return req; } diff --git a/src/Widgets/Status.vala b/src/Widgets/Status.vala index 6270dae4a..2b19dc1fd 100644 --- a/src/Widgets/Status.vala +++ b/src/Widgets/Status.vala @@ -102,6 +102,7 @@ public class Tuba.Widgets.Status : ListBoxRow { private GLib.SimpleActionGroup action_group; private SimpleAction edit_history_simple_action; private SimpleAction stats_simple_action; + private SimpleAction doas_simple_action; public bool is_conversation_open { get; set; default = false; } @@ -180,8 +181,12 @@ public class Tuba.Widgets.Status : ListBoxRow { stats_simple_action = new SimpleAction ("status-stats", null); stats_simple_action.activate.connect (view_stats); + doas_simple_action = new SimpleAction ("doas", null); + doas_simple_action.activate.connect (doas); + action_group = new GLib.SimpleActionGroup (); action_group.add_action_entries (action_entries, this); + action_group.add_action(doas_simple_action); action_group.add_action(stats_simple_action); action_group.add_action(edit_history_simple_action); @@ -251,6 +256,10 @@ public class Tuba.Widgets.Status : ListBoxRow { stats_menu_item.set_attribute_value("hidden-when", "action-disabled"); menu_model.append_item (stats_menu_item); + var doas_menu_item = new MenuItem(_("doas"), "status.doas"); + doas_menu_item.set_attribute_value("hidden-when", "action-disabled"); + menu_model.append_item (doas_menu_item); + var edit_history_menu_item = new MenuItem(_("View Edit History"), "status.edit-history"); edit_history_menu_item.set_attribute_value("hidden-when", "action-disabled"); menu_model.append_item (edit_history_menu_item); @@ -279,6 +288,89 @@ public class Tuba.Widgets.Status : ListBoxRow { app.main_window.open_view (new Views.StatusStats (status.formal.id)); } + private void doas () { + var preferences_page = new Adw.PreferencesPage(); + var preferences_group = new Adw.PreferencesGroup() { + title = "Select an account to act as:" + }; + + preferences_page.add (preferences_group); + + var clamp = new Adw.Clamp () { + child = preferences_page, + tightening_threshold = 100, + valign = Align.START + }; + var scroller = new Gtk.ScrolledWindow () { + hexpand = true, + vexpand = true + }; + scroller.child = clamp; + + var box = new Gtk.Box(Gtk.Orientation.VERTICAL, 0); + var headerbar = new Adw.HeaderBar() { + css_classes = { "flat" } + }; + + box.append(headerbar); + box.append(scroller); + + var dialog = new Adw.Window() { + modal = true, + title = "doas", + transient_for = app.main_window, + content = box, + default_width = 460, + default_height = 520 + }; + + accounts.saved.foreach (acc => { + if (acc == this.status.formal.doas || (acc == accounts.active && this.status.formal.doas == null)) return true; + var account_avi = new Widgets.Avatar () { + account = acc + }; + var account_row = new Adw.ActionRow () { + title = acc.handle, + activatable = true + }; + account_row.add_prefix (account_avi); + account_row.activated.connect (() => { + if (acc == accounts.active) { + this.remove_css_class ("doas"); + this.status.formal.doas = null; + dialog.close (); + } else { + new Request.GET ("/api/v2/search") + .with_account (acc) + .with_param ("q", this.status.formal.url) + .with_param ("resolve", "true") + .then ((sess, msg, in_stream) => { + var parser = Network.get_parser_from_inputstream(in_stream); + var root = network.parse (parser); + var statuses = root.get_array_member ("statuses"); + var node = statuses.get_element (0); + if (node != null){ + var status = API.Status.from (node); + this.status.formal.doas = acc; + this.status.formal.doas_id = status.formal.id; + this.add_css_class ("doas"); + } else { + this.remove_css_class ("doas"); + this.status.formal.doas = null; + } + dialog.close (); + }) + .exec (); + } + }); + + preferences_group.add (account_row); + return true; + }); + + dialog.show (); + } + private void on_edit (API.Status x) { this.status.patch(x); bind (); @@ -450,6 +542,7 @@ public class Tuba.Widgets.Status : ListBoxRow { } const string[] ALLOWED_CARD_TYPES = { "link", "video" }; + const string[] DOAS_ALLOWED_LIST = { "unlisted", "public" }; protected virtual void bind () { this.content.instance_emojis = status.formal.emojis_map; this.content.content = status.formal.content; @@ -474,6 +567,8 @@ public class Tuba.Widgets.Status : ListBoxRow { if (change_background_on_direct && status.formal.visibility == "direct") this.add_css_class ("direct"); + doas_simple_action.set_enabled(status.formal.visibility in DOAS_ALLOWED_LIST && accounts.saved.size > 1); + avatar.account = status.formal.account; reactions = status.formal.compat_status_reactions;