Skip to content

Commit

Permalink
Update core
Browse files Browse the repository at this point in the history
  • Loading branch information
tgoyne committed Mar 8, 2024
1 parent 9ece953 commit 04af045
Show file tree
Hide file tree
Showing 37 changed files with 294 additions and 276 deletions.
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ let package = Package(
targets: ["Realm", "RealmSwift"]),
],
dependencies: [
.package(url: "https://github.com/realm/realm-core.git", exact: coreVersion)
.package(path: "/opt/s/realm-core")
],
targets: [
.target(
Expand Down
2 changes: 1 addition & 1 deletion Realm/ObjectServerTests/ClientResetTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ class ClientResetTests: SwiftSyncTestCase {
}
XCTAssertTrue(path.contains("mongodb-realm/\(app.appId)/recovered-realms/recovered_realm"))
XCTAssertFalse(FileManager.default.fileExists(atPath: path))
SyncSession.immediatelyHandleError(token, syncManager: app.syncManager)
SyncSession.immediatelyHandleError(token)
XCTAssertTrue(FileManager.default.fileExists(atPath: path))
ex.fulfill()
}
Expand Down
11 changes: 6 additions & 5 deletions Realm/ObjectServerTests/RLMMongoClientTests.mm
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#import "RLMBSON_Private.hpp"
#import "RLMUser_Private.hpp"

#import <realm/object-store/sync/app_user.hpp>
#import <realm/object-store/sync/sync_manager.hpp>
#import <realm/util/bson/bson.hpp>

Expand Down Expand Up @@ -735,8 +736,8 @@ - (void)performMultipleWatchStreamsTest:(nullable dispatch_queue_t)delegateQueue
s << RLMConvertRLMBSONToBson(partitionValue);
// Intentionally not passing the correct partition value here as we (accidentally?)
// don't use the filename generated from the partition value
realm::SyncConfig config(user._syncUser, "null");
return @(user._syncUser->sync_manager()->path_for_realm(config, s.str()).c_str());
realm::SyncConfig config(user.user, "null");
return @(user.user->path_for_realm(config, s.str()).c_str());
}

- (void)testSyncFilePaths {
Expand All @@ -763,10 +764,10 @@ - (void)testSyncFilePaths {
}

static NSString *oldPathForPartitionValue(RLMUser *user, NSString *oldName) {
realm::SyncConfig config(user._syncUser, "null");
realm::SyncConfig config(user.user, "null");
return [NSString stringWithFormat:@"%@/%s%@.realm",
[@(user._syncUser->sync_manager()->path_for_realm(config).c_str()) stringByDeletingLastPathComponent],
user._syncUser->identity().c_str(), oldName];
[@(user.user->path_for_realm(config).c_str()) stringByDeletingLastPathComponent],
user.user->user_id().c_str(), oldName];
}

