Skip to content

Commit f2d6895

Browse files
authored
Merge pull request #1596 from matrix-org/gil/6797-Display_push_toggle_for_sessions_with_m.local_notification_settings.device-id
Add support for m.local_notification_settings.<device-id> in account_…
2 parents 1b643c7 + eba16c5 commit f2d6895

File tree

8 files changed

+266
-0
lines changed

8 files changed

+266
-0
lines changed

MatrixSDK.xcodeproj/project.pbxproj

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -687,6 +687,8 @@
687687
3A858DE327529183006322C1 /* MXRoomCapabilityType.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3A858DE027517C0E006322C1 /* MXRoomCapabilityType.swift */; };
688688
3A858DE8275511A4006322C1 /* MXRoomAliasAvailabilityCheckerResultTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3A858DE7275511A4006322C1 /* MXRoomAliasAvailabilityCheckerResultTests.swift */; };
689689
3A858DE9275511A4006322C1 /* MXRoomAliasAvailabilityCheckerResultTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3A858DE7275511A4006322C1 /* MXRoomAliasAvailabilityCheckerResultTests.swift */; };
690+
3A9E2B4328EB3960000DB2A7 /* MXMatrixVersionsUnitTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3A9E2B4228EB3960000DB2A7 /* MXMatrixVersionsUnitTests.swift */; };
691+
3A9E2B4428EB3960000DB2A7 /* MXMatrixVersionsUnitTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3A9E2B4228EB3960000DB2A7 /* MXMatrixVersionsUnitTests.swift */; };
690692
3AB5EBB4270B332B0058703A /* MXSpaceStore.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3AB5EBB3270B332B0058703A /* MXSpaceStore.swift */; };
691693
3AB5EBB5270B332B0058703A /* MXSpaceStore.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3AB5EBB3270B332B0058703A /* MXSpaceStore.swift */; };
692694
3AB5EBB7270ED1C00058703A /* MXSpaceFileStore.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3AB5EBB6270ED1C00058703A /* MXSpaceFileStore.swift */; };
@@ -2562,6 +2564,7 @@
25622564
3A858DDB275120D1006322C1 /* MXHomeserverCapabilitiesTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MXHomeserverCapabilitiesTests.swift; sourceTree = "<group>"; };
25632565
3A858DE027517C0E006322C1 /* MXRoomCapabilityType.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MXRoomCapabilityType.swift; sourceTree = "<group>"; };
25642566
3A858DE7275511A4006322C1 /* MXRoomAliasAvailabilityCheckerResultTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MXRoomAliasAvailabilityCheckerResultTests.swift; sourceTree = "<group>"; };
2567+
3A9E2B4228EB3960000DB2A7 /* MXMatrixVersionsUnitTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MXMatrixVersionsUnitTests.swift; sourceTree = "<group>"; };
25652568
3AB5EBB3270B332B0058703A /* MXSpaceStore.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MXSpaceStore.swift; sourceTree = "<group>"; };
25662569
3AB5EBB6270ED1C00058703A /* MXSpaceFileStore.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MXSpaceFileStore.swift; sourceTree = "<group>"; };
25672570
3AC135D72640335100EE1E74 /* MXDehydrationService.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = MXDehydrationService.h; sourceTree = "<group>"; };
@@ -4290,6 +4293,7 @@
42904293
B135067327EB201E00BD3276 /* MXLocationServiceTests.swift */,
42914294
B1EE98D328048ACF00AB63F0 /* MXGeoURIComponentsUnitTests.swift */,
42924295
B1F04B152811EFF700103EBE /* MXBeaconAggregationsTests.swift */,
4296+
3A9E2B4228EB3960000DB2A7 /* MXMatrixVersionsUnitTests.swift */,
42934297
);
42944298
path = MatrixSDKTests;
42954299
sourceTree = "<group>";
@@ -7113,6 +7117,7 @@
71137117
EDB4209527DF822B0036AF39 /* MXEventsByTypesEnumeratorOnArrayTests.swift in Sources */,
71147118
EC40385D28A16EDA0067D5B8 /* MXAes256KeyBackupTests.m in Sources */,
71157119
ED6DAC0728C77E1100ECDCB6 /* MXForwardedRoomKeyEventContentUnitTests.swift in Sources */,
7120+
3A9E2B4328EB3960000DB2A7 /* MXMatrixVersionsUnitTests.swift in Sources */,
71167121
3265CB3B1A151C3800E24B2F /* MXRoomStateTests.m in Sources */,
71177122
ED8F1D302885AB0300F897E7 /* MXTrustLevelSourceUnitTests.swift in Sources */,
71187123
B135066927EA100000BD3276 /* MXBeaconInfoUnitTests.swift in Sources */,
@@ -7735,6 +7740,7 @@
77357740
EDB4209627DF822B0036AF39 /* MXEventsByTypesEnumeratorOnArrayTests.swift in Sources */,
77367741
EC40385E28A16EDA0067D5B8 /* MXAes256KeyBackupTests.m in Sources */,
77377742
ED6DAC0828C77E1100ECDCB6 /* MXForwardedRoomKeyEventContentUnitTests.swift in Sources */,
7743+
3A9E2B4428EB3960000DB2A7 /* MXMatrixVersionsUnitTests.swift in Sources */,
77387744
32B477AA2638186000EA5800 /* MXHTTPAdditionalHeadersUnitTests.m in Sources */,
77397745
B135066A27EA100100BD3276 /* MXBeaconInfoUnitTests.swift in Sources */,
77407746
EC0B944727206D0B00B4D440 /* MXCoreDataRoomListDataManagerUnitTests.swift in Sources */,

