Skip to content

Commit 5952fc2

Browse files
author
Germain Souquet
committed
Add local notification settings capability
Support for matrix-org/matrix-spec-proposals#3890
1 parent efbf547 commit 5952fc2

File tree

4 files changed

+91
-3
lines changed

4 files changed

+91
-3
lines changed

spec/unit/local_notifications.ts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
Copyright 2022 The Matrix.org Foundation C.I.C.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
import { LocalNotificationSettings } from "../../src/@types/local_notifications";
18+
import { LOCAL_NOTIFICATION_SETTINGS_PREFIX, MatrixClient } from "../../src/matrix";
19+
import { TestClient } from '../TestClient';
20+
21+
let client: MatrixClient;
22+
23+
describe("Local notification settings", () => {
24+
beforeEach(() => {
25+
client = (new TestClient(
26+
"@alice:matrix.org", "123", undefined, undefined, undefined,
27+
)).client;
28+
client.setAccountData = jest.fn();
29+
});
30+
31+
describe("Lets you set local notification settings", () => {
32+
it("stores settings in account data", () => {
33+
const deviceId = "device";
34+
const settings: LocalNotificationSettings = { is_silenced: true };
35+
client.setLocalNotificationSettings(deviceId, settings);
36+
37+
expect(client.setAccountData).toHaveBeenCalledWith(
38+
`${LOCAL_NOTIFICATION_SETTINGS_PREFIX}.${deviceId}`,
39+
settings,
40+
);
41+
});
42+
});
43+
});

src/@types/event.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -200,14 +200,23 @@ export const PUSHER_ENABLED = new UnstableValue(
200200
"enabled",
201201
"org.matrix.msc3881.enabled");
202202

203+
/**
204+
* https://github.com/matrix-org/matrix-doc/pull/3881
205+
*
206+
* @experimental
207+
*/
208+
export const PUSHER_DEVICE_ID = new UnstableValue(
209+
"device_id",
210+
"org.matrix.msc3881.device_id");
211+
203212
/**
204213
* https://github.com/matrix-org/matrix-doc/pull/3881
205214
*
206215
* @experimental
207216
*/
208-
export const PUSHER_DEVICE_ID = new UnstableValue(
209-
"device_id",
210-
"org.matrix.msc3881.device_id");
217+
export const LOCAL_NOTIFICATION_SETTINGS_PREFIX = new UnstableValue(
218+
"m.local_notification_settings",
219+
"m.msc3890.local_notification_settings");
211220

212221
export interface IEncryptedFile {
213222
url: string;

src/@types/local_notifications.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/*
2+
Copyright 2022 The Matrix.org Foundation C.I.C.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
export interface LocalNotificationSettings {
18+
is_silenced: boolean;
19+
}

src/client.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@ import {
158158
} from "./@types/requests";
159159
import {
160160
EventType,
161+
LOCAL_NOTIFICATION_SETTINGS_PREFIX,
161162
MsgType,
162163
PUSHER_ENABLED,
163164
RelationType,
@@ -202,6 +203,7 @@ import { ToDeviceBatch } from "./models/ToDeviceMessage";
202203
import { MAIN_ROOM_TIMELINE } from "./models/read-receipt";
203204
import { IgnoredInvites } from "./models/invites-ignorer";
204205
import { UIARequest, UIAResponse } from "./@types/uia";
206+
import { LocalNotificationSettings } from "./@types/local_notifications";
205207

206208
export type Store = IStore;
207209

@@ -8201,6 +8203,21 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
82018203
return this.http.authedRequest(callback, Method.Post, path, undefined, pusher);
82028204
}
82038205

8206+
/**
8207+
* Persists local notification settings
8208+
* @param {string} deviceId
8209+
* @param {LocalNotificationSettings} notificationSettings
8210+
* @return {Promise} Resolves: an empty object
8211+
* @return {module:http-api.MatrixError} Rejects: with an error response.
8212+
*/
8213+
public setLocalNotificationSettings(
8214+
deviceId: string,
8215+
notificationSettings: LocalNotificationSettings,
8216+
): Promise<{}> {
8217+
const key = `${LOCAL_NOTIFICATION_SETTINGS_PREFIX.name}.${deviceId}`;
8218+
return this.setAccountData(key, notificationSettings);
8219+
}
8220+
82048221
/**
82058222
* Get the push rules for the account from the server.
82068223
* @param {module:client.callback} callback Optional.

0 commit comments

Comments
 (0)