[MM-58455] Add error handling when FocusStatus is not authorized on macOS (#3053) (#3054)

* [MM-58455] Add error handling when FocusStatus is not authorized on macOS

* Do the permission check very early so that it's less likely for users to miss it

* Move permissions check to initialize

(cherry picked from commit d11752e195)

Co-authored-by: Devin Binnie <52460000+devinbinnie@users.noreply.github.com>
This commit is contained in:
Mattermost Build 2024-06-06 21:31:22 +03:00 committed by GitHub
parent 8834720cb4
commit bd245ddfbe
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 47 additions and 7 deletions

View file

@ -143,6 +143,7 @@ jest.mock('main/CriticalErrorHandler', () => ({
}));
jest.mock('main/notifications', () => ({
displayDownloadCompleted: jest.fn(),
getDoNotDisturb: jest.fn(),
}));
jest.mock('main/ParseArgs', () => jest.fn());
jest.mock('common/servers/serverManager', () => ({

View file

@ -47,6 +47,7 @@ import {configPath, updatePaths} from 'main/constants';
import CriticalErrorHandler from 'main/CriticalErrorHandler';
import downloadsManager from 'main/downloadsManager';
import i18nManager from 'main/i18nManager';
import {getDoNotDisturb} from 'main/notifications';
import parseArgs from 'main/ParseArgs';
import PermissionsManager from 'main/permissionsManager';
import Tray from 'main/tray/tray';
@ -374,6 +375,9 @@ async function initializeAfterAppReady() {
}
}
// Call this to initiate a permissions check for DND state
getDoNotDisturb();
// listen for status updates and pass on to renderer
UserActivityMonitor.on('status', (status) => {
log.debug('UserActivityMonitor.on(status)', status);

View file

@ -34,7 +34,7 @@ const mentions: Array<{body: string; value: any}> = [];
jest.mock('child_process', () => ({
execSync: jest.fn(),
}));
jest.mock('electron-is-dev', () => false);
jest.mock('electron', () => {
class NotificationMock {
callbackMap: Map<string, () => void>;
@ -132,6 +132,7 @@ describe('main/notifications', () => {
afterEach(() => {
jest.resetAllMocks();
mentions.length = 0;
Config.notifications = {
flashWindow: 0,
bounceIcon: false,
@ -151,7 +152,7 @@ describe('main/notifications', () => {
{id: 1} as WebContents,
'',
);
expect(MainWindow.show).not.toBeCalled();
expect(mentions.length).toBe(0);
});
it('should do nothing when alarms only is enabled on windows', async () => {
@ -171,7 +172,7 @@ describe('main/notifications', () => {
{id: 1} as WebContents,
'',
);
expect(MainWindow.show).not.toBeCalled();
expect(mentions.length).toBe(0);
Object.defineProperty(process, 'platform', {
value: originalPlatform,
@ -195,7 +196,35 @@ describe('main/notifications', () => {
{id: 1} as WebContents,
'',
);
expect(MainWindow.show).not.toBeCalled();
expect(mentions.length).toBe(0);
Object.defineProperty(process, 'platform', {
value: originalPlatform,
});
});
it('should still show notification when dnd permission on mac is not authorized', async () => {
const originalPlatform = process.platform;
Object.defineProperty(process, 'platform', {
value: 'darwin',
});
getDarwinDoNotDisturb.mockImplementation(() => {
throw new Error('Unauthorized');
});
await NotificationManager.displayMention(
'test',
'test body',
'channel_id',
'team_id',
'http://server-1.com/team_id/channel_id',
false,
{id: 1} as WebContents,
'',
);
expect(mentions.length).toBe(1);
const mention = mentions[0];
expect(mention.value.show).toHaveBeenCalled();
Object.defineProperty(process, 'platform', {
value: originalPlatform,
@ -214,7 +243,7 @@ describe('main/notifications', () => {
{id: 1} as WebContents,
'',
);
expect(MainWindow.show).not.toBeCalled();
expect(mentions.length).toBe(0);
});
it('should play notification sound when custom sound is provided', async () => {

View file

@ -186,14 +186,20 @@ class NotificationManager {
}
}
async function getDoNotDisturb() {
export async function getDoNotDisturb() {
if (process.platform === 'win32') {
return getWindowsDoNotDisturb();
}
// We have to turn this off for dev mode because the Electron binary doesn't have the focus center API entitlement
if (process.platform === 'darwin' && !isDev) {
return getDarwinDoNotDisturb();
try {
const dnd = await getDarwinDoNotDisturb();
return dnd;
} catch (e) {
log.warn('macOS DND check threw an error', e);
return false;
}
}
if (process.platform === 'linux') {