Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add enableNewClientInformationFeature sdk option (PSG-799) #1588

Merged
merged 7 commits into from
Sep 30, 2022
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
20 changes: 20 additions & 0 deletions MatrixSDK.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -1791,6 +1791,10 @@
ECDA764D27BA963D000C48CF /* MXBooleanCapability.h in Headers */ = {isa = PBXBuildFile; fileRef = ECDA764A27BA963D000C48CF /* MXBooleanCapability.h */; settings = {ATTRIBUTES = (Public, ); }; };
ECDA764E27BA963D000C48CF /* MXBooleanCapability.m in Sources */ = {isa = PBXBuildFile; fileRef = ECDA764B27BA963D000C48CF /* MXBooleanCapability.m */; };
ECDA764F27BA963D000C48CF /* MXBooleanCapability.m in Sources */ = {isa = PBXBuildFile; fileRef = ECDA764B27BA963D000C48CF /* MXBooleanCapability.m */; };
ECDBE69028E5D961000C83AF /* MXClientInformationService.swift in Sources */ = {isa = PBXBuildFile; fileRef = ECDBE68F28E5D961000C83AF /* MXClientInformationService.swift */; };
ECDBE69128E5DC9A000C83AF /* MXClientInformationService.swift in Sources */ = {isa = PBXBuildFile; fileRef = ECDBE68F28E5D961000C83AF /* MXClientInformationService.swift */; };
ECDBE69328E5E16F000C83AF /* MXClientInformationServiceUnitTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = ECDBE69228E5E16F000C83AF /* MXClientInformationServiceUnitTests.swift */; };
ECDBE69428E5E16F000C83AF /* MXClientInformationServiceUnitTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = ECDBE69228E5E16F000C83AF /* MXClientInformationServiceUnitTests.swift */; };
ECE3DF842707370000FB4C96 /* MXRoomListDataPaginationOptions.swift in Sources */ = {isa = PBXBuildFile; fileRef = ECE3DF832707370000FB4C96 /* MXRoomListDataPaginationOptions.swift */; };
ECE3DF852707370000FB4C96 /* MXRoomListDataPaginationOptions.swift in Sources */ = {isa = PBXBuildFile; fileRef = ECE3DF832707370000FB4C96 /* MXRoomListDataPaginationOptions.swift */; };
ECE3DF9D270C660900FB4C96 /* MXMulticastDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = ECE3DF9C270C660900FB4C96 /* MXMulticastDelegate.swift */; };
Expand Down Expand Up @@ -2948,6 +2952,8 @@
ECDA764527BA939E000C48CF /* MXRoomVersionsCapability.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = MXRoomVersionsCapability.m; sourceTree = "<group>"; };
ECDA764A27BA963D000C48CF /* MXBooleanCapability.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = MXBooleanCapability.h; sourceTree = "<group>"; };
ECDA764B27BA963D000C48CF /* MXBooleanCapability.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = MXBooleanCapability.m; sourceTree = "<group>"; };
ECDBE68F28E5D961000C83AF /* MXClientInformationService.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MXClientInformationService.swift; sourceTree = "<group>"; };
ECDBE69228E5E16F000C83AF /* MXClientInformationServiceUnitTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MXClientInformationServiceUnitTests.swift; sourceTree = "<group>"; };
ECE3DF7E2707299C00FB4C96 /* MXSuggestedRoomListDataFetcher.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MXSuggestedRoomListDataFetcher.swift; sourceTree = "<group>"; };
ECE3DF8027072BE500FB4C96 /* MXSuggestedRoomListDataCache.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MXSuggestedRoomListDataCache.swift; sourceTree = "<group>"; };
ECE3DF832707370000FB4C96 /* MXRoomListDataPaginationOptions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MXRoomListDataPaginationOptions.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -4110,6 +4116,7 @@
32C6F92F19DD814400EA4E9C /* MatrixSDK */ = {
isa = PBXGroup;
children = (
ECDBE68E28E5D94C000C83AF /* ClientInformation */,
66836AB427CFA17200515780 /* EventStream */,
3A858DD82750EE0A006322C1 /* HomeServer */,
ECCA02B9273485A100B6F34F /* Threads */,
Expand Down Expand Up @@ -4172,6 +4179,7 @@
32B477452638128700EA5800 /* MXJSONModelUnitTests.m */,
32B090FC26201C8D002924AA /* MXAsyncTaskQueueUnitTests.swift */,
32EEA8492603FDD60041425B /* MXResponseUnitTests.swift */,
ECDBE69228E5E16F000C83AF /* MXClientInformationServiceUnitTests.swift */,
32EEA83E2603CA140041425B /* MXRestClientExtensionsTests.m */,
32C78BA6256D227D008130B1 /* MXCryptoMigrationTests.m */,
321EA11C24893A0400E35B02 /* MXCryptoRecoveryServiceTests.m */,
Expand Down Expand Up @@ -5191,6 +5199,14 @@
path = Capabilities;
sourceTree = "<group>";
};
ECDBE68E28E5D94C000C83AF /* ClientInformation */ = {
isa = PBXGroup;
children = (
ECDBE68F28E5D961000C83AF /* MXClientInformationService.swift */,
);
path = ClientInformation;
sourceTree = "<group>";
};
ECE3DF7D2707292500FB4C96 /* Common */ = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -6595,6 +6611,7 @@
8EC511062568216B00EC4E5B /* MXTaggedEventInfo.m in Sources */,
3AC135DB2640335100EE1E74 /* MXDehydrationService.m in Sources */,
32792BDD2296B90A00F4FC9D /* MXAggregatedEditsUpdater.m in Sources */,
ECDBE69028E5D961000C83AF /* MXClientInformationService.swift in Sources */,
ED44F01428180EAB00452A5D /* MXSharedHistoryKeyManager.swift in Sources */,
3259CD541DF860C300186944 /* MXRealmCryptoStore.m in Sources */,
ED44F01128180BCC00452A5D /* MXSharedHistoryKeyRequest.swift in Sources */,
Expand Down Expand Up @@ -7033,6 +7050,7 @@
ED2DD11D286C4F4400F06731 /* MXCryptoRequestsUnitTests.swift in Sources */,
32832B5D1BCC048300241108 /* MXStoreMemoryStoreTests.m in Sources */,
EDB4209927DF842F0036AF39 /* MXEventFixtures.swift in Sources */,
ECDBE69328E5E16F000C83AF /* MXClientInformationServiceUnitTests.swift in Sources */,
32114A7F1A24E15500FF2EC4 /* MXMyUserTests.m in Sources */,
32832B5E1BCC048300241108 /* MXStoreNoStoreTests.m in Sources */,
A816247C25F60C7700A46F05 /* MXDeviceListOperationsPoolUnitTests.swift in Sources */,
Expand Down Expand Up @@ -7210,6 +7228,7 @@
B14EF1E82397E90400758AF0 /* MXRoomPowerLevels.m in Sources */,
32EEA85E260401490041425B /* MXSummable.swift in Sources */,
B14EF1E92397E90400758AF0 /* MXRealmMediaScanMapper.m in Sources */,
ECDBE69128E5DC9A000C83AF /* MXClientInformationService.swift in Sources */,
ED44F01528180EAB00452A5D /* MXSharedHistoryKeyManager.swift in Sources */,
EC8A53E725B1BCC6004E0802 /* MXThirdPartyProtocol.m in Sources */,
ED44F01228180BCC00452A5D /* MXSharedHistoryKeyRequest.swift in Sources */,
Expand Down Expand Up @@ -7648,6 +7667,7 @@
B1E09A1A2397FCE90057C069 /* MXAggregatedEditsTests.m in Sources */,
B1E09A1F2397FCE90057C069 /* MXAutoDiscoveryTests.m in Sources */,
EDB4209A27DF842F0036AF39 /* MXEventFixtures.swift in Sources */,
ECDBE69428E5E16F000C83AF /* MXClientInformationServiceUnitTests.swift in Sources */,
B1E09A2E2397FD750057C069 /* MXRestClientTests.m in Sources */,
32C9B71923E81A1C00C6F30A /* MXCrossSigningVerificationTests.m in Sources */,
B1E09A1D2397FCE90057C069 /* MXCryptoKeyVerificationTests.m in Sources */,
Expand Down
95 changes: 95 additions & 0 deletions MatrixSDK/ClientInformation/MXClientInformationService.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
//
// Copyright 2022 The Matrix.org Foundation C.I.C
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

import Foundation

@objcMembers
public class MXClientInformationService: NSObject {

private weak var session: MXSession?
ismailgulek marked this conversation as resolved.
Show resolved Hide resolved
private let bundle: Bundle

public init(withSession session: MXSession,
bundle: Bundle) {
self.session = session
self.bundle = bundle
}

public func updateData() {
guard let session = session else {
return
}

guard MXSDKOptions.sharedInstance().enableNewClientInformationFeature else {
return removeDataIfNeeded(on: session)
}

guard let updatedInfo = createClientInformation() else {
return
}

let type = accountDataType(for: session)
let currentInfo = session.accountData.accountData(forEventType: type)

guard !NSDictionary(dictionary: updatedInfo).isEqual(to: currentInfo ?? [:]) else {
MXLog.debug("[MXClientInformationService] refresh: no need to update")
return
}

session.setAccountData(updatedInfo, forType: type) {
MXLog.debug("[MXClientInformationService] refresh: updated successfully")
} failure: { error in
MXLog.debug("[MXClientInformationService] refresh: update failed: \(String(describing: error))")
}
}

private func removeDataIfNeeded(on session: MXSession) {
let type = accountDataType(for: session)

guard let currentInfo = session.accountData.accountData(forEventType: type),
!currentInfo.isEmpty else {
// not exists, no need to do anything
MXLog.debug("[MXClientInformationService] removeDataIfNeeded: no need to remove")
return
}

session.setAccountData([:], forType: type) {
MXLog.debug("[MXClientInformationService] removeDataIfNeeded: removed successfully")
} failure: { error in
MXLog.debug("[MXClientInformationService] removeDataIfNeeded: remove failed: \(String(describing: error))")
}
}

private func createClientInformation() -> [AnyHashable: String]? {
guard let name = bundle.object(forInfoDictionaryKey: "CFBundleDisplayName")
?? bundle.object(forInfoDictionaryKey: "CFBundleName") as? String,
let version = bundle.object(forInfoDictionaryKey: "CFBundleShortVersionString") as? String else {
return nil
}

return [
"name": "\(name) iOS",
"version": version
]
}

private func accountDataType(for session: MXSession) -> String {
guard let deviceId = session.myDeviceId else {
fatalError("[MXClientInformationService] No device id")
}
return "\(kMXAccountDataTypeClientInformation).\(deviceId)"
}
}
7 changes: 7 additions & 0 deletions MatrixSDK/MXSDKOptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,13 @@ NS_ASSUME_NONNULL_BEGIN
*/
@property (nonatomic) BOOL enableSymmetricBackup;

/**
Enable new client information feature. (https://github.com/vector-im/element-meta/pull/656)

@remark NO by default
*/
@property (nonatomic) BOOL enableNewClientInformationFeature;

@end

NS_ASSUME_NONNULL_END
1 change: 1 addition & 0 deletions MatrixSDK/MXSDKOptions.m
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ - (instancetype)init

_enableGroupSessionCache = YES;
_enableSymmetricBackup = NO;
_enableNewClientInformationFeature = NO;
}

