From e68a13590a1bda1c4bfc5903c925db7fc8a8411f Mon Sep 17 00:00:00 2001 From: oneRain Date: Wed, 21 Aug 2019 17:55:08 +0800 Subject: [PATCH] =?UTF-8?q?chore:=20=E7=AE=80=E5=8C=96=20AVUser?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../User/Controller/AVUserController.cs | 8 +- Storage/Storage/Public/AVFile.cs | 204 ++++-------- Storage/Storage/Public/AVUser.cs | 309 ++++-------------- 3 files changed, 135 insertions(+), 386 deletions(-) diff --git a/Storage/Storage/Internal/User/Controller/AVUserController.cs b/Storage/Storage/Internal/User/Controller/AVUserController.cs index fa5ae7a..3b89d27 100644 --- a/Storage/Storage/Internal/User/Controller/AVUserController.cs +++ b/Storage/Storage/Internal/User/Controller/AVUserController.cs @@ -5,14 +5,14 @@ using System.Net.Http; namespace LeanCloud.Storage.Internal { public class AVUserController { - public async Task SignUpAsync(IObjectState state, IDictionary operations, CancellationToken cancellationToken) { + public async Task SignUpAsync(IObjectState state, IDictionary operations) { var objectJSON = AVObject.ToJSONObjectForSaving(operations); var command = new AVCommand { Path = "classes/_User", Method = HttpMethod.Post, Content = objectJSON }; - var ret = await AVPlugins.Instance.CommandRunner.RunCommandAsync>(command, cancellationToken); + var ret = await AVPlugins.Instance.CommandRunner.RunCommandAsync>(command); var serverState = AVObjectCoder.Instance.Decode(ret.Item2, AVDecoder.Instance); serverState = serverState.MutatedClone(mutableClone => { mutableClone.IsNew = true; @@ -43,7 +43,7 @@ namespace LeanCloud.Storage.Internal { return serverState; } - public async Task LogInAsync(string authType, IDictionary data, bool failOnNotExist, CancellationToken cancellationToken) { + public async Task LogInAsync(string authType, IDictionary data, bool failOnNotExist) { var authData = new Dictionary { [authType] = data }; @@ -55,7 +55,7 @@ namespace LeanCloud.Storage.Internal { { "authData", authData} } }; - var ret = await AVPlugins.Instance.CommandRunner.RunCommandAsync>(command, cancellationToken); + var ret = await AVPlugins.Instance.CommandRunner.RunCommandAsync>(command); var serverState = AVObjectCoder.Instance.Decode(ret.Item2, AVDecoder.Instance); serverState = serverState.MutatedClone(mutableClone => { mutableClone.IsNew = ret.Item1 == System.Net.HttpStatusCode.Created; diff --git a/Storage/Storage/Public/AVFile.cs b/Storage/Storage/Public/AVFile.cs index 004c49a..3828f1d 100644 --- a/Storage/Storage/Public/AVFile.cs +++ b/Storage/Storage/Public/AVFile.cs @@ -6,8 +6,7 @@ using System.Net.Http; using System.Threading; using System.Threading.Tasks; -namespace LeanCloud -{ +namespace LeanCloud { /// /// AVFile is a local representation of a file that is saved to the LeanCloud. /// @@ -24,8 +23,7 @@ namespace LeanCloud /// await obj.SaveAsync(); /// /// - public partial class AVFile : IJsonConvertible - { + public partial class AVFile : IJsonConvertible { internal static int objectCounter = 0; internal static readonly object Mutex = new object(); private FileState state; @@ -40,18 +38,15 @@ namespace LeanCloud /// 数据流 /// 文件类型 /// 文件源信息 - public AVFile(string name, Stream data, string mimeType = null, IDictionary metaData = null) - { + public AVFile(string name, Stream data, string mimeType = null, IDictionary metaData = null) { mimeType = mimeType == null ? GetMIMEType(name) : mimeType; - state = new FileState - { + state = new FileState { Name = name, MimeType = mimeType, MetaData = metaData }; this.dataStream = data; - lock (Mutex) - { + lock (Mutex) { objectCounter++; state.counter = objectCounter; } @@ -73,8 +68,7 @@ namespace LeanCloud /// 文件流数据 /// 文件类型 public AVFile(string name, Stream data, string mimeType = null) - : this(name, data, mimeType, new Dictionary()) - { + : this(name, data, mimeType, new Dictionary()) { } /// @@ -83,8 +77,7 @@ namespace LeanCloud /// 文件名 /// 文件的 byte[] 数据 public AVFile(string name, byte[] data) - : this(name, new MemoryStream(data), new Dictionary()) - { + : this(name, new MemoryStream(data), new Dictionary()) { } @@ -95,8 +88,7 @@ namespace LeanCloud /// 文件的 byte[] 数据 /// 元数据 public AVFile(string name, byte[] data, IDictionary metaData) - : this(name, new MemoryStream(data), metaData) - { + : this(name, new MemoryStream(data), metaData) { } @@ -107,8 +99,7 @@ namespace LeanCloud /// 文件的数据流 /// 元数据 public AVFile(string name, Stream data, IDictionary metaData) - : this(name, data, GetMIMEType(name), metaData) - { + : this(name, data, GetMIMEType(name), metaData) { } /// @@ -117,8 +108,7 @@ namespace LeanCloud /// 文件名 /// 文件的数据流 public AVFile(string name, Stream data) - : this(name, data, new Dictionary()) - { + : this(name, data, new Dictionary()) { } @@ -130,18 +120,15 @@ namespace LeanCloud /// 文件Uri /// 文件类型 /// 文件源信息 - public AVFile(string name, Uri uri, string mimeType = null, IDictionary metaData = null) - { + public AVFile(string name, Uri uri, string mimeType = null, IDictionary metaData = null) { mimeType = mimeType == null ? GetMIMEType(name) : mimeType; - state = new FileState - { + state = new FileState { Name = name, Url = uri, MetaData = metaData, MimeType = mimeType }; - lock (Mutex) - { + lock (Mutex) { objectCounter++; state.counter = objectCounter; } @@ -156,8 +143,7 @@ namespace LeanCloud /// 文件类型 /// 文件源信息 public AVFile(string name, string url, string mimeType = null, IDictionary metaData = null) - : this(name, new Uri(url), mimeType, metaData) - { + : this(name, new Uri(url), mimeType, metaData) { } @@ -168,8 +154,7 @@ namespace LeanCloud /// 文件 Url /// 文件源信息 public AVFile(string name, string url, IDictionary metaData) - : this(name, url, null, metaData) - { + : this(name, url, null, metaData) { } /// @@ -179,8 +164,7 @@ namespace LeanCloud /// 文件 Uri /// 文件类型 public AVFile(string name, Uri uri, string mimeType = null) - : this(name, uri, mimeType, new Dictionary()) - { + : this(name, uri, mimeType, new Dictionary()) { } @@ -190,8 +174,7 @@ namespace LeanCloud /// 文件名 /// 文件 Uri public AVFile(string name, Uri uri) - : this(name, uri, null, new Dictionary()) - { + : this(name, uri, null, new Dictionary()) { } /// @@ -200,20 +183,16 @@ namespace LeanCloud /// 文件名 /// 文件的 Url public AVFile(string name, string url) - : this(name, new Uri(url)) - { + : this(name, new Uri(url)) { } - internal AVFile(FileState filestate) - { + internal AVFile(FileState filestate) { this.state = filestate; } internal AVFile(string objectId) - : this(new FileState() - { + : this(new FileState() { ObjectId = objectId - }) - { + }) { } #endregion @@ -225,10 +204,8 @@ namespace LeanCloud /// /// Gets whether the file still needs to be saved. /// - public bool IsDirty - { - get - { + public bool IsDirty { + get { return state.Url == null; } } @@ -238,10 +215,8 @@ namespace LeanCloud /// the user. After save is called, that name gets prefixed with a unique identifier. /// [AVFieldName("name")] - public string Name - { - get - { + public string Name { + get { return state.Name; } } @@ -251,10 +226,8 @@ namespace LeanCloud /// inferred from the file extension. "unknown/unknown" will be used if neither is /// available. /// - public string MimeType - { - get - { + public string MimeType { + get { return state.MimeType; } } @@ -264,28 +237,22 @@ namespace LeanCloud /// you get the file from a . /// [AVFieldName("url")] - public Uri Url - { - get - { + public Uri Url { + get { return state.Url; } } - internal static AVFileController FileController - { - get - { + internal static AVFileController FileController { + get { return AVPlugins.Instance.FileController; } } #endregion - IDictionary IJsonConvertible.ToJSON() - { - if (this.IsDirty) - { + IDictionary IJsonConvertible.ToJSON() { + if (this.IsDirty) { throw new InvalidOperationException( "AVFile must be saved before it can be serialized."); } @@ -302,8 +269,7 @@ namespace LeanCloud /// /// Saves the file to the LeanCloud cloud. /// - public Task SaveAsync() - { + public Task SaveAsync() { return SaveAsync(null, CancellationToken.None); } @@ -311,8 +277,7 @@ namespace LeanCloud /// Saves the file to the LeanCloud cloud. /// /// The cancellation token. - public Task SaveAsync(CancellationToken cancellationToken) - { + public Task SaveAsync(CancellationToken cancellationToken) { return SaveAsync(null, cancellationToken); } @@ -320,8 +285,7 @@ namespace LeanCloud /// Saves the file to the LeanCloud cloud. /// /// The progress callback. - public Task SaveAsync(IProgress progress) - { + public Task SaveAsync(IProgress progress) { return SaveAsync(progress, CancellationToken.None); } @@ -331,21 +295,18 @@ namespace LeanCloud /// The progress callback. /// The cancellation token. public Task SaveAsync(IProgress progress, - CancellationToken cancellationToken) - { + CancellationToken cancellationToken) { if (this.isExternal) return this.SaveExternal(); return taskQueue.Enqueue( toAwait => FileController.SaveAsync(state, dataStream, AVUser.CurrentUser.SessionToken, progress, cancellationToken), cancellationToken) - .OnSuccess(t => - { + .OnSuccess(t => { state = t.Result; }); } - internal Task SaveExternal() - { + internal Task SaveExternal() { Dictionary strs = new Dictionary() { { "url", this.Url.ToString() }, @@ -369,8 +330,7 @@ namespace LeanCloud }; } - return AVPlugins.Instance.CommandRunner.RunCommandAsync>(cmd).OnSuccess(t => - { + return AVPlugins.Instance.CommandRunner.RunCommandAsync>(cmd).OnSuccess(t => { var result = t.Result.Item2; state.ObjectId = result["objectId"].ToString(); }); @@ -385,13 +345,10 @@ namespace LeanCloud /// /// 文件在 LeanCloud 的唯一Id 标识 /// - public string ObjectId - { - get - { + public string ObjectId { + get { String str; - lock (this.mutex) - { + lock (this.mutex) { str = state.ObjectId; } return str; @@ -401,10 +358,8 @@ namespace LeanCloud /// /// 文件的元数据。 /// - public IDictionary MetaData - { - get - { + public IDictionary MetaData { + get { return state.MetaData; } } @@ -414,16 +369,13 @@ namespace LeanCloud /// /// /// - public bool IsExternal - { - get - { + public bool IsExternal { + get { return isExternal; } } - static AVFile() - { + static AVFile() { Dictionary strs = new Dictionary() { { "ai", "application/postscript" }, @@ -617,19 +569,14 @@ namespace LeanCloud }; AVFile.MIMETypesDictionary = strs; } - internal static string GetMIMEType(string fileName) - { - try - { + internal static string GetMIMEType(string fileName) { + try { string str = Path.GetExtension(fileName).Remove(0, 1); - if (!AVFile.MIMETypesDictionary.ContainsKey(str)) - { + if (!AVFile.MIMETypesDictionary.ContainsKey(str)) { return "unknown/unknown"; } return AVFile.MIMETypesDictionary[str]; - } - catch - { + } catch { return "unknown/unknown"; } } @@ -639,28 +586,23 @@ namespace LeanCloud /// /// 获取之后并没有实际执行下载,只是加载了文件的元信息以及物理地址(Url) /// - public static Task GetFileWithObjectIdAsync(string objectId, CancellationToken cancellationToken) - { + public static Task GetFileWithObjectIdAsync(string objectId, CancellationToken cancellationToken) { string currentSessionToken = AVUser.CurrentUser.SessionToken; - return FileController.GetAsync(objectId, currentSessionToken, cancellationToken).OnSuccess(_ => - { + return FileController.GetAsync(objectId, currentSessionToken, cancellationToken).OnSuccess(_ => { var filestate = _.Result; return new AVFile(filestate); }); } - public static AVFile CreateWithoutData(string objectId) - { + public static AVFile CreateWithoutData(string objectId) { return new AVFile(objectId); } - public static AVFile CreateWithState(FileState state) - { + public static AVFile CreateWithState(FileState state) { return new AVFile(state); } - public static AVFile CreateWithData(string objectId,string name, string url,IDictionary metaData) - { + public static AVFile CreateWithData(string objectId, string name, string url, IDictionary metaData) { var fileState = new FileState(); fileState.Name = name; fileState.ObjectId = objectId; @@ -673,23 +615,18 @@ namespace LeanCloud /// /// 获取之后并没有实际执行下载,只是加载了文件的元信息以及物理地址(Url) /// - public static Task GetFileWithObjectIdAsync(string objectId) - { + public static Task GetFileWithObjectIdAsync(string objectId) { return GetFileWithObjectIdAsync(objectId, CancellationToken.None); } - internal void MergeFromJSON(IDictionary jsonData) - { - lock (this.mutex) - { + internal void MergeFromJSON(IDictionary jsonData) { + lock (this.mutex) { state.ObjectId = jsonData["objectId"] as string; state.Url = new Uri(jsonData["url"] as string, UriKind.Absolute); - if (jsonData.ContainsKey("name")) - { + if (jsonData.ContainsKey("name")) { state.Name = jsonData["name"] as string; } - if (jsonData.ContainsKey("metaData")) - { + if (jsonData.ContainsKey("metaData")) { state.MetaData = jsonData["metaData"] as Dictionary; } @@ -700,32 +637,25 @@ namespace LeanCloud /// 删除文件 /// /// Task - public Task DeleteAsync() - { + public Task DeleteAsync() { return DeleteAsync(CancellationToken.None); } - internal Task DeleteAsync(CancellationToken cancellationToken) - { + internal Task DeleteAsync(CancellationToken cancellationToken) { return taskQueue.Enqueue(toAwait => DeleteAsync(toAwait, cancellationToken), cancellationToken); } - internal Task DeleteAsync(Task toAwait, CancellationToken cancellationToken) - { - if (ObjectId == null) - { + internal Task DeleteAsync(Task toAwait, CancellationToken cancellationToken) { + if (ObjectId == null) { return Task.FromResult(0); } string sessionToken = AVUser.CurrentUser.SessionToken; - return toAwait.OnSuccess(_ => - { + return toAwait.OnSuccess(_ => { return FileController.DeleteAsync(state, sessionToken, cancellationToken); }).Unwrap().OnSuccess(_ => { }); } #endregion } - - } diff --git a/Storage/Storage/Public/AVUser.cs b/Storage/Storage/Public/AVUser.cs index 5149688..7a7f910 100644 --- a/Storage/Storage/Public/AVUser.cs +++ b/Storage/Storage/Public/AVUser.cs @@ -108,7 +108,7 @@ namespace LeanCloud { } /// - /// 手机号。 + /// 手机号 /// [AVFieldName("mobilePhoneNumber")] public string MobilePhoneNumber { @@ -143,29 +143,31 @@ namespace LeanCloud { } } - internal async Task SignUpAsync(Task toAwait, CancellationToken cancellationToken) { - await Create(toAwait, cancellationToken); - CurrentUser = this; - } - /// - /// Signs up a new user. This will create a new AVUser on the server and will also persist the - /// session on disk so that you can access the user using . A username and - /// password must be set before calling SignUpAsync. + /// 注册新用户,注册成功后将保存为当前用户。必须设置用户名和密码。 /// - public Task SignUpAsync() { - return SignUpAsync(CancellationToken.None); - } + /// + public async Task SignUpAsync() { + if (AuthData == null) { + if (string.IsNullOrEmpty(Username)) { + throw new InvalidOperationException("Cannot sign up user with an empty name."); + } + if (string.IsNullOrEmpty(Password)) { + throw new InvalidOperationException("Cannot sign up user with an empty password."); + } + } + if (!string.IsNullOrEmpty(ObjectId)) { + throw new InvalidOperationException("Cannot sign up a user that already exists."); + } - /// - /// Signs up a new user. This will create a new AVUser on the server and will also persist the - /// session on disk so that you can access the user using . A username and - /// password must be set before calling SignUpAsync. - /// - /// The cancellation token. - public Task SignUpAsync(CancellationToken cancellationToken) { - return taskQueue.Enqueue(toAwait => SignUpAsync(toAwait, cancellationToken), - cancellationToken); + IDictionary currentOperations = StartSave(); + try { + var serverState = await UserController.SignUpAsync(State, currentOperations); + HandleSave(serverState); + CurrentUser = this; + } catch (Exception) { + HandleFailedSave(currentOperations); + } } #region 事件流系统相关 API @@ -187,7 +189,7 @@ namespace LeanCloud { /// public Task FollowAsync(string userObjectId, IDictionary data) { if (data != null) { - data = this.EncodeForSaving(data); + data = EncodeForSaving(data); } var command = new AVCommand { Path = $"users/{ObjectId}/friendship/{userObjectId}", @@ -329,21 +331,19 @@ namespace LeanCloud { /// /// 更新用户的密码,需要用户的旧密码 /// - /// Old password. - /// New password. + /// 旧密码 + /// 新密码 public async Task UpdatePasswordAsync(string oldPassword, string newPassword) { IObjectState state = await UserController.UpdatePasswordAsync(ObjectId, oldPassword, newPassword); HandleFetchResult(state); } /// - /// Gets the authData for this user. + /// 用户数据 /// internal IDictionary> AuthData { get { - IDictionary> authData; - if (this.TryGetValue>>( - "authData", out authData)) { + if (TryGetValue("authData", out IDictionary> authData)) { return authData; } return null; @@ -354,8 +354,7 @@ namespace LeanCloud { } private static IAVAuthenticationProvider GetProvider(string providerName) { - IAVAuthenticationProvider provider; - if (authProviders.TryGetValue(providerName, out provider)) { + if (authProviders.TryGetValue(providerName, out IAVAuthenticationProvider provider)) { return provider; } return null; @@ -407,29 +406,27 @@ namespace LeanCloud { if (authData == null || provider == null) { return; } - IDictionary data; - if (authData.TryGetValue(provider.AuthType, out data)) { + if (authData.TryGetValue(provider.AuthType, out IDictionary data)) { restorationSuccess = provider.RestoreAuthentication(data); } } if (!restorationSuccess) { - this.UnlinkFromAsync(provider.AuthType, CancellationToken.None); + UnlinkFromAsync(provider.AuthType, CancellationToken.None); } } - public Task LinkWithAsync(string authType, IDictionary data, CancellationToken cancellationToken) { - return taskQueue.Enqueue(toAwait => { - AuthData = new Dictionary>(); - AuthData[authType] = data; - return SaveAsync(cancellationToken); - }, cancellationToken); + public Task LinkWithAsync(string authType, IDictionary data) { + AuthData = new Dictionary> { + [authType] = data + }; + return SaveAsync(); } public Task LinkWithAsync(string authType, CancellationToken cancellationToken) { var provider = GetProvider(authType); return provider.AuthenticateAsync(cancellationToken) - .OnSuccess(t => LinkWithAsync(authType, t.Result, cancellationToken)) + .OnSuccess(t => LinkWithAsync(authType, t.Result)) .Unwrap(); } @@ -437,7 +434,7 @@ namespace LeanCloud { /// Unlinks a user from a service. /// public Task UnlinkFromAsync(string authType, CancellationToken cancellationToken) { - return LinkWithAsync(authType, null, cancellationToken); + return LinkWithAsync(authType, null); } /// @@ -449,12 +446,8 @@ namespace LeanCloud { } } - internal static async Task LogInWithAsync(string authType, - IDictionary data, - bool failOnNotExist, - CancellationToken cancellationToken) { - - var ret = await UserController.LogInAsync(authType, data, failOnNotExist, cancellationToken); + internal static async Task LogInWithAsync(string authType, IDictionary data, bool failOnNotExist) { + var ret = await UserController.LogInAsync(authType, data, failOnNotExist); AVUser user = FromState(ret, "_User"); user.AuthData = new Dictionary>(); user.AuthData[authType] = data; @@ -463,17 +456,16 @@ namespace LeanCloud { return CurrentUser; } - internal static Task LogInWithAsync(string authType, - CancellationToken cancellationToken) { + internal static Task LogInWithAsync(string authType) { var provider = GetProvider(authType); - return provider.AuthenticateAsync(cancellationToken) - .OnSuccess(authData => LogInWithAsync(authType, authData.Result, false, cancellationToken)) + return provider.AuthenticateAsync(CancellationToken.None) + .OnSuccess(authData => LogInWithAsync(authType, authData.Result, false)) .Unwrap(); } internal static void RegisterProvider(IAVAuthenticationProvider provider) { authProviders[provider.AuthType] = provider; - var curUser = AVUser.CurrentUser; + var curUser = CurrentUser; if (curUser != null) { curUser.SynchronizeAuthData(provider); } @@ -482,8 +474,8 @@ namespace LeanCloud { #region 手机号登录 internal static async Task LogInWithParametersAsync(Dictionary strs) { - var ret = await UserController.LogInWithParametersAsync("login", strs); - var user = CreateWithoutData(null); + IObjectState ret = await UserController.LogInWithParametersAsync("login", strs); + AVUser user = CreateWithoutData(null); user.HandleFetchResult(ret); CurrentUser = user; return CurrentUser; @@ -523,7 +515,7 @@ namespace LeanCloud { /// 手机号 /// 短信验证码 /// - public static Task LogInBySmsCodeAsync(string mobilePhoneNumber, string smsCode) { + public static Task LogInByMobilePhoneNumberSmsCodeAsync(string mobilePhoneNumber, string smsCode) { Dictionary strs = new Dictionary { { "mobilePhoneNumber", mobilePhoneNumber }, { "smsCode", smsCode } @@ -532,44 +524,13 @@ namespace LeanCloud { } /// - /// Requests the login SMS code asynchronous. + /// 请求登录短信验证码 /// - /// The mobile phone number. - /// - public static Task RequestLogInSmsCodeAsync(string mobilePhoneNumber) { - return RequestLogInSmsCodeAsync(mobilePhoneNumber, CancellationToken.None); - } - - /// - /// Requests the login SMS code asynchronous. - /// - /// The mobile phone number. - /// Validate token. + /// 手机号 + /// 验证码 /// public static Task RequestLogInSmsCodeAsync(string mobilePhoneNumber, string validateToken) { - return RequestLogInSmsCodeAsync(mobilePhoneNumber, null, CancellationToken.None); - } - - /// - /// Requests the login SMS code asynchronous. - /// - /// The mobile phone number. - /// The cancellation token. - /// - public static Task RequestLogInSmsCodeAsync(string mobilePhoneNumber, CancellationToken cancellationToken) { - return RequestLogInSmsCodeAsync(mobilePhoneNumber, null, cancellationToken); - } - - /// - /// Requests the login SMS code asynchronous. - /// - /// The mobile phone number. - /// Validate token. - /// The cancellation token. - /// - public static Task RequestLogInSmsCodeAsync(string mobilePhoneNumber, string validateToken, CancellationToken cancellationToken) { - Dictionary strs = new Dictionary() - { + Dictionary strs = new Dictionary { { "mobilePhoneNumber", mobilePhoneNumber }, }; if (String.IsNullOrEmpty(validateToken)) { @@ -603,87 +564,15 @@ namespace LeanCloud { #endregion - #region 手机短信 - - /// - /// Send sign up sms code async. - /// - /// The sign up sms code async. - /// Mobile phone number. - public static Task SendSignUpSmsCodeAsync(string mobilePhoneNumber) { - return AVCloud.RequestSMSCodeAsync(mobilePhoneNumber); - } - - /// - /// Sign up by mobile phone async. - /// - /// The up by mobile phone async. - /// Mobile phone number. - /// Sms code. - public static Task SignUpByMobilePhoneAsync(string mobilePhoneNumber, string smsCode) { - return SignUpOrLogInByMobilePhoneAsync(mobilePhoneNumber, smsCode); - } - - /// - /// Send log in sms code async. - /// - /// The log in sms code async. - /// Mobile phone number. - public static Task SendLogInSmsCodeAsync(string mobilePhoneNumber) { - return RequestLogInSmsCodeAsync(mobilePhoneNumber); - } - - /// - /// Log in by mobile phone async. - /// - /// The in by mobile phone async. - /// Mobile phone number. - /// Sms code. - public static Task LogInByMobilePhoneAsync(string mobilePhoneNumber, string smsCode) { - return LogInBySmsCodeAsync(mobilePhoneNumber, smsCode); - } - - #endregion - #region 重置密码 /// /// 请求重置密码,需要传入注册时使用的手机号。 /// /// 注册时使用的手机号 + /// 图形验证码 /// - public static Task RequestPasswordResetBySmsCode(string mobilePhoneNumber) { - return RequestPasswordResetBySmsCode(mobilePhoneNumber, null, CancellationToken.None); - } - - /// - /// 请求重置密码,需要传入注册时使用的手机号。 - /// - /// 注册时使用的手机号 - /// cancellationToken - /// - public static Task RequestPasswordResetBySmsCode(string mobilePhoneNumber, CancellationToken cancellationToken) { - return RequestPasswordResetBySmsCode(mobilePhoneNumber, null, cancellationToken); - } - - /// - /// 请求重置密码,需要传入注册时使用的手机号。 - /// - /// 注册时使用的手机号 - /// Validate token. - /// - public static Task RequestPasswordResetBySmsCode(string mobilePhoneNumber, string validateToken) { - return RequestPasswordResetBySmsCode(mobilePhoneNumber, validateToken, CancellationToken.None); - } - - /// - /// 请求重置密码,需要传入注册时使用的手机号。 - /// - /// 注册时使用的手机号 - /// Validate token. - /// cancellationToken - /// - public static Task RequestPasswordResetBySmsCode(string mobilePhoneNumber, string validateToken, CancellationToken cancellationToken) { + public static Task RequestPasswordResetBySmsCode(string mobilePhoneNumber, string validateToken = null) { Dictionary strs = new Dictionary { { "mobilePhoneNumber", mobilePhoneNumber }, }; @@ -702,20 +591,9 @@ namespace LeanCloud { /// 通过验证码重置密码。 /// /// 新密码 - /// 6位数验证码 + /// 6 位数验证码 /// public static Task ResetPasswordBySmsCodeAsync(string newPassword, string smsCode) { - return ResetPasswordBySmsCodeAsync(newPassword, smsCode, CancellationToken.None); - } - - /// - /// 通过验证码重置密码。 - /// - /// 新密码 - /// 6位数验证码 - /// cancellationToken - /// - public static Task ResetPasswordBySmsCodeAsync(string newPassword, string smsCode, CancellationToken cancellationToken) { Dictionary strs = new Dictionary { { "password", newPassword } }; @@ -786,53 +664,6 @@ namespace LeanCloud { #endregion - #region in no-local-storage enviroment - - internal Task Create() { - return this.Create(CancellationToken.None); - } - internal Task Create(CancellationToken cancellationToken) { - return taskQueue.Enqueue(toAwait => Create(toAwait, cancellationToken), - cancellationToken); - } - - internal Task Create(Task toAwait, CancellationToken cancellationToken) { - if (AuthData == null) { - // TODO (hallucinogen): make an Extension of Task to create Task with exception/canceled. - if (string.IsNullOrEmpty(Username)) { - TaskCompletionSource tcs = new TaskCompletionSource(); - tcs.TrySetException(new InvalidOperationException("Cannot sign up user with an empty name.")); - return tcs.Task; - } - if (string.IsNullOrEmpty(Password)) { - TaskCompletionSource tcs = new TaskCompletionSource(); - tcs.TrySetException(new InvalidOperationException("Cannot sign up user with an empty password.")); - return tcs.Task; - } - } - if (!string.IsNullOrEmpty(ObjectId)) { - TaskCompletionSource tcs = new TaskCompletionSource(); - tcs.TrySetException(new InvalidOperationException("Cannot sign up a user that already exists.")); - return tcs.Task; - } - - IDictionary currentOperations = StartSave(); - - return toAwait.OnSuccess(_ => { - return UserController.SignUpAsync(State, currentOperations, cancellationToken); - }).Unwrap().ContinueWith(t => { - if (t.IsFaulted || t.IsCanceled) { - HandleFailedSave(currentOperations); - } else { - var serverState = t.Result; - HandleSave(serverState); - } - return t; - }).Unwrap(); - } - - #endregion - #region AVUser Extension public IDictionary> GetAuthData() { @@ -844,42 +675,32 @@ namespace LeanCloud { /// /// OAuth data, like {"accessToken":"xxxxxx"} /// auth platform,maybe "facebook"/"twiiter"/"weibo"/"weixin" .etc - /// /// - public static Task LogInWithAuthDataAsync(IDictionary data, - string platform, - AVUserAuthDataLogInOption options = null, - CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { + public static Task LogInWithAuthDataAsync(IDictionary data, string platform, AVUserAuthDataLogInOption options = null) { if (options == null) { options = new AVUserAuthDataLogInOption(); } - return LogInWithAsync(platform, data, options.FailOnNotExist, cancellationToken); + return LogInWithAsync(platform, data, options.FailOnNotExist); } public static Task LogInWithAuthDataAndUnionIdAsync( IDictionary authData, string platform, string unionId, - AVUserAuthDataLogInOption options = null, - CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { + AVUserAuthDataLogInOption options = null) { if (options == null) { options = new AVUserAuthDataLogInOption(); } MergeAuthData(authData, unionId, options); - return LogInWithAsync(platform, authData, options.FailOnNotExist, cancellationToken); + return LogInWithAsync(platform, authData, options.FailOnNotExist); } - public static Task LogInAnonymouslyAsync(CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { + public static Task LogInAnonymouslyAsync() { var data = new Dictionary { { "id", Guid.NewGuid().ToString() } }; var options = new AVUserAuthDataLogInOption(); - return LogInWithAuthDataAsync(data, "anonymous", options, cancellationToken); - } - - [Obsolete("please use LogInWithAuthDataAsync instead.")] - public static Task LogInWithAsync(string authType, IDictionary data, CancellationToken cancellationToken) { - return LogInWithAsync(authType, data, false, cancellationToken); + return LogInWithAuthDataAsync(data, "anonymous", options); } /// @@ -887,23 +708,21 @@ namespace LeanCloud { /// /// OAuth data, like {"accessToken":"xxxxxx"} /// auth platform,maybe "facebook"/"twiiter"/"weibo"/"weixin" .etc - /// /// - public Task AssociateAuthDataAsync(IDictionary data, string platform, CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { - return LinkWithAsync(platform, data, cancellationToken); + public Task AssociateAuthDataAsync(IDictionary data, string platform) { + return LinkWithAsync(platform, data); } public Task AssociateAuthDataAndUnionIdAsync( IDictionary authData, string platform, string unionId, - AVUserAuthDataLogInOption options = null, - CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { + AVUserAuthDataLogInOption options = null) { if (options == null) { options = new AVUserAuthDataLogInOption(); } MergeAuthData(authData, unionId, options); - return LinkWithAsync(platform, authData, cancellationToken); + return LinkWithAsync(platform, authData); } ///