[polari] userTracker: Allow to set usernames to 'muted'



commit 7ad4321b67b2ed5348e754b490cd41681a4f77d7
Author: Florian Müllner <fmuellner gnome org>
Date:   Sat Apr 20 03:20:04 2019 +0200

    userTracker: Allow to set usernames to 'muted'
    
    Bots are very common on IRC (NickServ, channelbot, etc.), and generally
    have a very low signal-to-noise ratio. A per-username 'muted' property
    will allow us to mitigate that to some extent by making it possible to
    selectively turn off some of the more attention-drawing features like
    notifications.
    
    https://gitlab.gnome.org/GNOME/polari/-/merge_requests/153

 data/org.gnome.Polari.gschema.xml |  7 +++++
 src/application.js                |  6 +++++
 src/userTracker.js                | 57 +++++++++++++++++++++++++++++++++++++++
 3 files changed, 70 insertions(+)
---
diff --git a/data/org.gnome.Polari.gschema.xml b/data/org.gnome.Polari.gschema.xml
index 2d561e82..0f2ef893 100644
--- a/data/org.gnome.Polari.gschema.xml
+++ b/data/org.gnome.Polari.gschema.xml
@@ -51,5 +51,12 @@
         Whether the identify command is known to support the username parameter
       </description>
     </key>
+    <key type="as" name="muted-usernames">
+      <default>[]</default>
+      <summary>List of muted usernames</summary>
+      <description>
+        A list of usernames for whose private messages not to show notifications
+      </description>
+    </key>
   </schema>
 </schemalist>
diff --git a/src/application.js b/src/application.js
index b413ef5c..f14a3703 100644
--- a/src/application.js
+++ b/src/application.js
@@ -225,6 +225,12 @@ var Application = GObject.registerClass({
             name: 'reconnect-account',
             activate: this._onConnectAccount.bind(this),
             parameter_type: GLib.VariantType.new('o'),
+        }, {
+            name: 'mute-nick',
+            parameter_type: GLib.VariantType.new('(ss)'),
+        }, {
+            name: 'unmute-nick',
+            parameter_type: GLib.VariantType.new('(ss)'),
         }, {
             name: 'user-list',
             activate: this._onToggleAction.bind(this),
diff --git a/src/userTracker.js b/src/userTracker.js
index da82ad8f..4f6761aa 100644
--- a/src/userTracker.js
+++ b/src/userTracker.js
@@ -17,6 +17,23 @@ var UserStatusMonitor = class {
         this._userTrackers = new Map();
         this._accountsMonitor = AccountsMonitor.getDefault();
 
+        this._app = Gio.Application.get_default();
+
+        let action;
+        action = this._app.lookup_action('mute-nick');
+        action.connect('activate', (a, params) => {
+            const [accountPath, nick] = params.deep_unpack();
+            const account = this._accountsMonitor.lookupAccount(accountPath);
+            this._userTrackers.get(account).muteNick(nick);
+        });
+
+        action = this._app.lookup_action('unmute-nick');
+        action.connect('activate', (a, params) => {
+            const [accountPath, nick] = params.deep_unpack();
+            const account = this._accountsMonitor.lookupAccount(accountPath);
+            this._userTrackers.get(account).unmuteNick(nick);
+        });
+
         this._accountsMonitor.connect('account-added',
             this._onAccountAdded.bind(this));
         this._accountsMonitor.connect('account-removed',
@@ -50,6 +67,10 @@ const UserTracker = GObject.registerClass({
             flags: GObject.SignalFlags.DETAILED,
             param_types: [GObject.TYPE_STRING, GObject.TYPE_INT],
         },
+        'muted-changed': {
+            flags: GObject.SignalFlags.DETAILED,
+            param_types: [GObject.TYPE_STRING, GObject.TYPE_BOOLEAN],
+        },
         'contacts-changed': {
             flags: GObject.SignalFlags.DETAILED,
             param_types: [GObject.TYPE_STRING],
@@ -66,6 +87,19 @@ const UserTracker = GObject.registerClass({
         this._handlerCounter = 0;
         this._app = Gio.Application.get_default();
 
+        const { settings } = account;
+        this._mutedUsers = settings.get_strv('muted-usernames');
+        settings.connect('changed::muted-usernames', () => {
+            const muted = settings.get_strv('muted-usernames');
+            const newlyMuted = muted.filter(s => !this._mutedUsers.includes(s));
+            const newlyUnmuted = this._mutedUsers.filter(s => !muted.includes(s));
+
+            this._mutedUsers = muted;
+
+            newlyMuted.forEach(s => this.emit(`muted-changed::${s}`, s, true));
+            newlyUnmuted.forEach(s => this.emit(`muted-changed::${s}`, s, false));
+        });
+
         this._app.connect('prepare-shutdown', this._onShutdown.bind(this));
 
         this._roomManager = RoomManager.getDefault();
@@ -280,6 +314,29 @@ const UserTracker = GObject.registerClass({
             : Tp.ConnectionPresenceType.AVAILABLE;
     }
 
+    isMuted(nickName) {
+        return this._mutedUsers.includes(nickName.toLowerCase());
+    }
+
+    muteNick(nickName) {
+        if (this.isMuted(nickName))
+            return;
+
+        let settings = this._account.settings;
+        settings.set_strv('muted-usernames',
+            [...this._mutedUsers, nickName.toLowerCase()]);
+    }
+
+    unmuteNick(nickName) {
+        if (!this.isMuted(nickName))
+            return;
+
+        let nick = nickName.toLowerCase();
+        let settings = this._account.settings;
+        settings.set_strv('muted-usernames',
+            this._mutedUsers.filter(s => s !== nick));
+    }
+
     lookupContact(nickName) {
         let baseNick = Polari.util_get_basenick(nickName);
 


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]