From 6969884bd93a1ab511d4d74182e037174da6c422 Mon Sep 17 00:00:00 2001 From: oneRain Date: Fri, 23 Aug 2019 16:16:18 +0800 Subject: [PATCH] =?UTF-8?q?*=20ObjectTest.cs:=20chore:=20=E6=95=B4?= =?UTF-8?q?=E7=90=86=20AVUser=20=E6=8E=A5=E5=8F=A3=EF=BC=8C=E5=B9=B6?= =?UTF-8?q?=E7=BC=96=E5=86=99=E7=AE=80=E5=8D=95=E6=B5=8B=E8=AF=95=E7=94=A8?= =?UTF-8?q?=E4=BE=8B=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * UserTest.cs: * AVUser.cs: * AVQuery.cs: * AVObject.cs: * MD5.cs: * AVCloudCodeController.cs: --- Storage/Storage.Test/ObjectTest.cs | 16 +- Storage/Storage.Test/UserTest.cs | 40 ++++ .../Cloud/Controller/AVCloudCodeController.cs | 6 +- .../Internal/File/Cryptography/MD5/MD5.cs | 10 +- Storage/Storage/Public/AVObject.cs | 2 +- Storage/Storage/Public/AVQuery.cs | 11 +- Storage/Storage/Public/AVUser.cs | 187 +++++++++--------- 7 files changed, 166 insertions(+), 106 deletions(-) diff --git a/Storage/Storage.Test/ObjectTest.cs b/Storage/Storage.Test/ObjectTest.cs index 49bb1ca..161eebb 100644 --- a/Storage/Storage.Test/ObjectTest.cs +++ b/Storage/Storage.Test/ObjectTest.cs @@ -3,7 +3,6 @@ using LeanCloud; using System.Net.Http; using System.Threading; using System.Threading.Tasks; -using System.Runtime.CompilerServices; namespace LeanCloudTests { public class ObjectTests { @@ -12,6 +11,7 @@ namespace LeanCloudTests { AVClient.Initialize(new AVClient.Configuration { ApplicationId = "BMYV4RKSTwo8WSqt8q9ezcWF-gzGzoHsz", ApplicationKey = "pbf6Nk5seyjilexdpyrPwjSp", + ApiServer = "https://avoscloud.com", RTMServer = "https://router-g0-push.avoscloud.com", }); AVClient.HttpLog(TestContext.Out.WriteLine); @@ -54,5 +54,19 @@ namespace LeanCloudTests { Assert.Pass(); } + [Test] + public async Task TestMassiveRequest() { + await Task.Run(() => { + for (int i = 0; i < 10; i++) { + for (int j = 0; j < 50; j++) { + AVObject obj = AVObject.Create("Foo"); + obj.SaveAsync().ContinueWith(_ => { + TestContext.Out.WriteLine($"{obj.ObjectId} saved"); + }); + } + Thread.Sleep(1000); + } + }); + } } } \ No newline at end of file diff --git a/Storage/Storage.Test/UserTest.cs b/Storage/Storage.Test/UserTest.cs index 024b93b..4c6a1a6 100644 --- a/Storage/Storage.Test/UserTest.cs +++ b/Storage/Storage.Test/UserTest.cs @@ -70,5 +70,45 @@ namespace LeanCloudTests { await user.UpdatePasswordAsync("111111", "222222"); await user.UpdatePasswordAsync("222222", "111111"); } + + [Test] + public async Task LoginWithAuthData() { + AVUser user = await AVUser.LogInWithAuthDataAsync(new Dictionary { + { "openid", "0395BA18A5CD6255E5BA185E7BEBA242" }, + { "access_token", "12345678-SaMpLeTuo3m2avZxh5cjJmIrAfx4ZYyamdofM7IjU" }, + { "expires_in", 1382686496 } + }, "qq"); + Assert.NotNull(user.SessionToken); + TestContext.Out.WriteLine(user.SessionToken); + } + + [Test] + public async Task AssociateAuthData() { + AVUser user = await AVUser.LogInAsync("111111", "111111"); + Assert.NotNull(user.SessionToken); + await user.AssociateAuthDataAsync(new Dictionary { + { "openid", "0395BA18A5CD6255E5BA185E7BEBA243" }, + { "access_token", "12345678-SaMpLeTuo3m2avZxh5cjJmIrAfx4ZYyamdofM7IjU" }, + { "expires_in", 1382686496 } + }, "qq"); + } + + [Test] + public async Task Anonymously() { + AVUser user = await AVUser.LogInAnonymouslyAsync(); + Assert.NotNull(user.SessionToken); + TestContext.Out.WriteLine(user.SessionToken); + } + + [Test] + public async Task GetRoles() { + AVUser user = await AVUser.LogInAsync("111111", "111111"); + Assert.NotNull(user.SessionToken); + IEnumerable roles = await user.GetRolesAsync(); + Assert.Greater(roles.Count(), 0); + foreach (AVRole role in roles) { + TestContext.Out.WriteLine(role.Name); + } + } } } diff --git a/Storage/Storage/Internal/Cloud/Controller/AVCloudCodeController.cs b/Storage/Storage/Internal/Cloud/Controller/AVCloudCodeController.cs index 0777066..ae1fc6d 100644 --- a/Storage/Storage/Internal/Cloud/Controller/AVCloudCodeController.cs +++ b/Storage/Storage/Internal/Cloud/Controller/AVCloudCodeController.cs @@ -7,7 +7,7 @@ using System.Net.Http; namespace LeanCloud.Storage.Internal { public class AVCloudCodeController { - public Task CallFunctionAsync(String name, + public Task CallFunctionAsync(string name, IDictionary parameters, string sessionToken, CancellationToken cancellationToken) { @@ -19,7 +19,7 @@ namespace LeanCloud.Storage.Internal { return AVPlugins.Instance.CommandRunner.RunCommandAsync>(command, cancellationToken: cancellationToken).OnSuccess(t => { var decoded = AVDecoder.Instance.Decode(t.Result.Item2) as IDictionary; if (!decoded.ContainsKey("result")) { - return default(T); + return default; } return Conversion.To(decoded["result"]); }); @@ -34,7 +34,7 @@ namespace LeanCloud.Storage.Internal { return AVPlugins.Instance.CommandRunner.RunCommandAsync>(command, cancellationToken: cancellationToken).OnSuccess(t => { var decoded = AVDecoder.Instance.Decode(t.Result.Item2) as IDictionary; if (!decoded.ContainsKey("result")) { - return default(T); + return default; } return Conversion.To(decoded["result"]); }); diff --git a/Storage/Storage/Internal/File/Cryptography/MD5/MD5.cs b/Storage/Storage/Internal/File/Cryptography/MD5/MD5.cs index aa3eb2b..53efa04 100644 --- a/Storage/Storage/Internal/File/Cryptography/MD5/MD5.cs +++ b/Storage/Storage/Internal/File/Cryptography/MD5/MD5.cs @@ -75,11 +75,11 @@ namespace LeanCloud.Storage.Internal private const byte S42 = 10; private const byte S43 = 15; private const byte S44 = 21; - static private byte[] PADDING = new byte[] { - 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 - }; + private static readonly byte[] PADDING = { + 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 + }; #endregion #region F, G, H and I are basic MD5 functions. diff --git a/Storage/Storage/Public/AVObject.cs b/Storage/Storage/Public/AVObject.cs index c2addcb..cffad57 100644 --- a/Storage/Storage/Public/AVObject.cs +++ b/Storage/Storage/Public/AVObject.cs @@ -570,7 +570,7 @@ string propertyName // Get the JSON representation of the object. currentOperations = StartSave(); - sessionToken = AVUser.CurrentUser.SessionToken; + sessionToken = AVUser.CurrentUser?.SessionToken; deepSaveTask = DeepSaveAsync(estimatedData, sessionToken, cancellationToken); } diff --git a/Storage/Storage/Public/AVQuery.cs b/Storage/Storage/Public/AVQuery.cs index ca27a60..d5648bf 100644 --- a/Storage/Storage/Public/AVQuery.cs +++ b/Storage/Storage/Public/AVQuery.cs @@ -654,10 +654,13 @@ namespace LeanCloud internal AVQuery WhereRelatedTo(AVObject parent, string key) { MergeWhereClauses(new Dictionary { - { key, new Dictionary{ { "$relatedTo", new Dictionary { - { "object", parent}, - { "key", key} - } } } } + { + "$relatedTo", + new Dictionary { + { "object", parent }, + { "key", key } + } + } }); return this; } diff --git a/Storage/Storage/Public/AVUser.cs b/Storage/Storage/Public/AVUser.cs index 465397f..3462624 100644 --- a/Storage/Storage/Public/AVUser.cs +++ b/Storage/Storage/Public/AVUser.cs @@ -269,96 +269,6 @@ namespace LeanCloud { #endregion - - #region 第三方登录 - - /// - /// 使用第三方数据注册;如果已经存在相同的 Auth Data,则执行登录 - /// - /// Auth Data - /// 平台 - /// - /// - public static Task LogInWithAuthDataAsync(IDictionary authData, string platform, AVUserAuthDataLogInOption options = null) { - if (options == null) { - options = new AVUserAuthDataLogInOption(); - } - return LogInWithAsync(platform, authData, options.FailOnNotExist); - } - - /// - /// 使用第三方数据注册; - /// - /// Auth data - /// 平台标识 - /// - /// - /// - public static Task LogInWithAuthDataAndUnionIdAsync(IDictionary authData, string platform, string unionId, - AVUserAuthDataLogInOption options = null) { - if (options == null) { - options = new AVUserAuthDataLogInOption(); - } - MergeAuthData(authData, unionId, options); - return LogInWithAsync(platform, authData, options.FailOnNotExist); - } - - /// - /// 绑定第三方登录 - /// - /// Auth data - /// 平台标识 - /// - public Task AssociateAuthDataAsync(IDictionary authData, string platform) { - return LinkWithAsync(platform, authData); - } - - /// - /// 绑定第三方登录 - /// - /// OAuth data - /// 平台标识 - /// - /// - /// - public Task AssociateAuthDataAndUnionIdAsync(IDictionary authData, string platform, string unionId, - AVUserAuthDataLogInOption options = null) { - if (options == null) { - options = new AVUserAuthDataLogInOption(); - } - MergeAuthData(authData, unionId, options); - return LinkWithAsync(platform, authData); - } - - /// - /// 解绑第三方登录 - /// - /// 平台标识 - /// - public Task DisassociateWithAuthDataAsync(string platform) { - return LinkWithAsync(platform, null); - } - - #endregion - - - public Task LinkWithAsync(string authType, IDictionary data) { - AuthData = new Dictionary> { - [authType] = data - }; - return SaveAsync(); - } - - 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> { - [authType] = data - }; - CurrentUser = user; - return CurrentUser; - } - #region 手机号登录 /// @@ -430,6 +340,77 @@ namespace LeanCloud { #endregion + #region 第三方登录 + + /// + /// 使用第三方数据注册;如果已经存在相同的 Auth Data,则执行登录 + /// + /// Auth Data + /// 平台 + /// + /// + public static Task LogInWithAuthDataAsync(IDictionary authData, string platform, AVUserAuthDataLogInOption options = null) { + if (options == null) { + options = new AVUserAuthDataLogInOption(); + } + return LogInWithAsync(platform, authData, options.FailOnNotExist); + } + + /// + /// 使用第三方数据注册; + /// + /// Auth data + /// 平台标识 + /// + /// + /// + public static Task LogInWithAuthDataAndUnionIdAsync(IDictionary authData, string platform, string unionId, + AVUserAuthDataLogInOption options = null) { + if (options == null) { + options = new AVUserAuthDataLogInOption(); + } + MergeAuthData(authData, unionId, options); + return LogInWithAsync(platform, authData, options.FailOnNotExist); + } + + /// + /// 绑定第三方登录 + /// + /// Auth data + /// 平台标识 + /// + public Task AssociateAuthDataAsync(IDictionary authData, string platform) { + return LinkWithAuthDataAsync(platform, authData); + } + + /// + /// 绑定第三方登录 + /// + /// Auth data + /// 平台标识 + /// + /// + /// + public Task AssociateAuthDataAndUnionIdAsync(IDictionary authData, string platform, string unionId, + AVUserAuthDataLogInOption options = null) { + if (options == null) { + options = new AVUserAuthDataLogInOption(); + } + MergeAuthData(authData, unionId, options); + return LinkWithAuthDataAsync(platform, authData); + } + + /// + /// 解绑第三方登录 + /// + /// 平台标识 + /// + public Task DisassociateWithAuthDataAsync(string platform) { + return LinkWithAuthDataAsync(platform, null); + } + + #endregion + #region 重置密码 /// @@ -552,7 +533,7 @@ namespace LeanCloud { /// /// 关注某个用户 /// - /// 被关注的用户Id + /// 被关注的用户 Id /// 关注的时候附加属性 /// public Task FollowAsync(string userObjectId, IDictionary data) { @@ -570,7 +551,7 @@ namespace LeanCloud { /// /// 取关某一个用户 /// - /// + /// 用户 Id /// public Task UnfollowAsync(string userObjectId) { var command = new AVCommand { @@ -631,6 +612,28 @@ namespace LeanCloud { #endregion + public Task> GetRolesAsync() { + AVQuery query = new AVQuery(); + query.WhereRelatedTo(this, "users"); + return query.FindAsync(); + } + + Task LinkWithAuthDataAsync(string authType, IDictionary data) { + AuthData = new Dictionary> { + [authType] = data + }; + return SaveAsync(); + } + + 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> { + [authType] = data + }; + CurrentUser = user; + return CurrentUser; + } internal static async Task LogInWithParametersAsync(Dictionary strs) { IObjectState ret = await UserController.LogInWithParametersAsync("login", strs);