using System; using System.Collections.Generic; using System.Threading.Tasks; using LeanCloud.Storage; using LeanCloud.LiveQuery; using TapTap.Common; using TapTap.Bootstrap.@internal; using TapTap.Login; using TapTap.Login.Internal; namespace TapTap.Bootstrap { public class TDSUser : LCUser { public new string Password { get { throw new NotImplementedException(); } set { throw new NotImplementedException(); } } public new string Email { get { throw new NotImplementedException(); } set { throw new NotImplementedException(); } } public new string Mobile { get { throw new NotImplementedException(); } set { throw new NotImplementedException(); } } public new bool EmailVerified { get { throw new NotImplementedException(); } } public new bool MobileVerified { get { throw new NotImplementedException(); } } public new Task SignUp() { throw new NotImplementedException(); } public static new Task RequestLoginSMSCode(string mobile) { throw new NotImplementedException(); } public static new Task SignUpOrLoginByMobilePhone(string mobile, string code) { throw new NotImplementedException(); } public static new Task Login(string username, string password) { throw new NotImplementedException(); } public static new Task LoginByEmail(string email, string password) { throw new NotImplementedException(); } public static new Task LoginByMobilePhoneNumber(string mobile, string password) { throw new NotImplementedException(); } public static new Task LoginBySMSCode(string mobile, string code) { throw new NotImplementedException(); } public static new Task RequestEmailVerify(string email) { throw new NotImplementedException(); } public static new Task RequestMobilePhoneVerify(string mobile) { throw new NotImplementedException(); } public static new Task VerifyMobilePhone(string mobile, string code) { throw new NotImplementedException(); } public static new Task RequestPasswordReset(string email) { throw new NotImplementedException(); } public static new Task RequestPasswordResetBySmsCode(string mobile) { throw new NotImplementedException(); } public static new Task ResetPasswordBySmsCode(string mobile, string code, string newPassword) { throw new NotImplementedException(); } public new Task UpdatePassword(string oldPassword, string newPassword) { throw new NotImplementedException(); } public static new Task RequestSMSCodeForUpdatingPhoneNumber(string mobile, int ttl = 360, string captchaToken = null) { throw new NotImplementedException(); } public static new Task VerifyCodeForUpdatingPhoneNumber(string mobile, string code) { throw new NotImplementedException(); } #region API /// /// Gets the currently logged in TDSUser with a valid session, from /// memory or disk if necessary. /// /// public static new async Task GetCurrent() { LCUser user = await LCUser.GetCurrent(); var result = user as TDSUser; TDSUserEventTrigger.TriggerLoginInfo(result); return result; } /// /// Signs in a user with a sessionToken. /// /// /// public static new async Task BecomeWithSessionToken(string sessionToken) { return (await LCUser.BecomeWithSessionToken(sessionToken)) as TDSUser; } /// /// Signs in a user with a sessionToken for XDSDK. /// /// /// public static async Task LoginWithSessionToken(string sessionToken) { var result = (await LCUser.BecomeWithSessionToken(sessionToken)) as TDSUser; TDSUserEventTrigger.TriggerLoginInfo(result); return result; } /// /// Signs up or signs in a user with third party authData. /// /// /// /// /// public static new async Task LoginWithAuthData(Dictionary authData, string platform, LCUserAuthDataLoginOption option = null) { var result = (await LCUser.LoginWithAuthData(authData, platform, option)) as TDSUser; TDSUserEventTrigger.TriggerLoginInfo(result); return result; } public new Task AssociateAuthData(Dictionary authData, string platform) { var result = base.AssociateAuthData(authData, platform); TDSUserEventTrigger.TriggerBindInfo(platform, ObjectId); return result; } public new Task AssociateAuthDataAndUnionId(Dictionary authData, string platform, string unionId, LCUserAuthDataLoginOption option = null) { var result = base.AssociateAuthDataAndUnionId(authData, platform, unionId, option); TDSUserEventTrigger.TriggerBindInfo(platform, ObjectId); return result; } public new Task DisassociateWithAuthData(string platform) { var result = base.DisassociateWithAuthData(platform); TDSUserEventTrigger.TriggerUnbindInfo(platform); return result; } /// /// Signs up or signs in a user with third party authData and unionId. /// /// /// /// /// /// public static new async Task LoginWithAuthDataAndUnionId(Dictionary authData, string platform, string unionId, LCUserAuthDataLoginOption option = null) { var result = (await LCUser.LoginWithAuthDataAndUnionId(authData, platform, unionId, option)) as TDSUser; TDSUserEventTrigger.TriggerLoginInfo(result); return result; } /// /// Creates an anonymous user. /// /// public static new async Task LoginAnonymously() { var result = (await LCUser.LoginAnonymously()) as TDSUser; TDSUserEventTrigger.TriggerLoginInfo(result); return result; } /// /// Signs up or signs in a user with TapTap. /// /// public static async Task LoginWithTapTap(string[] permissions = null) { Dictionary authData = await LoginTapTap(permissions); LCUser user = await LoginWithAuthData(authData, "taptap"); TDSUserEventTrigger.TriggerLoginInfo(user as TDSUser); return user as TDSUser; } /// /// Logs out the currently logged in user session. /// public static new async Task Logout() { TapLogin.Logout(); TDSUserEventTrigger.TriggerLogoutInfo(UnityTDSUser.TDS_CHANNEL); await LCUser.Logout(); } /// /// Constructs a LCQuery for TDSUser. /// /// public static new LCQuery GetQuery() { return new LCQuery(CLASS_NAME); } /// /// Save this user to the cloud. /// /// /// /// public new async Task Save(bool fetchWhenSave = false, LCQuery query = null) { return (await base.Save(fetchWhenSave, query)) as TDSUser; } #endregion private static async Task> LoginTapTap(string[] permissions) { AccessToken token; if (permissions == null) { token = await TapLogin.Login(); } else { token = await TapLogin.Login(permissions); } var profile = await TapLogin.GetProfile(); var result = new Dictionary { {"kid", token.kid}, {"access_token", token.accessToken}, {"token_type", token.tokenType}, {"mac_key", token.macKey}, {"mac_algorithm", token.macAlgorithm}, {"openid", profile.openid}, {"name", profile.name}, {"avatar", profile.avatar}, {"unionid", profile.unionid} }; return result; } #region Friendship public class FriendshipNotification { public Action OnNewRequestComing { get; set; } public Action OnRequestAccepted { get; set; } public Action OnRequestDeclined { get; set; } } private LCLiveQuery friendshipLivequery; public Task ApplyFriendship(TDSUser user, Dictionary attributes = null) { if (user == null || string.IsNullOrEmpty(user.ObjectId)) { throw new ArgumentNullException("User or userId is null."); } return ApplyFriendship(user.ObjectId, attributes); } public Task ApplyFriendship(string userId, Dictionary attributes = null) { if (string.IsNullOrEmpty(userId)) { throw new ArgumentNullException(nameof(userId)); } return LCFriendship.Request(userId, attributes); } public Task AcceptFriendshipRequest(LCFriendshipRequest request, Dictionary attributes = null) { if (request == null) { throw new ArgumentNullException(nameof(request)); } return LCFriendship.AcceptRequest(request, attributes); } public Task DeclineFriendshipRequest(LCFriendshipRequest request) { if (request == null) { throw new ArgumentNullException(nameof(request)); } return LCFriendship.DeclineRequest(request); } public Task DeleteFriendshipRequest(LCFriendshipRequest request) { if (request == null) { throw new ArgumentNullException(nameof(request)); } return request.Delete(); } public LCQuery GetFriendshipRequestQuery(int status, bool includeTargetUser, bool reachToCurrentUser) { List statusList = new List(); if ((status & LCFriendshipRequest.STATUS_PENDING) == LCFriendshipRequest.STATUS_PENDING) { statusList.Add("pending"); } if ((status & LCFriendshipRequest.STATUS_ACCEPTED) == LCFriendshipRequest.STATUS_ACCEPTED) { statusList.Add("accepted"); } if ((status & LCFriendshipRequest.STATUS_DECLINED) == LCFriendshipRequest.STATUS_DECLINED) { statusList.Add("declined"); } if (statusList.Count < 1) { throw new ArgumentException("status is invalid."); } LCQuery query = LCFriendshipRequest.GetQuery(); query.WhereContainedIn("status", statusList); if (reachToCurrentUser) { query.WhereEqualTo("friend", this); if (includeTargetUser) { query.Include("user"); } } else { query.WhereEqualTo("user", this); if (includeTargetUser) { query.Include("friend"); } } query.AddDescendingOrder("updatedAt"); return query; } public LCQuery GetFriendshipQuery() { return FriendshipQuery(); } public async Task RegisterFriendshipNotification(FriendshipNotification notification) { if (friendshipLivequery != null) { // 避免重复注册 return; } // 构建 LiveQuery LCQuery selfRequestQuery = new LCQuery(LCFriendshipRequest.CLASS_NAME) .WhereEqualTo("user", this); LCQuery otherRequestQuery = new LCQuery(LCFriendshipRequest.CLASS_NAME) .WhereEqualTo("friend", this); LCQuery allQuery = LCQuery.Or(new LCQuery[] { selfRequestQuery, otherRequestQuery }); friendshipLivequery = await allQuery.Subscribe(); friendshipLivequery.OnCreate = obj => { if (!(obj is LCFriendshipRequest req)) { return; } LCUser friend = req.Friend; if (friend == null || friend.ObjectId != ObjectId) { return; } notification.OnNewRequestComing(req); }; friendshipLivequery.OnUpdate = (obj, keys) => { if (!(obj is LCFriendshipRequest req)) { return; } if (keys == null || !keys.Contains("status")) { return; } LCUser user = req.User; if (user == null || user.ObjectId != ObjectId) { return; } string status = req.Status; if (status == "accepted") { notification.OnRequestAccepted(req); } else if (status == "declined") { notification.OnRequestDeclined(req); } }; } public async Task UnregisterFriendshipNotification() { if (friendshipLivequery == null) { return; } await friendshipLivequery.Unsubscribe(); friendshipLivequery = null; } #endregion } }