Skip to content
This repository was archived by the owner on Sep 11, 2024. It is now read-only.
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
2 changes: 1 addition & 1 deletion src/Notifier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
13 changes: 0 additions & 13 deletions src/components/structures/MatrixChat.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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
Expand All @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/components/views/settings/Notifications.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down
2 changes: 1 addition & 1 deletion src/settings/Settings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -792,7 +792,7 @@ export const SETTINGS: {[setting: string]: ISetting} = {
},
"deviceNotificationsEnabled": {
supportedLevels: [SettingLevel.DEVICE],
default: false,
default: true,
},
"notificationSound": {
supportedLevels: LEVELS_ROOM_OR_ACCOUNT,
Expand Down
7 changes: 7 additions & 0 deletions src/toasts/DesktopNotificationsToast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = () => {
Expand Down
26 changes: 0 additions & 26 deletions src/utils/notifications.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
33 changes: 0 additions & 33 deletions test/utils/notifications-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import { mocked } from "jest-mock";

import {
localNotificationsAreSilenced,
createLocalNotificationSettingsIfNeeded,
getLocalNotificationAccountDataEventType,
} from "../../src/utils/notifications";
import SettingsStore from "../../src/settings/SettingsStore";
Expand Down Expand Up @@ -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();
Expand Down