Skip to content

Commit

Permalink
Merge pull request #96 from onerain88/friend
Browse files Browse the repository at this point in the history
好友和信息流
  • Loading branch information
onerain88 authored Jan 14, 2021
2 parents 2add0af + 91e281a commit 0d86389
Show file tree
Hide file tree
Showing 13 changed files with 741 additions and 78 deletions.
18 changes: 18 additions & 0 deletions Storage/Storage-Unity/Storage-Unity.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,24 @@
<Compile Include="..\Storage\Leaderboard\LCRanking.cs">
<Link>Leaderboard\LCRanking.cs</Link>
</Compile>
<Compile Include="..\Storage\LCFollowersAndFollowees.cs">
<Link>LCFollowersAndFollowees.cs</Link>
</Compile>
<Compile Include="..\Storage\LCFriendship.cs">
<Link>LCFriendship.cs</Link>
</Compile>
<Compile Include="..\Storage\LCFriendshipRequest.cs">
<Link>LCFriendshipRequest.cs</Link>
</Compile>
<Compile Include="..\Storage\LCStatus.cs">
<Link>LCStatus.cs</Link>
</Compile>
<Compile Include="..\Storage\LCStatusCount.cs">
<Link>LCStatusCount.cs</Link>
</Compile>
<Compile Include="..\Storage\LCStatusQuery.cs">
<Link>LCStatusQuery.cs</Link>
</Compile>
</ItemGroup>
<ItemGroup>
<Reference Include="Newtonsoft.Json">
Expand Down
103 changes: 103 additions & 0 deletions Storage/Storage.Test/FriendTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
using NUnit.Framework;
using System;
using System.Linq;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Threading.Tasks;
using LeanCloud;
using LeanCloud.Storage;

namespace Storage.Test {
public class FriendTest {
async Task<LCUser> SignUp() {
LCUser user = new LCUser {
Username = Guid.NewGuid().ToString(),
Password = "world"
};
return await user.SignUp();
}

async Task<LCFriendshipRequest> GetRequest() {
LCUser user = await LCUser.GetCurrent();
LCQuery<LCFriendshipRequest> query = new LCQuery<LCFriendshipRequest>("_FriendshipRequest")
.WhereEqualTo("friend", user)
.WhereEqualTo("status", "pending");
return await query.First();
}

async Task<ReadOnlyCollection<LCObject>> GetFriends() {
LCUser user = await LCUser.GetCurrent();
LCQuery<LCObject> query = new LCQuery<LCObject>("_Followee")
.WhereEqualTo("user", user)
.WhereEqualTo("friendStatus", true);
return await query.Find();
}

private LCUser user1;
private LCUser user2;

[SetUp]
public void SetUp() {
LCLogger.LogDelegate += Utils.Print;
LCApplication.Initialize("ikGGdRE2YcVOemAaRbgp1xGJ-gzGzoHsz", "NUKmuRbdAhg1vrb2wexYo1jo",
"https://ikggdre2.lc-cn-n1-shared.com");
}

[TearDown]
public void TearDown() {
LCLogger.LogDelegate -= Utils.Print;
}

[Test]
[Order(0)]
public async Task Init() {
user1 = await SignUp();
user2 = await SignUp();
Dictionary<string, object> attrs = new Dictionary<string, object> {
{ "group", "sport" }
};
await LCFriendship.Request(user1.ObjectId, attrs);

await SignUp();
await LCFriendship.Request(user1.ObjectId);

await LCUser.BecomeWithSessionToken(user1.SessionToken);
}

[Test]
[Order(1)]
public async Task Accept() {
// 查询好友请求
LCFriendshipRequest request = await GetRequest();
// 接受
await LCFriendship.AcceptRequest(request);
// 查询好友
Assert.Greater((await GetFriends()).Count, 0);
}

[Test]
[Order(2)]
public async Task Decline() {
// 查询好友请求
LCFriendshipRequest request = await GetRequest();
// 拒绝
await LCFriendship.DeclineRequest(request);
}

[Test]
[Order(3)]
public async Task Attributes() {
LCObject followee = (await GetFriends()).First();
followee["group"] = "friend";
await followee.Save();
}

[Test]
[Order(4)]
public async Task Delete() {
await user1.Unfollow(user2.ObjectId);
// 查询好友
Assert.AreEqual((await GetFriends()).Count, 0);
}
}
}
143 changes: 143 additions & 0 deletions Storage/Storage.Test/StatusTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
using NUnit.Framework;
using System;
using System.Threading.Tasks;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using LeanCloud;
using LeanCloud.Storage;

