Skip to content

Commit

Permalink
Add push client API + rename some things (#2063)
Browse files Browse the repository at this point in the history
  • Loading branch information
nirinchev authored Oct 5, 2020
1 parent e112dab commit 186f36c
Show file tree
Hide file tree
Showing 12 changed files with 210 additions and 68 deletions.
2 changes: 1 addition & 1 deletion Realm/Realm/Configurations/SyncConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ private SyncConfiguration(object partition, User user, string path)

User = user;
Partition = partition;
DatabasePath = GetPathToRealm(path ?? user.App.AppHandle.GetRealmPath(User, SerializationHelper.ToJson(partition)));
DatabasePath = GetPathToRealm(path ?? user.App.Handle.GetRealmPath(User, SerializationHelper.ToJson(partition)));
}

internal override Realm CreateRealm(RealmSchema schema)
Expand Down
2 changes: 1 addition & 1 deletion Realm/Realm/Exceptions/ClientResetException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ internal ClientResetException(App app, string message, IDictionary<string, strin
/// </remarks>
public bool InitiateClientReset()
{
return _app.AppHandle.ImmediatelyRunFileActions(_originalFilePath);
return _app.Handle.ImmediatelyRunFileActions(_originalFilePath);
}
}
}
2 changes: 1 addition & 1 deletion Realm/Realm/Exceptions/PermissionDeniedException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public bool DeleteRealmUserInfo()
Argument.Ensure<NotSupportedException>(!_actionInvoked, $"{nameof(DeleteRealmUserInfo)} can only be called once.");
_actionInvoked = true;

return _app.AppHandle.ImmediatelyRunFileActions(_originalFilePath);
return _app.Handle.ImmediatelyRunFileActions(_originalFilePath);
}
}
}
2 changes: 1 addition & 1 deletion Realm/Realm/Extensions/RealmSyncExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public static Session GetSession(this Realm realm)
var syncConfig = Argument.EnsureType<SyncConfiguration>(realm.Config, "Cannot get a Session for a Realm without a SyncConfiguration", nameof(realm));

