Skip to content
This repository was archived by the owner on Sep 11, 2024. It is now read-only.

Commit c2e2f40

Browse files
author
Germain
authored
Fixes silenced notification preventing notification prompt to be shown (#9336)
1 parent a704a2f commit c2e2f40

File tree

7 files changed

+10
-75
lines changed

7 files changed

+10
-75
lines changed

src/Notifier.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,7 @@ export const Notifier = {
336336
}
337337
const isGuest = client.isGuest();
338338
return !isGuest && this.supportsDesktopNotifications() && !isPushNotifyDisabled() &&
339-
!localNotificationsAreSilenced(client) && !this.isEnabled() && !this._isPromptHidden();
339+
!this.isEnabled() && !this._isPromptHidden();
340340
},
341341

342342
_isPromptHidden: function() {

src/components/structures/MatrixChat.tsx

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,6 @@ import { TimelineRenderingType } from "../../contexts/RoomContext";
137137
import { UseCaseSelection } from '../views/elements/UseCaseSelection';
138138
import { ValidatedServerConfig } from '../../utils/ValidatedServerConfig';
139139
import { isLocalRoom } from '../../utils/localRoom/isLocalRoom';
140-
import { createLocalNotificationSettingsIfNeeded } from '../../utils/notifications';
141140

142141
// legacy export
143142
export { default as Views } from "../../Views";
@@ -401,8 +400,6 @@ export default class MatrixChat extends React.PureComponent<IProps, IState> {
401400
}
402401
}
403402

