Fixes silenced notification preventing notification prompt to be shown (#9336)

pull/28788/head^2
Germain 2022-09-30 16:11:04 +01:00 committed by GitHub
parent a704a2fbb7
commit c2e2f406af
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 10 additions and 75 deletions

View File

@ -336,7 +336,7 @@ export const Notifier = {
}
const isGuest = client.isGuest();
return !isGuest && this.supportsDesktopNotifications() && !isPushNotifyDisabled() &&
!localNotificationsAreSilenced(client) && !this.isEnabled() && !this._isPromptHidden();
!this.isEnabled() && !this._isPromptHidden();
},
_isPromptHidden: function() {

View File

@ -137,7 +137,6 @@ import { TimelineRenderingType } from "../../contexts/RoomContext";
import { UseCaseSelection } from '../views/elements/UseCaseSelection';
import { ValidatedServerConfig } from '../../utils/ValidatedServerConfig';
import { isLocalRoom } from '../../utils/localRoom/isLocalRoom';
import { createLocalNotificationSettingsIfNeeded } from '../../utils/notifications';
// legacy export
export { default as Views } from "../../Views";
@ -401,8 +400,6 @@ export default class MatrixChat extends React.PureComponent<IProps, IState> {
}
}
private get cli(): MatrixClient { return MatrixClientPeg.get(); }
public componentDidMount(): void {
window.addEventListener("resize", this.onWindowResized);
}
@ -1260,8 +1257,6 @@ export default class MatrixChat extends React.PureComponent<IProps, IState> {
this.themeWatcher.recheck();
StorageManager.tryPersistStorage();
this.cli.on(ClientEvent.Sync, this.onInitialSync);
if (
MatrixClientPeg.currentUserIsJustRegistered() &&
SettingsStore.getValue("FTUE.useCaseSelection") === null
@ -1288,14 +1283,6 @@ export default class MatrixChat extends React.PureComponent<IProps, IState> {
}
}
private onInitialSync = (): void => {
if (this.cli.isInitialSyncComplete()) {
this.cli.off(ClientEvent.Sync, this.onInitialSync);
}
createLocalNotificationSettingsIfNeeded(this.cli);
};
private async onShowPostLoginScreen(useCase?: UseCase) {
if (useCase) {
PosthogAnalytics.instance.setProperty("ftueUseCaseSelection", useCase);

View File

@ -122,7 +122,7 @@ export default class Notifications extends React.PureComponent<IProps, IState> {
this.state = {
phase: Phase.Loading,
deviceNotificationsEnabled: SettingsStore.getValue("deviceNotificationsEnabled") ?? false,
deviceNotificationsEnabled: SettingsStore.getValue("deviceNotificationsEnabled") ?? true,
desktopNotifications: SettingsStore.getValue("notificationsEnabled"),
desktopShowBody: SettingsStore.getValue("notificationBodyEnabled"),
audioNotifications: SettingsStore.getValue("audioNotificationsEnabled"),

View File

@ -792,7 +792,7 @@ export const SETTINGS: {[setting: string]: ISetting} = {
},
"deviceNotificationsEnabled": {
supportedLevels: [SettingLevel.DEVICE],
default: false,
default: true,
},
"notificationSound": {
supportedLevels: LEVELS_ROOM_OR_ACCOUNT,

View File

@ -18,9 +18,16 @@ import { _t } from "../languageHandler";
import Notifier from "../Notifier";
import GenericToast from "../components/views/toasts/GenericToast";
import ToastStore from "../stores/ToastStore";
import { MatrixClientPeg } from "../MatrixClientPeg";
import { getLocalNotificationAccountDataEventType } from "../utils/notifications";
const onAccept = () => {
Notifier.setEnabled(true);
const cli = MatrixClientPeg.get();
const eventType = getLocalNotificationAccountDataEventType(cli.deviceId);
cli.setAccountData(eventType, {
is_silenced: false,
});
};
const onReject = () => {

View File

@ -18,36 +18,10 @@ import { LOCAL_NOTIFICATION_SETTINGS_PREFIX } from "matrix-js-sdk/src/@types/eve
import { LocalNotificationSettings } from "matrix-js-sdk/src/@types/local_notifications";
import { MatrixClient } from "matrix-js-sdk/src/client";
import SettingsStore from "../settings/SettingsStore";
export const deviceNotificationSettingsKeys = [
"notificationsEnabled",
"notificationBodyEnabled",
"audioNotificationsEnabled",
];
export function getLocalNotificationAccountDataEventType(deviceId: string): string {
return `${LOCAL_NOTIFICATION_SETTINGS_PREFIX.name}.${deviceId}`;
}
export async function createLocalNotificationSettingsIfNeeded(cli: MatrixClient): Promise<void> {
const eventType = getLocalNotificationAccountDataEventType(cli.deviceId);
const event = cli.getAccountData(eventType);
// New sessions will create an account data event to signify they support
// remote toggling of push notifications on this device. Default `is_silenced=true`
// For backwards compat purposes, older sessions will need to check settings value
// to determine what the state of `is_silenced`
if (!event) {
// If any of the above is true, we fall in the "backwards compat" case,
// and `is_silenced` will be set to `false`
const isSilenced = !deviceNotificationSettingsKeys.some(key => SettingsStore.getValue(key));
await cli.setAccountData(eventType, {
is_silenced: isSilenced,
});
}
}
export function localNotificationsAreSilenced(cli: MatrixClient): boolean {
const eventType = getLocalNotificationAccountDataEventType(cli.deviceId);
const event = cli.getAccountData(eventType);

View File

@ -19,7 +19,6 @@ import { mocked } from "jest-mock";
import {
localNotificationsAreSilenced,
createLocalNotificationSettingsIfNeeded,
getLocalNotificationAccountDataEventType,
} from "../../src/utils/notifications";
import SettingsStore from "../../src/settings/SettingsStore";
@ -47,38 +46,6 @@ describe('notifications', () => {
mocked(SettingsStore).getValue.mockReturnValue(false);
});
describe('createLocalNotification', () => {
it('creates account data event', async () => {
await createLocalNotificationSettingsIfNeeded(mockClient);
const event = mockClient.getAccountData(accountDataEventKey);
expect(event?.getContent().is_silenced).toBe(true);
});
// Can't figure out why the mock does not override the value here
/*.each(deviceNotificationSettingsKeys) instead of skip */
it.skip("unsilenced for existing sessions", async (/*settingKey*/) => {
mocked(SettingsStore)
.getValue
.mockImplementation((key) => {
// return key === settingKey;
});
await createLocalNotificationSettingsIfNeeded(mockClient);
const event = mockClient.getAccountData(accountDataEventKey);
expect(event?.getContent().is_silenced).toBe(false);
});
it("does not override an existing account event data", async () => {
mockClient.setAccountData(accountDataEventKey, {
is_silenced: false,
});
await createLocalNotificationSettingsIfNeeded(mockClient);
const event = mockClient.getAccountData(accountDataEventKey);
expect(event?.getContent().is_silenced).toBe(false);
});
});
describe('localNotificationsAreSilenced', () => {
it('defaults to true when no setting exists', () => {
expect(localNotificationsAreSilenced(mockClient)).toBeTruthy();