return self;
Expand Down
34 changes: 5 additions & 29 deletions MatrixSDK/MXSession.m
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,8 @@ Queue of requested direct room change operations ([MXSession setRoom:directWithU

@property (nonatomic, readonly) MXStoreService *storeService;

@property (nonatomic, readwrite) MXClientInformationService *clientInformationService;

@end

@implementation MXSession
Expand Down Expand Up @@ -254,6 +256,8 @@ - (id)initWithMatrixRestClient:(MXRestClient*)mxRestClient
_eventStreamService = [[MXEventStreamService alloc] init];
_preferredSyncPresence = MXPresenceOnline;
_locationService = [[MXLocationService alloc] initWithSession:self];
_clientInformationService = [[MXClientInformationService alloc] initWithSession:self
bundle:NSBundle.mainBundle];

[self setIdentityServer:mxRestClient.identityServer andAccessToken:mxRestClient.credentials.identityServerAccessToken];

Expand Down Expand Up @@ -1138,7 +1142,7 @@ - (void)_resume:(void (^)(void))resumeDone
[self serverSyncWithServerTimeout:0 success:nil failure:nil clientTimeout:CLIENT_TIMEOUT_MS setPresence:self.preferredSyncPresenceString];
}

[self refreshClientInformationIfNeeded];
[self.clientInformationService updateData];
}

