feat: friendship
parent
d263de14b7
commit
0a225434c8
|
@ -0,0 +1,21 @@
|
|||
using System.Collections.Generic;
|
||||
|
||||
namespace LeanCloud.Storage {
|
||||
public class LCFollowersAndFollowees {
|
||||
public List<LCObject> Followers {
|
||||
get; internal set;
|
||||
}
|
||||
|
||||
public List<LCObject> Followees {
|
||||
get; internal set;
|
||||
}
|
||||
|
||||
public int FollowersCount {
|
||||
get; internal set;
|
||||
}
|
||||
|
||||
public int FolloweeCount {
|
||||
get; internal set;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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<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 LCApplication.HttpClient.Post<Dictionary<string, object>>(path, data: data);
|
||||
}
|
||||
|
||||
public static async Task AcceptRequest(LCFriendshipRequest request, Dictionary<string, object> attributes = null) {
|
||||
if (request == null) {
|
||||
throw new ArgumentNullException(nameof(request));
|
||||
}
|
||||
string path = $"users/friendshipRequests/{request.ObjectId}/accept";
|
||||
Dictionary<string, object> data = null;
|
||||
if (attributes != null) {
|
||||
data = new Dictionary<string, object> {
|
||||
{ "friendship", attributes }
|
||||
};
|
||||
}
|
||||
await LCApplication.HttpClient.Put<Dictionary<string, object>>(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<Dictionary<string, object>>(path);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,8 @@
|
|||
namespace LeanCloud.Storage {
|
||||
public class LCFriendshipRequest : LCObject {
|
||||
public const string CLASS_NAME = "_FriendshipRequest";
|
||||
|
||||
public LCFriendshipRequest() : base(CLASS_NAME) {
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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<Dictionary<string, object>>(path, data: data);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Follows a user.
|
||||
/// </summary>
|
||||
/// <param name="targetId"></param>
|
||||
/// <param name="attrs"></param>
|
||||
/// <returns></returns>
|
||||
public async Task Follow(string targetId, Dictionary<string, object> attrs = null) {
|
||||
if (string.IsNullOrEmpty(targetId)) {
|
||||
throw new ArgumentNullException(nameof(targetId));
|
||||
}
|
||||
string path = $"users/self/friendship/{targetId}";
|
||||
await LCApplication.HttpClient.Post<Dictionary<string, object>>(path, data: attrs);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Unfollows a user.
|
||||
/// </summary>
|
||||
/// <param name="targetId"></param>
|
||||
/// <returns></returns>
|
||||
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);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Constructs a follower query.
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public LCQuery<LCObject> FollowerQuery() {
|
||||
return new LCQuery<LCObject>("_Follower")
|
||||
.WhereEqualTo("user", this)
|
||||
.Include("follower");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Constructs a followee query.
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public LCQuery<LCObject> FolloweeQuery() {
|
||||
return new LCQuery<LCObject>("_Followee")
|
||||
.WhereEqualTo("user", this)
|
||||
.Include("followee");
|
||||
}
|
||||
|
||||
public async Task<LCFollowersAndFollowees> GetFollowersAndFollowees(bool includeFollower = false,
|
||||
bool includeFollowee = false, bool returnCount = false) {
|
||||
Dictionary<string, object> queryParams = new Dictionary<string, object>();
|
||||
if (returnCount) {
|
||||
queryParams["count"] = 1;
|
||||
}
|
||||
if (includeFollower || includeFollowee) {
|
||||
List<string> includes = new List<string>();
|
||||
if (includeFollower) {
|
||||
includes.Add("follower");
|
||||
}
|
||||
if (includeFollowee) {
|
||||
includes.Add("followee");
|
||||
}
|
||||
queryParams["include"] = string.Join(",", includes);
|
||||
}
|
||||
string path = $"users/{ObjectId}/followersAndFollowees";
|
||||
Dictionary<string, object> response = await LCApplication.HttpClient.Get<Dictionary<string, object>>(path,
|
||||
queryParams: queryParams);
|
||||
LCFollowersAndFollowees result = new LCFollowersAndFollowees();
|
||||
if (response.TryGetValue("followers", out object followersObj) &&
|
||||
(followersObj is List<object> followers)) {
|
||||
result.Followers = new List<LCObject>();
|
||||
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<object> followees)) {
|
||||
result.Followees = new List<LCObject>();
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue