Skip to content

Commit

Permalink
feat(NotificationsSettings): move policy prefs here (#1252)
Browse files Browse the repository at this point in the history
  • Loading branch information
GeopJr authored Dec 10, 2024
1 parent 50d2ad5 commit d23027f
Show file tree
Hide file tree
Showing 5 changed files with 175 additions and 155 deletions.
1 change: 1 addition & 0 deletions data/gresource.xml
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@
<file>ui/dialogs/advanced_search.ui</file>
<file>ui/dialogs/new_account.ui</file>
<file>ui/dialogs/compose.ui</file>
<file>ui/dialogs/notification_settings.ui</file>
<file>ui/dialogs/main.ui</file>
<file>ui/dialogs/preferences.ui</file>
<file>ui/dialogs/profile_edit.ui</file>
Expand Down
88 changes: 88 additions & 0 deletions data/ui/dialogs/notification_settings.ui
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<?xml version="1.0" encoding="UTF-8"?>
<interface>
<requires lib="gtk" version="4.0" />
<requires lib="libadwaita" version="1.0" />
<template class="TubaDialogsNotificationSettings" parent="AdwDialog">
<property name="title" translatable="yes">Filter</property>
<property name="content-width">460</property>
<property name="content-height">780</property>
<child>
<object class="AdwToolbarView">
<child type="top">
<object class="AdwHeaderBar">
<child type="start">
<object class="GtkButton" id="clear_button">
<property name="icon-name">user-trash-symbolic</property>
<property name="tooltip-text" translatable="yes">Clear All Notifications</property>
<signal name="clicked" handler="clear_all_notifications" swapped="no" />
<style>
<class name="flat" />
<class name="error" />
</style>
</object>
</child>
</object>
</child>

<property name="content">
<object class="AdwToastOverlay" id="toast_overlay">
<property name="hexpand">1</property>
<property name="vexpand">1</property>
<child>
<object class="AdwPreferencesPage">
<property name="icon_name">tuba-bell-outline-symbolic</property>
<property name="title" translatable="yes">Notifications</property>
<child>
<object class="AdwPreferencesGroup" id="filters_group">
<property name="title" translatable="yes">Included Notifications</property>
</object>
</child>
<child>
<object class="AdwPreferencesGroup" id="filtered_notifications_group">
<property name="visible">0</property>
<!-- translators: Preferences group title -->
<property name="title" translatable="yes">Filter Notifications Out</property>
<!-- translators: Preferences group description -->
<property name="description" translatable="yes">Filter out notifications from people you may not know</property>
<child>
<object class="AdwSwitchRow" id="filter_notifications_following_switch">
<!-- translators: Preferences switch title about filtering out notifications; you can find this string translated on https://github.com/mastodon/mastodon/tree/main/app/javascript/mastodon/locales -->
<property name="title" translatable="yes">People you don't follow</property>
<!-- translators: Preferences switch subtitle about filtering out 'People you don't follow'; you can find this string translated on https://github.com/mastodon/mastodon/tree/main/app/javascript/mastodon/locales -->
<property name="subtitle" translatable="yes">Until you manually approve them</property>
</object>
</child>
<child>
<object class="AdwSwitchRow" id="filter_notifications_follower_switch">
<!-- translators: Preferences switch title about filtering out notifications; you can find this string translated on https://github.com/mastodon/mastodon/tree/main/app/javascript/mastodon/locales -->
<property name="title" translatable="yes">People not following you</property>
<!-- translators: Preferences switch subtitle about filtering out 'People not following you'; you can find this string translated on https://github.com/mastodon/mastodon/tree/main/app/javascript/mastodon/locales -->
<property name="subtitle" translatable="yes">Including people who have been following you fewer than 3 days</property>
</object>
</child>
<child>
<object class="AdwSwitchRow" id="filter_notifications_new_account_switch">
<!-- translators: Preferences switch title about filtering out notifications; you can find this string translated on https://github.com/mastodon/mastodon/tree/main/app/javascript/mastodon/locales -->
<property name="title" translatable="yes">New accounts</property>
<!-- translators: Preferences switch subtitle about filtering out 'New accounts'; you can find this string translated on https://github.com/mastodon/mastodon/tree/main/app/javascript/mastodon/locales -->
<property name="subtitle" translatable="yes">Created within the past 30 days</property>
</object>
</child>
<child>
<object class="AdwSwitchRow" id="filter_notifications_dm_switch">
<!-- translators: Preferences switch title about filtering out notifications; you can find this string translated on https://github.com/mastodon/mastodon/tree/main/app/javascript/mastodon/locales -->
<property name="title" translatable="yes">Unsolicited private mentions</property>
<!-- translators: Preferences switch subtitle about filtering out 'Unsolicited private mentions'; you can find this string translated on https://github.com/mastodon/mastodon/tree/main/app/javascript/mastodon/locales -->
<property name="subtitle" translatable="yes">Filtered unless it's in reply to your own mention or if you follow the sender</property>
</object>
</child>
</object>
</child>
</object>
</child>
</object>
</property>
</object>
</child>
</template>
</interface>
41 changes: 0 additions & 41 deletions data/ui/dialogs/preferences.ui
Original file line number Diff line number Diff line change
Expand Up @@ -306,47 +306,6 @@
</child>
</object>
</child>
<child>
<object class="AdwPreferencesGroup" id="filtered_notifications_group">
<property name="visible">0</property>
<!-- translators: Preferences group title -->
<property name="title" translatable="yes">Filter Notifications Out</property>
<!-- translators: Preferences group description -->
<property name="description" translatable="yes">Filter out notifications from people you may not know</property>
<child>
<object class="AdwSwitchRow" id="filter_notifications_following_switch">
<!-- translators: Preferences switch title about filtering out notifications; you can find this string translated on https://github.com/mastodon/mastodon/tree/main/app/javascript/mastodon/locales -->
<property name="title" translatable="yes">People you don't follow</property>
<!-- translators: Preferences switch subtitle about filtering out 'People you don't follow'; you can find this string translated on https://github.com/mastodon/mastodon/tree/main/app/javascript/mastodon/locales -->
<property name="subtitle" translatable="yes">Until you manually approve them</property>
</object>
</child>
<child>
<object class="AdwSwitchRow" id="filter_notifications_follower_switch">
<!-- translators: Preferences switch title about filtering out notifications; you can find this string translated on https://github.com/mastodon/mastodon/tree/main/app/javascript/mastodon/locales -->
<property name="title" translatable="yes">People not following you</property>
<!-- translators: Preferences switch subtitle about filtering out 'People not following you'; you can find this string translated on https://github.com/mastodon/mastodon/tree/main/app/javascript/mastodon/locales -->
<property name="subtitle" translatable="yes">Including people who have been following you fewer than 3 days</property>
</object>
</child>
<child>
<object class="AdwSwitchRow" id="filter_notifications_new_account_switch">
<!-- translators: Preferences switch title about filtering out notifications; you can find this string translated on https://github.com/mastodon/mastodon/tree/main/app/javascript/mastodon/locales -->
<property name="title" translatable="yes">New accounts</property>
<!-- translators: Preferences switch subtitle about filtering out 'New accounts'; you can find this string translated on https://github.com/mastodon/mastodon/tree/main/app/javascript/mastodon/locales -->
<property name="subtitle" translatable="yes">Created within the past 30 days</property>
</object>
</child>
<child>
<object class="AdwSwitchRow" id="filter_notifications_dm_switch">
<!-- translators: Preferences switch title about filtering out notifications; you can find this string translated on https://github.com/mastodon/mastodon/tree/main/app/javascript/mastodon/locales -->
<property name="title" translatable="yes">Unsolicited private mentions</property>
<!-- translators: Preferences switch subtitle about filtering out 'Unsolicited private mentions'; you can find this string translated on https://github.com/mastodon/mastodon/tree/main/app/javascript/mastodon/locales -->
<property name="subtitle" translatable="yes">Filtered unless it's in reply to your own mention or if you follow the sender</property>
</object>
</child>
</object>
</child>
</object>
</child>
<child>
Expand Down
119 changes: 86 additions & 33 deletions src/Dialogs/NotificationSettings.vala
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[GtkTemplate (ui = "/dev/geopjr/Tuba/ui/dialogs/notification_settings.ui")]
public class Tuba.Dialogs.NotificationSettings : Adw.Dialog {
public signal void filters_changed ();

// Why is AdwSwitchRow sealed???
class NotificationRow : Adw.ActionRow {
public bool active {
get { return row_switch.active; }
Expand Down Expand Up @@ -34,14 +34,17 @@ public class Tuba.Dialogs.NotificationSettings : Adw.Dialog {
debug ("Destroying Dialog NotificationSettings");
}

Adw.ToastOverlay toast_overlay;
Gtk.Button clear_button;
[GtkChild] protected unowned Adw.ToastOverlay toast_overlay;
[GtkChild] protected unowned Gtk.Button clear_button;
[GtkChild] protected unowned Adw.PreferencesGroup filters_group;
[GtkChild] protected unowned Adw.PreferencesGroup filtered_notifications_group;
[GtkChild] protected unowned Adw.SwitchRow filter_notifications_following_switch;
[GtkChild] protected unowned Adw.SwitchRow filter_notifications_follower_switch;
[GtkChild] protected unowned Adw.SwitchRow filter_notifications_new_account_switch;
[GtkChild] protected unowned Adw.SwitchRow filter_notifications_dm_switch;

NotificationRow[] notification_rows;
construct {
this.title = _("Filter");
this.content_width = 460;
this.content_height = 464;

notification_rows = {
new NotificationRow (InstanceAccount.KIND_MENTION, _("Mentions"), "tuba-chat-symbolic"),
new NotificationRow (InstanceAccount.KIND_FAVOURITE, _("Favorites"), "tuba-starred-symbolic"),
Expand All @@ -51,36 +54,12 @@ public class Tuba.Dialogs.NotificationSettings : Adw.Dialog {
new NotificationRow (InstanceAccount.KIND_FOLLOW, _("Follows"), "contact-new-symbolic")
};

var page = new Adw.PreferencesPage ();
toast_overlay = new Adw.ToastOverlay () {
vexpand = true,
hexpand = true,
child = page,
valign = Gtk.Align.CENTER
};
var toolbarview = new Adw.ToolbarView () {
content = toast_overlay
};

var headerbar = new Adw.HeaderBar ();
clear_button = new Gtk.Button.from_icon_name ("user-trash-symbolic") {
css_classes = { "flat", "error" },
tooltip_text = _("Clear All Notifications")
};
clear_button.clicked.connect (clear_all_notifications);
headerbar.pack_start (clear_button);
toolbarview.add_top_bar (headerbar);

var filters_group = new Adw.PreferencesGroup () {
title = _("Included Notifications")
};
foreach (var row in notification_rows) {
filters_group.add (row);
};

page.add (filters_group);
this.child = toolbarview;
this.closed.connect (save);
setup_notification_filters ();
}

private void save () {
Expand All @@ -107,9 +86,47 @@ public class Tuba.Dialogs.NotificationSettings : Adw.Dialog {
settings.notification_filters = new_filters;
filters_changed ();
}

if (notification_filter_policy_status != null) {
bool nfp_changed = false;
notification_filter_policy_status.@foreach (entry => {
if (((Adw.SwitchRow) entry.key).active != (bool) entry.value) {
nfp_changed = true;
return false;
}

return true;
});

if (nfp_changed) {
var builder = new Json.Builder ();
builder.begin_object ();

builder.set_member_name ("filter_not_following");
builder.add_boolean_value (filter_notifications_following_switch.active);

builder.set_member_name ("filter_not_followers");
builder.add_boolean_value (filter_notifications_follower_switch.active);

builder.set_member_name ("filter_new_accounts");
builder.add_boolean_value (filter_notifications_new_account_switch.active);

builder.set_member_name ("filter_private_mentions");
builder.add_boolean_value (filter_notifications_dm_switch.active);

builder.end_object ();

new Request.PUT ("/api/v1/notifications/policy")
.with_account (accounts.active)
.body_json (builder)
.exec ();
}

notification_filter_policy_status.clear ();
}
}

private void clear_all_notifications () {
[GtkCallback] private void clear_all_notifications () {
var dlg = new Adw.AlertDialog (
_("Clear All Notifications?"),
null
Expand Down Expand Up @@ -140,4 +157,40 @@ public class Tuba.Dialogs.NotificationSettings : Adw.Dialog {
}
});
}

private Gee.HashMap<Adw.SwitchRow, bool>? notification_filter_policy_status = null;
void setup_notification_filters () {
if (!accounts.active.probably_has_notification_filters) return;

new Request.GET ("/api/v1/notifications/policy")
.with_account (accounts.active)
.then ((in_stream) => {
var parser = Network.get_parser_from_inputstream (in_stream);
var node = network.parse_node (parser);
if (node == null) return;

filtered_notifications_group.visible = true;
var policies = API.NotificationFilter.Policy.from (node);

notification_filter_policy_status = new Gee.HashMap<Adw.SwitchRow, bool> ();
notification_filter_policy_status.set (filter_notifications_following_switch, policies.filter_not_following);
notification_filter_policy_status.set (filter_notifications_follower_switch, policies.filter_not_followers);
notification_filter_policy_status.set (filter_notifications_new_account_switch, policies.filter_new_accounts);
notification_filter_policy_status.set (filter_notifications_dm_switch, policies.filter_private_mentions);

notification_filter_policy_status.@foreach (entry => {
((Adw.SwitchRow) entry.key).active = (bool) entry.value;

return true;
});
})
.on_error ((code, message) => {
if (code == 404) {
accounts.active.probably_has_notification_filters = false;
} else {
warning (@"Error while trying to get notification policy: $code $message");
}
})
.exec ();
}
}
Loading

0 comments on commit d23027f

Please sign in to comment.