var app = syncConfig.User.App;
var session = app.AppHandle.GetSessionForPath(realm.SharedRealmHandle);
var session = app.Handle.GetSessionForPath(realm.SharedRealmHandle);
return new Session(session);
}
}
Expand Down
47 changes: 41 additions & 6 deletions Realm/Realm/Handles/SyncUserHandle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,34 +93,49 @@ public static extern void call_function(SyncUserHandle handle, AppHandle app,
[DllImport(InteropConfig.DLL_NAME, EntryPoint = "realm_syncuser_get_serialized_identities", CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr get_identities(SyncUserHandle handle, IntPtr buffer, IntPtr bufsize, out NativeException ex);

#region Push

[DllImport(InteropConfig.DLL_NAME, EntryPoint = "realm_syncuser_push_register", CallingConvention = CallingConvention.Cdecl)]
public static extern void push_register(SyncUserHandle handle, AppHandle app,
[MarshalAs(UnmanagedType.LPWStr)] string service, IntPtr service_len,
[MarshalAs(UnmanagedType.LPWStr)] string token, IntPtr token_len,
IntPtr tcs_ptr, out NativeException ex);

[DllImport(InteropConfig.DLL_NAME, EntryPoint = "realm_syncuser_push_deregister", CallingConvention = CallingConvention.Cdecl)]
public static extern void push_deregister(SyncUserHandle handle, AppHandle app,
[MarshalAs(UnmanagedType.LPWStr)] string service, IntPtr service_len,
IntPtr tcs_ptr, out NativeException ex);

#endregion

#region Api Keys

[DllImport(InteropConfig.DLL_NAME, EntryPoint = "realm_syncuser_create_api_key", CallingConvention = CallingConvention.Cdecl)]
[DllImport(InteropConfig.DLL_NAME, EntryPoint = "realm_syncuser_api_key_create", CallingConvention = CallingConvention.Cdecl)]
public static extern void create_api_key(SyncUserHandle handle, AppHandle app,
[MarshalAs(UnmanagedType.LPWStr)] string name, IntPtr name_len,
IntPtr tcs_ptr, out NativeException ex);

// id is IntPtr rather than PrimitiveValue due to a bug in .NET Core on Linux and Mac
// that causes incorrect marshalling of the struct.
[DllImport(InteropConfig.DLL_NAME, EntryPoint = "realm_syncuser_fetch_api_key", CallingConvention = CallingConvention.Cdecl)]
[DllImport(InteropConfig.DLL_NAME, EntryPoint = "realm_syncuser_api_key_fetch", CallingConvention = CallingConvention.Cdecl)]
public static extern void fetch_api_key(SyncUserHandle handle, AppHandle app, IntPtr id, IntPtr tcs_ptr, out NativeException ex);

[DllImport(InteropConfig.DLL_NAME, EntryPoint = "realm_syncuser_fetch_api_keys", CallingConvention = CallingConvention.Cdecl)]
[DllImport(InteropConfig.DLL_NAME, EntryPoint = "realm_syncuser_api_key_fetch_all", CallingConvention = CallingConvention.Cdecl)]
public static extern void fetch_api_keys(SyncUserHandle handle, AppHandle app, IntPtr tcs_ptr, out NativeException ex);

// id is IntPtr rather than PrimitiveValue due to a bug in .NET Core on Linux and Mac
// that causes incorrect marshalling of the struct.
[DllImport(InteropConfig.DLL_NAME, EntryPoint = "realm_syncuser_delete_api_key", CallingConvention = CallingConvention.Cdecl)]
[DllImport(InteropConfig.DLL_NAME, EntryPoint = "realm_syncuser_api_key_delete", CallingConvention = CallingConvention.Cdecl)]
public static extern void delete_api_key(SyncUserHandle handle, AppHandle app, IntPtr id, IntPtr tcs_ptr, out NativeException ex);

// id is IntPtr rather than PrimitiveValue due to a bug in .NET Core on Linux and Mac
// that causes incorrect marshalling of the struct.
[DllImport(InteropConfig.DLL_NAME, EntryPoint = "realm_syncuser_disable_api_key", CallingConvention = CallingConvention.Cdecl)]
[DllImport(InteropConfig.DLL_NAME, EntryPoint = "realm_syncuser_api_key_disable", CallingConvention = CallingConvention.Cdecl)]
public static extern void disable_api_key(SyncUserHandle handle, AppHandle app, IntPtr id, IntPtr tcs_ptr, out NativeException ex);

// id is IntPtr rather than PrimitiveValue due to a bug in .NET Core on Linux and Mac
// that causes incorrect marshalling of the struct.
[DllImport(InteropConfig.DLL_NAME, EntryPoint = "realm_syncuser_enable_api_key", CallingConvention = CallingConvention.Cdecl)]
[DllImport(InteropConfig.DLL_NAME, EntryPoint = "realm_syncuser_api_key_enable", CallingConvention = CallingConvention.Cdecl)]
public static extern void enable_api_key(SyncUserHandle handle, AppHandle app, IntPtr id, IntPtr tcs_ptr, out NativeException ex);

#endregion
Expand Down Expand Up @@ -265,6 +280,26 @@ public string GetIdentities()
});
}

#region Push

public void RegisterPushToken(AppHandle app, string service, string token, TaskCompletionSource<object> tcs)
{
var tcsHandle = GCHandle.Alloc(tcs);

NativeMethods.push_register(this, app, service, (IntPtr)service.Length, token, (IntPtr)token.Length, GCHandle.ToIntPtr(tcsHandle), out var ex);
ex.ThrowIfNecessary();
}

public void DeregisterPushToken(AppHandle app, string service, TaskCompletionSource<object> tcs)
{
var tcsHandle = GCHandle.Alloc(tcs);

NativeMethods.push_deregister(this, app, service, (IntPtr)service.Length, GCHandle.ToIntPtr(tcsHandle), out var ex);
ex.ThrowIfNecessary();
}

#endregion

#region Api Keys

public void CreateApiKey(AppHandle app, string name, TaskCompletionSource<UserApiKey[]> tcs)
Expand Down
2 changes: 1 addition & 1 deletion Realm/Realm/Sync/ApiKey.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public class ApiKey
/// <summary>
/// Gets the name of the key.
/// </summary>
/// <value>The friendly name of the key, specified when calling <see cref="User.ApiKeyApi.CreateAsync"/>.</value>
/// <value>The friendly name of the key, specified when calling <see cref="User.ApiKeyClient.CreateAsync"/>.</value>
public string Name { get; }