MatrixSDK/Data/MXAccountData.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,4 +73,7 @@
7373
*/
7474
@property (nonatomic, readonly) NSDictionary<NSString *, id> *accountData;
7575

76+
+ (nonnull NSString *)localNotificationSettingsKeyForDeviceWithId:(nonnull NSString*)deviceId;
77+
- (nullable NSDictionary <NSString *, id>*)localNotificationSettingsForDeviceWithId:(nonnull NSString*)deviceId;
78+
7679
@end

MatrixSDK/Data/MXAccountData.m

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#import "MXAccountData.h"
1919

2020
#import "MXJSONModel.h"
21+
#import "MXRestClient.h"
2122

2223
@interface MXAccountData ()
2324
{
@@ -90,4 +91,23 @@ - (NSDictionary *)accountDataForEventType:(NSString*)eventType
9091
return @{@"events": events};
9192
}
9293

94+
+ (NSString *)localNotificationSettingsKeyForDeviceWithId:(NSString*)deviceId
95+
{
96+
return [kMXAccountDataLocalNotificationKeyPrefix stringByAppendingString:deviceId];
97+
}
98+
99+
- (NSDictionary <NSString *, id>*)localNotificationSettingsForDeviceWithId:(NSString*)deviceId
100+
{
101+
if (!deviceId)
102+
{
103+
return nil;
104+
}
105+
106+
107+
NSString *deviceNotificationKey = [MXAccountData localNotificationSettingsKeyForDeviceWithId:deviceId];
108+
NSDictionary <NSString *, id>*deviceNotificationSettings;
109+
MXJSONModelSetDictionary(deviceNotificationSettings, accountDataDict[deviceNotificationKey]);
110+
return deviceNotificationSettings;
111+
}
112+
93113
@end

MatrixSDK/MXRestClient.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,8 @@ FOUNDATION_EXPORT NSString *const kMXAccountDataTypeClientInformation;
8585
FOUNDATION_EXPORT NSString *const kMXAccountDataKeyIgnoredUser;
8686
FOUNDATION_EXPORT NSString *const kMXAccountDataKeyIdentityServer;
8787
FOUNDATION_EXPORT NSString *const kMXAccountDataTypeRecentRoomsKey;
88+
FOUNDATION_EXPORT NSString *const kMXAccountDataLocalNotificationKeyPrefix;
89+
FOUNDATION_EXPORT NSString *const kMXAccountDataIsSilencedKey;
8890