404-
private get cli(): MatrixClient { return MatrixClientPeg.get(); }
405-
406403
public componentDidMount(): void {
407404
window.addEventListener("resize", this.onWindowResized);
408405
}
@@ -1260,8 +1257,6 @@ export default class MatrixChat extends React.PureComponent<IProps, IState> {
12601257
this.themeWatcher.recheck();
12611258
StorageManager.tryPersistStorage();
12621259

1263-
this.cli.on(ClientEvent.Sync, this.onInitialSync);
1264-
12651260
if (
12661261
MatrixClientPeg.currentUserIsJustRegistered() &&
12671262
SettingsStore.getValue("FTUE.useCaseSelection") === null
@@ -1288,14 +1283,6 @@ export default class MatrixChat extends React.PureComponent<IProps, IState> {
12881283
}
12891284
}
12901285

1291-
private onInitialSync = (): void => {
1292-
if (this.cli.isInitialSyncComplete()) {
1293-
this.cli.off(ClientEvent.Sync, this.onInitialSync);
1294-
}
1295-
1296-
createLocalNotificationSettingsIfNeeded(this.cli);
1297-
};
1298-
12991286
private async onShowPostLoginScreen(useCase?: UseCase) {
13001287
if (useCase) {
13011288
PosthogAnalytics.instance.setProperty("ftueUseCaseSelection", useCase);

src/components/views/settings/Notifications.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ export default class Notifications extends React.PureComponent<IProps, IState> {
122122

123123
this.state = {
124124
phase: Phase.Loading,
125-
deviceNotificationsEnabled: SettingsStore.getValue("deviceNotificationsEnabled") ?? false,
125+
deviceNotificationsEnabled: SettingsStore.getValue("deviceNotificationsEnabled") ?? true,
126126
desktopNotifications: SettingsStore.getValue("notificationsEnabled"),
127127
desktopShowBody: SettingsStore.getValue("notificationBodyEnabled"),
128128
audioNotifications: SettingsStore.getValue("audioNotificationsEnabled"),

src/settings/Settings.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -792,7 +792,7 @@ export const SETTINGS: {[setting: string]: ISetting} = {
792792
},
793793
"deviceNotificationsEnabled": {
794794
supportedLevels: [SettingLevel.DEVICE],
795-
default: false,
795+
default: true,
796796
},
797797
"notificationSound": {
798798
supportedLevels: LEVELS_ROOM_OR_ACCOUNT,

src/toasts/DesktopNotificationsToast.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,16 @@ import { _t } from "../languageHandler";
1818
import Notifier from "../Notifier";
1919
import GenericToast from "../components/views/toasts/GenericToast";
2020
import ToastStore from "../stores/ToastStore";
21+
import { MatrixClientPeg } from "../MatrixClientPeg";
22+
import { getLocalNotificationAccountDataEventType } from "../utils/notifications";
2123

2224
const onAccept = () => {
2325
Notifier.setEnabled(true);
26+
const cli = MatrixClientPeg.get();
27+
const eventType = getLocalNotificationAccountDataEventType(cli.deviceId);
28+
cli.setAccountData(eventType, {
29+
is_silenced: false,
30+
});
2431
};
2532

2633
const onReject = () => {

src/utils/notifications.ts

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -18,36 +18,10 @@ import { LOCAL_NOTIFICATION_SETTINGS_PREFIX } from "matrix-js-sdk/src/@types/eve
1818
import { LocalNotificationSettings } from "matrix-js-sdk/src/@types/local_notifications";
1919
import { MatrixClient } from "matrix-js-sdk/src/client";
2020

21-
import SettingsStore from "../settings/SettingsStore";
22-
23-
export const deviceNotificationSettingsKeys = [
24-
"notificationsEnabled",
25-
"notificationBodyEnabled",
26-
"audioNotificationsEnabled",
27-
];
28-
2921
export function getLocalNotificationAccountDataEventType(deviceId: string): string {
3022
return `${LOCAL_NOTIFICATION_SETTINGS_PREFIX.name}.${deviceId}`;
3123
}
3224

33-
export async function createLocalNotificationSettingsIfNeeded(cli: MatrixClient): Promise<void> {
34-
const eventType = getLocalNotificationAccountDataEventType(cli.deviceId);
35-
const event = cli.getAccountData(eventType);
36-
// New sessions will create an account data event to signify they support
37-
// remote toggling of push notifications on this device. Default `is_silenced=true`
38-
// For backwards compat purposes, older sessions will need to check settings value
39-
// to determine what the state of `is_silenced`
40-
if (!event) {
41-
// If any of the above is true, we fall in the "backwards compat" case,
42-
// and `is_silenced` will be set to `false`
43-
const isSilenced = !deviceNotificationSettingsKeys.some(key => SettingsStore.getValue(key));
44-
45-
await cli.setAccountData(eventType, {
46-
is_silenced: isSilenced,
47-
});
48-
}
49-
}
50-
5125
export function localNotificationsAreSilenced(cli: MatrixClient): boolean {
5226
const eventType = getLocalNotificationAccountDataEventType(cli.deviceId);
5327
const event = cli.getAccountData(eventType);

test/utils/notifications-test.ts

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ import { mocked } from "jest-mock";
1919

2020
import {
2121
localNotificationsAreSilenced,
22-
createLocalNotificationSettingsIfNeeded,
2322
getLocalNotificationAccountDataEventType,
2423
} from "../../src/utils/notifications";
2524
import SettingsStore from "../../src/settings/SettingsStore";
@@ -47,38 +46,6 @@ describe('notifications', () => {
4746
mocked(SettingsStore).getValue.mockReturnValue(false);
4847
});
4948

50-
describe('createLocalNotification', () => {
51-
it('creates account data event', async () => {
52-
await createLocalNotificationSettingsIfNeeded(mockClient);
53-
const event = mockClient.getAccountData(accountDataEventKey);
54-
expect(event?.getContent().is_silenced).toBe(true);
55-
});
56-
57-
// Can't figure out why the mock does not override the value here
58-
/*.each(deviceNotificationSettingsKeys) instead of skip */
59-
it.skip("unsilenced for existing sessions", async (/*settingKey*/) => {
60-
mocked(SettingsStore)
61-
.getValue
62-
.mockImplementation((key) => {
63-
// return key === settingKey;
64-
});
65-
66-
await createLocalNotificationSettingsIfNeeded(mockClient);
67-
const event = mockClient.getAccountData(accountDataEventKey);
68-
expect(event?.getContent().is_silenced).toBe(false);
69-
});
70-
71-
it("does not override an existing account event data", async () => {
72-
mockClient.setAccountData(accountDataEventKey, {
73-
is_silenced: false,
74-
});
75-
76-
await createLocalNotificationSettingsIfNeeded(mockClient);
77-
const event = mockClient.getAccountData(accountDataEventKey);
78-
expect(event?.getContent().is_silenced).toBe(false);
79-
});
80-
});
81-
8249
describe('localNotificationsAreSilenced', () => {
8350
it('defaults to true when no setting exists', () => {
8451
expect(localNotificationsAreSilenced(mockClient)).toBeTruthy();

0 commit comments

Comments
 (0)