- (void)testLegacyFilePathsAreUsedIfFilesArePresent {
Expand Down
24 changes: 12 additions & 12 deletions Realm/ObjectServerTests/RLMObjectServerTests.mm
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
#import "RLMWatchTestUtility.h"

#import <realm/object-store/shared_realm.hpp>
#import <realm/object-store/sync/app_user.hpp>
#import <realm/object-store/sync/sync_manager.hpp>
#import <realm/object-store/thread_safe_reference.hpp>
#import <realm/util/file.hpp>
Expand Down Expand Up @@ -166,7 +167,7 @@ - (void)testRemoveUser {
}

- (void)testDeleteUser {
[self createUser];
RLMUser *firstUser = [self createUser];
RLMUser *secondUser = [self createUser];

XCTAssert([self.app.currentUser.identifier isEqualToString:secondUser.identifier]);
Expand All @@ -176,7 +177,7 @@ - (void)testDeleteUser {
[secondUser deleteWithCompletion:^(NSError *error) {
XCTAssert(!error);
XCTAssert(self.app.allUsers.count == 1);
XCTAssertNil(self.app.currentUser);
XCTAssertEqualObjects(self.app.currentUser, firstUser);
XCTAssertEqual(secondUser.state, RLMUserStateRemoved);
[deleteUserExpectation fulfill];
}];
Expand Down Expand Up @@ -560,7 +561,7 @@ - (void)testSyncErrorHandlerErrorDomain {
#pragma mark - User Profile

- (void)testUserProfileInitialization {
RLMUserProfile *profile = [[RLMUserProfile alloc] initWithUserProfile:realm::SyncUserProfile()];
RLMUserProfile *profile = [[RLMUserProfile alloc] initWithUserProfile:realm::app::UserProfile()];
XCTAssertNil(profile.name);
XCTAssertNil(profile.maxAge);
XCTAssertNil(profile.minAge);
Expand All @@ -572,7 +573,7 @@ - (void)testUserProfileInitialization {

auto metadata = realm::bson::BsonDocument({{"some_key", "some_value"}});

profile = [[RLMUserProfile alloc] initWithUserProfile:realm::SyncUserProfile(realm::bson::BsonDocument({
profile = [[RLMUserProfile alloc] initWithUserProfile:realm::app::UserProfile(realm::bson::BsonDocument({
{"name", "Jane"},
{"max_age", "40"},
{"min_age", "30"},
Expand Down Expand Up @@ -758,7 +759,7 @@ - (void)testSessionRefresh {

// Should result in an access token error followed by a refresh when we
// open the Realm which is entirely transparent to the user
user._syncUser->update_access_token(self.badAccessToken.UTF8String);
// user._syncUser->update_access_token(self.badAccessToken.UTF8String);
RLMRealm *realm = [self openRealmForPartitionValue:self.name user:user];

RLMRealm *realm2 = [self openRealm];
Expand Down Expand Up @@ -1367,8 +1368,7 @@ - (void)testClientResetManualInitiation {
// At this point the Realm should be invalidated and client reset should be possible.
NSString *pathValue = [theError rlmSync_clientResetBackedUpRealmPath];
XCTAssertFalse([NSFileManager.defaultManager fileExistsAtPath:pathValue]);
[RLMSyncSession immediatelyHandleError:theError.rlmSync_errorActionToken
syncManager:self.app.syncManager];
[RLMSyncSession immediatelyHandleError:theError.rlmSync_errorActionToken];
XCTAssertTrue([NSFileManager.defaultManager fileExistsAtPath:pathValue]);
}

Expand Down Expand Up @@ -2016,8 +2016,8 @@ - (void)testWriteCopyForConfigurationLocalRealmForSyncWithExistingData {
s << RLMConvertRLMBSONToBson(partitionValue);
// Intentionally not passing the correct partition value here as we (accidentally?)
// don't use the filename generated from the partition value
realm::SyncConfig config(user._syncUser, "null");
return @(user._syncUser->sync_manager()->path_for_realm(config, s.str()).c_str());
realm::SyncConfig config(user.user, "null");
return @(user.user->path_for_realm(config, s.str()).c_str());
}

- (void)testSyncFilePaths {
Expand All @@ -2044,10 +2044,10 @@ - (void)testSyncFilePaths {
}

static NSString *oldPathForPartitionValue(RLMUser *user, NSString *oldName) {
realm::SyncConfig config(user._syncUser, "null");
realm::SyncConfig config(user.user, "null");
return [NSString stringWithFormat:@"%@/%s%@.realm",
[@(user._syncUser->sync_manager()->path_for_realm(config).c_str()) stringByDeletingLastPathComponent],
user._syncUser->identity().c_str(), oldName];
[@(user.user->path_for_realm(config).c_str()) stringByDeletingLastPathComponent],
user.user->user_id().c_str(), oldName];
}

- (void)testLegacyFilePathsAreUsedIfFilesArePresent {
Expand Down
26 changes: 13 additions & 13 deletions Realm/ObjectServerTests/RLMSyncTestCase.mm
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@
#import "RLMChildProcessEnvironment.h"
#import "RLMRealmUtil.hpp"

#import <realm/object-store/sync/app_user.hpp>
#import <realm/object-store/sync/sync_manager.hpp>
#import <realm/object-store/sync/sync_session.hpp>
#import <realm/object-store/sync/sync_user.hpp>

#if TARGET_OS_OSX

Expand All @@ -56,7 +56,7 @@ - (BOOL)waitForDownloadCompletionOnQueue:(dispatch_queue_t)queue callback:(void(
@end

@interface RLMUser ()
- (std::shared_ptr<realm::SyncUser>)_syncUser;
- (std::shared_ptr<realm::app::User>)_syncUser;
@end

@interface TestNetworkTransport : RLMNetworkTransport
Expand Down Expand Up @@ -421,9 +421,11 @@ - (void)waitForDownloadsForRealm:(RLMRealm *)realm error:(NSError **)error {
}

- (void)setInvalidTokensForUser:(RLMUser *)user {
auto token = self.badAccessToken.UTF8String;
user._syncUser->log_out();
user._syncUser->log_in(token, token);
realm::RealmJWT token(std::string_view(self.badAccessToken.UTF8String));
user._syncUser->update_data_for_testing([&](auto& data) {
data.access_token = token;
data.refresh_token = token;
});
}

- (void)writeToPartition:(NSString *)partition block:(void (^)(RLMRealm *))block {
Expand Down Expand Up @@ -554,15 +556,13 @@ - (void)resetAppCache {
[user logOutWithCompletion:^(NSError *) {
[ex fulfill];
}];

// Sessions are removed from the user asynchronously after a logout.
// We need to wait for this to happen before calling resetForTesting as
// that expects all sessions to be cleaned up first.
if (user.allSessions.count) {
[exs addObject:[self expectationForPredicate:[NSPredicate predicateWithFormat:@"allSessions.@count == 0"]
evaluatedWithObject:user handler:nil]];
}
}];

// Sessions are removed from the user asynchronously after a logout.
// We need to wait for this to happen before calling resetForTesting as
// that expects all sessions to be cleaned up first.
[exs addObject:[self expectationForPredicate:[NSPredicate predicateWithFormat:@"hasAnySessions = false"]
evaluatedWithObject:app.syncManager handler:nil]];
}

if (exs.count) {
Expand Down
2 changes: 1 addition & 1 deletion Realm/RLMAPIKeyAuth.mm
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ @implementation RLMAPIKeyAuth
return self.app._realmApp->provider_client<App::UserAPIKeyProviderClient>();
}

- (std::shared_ptr<realm::SyncUser>)currentUser {
- (std::shared_ptr<realm::app::User>)currentUser {
return self.app._realmApp->current_user();
}

Expand Down
64 changes: 33 additions & 31 deletions Realm/RLMApp.mm
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,13 @@
#import "RLMCredentials_Private.hpp"
#import "RLMEmailPasswordAuth.h"
#import "RLMLogger.h"
#import "RLMProviderClient_Private.hpp"
#import "RLMPushClient_Private.hpp"
#import "RLMSyncManager_Private.hpp"
#import "RLMUser_Private.hpp"
#import "RLMUtil.hpp"

#import <realm/object-store/sync/app_user.hpp>
#import <realm/object-store/sync/sync_manager.hpp>
#import <realm/sync/config.hpp>

Expand Down Expand Up @@ -94,15 +96,14 @@ void send_request_to_server(const app::Request& request,

#pragma mark RLMAppConfiguration
@implementation RLMAppConfiguration {
realm::app::App::Config _config;
SyncClientConfig _clientConfig;
realm::app::AppConfig _config;
}

- (instancetype)init {
if (self = [super init]) {
self.enableSessionMultiplexing = true;
self.encryptMetadata = !getenv("REALM_DISABLE_METADATA_ENCRYPTION") && !RLMIsRunningInPlayground();
RLMNSStringToStdString(_clientConfig.base_file_path, RLMDefaultDirectoryForBundleIdentifier(nil));
RLMNSStringToStdString(_config.base_file_path, RLMDefaultDirectoryForBundleIdentifier(nil));
configureSyncConnectionParameters(_config);
}
return self;
Expand Down Expand Up @@ -152,7 +153,7 @@ - (instancetype)initWithBaseURL:(nullable NSString *)baseURL
return self;
}

static void configureSyncConnectionParameters(realm::app::App::Config& config) {
static void configureSyncConnectionParameters(realm::app::AppConfig& config) {
// Anonymized BundleId
NSString *bundleId = [[NSBundle mainBundle] bundleIdentifier];
NSData *bundleIdData = [bundleId dataUsingEncoding:NSUTF8StringEncoding];
Expand All @@ -178,21 +179,16 @@ static void configureSyncConnectionParameters(realm::app::App::Config& config) {
config.device_info.device_version = systemInfo.machine;
}

- (const realm::app::App::Config&)config {
- (const realm::app::AppConfig&)config {
if (!_config.transport) {
self.transport = nil;
}
return _config;
}

- (const realm::SyncClientConfig&)clientConfig {
return _clientConfig;
}

- (id)copyWithZone:(NSZone *)zone {
RLMAppConfiguration *copy = [[RLMAppConfiguration alloc] init];
copy->_config = _config;
copy->_clientConfig = _clientConfig;
return copy;
}

Expand Down Expand Up @@ -250,36 +246,36 @@ - (void)setDefaultRequestTimeoutMS:(NSUInteger)defaultRequestTimeoutMS {
}

- (BOOL)enableSessionMultiplexing {
return _clientConfig.multiplex_sessions;
return _config.sync_client_config.multiplex_sessions;
}

- (void)setEnableSessionMultiplexing:(BOOL)enableSessionMultiplexing {
_clientConfig.multiplex_sessions = enableSessionMultiplexing;
_config.sync_client_config.multiplex_sessions = enableSessionMultiplexing;
}

- (BOOL)encryptMetadata {
return _clientConfig.metadata_mode == SyncManager::MetadataMode::Encryption;
return _config.metadata_mode == app::AppConfig::MetadataMode::Encryption;
}

- (void)setEncryptMetadata:(BOOL)encryptMetadata {
_clientConfig.metadata_mode = encryptMetadata ? SyncManager::MetadataMode::Encryption
: SyncManager::MetadataMode::NoEncryption;
_config.metadata_mode = encryptMetadata ? app::AppConfig::MetadataMode::Encryption
: app::AppConfig::MetadataMode::NoEncryption;
}

- (NSURL *)rootDirectory {
return [NSURL fileURLWithPath:RLMStringViewToNSString(_clientConfig.base_file_path)];
return [NSURL fileURLWithPath:RLMStringViewToNSString(_config.base_file_path)];
}

- (void)setRootDirectory:(NSURL *)rootDirectory {
RLMNSStringToStdString(_clientConfig.base_file_path, rootDirectory.path);
RLMNSStringToStdString(_config.base_file_path, rootDirectory.path);
}

- (RLMSyncTimeoutOptions *)syncTimeouts {
return [[RLMSyncTimeoutOptions alloc] initWithOptions:_clientConfig.timeouts];
return [[RLMSyncTimeoutOptions alloc] initWithOptions:_config.sync_client_config.timeouts];
}

- (void)setSyncTimeouts:(RLMSyncTimeoutOptions *)syncTimeouts {
_clientConfig.timeouts = syncTimeouts->_options;
_config.sync_client_config.timeouts = syncTimeouts->_options;
}

@end
Expand Down Expand Up @@ -334,16 +330,16 @@ - (instancetype)initWithApp:(std::shared_ptr<realm::app::App>&&)app config:(RLMA
- (instancetype)initWithConfiguration:(RLMAppConfiguration *)configuration {
if (self = [super init]) {
_app = RLMTranslateError([&] {
return app::App::get_app(app::App::CacheMode::Enabled, configuration.config, configuration.clientConfig);
return app::App::get_app(app::App::CacheMode::Disabled, configuration.config);
});
_configuration = configuration;
_syncManager = [[RLMSyncManager alloc] initWithSyncManager:_app->sync_manager()];
}
return self;
}

static RLMUnfairMutex s_appMutex;
static NSMutableDictionary *s_apps = [NSMutableDictionary new];
static std::mutex& s_appMutex = *new std::mutex();

+ (NSArray *)allApps {
std::lock_guard lock(s_appMutex);
Expand Down Expand Up @@ -385,6 +381,11 @@ + (instancetype)appWithId:(NSString *)appId {
return s_apps[appId] = [[RLMApp alloc] initWithConfiguration:config];
}

+ (RLMApp *_Nullable)cachedAppWithId:(NSString *)appId {
std::lock_guard lock(s_appMutex);
return s_apps[appId];
}

- (NSString *)appId {
return @(_app->config().app_id.c_str());
}
Expand All @@ -395,42 +396,43 @@ - (NSString *)appId {

- (NSDictionary<NSString *, RLMUser *> *)allUsers {
NSMutableDictionary *buffer = [NSMutableDictionary new];
for (auto&& user : _app->sync_manager()->all_users()) {
NSString *identity = @(user->identity().c_str());
buffer[identity] = [[RLMUser alloc] initWithUser:std::move(user) app:self];
for (auto&& user : _app->all_users()) {
NSString *user_id = @(user->user_id().c_str());
buffer[user_id] = [[RLMUser alloc] initWithUser:std::move(user)];
}
return buffer;
}

- (RLMUser *)currentUser {
if (auto user = _app->sync_manager()->get_current_user()) {
return [[RLMUser alloc] initWithUser:user app:self];
if (auto user = _app->current_user()) {
return [[RLMUser alloc] initWithUser:user];
}
return nil;
}

- (RLMEmailPasswordAuth *)emailPasswordAuth {
return [[RLMEmailPasswordAuth alloc] initWithApp: self];
return [[RLMEmailPasswordAuth alloc] initWithApp:_app];
}

- (void)loginWithCredential:(RLMCredentials *)credentials
completion:(RLMUserCompletionBlock)completionHandler {
auto completion = ^(std::shared_ptr<SyncUser> user, std::optional<app::AppError> error) {
auto completion = ^(std::shared_ptr<app::User> user, std::optional<app::AppError> error) {
if (error) {
return completionHandler(nil, makeError(*error));
}

completionHandler([[RLMUser alloc] initWithUser:user app:self], nil);
completionHandler([[RLMUser alloc] initWithUser:user], nil);
};
return RLMTranslateError([&] {
return _app->log_in_with_credentials(credentials.appCredentials, completion);
});
}

- (RLMUser *)switchToUser:(RLMUser *)syncUser {
return RLMTranslateError([&] {
return [[RLMUser alloc] initWithUser:_app->switch_user(syncUser._syncUser) app:self];
RLMTranslateError([&] {
_app->switch_user(syncUser.user);
});
return syncUser;
}

- (RLMPushClient *)pushClientWithServiceName:(NSString *)serviceName {
Expand Down
Loading

0 comments on commit 04af045

Please sign in to comment.