/// <summary>
Expand Down
52 changes: 26 additions & 26 deletions Realm/Realm/Sync/App.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,41 +91,41 @@ namespace Realms.Sync
/// <seealso cref="AppConfiguration"/>
public class App
{
internal readonly AppHandle AppHandle;
internal readonly AppHandle Handle;

/// <summary>
/// Gets a <see cref="SyncApi"/> instance that exposes API for interacting with the synchronization client for this <see cref="App"/>.
/// Gets a <see cref="SyncClient"/> instance that exposes API for interacting with the synchronization client for this <see cref="App"/>.
/// </summary>
/// <value>A <see cref="SyncApi"/> instance scoped to this <see cref="App"/>.</value>
public SyncApi Sync { get; }
/// <value>A <see cref="SyncClient"/> instance scoped to this <see cref="App"/>.</value>
public SyncClient Sync { get; }

/// <summary>
/// Gets a <see cref="EmailPasswordApi"/> instance that exposes functionality related to users either being created or logged in using
/// Gets a <see cref="EmailPasswordClient"/> instance that exposes functionality related to users either being created or logged in using
/// the <see cref="Credentials.AuthProvider.EmailPassword"/> provider.
/// </summary>
/// <value>An <see cref="EmailPasswordApi"/> instance scoped to this <see cref="App"/>.</value>
public EmailPasswordApi EmailPasswordAuth { get; }
/// <value>An <see cref="EmailPasswordClient"/> instance scoped to this <see cref="App"/>.</value>
public EmailPasswordClient EmailPasswordAuth { get; }

/// <summary>
/// Gets the currently logged-in user. If none exists, null is returned.
/// </summary>
/// <value>Valid user or <c>null</c> to indicate nobody logged in.</value>
[SuppressMessage("Reliability", "CA2000:Dispose objects before losing scope", Justification = "The User instance will own its handle.")]
public User CurrentUser => AppHandle.TryGetCurrentUser(out var userHandle) ? new User(userHandle, this) : null;
public User CurrentUser => Handle.TryGetCurrentUser(out var userHandle) ? new User(userHandle, this) : null;

/// <summary>
/// Gets all currently logged in users.
/// </summary>
/// <value>An array of valid logged in users.</value>
public User[] AllUsers => AppHandle.GetAllLoggedInUsers()
public User[] AllUsers => Handle.GetAllLoggedInUsers()
.Select(handle => new User(handle, this))
.ToArray();

internal App(AppHandle handle)
{
AppHandle = handle;
Sync = new SyncApi(this);
EmailPasswordAuth = new EmailPasswordApi(this);
Handle = handle;
Sync = new SyncClient(this);
EmailPasswordAuth = new EmailPasswordClient(this);
}

/// <summary>
Expand Down Expand Up @@ -200,7 +200,7 @@ public async Task<User> LogInAsync(Credentials credentials)
Argument.NotNull(credentials, nameof(credentials));

var tcs = new TaskCompletionSource<SyncUserHandle>();
AppHandle.LogIn(credentials.ToNative(), tcs);
Handle.LogIn(credentials.ToNative(), tcs);
var handle = await tcs.Task;

return new User(handle, this);
Expand All @@ -214,7 +214,7 @@ public void SwitchUser(User user)
{
Argument.NotNull(user, nameof(user));

AppHandle.SwitchUser(user.Handle);
Handle.SwitchUser(user.Handle);
}

/// <summary>
Expand All @@ -233,19 +233,19 @@ public async Task RemoveUserAsync(User user)
Argument.NotNull(user, nameof(user));

var tcs = new TaskCompletionSource<object>();
AppHandle.Remove(user.Handle, tcs);
Handle.Remove(user.Handle, tcs);
await tcs.Task;
}

/// <summary>
/// A sync manager, handling synchronization of local Realm with remote MongoDB Realm apps. It is always scoped to a
/// particular app and can only be accessed via <see cref="Sync"/>.
/// </summary>
public class SyncApi
public class SyncClient
{
private readonly App _app;

internal SyncApi(App app)
internal SyncClient(App app)
{
_app = app;
}
Expand All @@ -262,19 +262,19 @@ internal SyncApi(App app)
/// </remarks>
public void Reconnect()
{
_app.AppHandle.Reconnect();
_app.Handle.Reconnect();
}
}