for (MXPeekingRoom *peekingRoom in peekingRooms)
Expand Down Expand Up @@ -4561,34 +4565,6 @@ - (void)updateBreadcrumbsWithRoomWithId:(NSString *)roomId
}];
}

- (void)refreshClientInformationIfNeeded
{
NSString *bundleDisplayName = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleDisplayName"];
NSString *name = [NSString stringWithFormat:@"%@ iOS", bundleDisplayName];
NSString *version = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleShortVersionString"];
NSDictionary *updatedInfo = @{
@"name": name,
@"version": version
};
NSString *type = [NSString stringWithFormat:@"%@.%@", kMXAccountDataTypeClientInformation, self.myDeviceId];

NSDictionary *currentInfo = [self.accountData accountDataForEventType:type];

if ([updatedInfo isEqualToDictionary:currentInfo])
{
MXLogDebug(@"[MXSession] refreshClientInformationIfNeeded: no need to update");
}
else
{
// there is change, update the event
[self setAccountData:updatedInfo forType:type success:^{
MXLogDebug(@"[MXSession] refreshClientInformationIfNeeded: updated successfully");
} failure:^(NSError *error) {
MXLogDebug(@"[MXSession] refreshClientInformationIfNeeded: update failed: %@", error);
}];
}
}

#pragma mark - Homeserver information
- (MXWellKnown *)homeserverWellknown
{
Expand Down
Loading