diff --git a/.vscode/settings.json b/.vscode/settings.json index cef4195f..ef5d27fd 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -20,6 +20,7 @@ "filechooser", "flac", "FOCALBOARD", + "gsettings", "ICONNAME", "loadscreen", "mmjstool", diff --git a/src/main/notifications/dnd-linux.ts b/src/main/notifications/dnd-linux.ts new file mode 100644 index 00000000..8ab6a962 --- /dev/null +++ b/src/main/notifications/dnd-linux.ts @@ -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; diff --git a/src/main/notifications/index.test.js b/src/main/notifications/index.test.js index a4639c3c..759e68ff 100644 --- a/src/main/notifications/index.test.js +++ b/src/main/notifications/index.test.js @@ -2,6 +2,7 @@ // See LICENSE.txt for license information. 'use strict'; +import cp from 'child_process'; import {Notification, shell} from 'electron'; @@ -15,10 +16,16 @@ import {localizeMessage} from 'main/i18nManager'; import WindowManager from '../windows/windowManager'; +import getLinuxDoNotDisturb from './dnd-linux'; + import {displayMention, displayDownloadCompleted, currentNotifications} from './index'; const mentions = []; +jest.mock('child_process', () => ({ + execSync: jest.fn(), +})); + jest.mock('electron', () => { class NotificationMock { static isSupported = jest.fn(); @@ -231,4 +238,23 @@ describe('main/notifications', () => { 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); + }); + }); }); diff --git a/src/main/notifications/index.ts b/src/main/notifications/index.ts index 23c2dfb1..f869befe 100644 --- a/src/main/notifications/index.ts +++ b/src/main/notifications/index.ts @@ -17,6 +17,7 @@ import WindowManager from '../windows/windowManager'; import {Mention} from './Mention'; import {DownloadNotification} from './Download'; import {NewVersionNotification, UpgradeNotification} from './Upgrade'; +import getLinuxDoNotDisturb from './dnd-linux'; export const currentNotifications = new Map(); @@ -152,5 +153,9 @@ function getDoNotDisturb() { return getDarwinDoNotDisturb(); } + if (process.platform === 'linux') { + return getLinuxDoNotDisturb(); + } + return false; }