/// <summary>
/// A class, encapsulating functionality for users, logged in with the <see cref="Credentials.AuthProvider.EmailPassword"/> provider.
/// It is always scoped to a particular app and can only be accessed via <see cref="EmailPasswordAuth"/>.
/// </summary>
public class EmailPasswordApi
public class EmailPasswordClient
{
private readonly App _app;

internal EmailPasswordApi(App app)
internal EmailPasswordClient(App app)
{
_app = app;
}
Expand All @@ -297,7 +297,7 @@ public Task RegisterUserAsync(string email, string password)
Argument.NotNullOrEmpty(password, nameof(password));

var tcs = new TaskCompletionSource<object>();
_app.AppHandle.EmailPassword.RegisterUser(email, password, tcs);
_app.Handle.EmailPassword.RegisterUser(email, password, tcs);
return tcs.Task;
}

Expand All @@ -321,7 +321,7 @@ public Task ConfirmUserAsync(string token, string tokenId)
Argument.NotNullOrEmpty(tokenId, nameof(tokenId));

var tcs = new TaskCompletionSource<object>();
_app.AppHandle.EmailPassword.ConfirmUser(token, tokenId, tcs);
_app.Handle.EmailPassword.ConfirmUser(token, tokenId, tcs);
return tcs.Task;
}

Expand All @@ -339,7 +339,7 @@ public Task ResendConfirmationEmailAsync(string email)
Argument.NotNullOrEmpty(email, nameof(email));

var tcs = new TaskCompletionSource<object>();
_app.AppHandle.EmailPassword.ResendConfirmationEmail(email, tcs);
_app.Handle.EmailPassword.ResendConfirmationEmail(email, tcs);
return tcs.Task;
}

Expand All @@ -357,7 +357,7 @@ public Task SendResetPasswordEmailAsync(string email)
Argument.NotNullOrEmpty(email, nameof(email));

var tcs = new TaskCompletionSource<object>();
_app.AppHandle.EmailPassword.SendResetPasswordEmail(email, tcs);
_app.Handle.EmailPassword.SendResetPasswordEmail(email, tcs);
return tcs.Task;
}

Expand All @@ -382,7 +382,7 @@ public Task ResetPasswordAsync(string password, string token, string tokenId)
Argument.NotNullOrEmpty(password, nameof(password));

var tcs = new TaskCompletionSource<object>();
_app.AppHandle.EmailPassword.ResetPassword(password, token, tokenId, tcs);
_app.Handle.EmailPassword.ResetPassword(password, token, tokenId, tcs);
return tcs.Task;
}

Expand All @@ -406,7 +406,7 @@ public Task CallResetPasswordFunctionAsync(string email, string password, params
Argument.NotNullOrEmpty(password, nameof(password));

var tcs = new TaskCompletionSource<object>();
_app.AppHandle.EmailPassword.CallResetPasswordFunction(email, password, SerializationHelper.ToJson(functionArgs), tcs);
_app.Handle.EmailPassword.CallResetPasswordFunction(email, password, SerializationHelper.ToJson(functionArgs), tcs);
return tcs.Task;
}
}
Expand Down
4 changes: 2 additions & 2 deletions Realm/Realm/Sync/Credentials.cs
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,8 @@ public static Credentials JWT(string customToken)
/// <returns>A Credentials that can be used to authenticate a user with their email and password.</returns>
/// <remarks>
/// A user can login with email and password only after they've registered their account and verified their
/// email. To register an email/password user via the SDK, use <see cref="App.EmailPasswordApi.RegisterUserAsync"/>.
/// To verify an email from the SDK, use <see cref="App.EmailPasswordApi.ConfirmUserAsync"/>. The email/password
/// email. To register an email/password user via the SDK, use <see cref="App.EmailPasswordClient.RegisterUserAsync"/>.
/// To verify an email from the SDK, use <see cref="App.EmailPasswordClient.ConfirmUserAsync"/>. The email/password
/// provider can also be configured to automatically confirm users or to run a custom confirmation function upon
/// user registration.
/// </remarks>
Expand Down
Loading

0 comments on commit 186f36c

Please sign in to comment.