[MM-36456] Add support for Gnome's do-not-disturb (#2278)

* Add support for gnome do-not-disturb mode

* Add tests

* Invert condition
This commit is contained in:
Tasos Boulis 2022-10-17 16:37:42 +03:00 committed by GitHub
parent d5cf3e76e1
commit cf647ce3e9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 55 additions and 0 deletions

View file

@ -20,6 +20,7 @@
"filechooser", "filechooser",
"flac", "flac",
"FOCALBOARD", "FOCALBOARD",
"gsettings",
"ICONNAME", "ICONNAME",
"loadscreen", "loadscreen",
"mmjstool", "mmjstool",

View file

@ -0,0 +1,23 @@
// Copyright (c) 2016-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
import {execSync} from 'child_process';
import log from 'electron-log';
const GNOME_READ_DND = 'gsettings get org.gnome.desktop.notifications show-banners';
function getLinuxDoNotDisturb() {
try {
const showNotifications = execSync(GNOME_READ_DND).toString().replace('\n', '');
log.debug('getLinuxDoNotDisturb', {showNotifications});
return showNotifications !== 'true';
} catch (error) {
log.error('getLinuxDoNotDisturb Error:', {error});
return false;
}
}
export default getLinuxDoNotDisturb;

View file

@ -2,6 +2,7 @@
// See LICENSE.txt for license information. // See LICENSE.txt for license information.
'use strict'; 'use strict';
import cp from 'child_process';
import {Notification, shell} from 'electron'; import {Notification, shell} from 'electron';
@ -15,10 +16,16 @@ import {localizeMessage} from 'main/i18nManager';
import WindowManager from '../windows/windowManager'; import WindowManager from '../windows/windowManager';
import getLinuxDoNotDisturb from './dnd-linux';
import {displayMention, displayDownloadCompleted, currentNotifications} from './index'; import {displayMention, displayDownloadCompleted, currentNotifications} from './index';
const mentions = []; const mentions = [];
jest.mock('child_process', () => ({
execSync: jest.fn(),
}));
jest.mock('electron', () => { jest.mock('electron', () => {
class NotificationMock { class NotificationMock {
static isSupported = jest.fn(); static isSupported = jest.fn();
@ -231,4 +238,23 @@ describe('main/notifications', () => {
expect(shell.showItemInFolder).toHaveBeenCalledWith('/path/to/file'); expect(shell.showItemInFolder).toHaveBeenCalledWith('/path/to/file');
}); });
}); });
describe('getLinuxDoNotDisturb', () => {
it('should return false', () => {
cp.execSync.mockReturnValue('true');
expect(getLinuxDoNotDisturb()).toBe(false);
});
it('should return false if error is thrown', () => {
cp.execSync.mockImplementation(() => {
throw Error('error');
});
expect(getLinuxDoNotDisturb()).toBe(false);
});
it('should return true', () => {
cp.execSync.mockReturnValue('false');
expect(getLinuxDoNotDisturb()).toBe(true);
});
});
}); });

View file

@ -17,6 +17,7 @@ import WindowManager from '../windows/windowManager';
import {Mention} from './Mention'; import {Mention} from './Mention';
import {DownloadNotification} from './Download'; import {DownloadNotification} from './Download';
import {NewVersionNotification, UpgradeNotification} from './Upgrade'; import {NewVersionNotification, UpgradeNotification} from './Upgrade';
import getLinuxDoNotDisturb from './dnd-linux';
export const currentNotifications = new Map(); export const currentNotifications = new Map();
@ -152,5 +153,9 @@ function getDoNotDisturb() {
return getDarwinDoNotDisturb(); return getDarwinDoNotDisturb();
} }
if (process.platform === 'linux') {
return getLinuxDoNotDisturb();
}
return false; return false;
} }