8991

9092
/**

MatrixSDK/MXRestClient.m

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@
6161
NSString *const kMXAccountDataKeyIdentityServer = @"base_url";
6262
NSString *const kMXAccountDataTypeAcceptedTermsKey = @"accepted";
6363
NSString *const kMXAccountDataTypeRecentRoomsKey = @"recent_rooms";
64+
NSString *const kMXAccountDataLocalNotificationKeyPrefix = @"org.matrix.msc3890.local_notification_settings.";
65+
NSString *const kMXAccountDataIsSilencedKey = @"is_silenced";
6466

6567
/**
6668
Types of third party media.

MatrixSDKTests/MXAccountDataTests.m

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,48 @@ - (void)testIgnoreUserUpdateHasNoCollateralDamageOnPushRules
205205
}
206206

207207

208+
// Test the capability to read or write `m.local_notification_settings.<device-id>`
209+
// events in account data.
210+
- (void)testReadWriteLocalNotificationSettings
211+
{
212+
[matrixSDKTestsData doMXSessionTestWithBob:self readyToTest:^(MXSession *mxSession, XCTestExpectation *expectation) {
213+
XCTAssertNotNil(mxSession.accountData, @"account data shouldn't be nil.");
214+
XCTAssertNil([mxSession.accountData localNotificationSettingsForDeviceWithId:mxSession.myDeviceId], @"account local notification settings should be nil.");
215+
216+
NSDictionary *localNotificationSettings = @{
217+
kMXAccountDataIsSilencedKey: @(YES)
218+
};
219+
220+
[mxSession setAccountData:localNotificationSettings forType:[MXAccountData localNotificationSettingsKeyForDeviceWithId:mxSession.myDeviceId] success:^{
221+
NSDictionary *localNotificationSettings = [mxSession.accountData localNotificationSettingsForDeviceWithId:mxSession.myDeviceId];
222+
XCTAssertNotNil(localNotificationSettings, @"account local notification settings shouldn't be nil.");
223+
NSNumber *isSilenced = localNotificationSettings[kMXAccountDataIsSilencedKey];
224+
XCTAssertNotNil(isSilenced, @"account local notification is_silenced settings shouldn't be nil.");
225+
XCTAssertTrue(isSilenced.boolValue, @"is_silenced settings should be set to true.");
226+
227+
localNotificationSettings = @{
228+
kMXAccountDataIsSilencedKey: @(NO)
229+
};
230+
231+
[mxSession setAccountData:localNotificationSettings forType:[MXAccountData localNotificationSettingsKeyForDeviceWithId:mxSession.myDeviceId] success:^{
232+
NSDictionary *localNotificationSettings = [mxSession.accountData localNotificationSettingsForDeviceWithId:mxSession.myDeviceId];
233+
XCTAssertNotNil(localNotificationSettings, @"account local notification settings shouldn't be nil.");
234+
NSNumber *isSilenced = localNotificationSettings[kMXAccountDataIsSilencedKey];
235+
XCTAssertNotNil(isSilenced, @"account local notification is_silenced settings shouldn't be nil.");
236+
XCTAssertFalse(isSilenced.boolValue, @"is_silenced settings should be set to false.");
237+
[expectation fulfill];
238+
} failure:^(NSError *error) {
239+
XCTFail(@"MXSession setAccountData failed due to error %@", error);
240+
[expectation fulfill];
241+
}];
242+
} failure:^(NSError *error) {
243+
XCTFail(@"MXSession setAccountData failed due to error %@", error);
244+
[expectation fulfill];
245+
}];
246+
}];
247+
}
248+
249+
208250
@end
209251

210252
#pragma clang diagnostic pop
Lines changed: 190 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,190 @@
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 XCTest
18+
19+
class MXMatrixVersionsUnitTests: XCTestCase {
20+
21+
private static let emptyVersions: [String: Any] =
22+
[
23+
"unstable_features": [:],
24+
"versions": []
25+
]
26+
27+
private static let fullSupportVersions: [String: Any] =
28+
[
29+
"unstable_features": [
30+
"org.matrix.msc2716": true,
31+
"io.element.e2ee_forced.private": true,
32+
"io.element.e2ee_forced.public": true,
33+
"org.matrix.msc3030": true,
34+
"org.matrix.e2e_cross_signing": true,
35+
"org.matrix.msc2432": true,
36+
"io.element.e2ee_forced.trusted_private": true,
37+
"org.matrix.msc3440.stable": true,
38+
"org.matrix.msc3827.stable": true,
39+
"fi.mau.msc2815": true,
40+
"uk.half-shot.msc2666.mutual_rooms": true,
41+
"org.matrix.label_based_filtering": true,
42+
"org.matrix.msc3026.busy_presence": true,
43+
"org.matrix.msc2285.stable": true,
44+
"org.matrix.msc3881.stable": true
45+
],
46+
"versions": [
47+
"r0.0.1",
48+
"r0.1.0",
49+
"r0.2.0",
50+
"r0.3.0",
51+
"r0.4.0",
52+
"r0.5.0",
53+
"r0.6.0",
54+
"r0.6.1",
55+
"v1.1",
56+
"v1.2"
57+
]
58+
]
59+
60+
private static let noSupportVersions: [String: Any] =
61+
[
62+
"unstable_features": [
63+
"org.matrix.msc2716": false,
64+
"io.element.e2ee_forced.private": false,
65+
"io.element.e2ee_forced.public": false,
66+
"org.matrix.msc3030": false,
67+
"org.matrix.e2e_cross_signing": false,
68+
"org.matrix.msc2432": false,
69+
"io.element.e2ee_forced.trusted_private": false,
70+
"org.matrix.msc3440.stable": false,
71+
"org.matrix.msc3827.stable": false,
72+
"fi.mau.msc2815": false,
73+
"uk.half-shot.msc2666.mutual_rooms": false,
74+
"org.matrix.label_based_filtering": false,
75+
"org.matrix.msc3026.busy_presence": false,
76+
"org.matrix.msc2285.stable": false,
77+
"org.matrix.msc3881.stable": false
78+
],
79+
"versions": [
80+
"r0.0.1",
81+
"r0.1.0",
82+
"r0.2.0",
83+
"r0.3.0",
84+
"r0.4.0",
85+
"r0.6.1",
86+
"v1.1",
87+
"v1.2"
88+
]
89+
]
90+
91+
// MARK: - Properties
92+
93+
private var testData: MatrixSDKTestsData!
94+
95+
// MARK: - Setup
96+
97+
override func setUpWithError() throws {
98+
// Put setup code here. This method is called before the invocation of each test method in the class.
99+
try super.setUpWithError()
100+
testData = MatrixSDKTestsData()
101+
}
102+
103+
override func tearDownWithError() throws {
104+
// Put teardown code here. This method is called after the invocation of each test method in the class.
105+
testData = nil
106+
try super.tearDownWithError()
107+
}
108+
109+
// MARK: - Tests
110+
111+
/// - Create Bob
112+
/// - Setup Bob session
113+
///
114+
/// -> Supported Matrix versions should be initialised and not empty
115+
func testSupportedMatrixVersionsInitialised() throws {
116+
testData.doMXSessionTest(withBob: self) { session, expectation in
117+
guard let session = session else {
118+
XCTFail("session shouldn't be nil")
119+
expectation?.fulfill()
120+
return
121+
}
122+
123+
session.supportedMatrixVersions { response in
124+
switch response {
125+
case .success(let versions):
126+
XCTAssertFalse(versions.versions.isEmpty, "versions shouldn't be empty")
127+
XCTAssertNotNil(versions.unstableFeatures, "versions should contain unstable features")
128+
case .failure(let error):
129+
XCTFail("supportedMatrixVersions failed due to error \(error)")
130+
}
131+
132+
expectation?.fulfill()
133+
}
134+
}
135+
}
136+
137+
func testEmptyVersions() throws {
138+
guard let versions = MXMatrixVersions(fromJSON: Self.emptyVersions) else {
139+
XCTFail("Unable to instantiate MXMatrixVersions")
140+
return
141+
}
142+
143+
XCTAssertTrue(versions.versions.isEmpty, "versions should be empty")
144+
guard let unstableFeatures = versions.unstableFeatures else {
145+
XCTFail("MXMatrixVersions instance should have unstableFeatures")
146+
return
147+
}
148+
XCTAssertTrue(unstableFeatures.isEmpty, "unstableFeatures should be empty")
149+
XCTAssertFalse(versions.supportLazyLoadMembers, "versions shouldn't support Lazy Load Members")
150+
XCTAssertFalse(versions.supportsThreads, "versions shouldn't support threads")
151+
XCTAssertFalse(versions.doesServerSupportSeparateAddAndBind, "versions shouldn't support separate Add And Bind")
152+
XCTAssertFalse(versions.supportsRemotelyTogglingPushNotifications, "versions shouldn't support remotely toggling push notifications")
153+
}
154+
155+
func testFullSupportVersions() throws {
156+
guard let versions = MXMatrixVersions(fromJSON: Self.fullSupportVersions) else {
157+
XCTFail("Unable to instantiate MXMatrixVersions")
158+
return
159+
}
160+
161+
XCTAssertFalse(versions.versions.isEmpty, "versions shouldn't be empty")
162+
guard let unstableFeatures = versions.unstableFeatures else {
163+
XCTFail("MXMatrixVersions instance should have unstableFeatures")
164+
return
165+
}
166+
XCTAssertFalse(unstableFeatures.isEmpty, "unstableFeatures shouldn't be empty")
167+
XCTAssertTrue(versions.supportLazyLoadMembers, "versions should support Lazy Load Members")
168+
XCTAssertTrue(versions.supportsThreads, "versions should support threads")
169+
XCTAssertTrue(versions.doesServerSupportSeparateAddAndBind, "versions should support separate Add And Bind")
170+
XCTAssertTrue(versions.supportsRemotelyTogglingPushNotifications, "versions should support remotely toggling push notifications")
171+
}
172+
173+
func testNoSupportVersions() throws {
174+
guard let versions = MXMatrixVersions(fromJSON: Self.noSupportVersions) else {
175+
XCTFail("Unable to instantiate MXMatrixVersions")
176+
return
177+
}
178+
179+
XCTAssertFalse(versions.versions.isEmpty, "versions shouldn't be empty")
180+
guard let unstableFeatures = versions.unstableFeatures else {
181+
XCTFail("MXMatrixVersions instance should have unstableFeatures")
182+
return
183+
}
184+
XCTAssertFalse(unstableFeatures.isEmpty, "unstableFeatures shouldn't be empty")
185+
XCTAssertFalse(versions.supportLazyLoadMembers, "versions shouldn't support Lazy Load Members")
186+
XCTAssertFalse(versions.supportsThreads, "versions shouldn't support threads")
187+
XCTAssertFalse(versions.doesServerSupportSeparateAddAndBind, "versions shouldn't support separate Add And Bind")
188+
XCTAssertFalse(versions.supportsRemotelyTogglingPushNotifications, "versions shouldn't support remotely toggling push notifications")
189+
}
190+
}

changelog.d/6797.change

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add support for m.local_notification_settings.<device-id> in account_data

0 commit comments

Comments
 (0)