Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 3 commits into from
Jun 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/main/app/initialize.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => ({
Expand Down
4 changes: 4 additions & 0 deletions src/main/app/initialize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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);
Expand Down
39 changes: 34 additions & 5 deletions src/main/notifications/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,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>;
Expand Down Expand Up @@ -137,6 +137,7 @@ describe('main/notifications', () => {

afterEach(() => {
jest.resetAllMocks();
mentions.length = 0;
Config.notifications = {
flashWindow: 0,
bounceIcon: false,
Expand All @@ -156,7 +157,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 () => {
Expand All @@ -176,7 +177,7 @@ describe('main/notifications', () => {
{id: 1} as WebContents,
'',
);
expect(MainWindow.show).not.toBeCalled();
expect(mentions.length).toBe(0);

Object.defineProperty(process, 'platform', {
value: originalPlatform,
Expand All @@ -200,7 +201,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,
Expand All @@ -219,7 +248,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 () => {
Expand Down
10 changes: 8 additions & 2 deletions src/main/notifications/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,14 +197,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') {
Expand Down
Loading