From 0a225434c8d398fd6e90abca7e5e58d9987acc58 Mon Sep 17 00:00:00 2001 From: oneRain Date: Tue, 12 Jan 2021 18:11:08 +0800 Subject: [PATCH] feat: friendship --- Storage/Storage/LCFollowersAndFollowees.cs | 21 +++++ Storage/Storage/LCFriendship.cs | 47 ++++++++++ Storage/Storage/LCFriendshipRequest.cs | 8 ++ Storage/Storage/LCUser.cs | 99 ++++++++++++++++++++++ 4 files changed, 175 insertions(+) create mode 100644 Storage/Storage/LCFollowersAndFollowees.cs create mode 100644 Storage/Storage/LCFriendship.cs create mode 100644 Storage/Storage/LCFriendshipRequest.cs diff --git a/Storage/Storage/LCFollowersAndFollowees.cs b/Storage/Storage/LCFollowersAndFollowees.cs new file mode 100644 index 0000000..288b1b5 --- /dev/null +++ b/Storage/Storage/LCFollowersAndFollowees.cs @@ -0,0 +1,21 @@ +using System.Collections.Generic; + +namespace LeanCloud.Storage { + public class LCFollowersAndFollowees { + public List Followers { + get; internal set; + } + + public List Followees { + get; internal set; + } + + public int FollowersCount { + get; internal set; + } + + public int FolloweeCount { + get; internal set; + } + } +} diff --git a/Storage/Storage/LCFriendship.cs b/Storage/Storage/LCFriendship.cs new file mode 100644 index 0000000..40c0d13 --- /dev/null +++ b/Storage/Storage/LCFriendship.cs @@ -0,0 +1,47 @@ +using System; +using System.Threading.Tasks; +using System.Collections.Generic; +using LeanCloud.Storage.Internal.Codec; + +namespace LeanCloud.Storage { + public static class LCFriendship { + public static async Task Request(string userId, Dictionary 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 data = new Dictionary { + { "user", LCEncoder.EncodeLCObject(user) }, + { "friend", LCEncoder.EncodeLCObject(friend) }, + }; + if (attributes != null) { + data["friendship"] = attributes; + } + await LCApplication.HttpClient.Post>(path, data: data); + } + + public static async Task AcceptRequest(LCFriendshipRequest request, Dictionary attributes = null) { + if (request == null) { + throw new ArgumentNullException(nameof(request)); + } + string path = $"users/friendshipRequests/{request.ObjectId}/accept"; + Dictionary data = null; + if (attributes != null) { + data = new Dictionary { + { "friendship", attributes } + }; + } + await LCApplication.HttpClient.Put>(path, data: data); + } + + public static async Task DeclineRequest(LCFriendshipRequest request) { + if (request == null) { + throw new ArgumentNullException(nameof(request)); + } + string path = $"users/friendshipRequests/{request.ObjectId}/decline"; + await LCApplication.HttpClient.Put>(path); + } + } +} diff --git a/Storage/Storage/LCFriendshipRequest.cs b/Storage/Storage/LCFriendshipRequest.cs new file mode 100644 index 0000000..6306a62 --- /dev/null +++ b/Storage/Storage/LCFriendshipRequest.cs @@ -0,0 +1,8 @@ +namespace LeanCloud.Storage { + public class LCFriendshipRequest : LCObject { + public const string CLASS_NAME = "_FriendshipRequest"; + + public LCFriendshipRequest() : base(CLASS_NAME) { + } + } +} diff --git a/Storage/Storage/LCUser.cs b/Storage/Storage/LCUser.cs index af1aa03..46ad157 100644 --- a/Storage/Storage/LCUser.cs +++ b/Storage/Storage/LCUser.cs @@ -1,5 +1,6 @@ using System; using System.Threading.Tasks; +using System.Collections; using System.Collections.Generic; using LeanCloud.Storage.Internal.Object; @@ -589,5 +590,103 @@ namespace LeanCloud.Storage { }; await LCApplication.HttpClient.Post>(path, data: data); } + + /// + /// Follows a user. + /// + /// + /// + /// + public async Task Follow(string targetId, Dictionary attrs = null) { + if (string.IsNullOrEmpty(targetId)) { + throw new ArgumentNullException(nameof(targetId)); + } + string path = $"users/self/friendship/{targetId}"; + await LCApplication.HttpClient.Post>(path, data: attrs); + } + + /// + /// Unfollows a user. + /// + /// + /// + public async Task Unfollow(string targetId) { + if (string.IsNullOrEmpty(targetId)) { + throw new ArgumentNullException(nameof(targetId)); + } + string path = $"users/self/friendship/{targetId}"; + await LCApplication.HttpClient.Delete(path); + } + + /// + /// Constructs a follower query. + /// + /// + public LCQuery FollowerQuery() { + return new LCQuery("_Follower") + .WhereEqualTo("user", this) + .Include("follower"); + } + + /// + /// Constructs a followee query. + /// + /// + public LCQuery FolloweeQuery() { + return new LCQuery("_Followee") + .WhereEqualTo("user", this) + .Include("followee"); + } + + public async Task GetFollowersAndFollowees(bool includeFollower = false, + bool includeFollowee = false, bool returnCount = false) { + Dictionary queryParams = new Dictionary(); + if (returnCount) { + queryParams["count"] = 1; + } + if (includeFollower || includeFollowee) { + List includes = new List(); + if (includeFollower) { + includes.Add("follower"); + } + if (includeFollowee) { + includes.Add("followee"); + } + queryParams["include"] = string.Join(",", includes); + } + string path = $"users/{ObjectId}/followersAndFollowees"; + Dictionary response = await LCApplication.HttpClient.Get>(path, + queryParams: queryParams); + LCFollowersAndFollowees result = new LCFollowersAndFollowees(); + if (response.TryGetValue("followers", out object followersObj) && + (followersObj is List followers)) { + result.Followers = new List(); + foreach (object followerObj in followers) { + LCObjectData objectData = LCObjectData.Decode(followerObj as IDictionary); + LCObject follower = new LCObject("_Follower"); + follower.Merge(objectData); + result.Followers.Add(follower); + } + } + if (response.TryGetValue("followees", out object followeesObj) && + (followeesObj is List followees)) { + result.Followees = new List(); + foreach (object followeeObj in followees) { + LCObjectData objectData = LCObjectData.Decode(followeeObj as IDictionary); + LCObject followee = new LCObject("_Followee"); + followee.Merge(objectData); + result.Followees.Add(followee); + } + } + if (response.TryGetValue("followers_count", out object followersCountObj) && + (followersCountObj is int followersCount)) { + result.FollowersCount = followersCount; + } + if (response.TryGetValue("followees_count", out object followeesCountObj) && + (followeesCountObj is int followeesCount)) { + result.FolloweeCount = followeesCount; + } + return result; + } } }