public static LCUser GenerateUser(LCObjectData objectData) { LCUser user = Create(CLASS_NAME) as LCUser; user.Merge(objectData); return(user); }
/// <summary> /// Set whether the given user is allowed to write this object. /// </summary> /// <param name="user">The user.</param> /// <param name="value">Whether the user has permission.</param> public void SetUserWriteAccess(LCUser user, bool value) { if (user == null) { throw new ArgumentNullException(nameof(user)); } SetUserIdWriteAccess(user.ObjectId, value); }
/// <summary> /// Detects whether the given user is *explicitly* allowed to write this /// object. Even if this returns false, the user may still be able to write /// it if <see cref="PublicReadAccess"/> is true or a role that the user /// belongs to has write access. /// </summary> /// <param name="userId">The user to check.</param> /// <returns></returns> public bool GetUserWriteAccess(LCUser user) { if (user == null) { throw new ArgumentNullException(nameof(user)); } return(GetUserIdWriteAccess(user.ObjectId)); }
static async Task <LCUser> Login(Dictionary <string, object> data) { Dictionary <string, object> response = await LeanCloud.HttpClient.Post <Dictionary <string, object> >("login", data : data); LCObjectData objectData = LCObjectData.Decode(response); currentUser = new LCUser(objectData); return(currentUser); }
public new async Task <LCUser> Save(bool fetchWhenSave = false, LCQuery <LCObject> query = null) { await base.Save(fetchWhenSave, query); currentUser = this; await SaveToLocal(); return(this); }
/// <summary> /// Get the rankings that around the currently logged in user. /// </summary> /// <param name="version"></param> /// <param name="skip"></param> /// <param name="limit"></param> /// <param name="selectUserKeys"></param> /// <param name="includeStatistics"></param> /// <returns></returns> public async Task <ReadOnlyCollection <LCRanking> > GetResultsAroundUser(int version = -1, int skip = 0, int limit = 10, IEnumerable <string> selectUserKeys = null, IEnumerable <string> includeStatistics = null) { LCUser user = await LCUser.GetCurrent(); return(await GetResults(user, version, skip, limit, selectUserKeys, includeStatistics)); }
static async Task <LCUser> Login(Dictionary <string, object> data) { Dictionary <string, object> response = await LCCore.HttpClient.Post <Dictionary <string, object> >("login", data : data); LCObjectData objectData = LCObjectData.Decode(response); currentUser = GenerateUser(objectData); await SaveToLocal(); return(currentUser); }
/// <summary> /// Creates a LCACL that is allowed to read and write this object /// for the user. /// </summary> /// <param name="owner">The user.</param> /// <returns></returns> public static LCACL CreateWithOwner(LCUser owner) { if (owner == null) { throw new ArgumentNullException(nameof(owner)); } LCACL acl = new LCACL(); acl.SetUserReadAccess(owner, true); acl.SetUserWriteAccess(owner, true); return(acl); }
/// <summary> /// Deletes the statistics of the user with the given name. /// </summary> /// <param name="user"></param> /// <param name="statisticNames"></param> /// <returns></returns> public static async Task DeleteStatistics(LCUser user, IEnumerable <string> statisticNames) { if (user == null) { throw new ArgumentNullException(nameof(user)); } if (statisticNames == null || statisticNames.Count() == 0) { throw new ArgumentNullException(nameof(statisticNames)); } string names = string.Join(",", statisticNames); string path = $"leaderboard/users/{user.ObjectId}/statistics?statistics={names}"; await LCCore.HttpClient.Delete(path); }
static async Task <LCUser> LoginWithAuthData(string authType, Dictionary <string, object> data, bool failOnNotExist) { Dictionary <string, object> authData = new Dictionary <string, object> { { authType, data } }; string path = failOnNotExist ? "users?failOnNotExist=true" : "users"; Dictionary <string, object> response = await LeanCloud.HttpClient.Post <Dictionary <string, object> >(path, data : new Dictionary <string, object> { { "authData", authData } }); LCObjectData objectData = LCObjectData.Decode(response); currentUser = new LCUser(objectData); return(currentUser); }
/// <summary> /// 设置当前用户 /// </summary> /// <param name="sessionToken"></param> /// <returns></returns> public static async Task <LCUser> BecomeWithSessionToken(string sessionToken) { if (string.IsNullOrEmpty(sessionToken)) { throw new ArgumentNullException(nameof(sessionToken)); } Dictionary <string, object> headers = new Dictionary <string, object> { { "X-LC-Session", sessionToken } }; Dictionary <string, object> response = await LeanCloud.HttpClient.Get <Dictionary <string, object> >("users/me", headers : headers); LCObjectData objectData = LCObjectData.Decode(response); currentUser = new LCUser(objectData); return(currentUser); }
/// <summary> /// Gets the rankings of the user. /// </summary> /// <param name="user"></param> /// <param name="version"></param> /// <param name="skip"></param> /// <param name="limit"></param> /// <param name="selectUserKeys"></param> /// <param name="includeStatistics"></param> /// <returns></returns> private async Task <ReadOnlyCollection <LCRanking> > GetResults(LCUser user, int version, int skip, int limit, IEnumerable <string> selectUserKeys, IEnumerable <string> includeStatistics) { string path = $"leaderboard/leaderboards/{StatisticName}/ranks"; if (user != null) { path = $"{path}/{user.ObjectId}"; } path = $"{path}?skip={skip}&limit={limit}"; if (version != -1) { path = $"{path}&version={version}"; } if (selectUserKeys != null) { string keys = string.Join(",", selectUserKeys); path = $"{path}&includeUser={keys}"; } if (includeStatistics != null) { string statistics = string.Join(",", includeStatistics); path = $"{path}&includeStatistics={statistics}"; } Dictionary <string, object> result = await LCCore.HttpClient.Get <Dictionary <string, object> >(path); if (result.TryGetValue("results", out object results) && results is List <object> list) { List <LCRanking> rankings = new List <LCRanking>(); foreach (object item in list) { LCRanking ranking = LCRanking.Parse(item as IDictionary <string, object>); rankings.Add(ranking); } return(rankings.AsReadOnly()); } return(null); }
/// <summary> /// Sends this status. /// </summary> /// <returns></returns> public async Task <LCStatus> Send() { LCUser user = await LCUser.GetCurrent(); if (user == null) { throw new ArgumentNullException("current user"); } Dictionary <string, object> formData = new Dictionary <string, object> { { InboxTypeKey, InboxType } }; if (Data != null) { formData["data"] = LCEncoder.Encode(Data); } if (query != null) { Dictionary <string, object> queryData = new Dictionary <string, object> { { "className", query.ClassName } }; Dictionary <string, object> ps = query.BuildParams(); if (ps.TryGetValue("where", out object whereObj) && whereObj is string where) { queryData["where"] = JsonConvert.DeserializeObject(where); } if (ps.TryGetValue("keys", out object keys)) { queryData["keys"] = keys; } formData["query"] = queryData; } Dictionary <string, object> response = await LCCore.HttpClient.Post <Dictionary <string, object> >("statuses", data : formData); LCObjectData objectData = LCObjectData.Decode(response); Merge(objectData); return(this); }
/// <summary> /// Gets the currently logged in LCUser with a valid session, from /// memory or disk if necessary. /// </summary> /// <returns></returns> public static async Task <LCUser> GetCurrent() { if (currentUser != null) { return(currentUser); } string data = await LCCore.PersistenceController.ReadText(USER_DATA); if (!string.IsNullOrEmpty(data)) { try { currentUser = ParseObject(data) as LCUser; } catch (Exception e) { LCLogger.Error(e); await LCCore.PersistenceController.Delete(USER_DATA); } } return(currentUser); }
/// <summary> /// Resets the count of the status to be zero. /// </summary> /// <param name="inboxType"></param> /// <returns></returns> public static async Task ResetUnreadCount(string inboxType = null) { LCUser user = await LCUser.GetCurrent(); if (user == null) { throw new ArgumentNullException("current user"); } Dictionary <string, object> queryParams = new Dictionary <string, object> { { OwnerKey, JsonConvert.SerializeObject(LCEncoder.Encode(user)) } }; if (!string.IsNullOrEmpty(inboxType)) { queryParams[InboxTypeKey] = inboxType; } await LCCore.HttpClient.Post <Dictionary <string, object> >("subscribe/statuses/resetUnreadCount", queryParams : queryParams); }
/// <summary> /// 使用手机号和验证码注册或登录 /// </summary> /// <param name="mobile"></param> /// <param name="code"></param> /// <returns></returns> public static async Task <LCUser> SignUpOrLoginByMobilePhone(string mobile, string code) { if (string.IsNullOrEmpty(mobile)) { throw new ArgumentNullException(nameof(mobile)); } if (string.IsNullOrEmpty(mobile)) { throw new ArgumentNullException(nameof(code)); } Dictionary <string, object> data = new Dictionary <string, object> { { "mobilePhoneNumber", mobile }, { "smsCode", code } }; Dictionary <string, object> response = await LeanCloud.HttpClient.Post <Dictionary <string, object> >("usersByMobilePhone", data : data); LCObjectData objectData = LCObjectData.Decode(response); currentUser = new LCUser(objectData); return(currentUser); }
/// <summary> /// Requests to add a friend. /// </summary> /// <param name="userId">The user id to add.</param> /// <param name="attributes">The additional attributes for the friendship.</param> /// <returns></returns> public static async Task Request(string userId, Dictionary <string, object> attributes = null) { LCUser user = await LCUser.GetCurrent(); if (user == null) { throw new ArgumentNullException("current user"); } string path = "users/friendshipRequests"; LCObject friend = LCObject.CreateWithoutData("_User", userId); Dictionary <string, object> data = new Dictionary <string, object> { { "user", LCEncoder.EncodeLCObject(user) }, { "friend", LCEncoder.EncodeLCObject(friend) }, }; if (attributes != null) { data["friendship"] = attributes; } await LCCore.HttpClient.Post <Dictionary <string, object> >(path, data : data); }
/// <summary> /// 注册 /// </summary> /// <returns></returns> public async Task <LCUser> SignUp() { if (string.IsNullOrEmpty(Username)) { throw new ArgumentNullException(nameof(Username)); } if (string.IsNullOrEmpty(Password)) { throw new ArgumentNullException(nameof(Password)); } if (!string.IsNullOrEmpty(ObjectId)) { throw new ArgumentException("Cannot sign up a user that already exists."); } await Save(); currentUser = this; // TODO Persistence return(this); }
/// <summary> /// Retrieves a list of LCStatus that satisfy the query from the cloud. /// </summary> /// <returns></returns> public new async Task <ReadOnlyCollection <LCStatus> > Find() { LCUser user = await LCUser.GetCurrent(); if (user == null) { throw new ArgumentNullException("current user"); } Dictionary <string, object> queryParams = new Dictionary <string, object> { { LCStatus.OwnerKey, JsonConvert.SerializeObject(LCEncoder.Encode(user)) }, { LCStatus.InboxTypeKey, InboxType }, { "where", BuildWhere() }, { "sinceId", SinceId }, { "maxId", MaxId }, { "limit", Condition.Limit } }; Dictionary <string, object> response = await LCCore.HttpClient.Get <Dictionary <string, object> >("subscribe/statuses", queryParams : queryParams); List <object> results = response["results"] as List <object>; List <LCStatus> statuses = new List <LCStatus>(); foreach (object item in results) { LCObjectData objectData = LCObjectData.Decode(item as IDictionary); LCStatus status = new LCStatus(); status.Merge(objectData); status.MessageId = (int)objectData.CustomPropertyDict[LCStatus.MessageIdKey]; status.Data = objectData.CustomPropertyDict; status.InboxType = objectData.CustomPropertyDict[LCStatus.InboxTypeKey] as string; statuses.Add(status); } return(statuses.AsReadOnly()); }
/// <summary> /// Logs out the currently logged in user session. /// </summary> public static Task Logout() { currentUser = null; // 清理持久化数据 return(LCCore.PersistenceController.Delete(USER_DATA)); }
/// <summary> /// Updates the statistic of the user. /// </summary> /// <param name="user"></param> /// <param name="statistics"></param> /// <param name="overwrite"></param> /// <returns></returns> public static async Task <ReadOnlyCollection <LCStatistic> > UpdateStatistics(LCUser user, Dictionary <string, double> statistics, bool overwrite = false) { if (user == null) { throw new ArgumentNullException(nameof(user)); } if (statistics == null || statistics.Count == 0) { throw new ArgumentNullException(nameof(statistics)); } List <Dictionary <string, object> > data = statistics.Select(statistic => new Dictionary <string, object> { { "statisticName", statistic.Key }, { "statisticValue", statistic.Value }, }).ToList(); string path = $"leaderboard/users/{user.ObjectId}/statistics"; if (overwrite) { path = $"{path}?overwrite=1"; } Dictionary <string, object> result = await LCCore.HttpClient.Post <Dictionary <string, object> >(path, data : data); if (result.TryGetValue("results", out object results) && results is List <object> list) { List <LCStatistic> statisticList = new List <LCStatistic>(); foreach (object item in list) { LCStatistic statistic = LCStatistic.Parse(item as IDictionary <string, object>); statisticList.Add(statistic); } return(statisticList.AsReadOnly()); } return(null); }