namespace Storage.Test {
public class StatusTest {
private LCUser user1;
private LCUser user2;
private LCUser user3;

[SetUp]
public void SetUp() {
LCLogger.LogDelegate += Utils.Print;
LCApplication.Initialize("ikGGdRE2YcVOemAaRbgp1xGJ-gzGzoHsz", "NUKmuRbdAhg1vrb2wexYo1jo",
"https://ikggdre2.lc-cn-n1-shared.com");
}

[TearDown]
public void TearDown() {
LCLogger.LogDelegate -= Utils.Print;
}

[Test]
[Order(0)]
public async Task Init() {
user1 = new LCUser {
Username = Guid.NewGuid().ToString(),
Password = "world"
};
await user1.SignUp();

user2 = new LCUser {
Username = Guid.NewGuid().ToString(),
Password = "world"
};
await user2.SignUp();

user3 = new LCUser {
Username = Guid.NewGuid().ToString(),
Password = "world"
};
await user3.SignUp();
}

[Test]
[Order(1)]
public async Task Follow() {
await LCUser.BecomeWithSessionToken(user2.SessionToken);
Dictionary<string, object> attrs = new Dictionary<string, object> {
{ "score", 100 }
};
await user2.Follow(user1.ObjectId, attrs);

await LCUser.BecomeWithSessionToken(user3.SessionToken);
await user3.Follow(user2.ObjectId);
}

[Test]
[Order(2)]
public async Task QueryFollowersAndFollowees() {
await LCUser.BecomeWithSessionToken(user2.SessionToken);

LCQuery<LCObject> query = user2.FolloweeQuery();
ReadOnlyCollection<LCObject> results = await query.Find();
Assert.Greater(results.Count, 0);
foreach (LCObject item in results) {
Assert.IsTrue(item["followee"] is LCObject);
Assert.AreEqual(user1.ObjectId, (item["followee"] as LCObject).ObjectId);
}

query = user2.FollowerQuery();
results = await query.Find();
Assert.Greater(results.Count, 0);
foreach (LCObject item in results) {
Assert.IsTrue(item["follower"] is LCObject);
Assert.AreEqual(user3.ObjectId, (item["follower"] as LCObject).ObjectId);
}

LCFollowersAndFollowees followersAndFollowees = await user2.GetFollowersAndFollowees(true, true, true);
Assert.AreEqual(followersAndFollowees.FollowersCount, 1);
Assert.AreEqual(followersAndFollowees.FolloweesCount, 1);
}

[Test]
[Order(3)]
public async Task Send() {
await LCUser.BecomeWithSessionToken(user1.SessionToken);

// 给粉丝发送状态
LCStatus status = new LCStatus {
Data = new Dictionary<string, object> {
{ "image", "xxx.jpg" },
{ "content", "hello, world" }
}
};
await LCStatus.SendToFollowers(status);

// 给某个用户发送私信
LCStatus privateStatus = new LCStatus {
Data = new Dictionary<string, object> {
{ "image", "xxx.jpg" },
{ "content", "hello, game" }
}
};
await LCStatus.SendPrivately(privateStatus, user2.ObjectId);
}

[Test]
[Order(4)]
public async Task Query() {
await Task.Delay(5000);
await LCUser.BecomeWithSessionToken(user2.SessionToken);

LCStatusCount statusCount = await LCStatus.GetCount(LCStatus.InboxTypeDefault);
Assert.Greater(statusCount.Total, 0);
LCStatusCount privateCount = await LCStatus.GetCount(LCStatus.InboxTypePrivate);
Assert.Greater(privateCount.Total, 0);

LCStatusQuery query = new LCStatusQuery(LCStatus.InboxTypeDefault);
ReadOnlyCollection<LCStatus> statuses = await query.Find();
foreach (LCStatus status in statuses) {
Assert.AreEqual((status["source"] as LCObject).ObjectId, user1.ObjectId);
await status.Delete();
}

await LCStatus.ResetUnreadCount(LCStatus.InboxTypePrivate);
}

[Test]
[Order(5)]
public async Task Unfollow() {
await LCUser.BecomeWithSessionToken(user2.SessionToken);
await user2.Unfollow(user1.ObjectId);

await LCUser.BecomeWithSessionToken(user3.SessionToken);
await user3.Unfollow(user1.ObjectId);
}
}
}
36 changes: 33 additions & 3 deletions Storage/Storage.Test/UserTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Threading.Tasks;
using LeanCloud;
using LeanCloud.Storage;
using Newtonsoft.Json;

namespace Storage.Test {
public class UserTest {
Expand All @@ -26,7 +27,8 @@ public async Task SignUp() {
user.Password = "world";
string email = $"{unixTime}@qq.com";
user.Email = email;
string mobile = $"{unixTime / 100}";
Random random = new Random();
string mobile = $"151{random.Next(10000000, 99999999)}";
user.Mobile = mobile;
await user.SignUp();

Expand Down Expand Up @@ -72,7 +74,6 @@ public async Task RelateObject() {
LCObject account = new LCObject("Account");
account["user"] = user;
await account.Save();
Assert.AreEqual(user.ObjectId, "5e0d5c667d5774006a5c1177");
}

[Test]
Expand Down Expand Up @@ -239,7 +240,36 @@ public async Task RequestSMSCodeForUpdatingPhoneNumber() {
[Test]
public async Task VerifyCodeForUpdatingPhoneNumber() {
await LCUser.Login("hello", "world");
await LCUser.VerifyCodeForUpdatingPhoneNumber("15101006007", "055595");
await LCUser.VerifyCodeForUpdatingPhoneNumber("15101006007", "969327");
}

[Test]
public async Task AuthData() {
string uuid = Guid.NewGuid().ToString();
Dictionary<string, object> authData = new Dictionary<string, object> {
{ "expires_in", 7200 },
{ "openid", uuid },
{ "access_token", uuid }
};
LCUser currentUser = await LCUser.LoginWithAuthData(authData, "weixin");
TestContext.WriteLine(currentUser.SessionToken);
Assert.NotNull(currentUser.SessionToken);
string userId = currentUser.ObjectId;
TestContext.WriteLine($"userId: {userId}");
TestContext.WriteLine(JsonConvert.SerializeObject(currentUser.AuthData));

try {
authData = new Dictionary<string, object> {
{ "expires_in", 7200 },
{ "openid", uuid },
{ "access_token", uuid }
};
await currentUser.AssociateAuthData(authData, "qq");
TestContext.WriteLine(JsonConvert.SerializeObject(currentUser.AuthData));
} catch (LCException e) {
TestContext.WriteLine($"{e.Code} : {e.Message}");
TestContext.WriteLine(JsonConvert.SerializeObject(currentUser.AuthData));
}
}
}
}
Loading

0 comments on commit 0d86389

Please sign in to comment.