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 2 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
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
13 changes: 12 additions & 1 deletion src/main/notifications/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ class NotificationManager {
private upgradeNotification?: NewVersionNotification;
private restartToUpgradeNotification?: UpgradeNotification;

constructor() {
// Run this very early to perform an early permission check
getDoNotDisturb();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will this trigger a permission modal before getting a notification?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it shows the macOS one that people were missing.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this happen as an explicit call from somewhere in the initialization code? It feels a bit weird to have this be a side effect of importing this file

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I guess that would be better, I wanted to keep everything centralized but the import case does make that a bit awkward.

}

public async displayMention(title: string, body: string, channelId: string, teamId: string, url: string, silent: boolean, webcontents: Electron.WebContents, soundName: string) {
log.debug('displayMention', {title, channelId, teamId, url, silent, soundName});

Expand Down Expand Up @@ -204,7 +209,13 @@ async function getDoNotDisturb() {

// 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