Skip to content

Commit

Permalink
Merge pull request #92 from onerain88/chore
Browse files Browse the repository at this point in the history
完善开发指南中的接口
  • Loading branch information
onerain88 authored Dec 24, 2020
2 parents 0ef89b3 + fb57f15 commit d263de1
Show file tree
Hide file tree
Showing 24 changed files with 252 additions and 70 deletions.
1 change: 1 addition & 0 deletions Common/Common-Unity/Common-Unity.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<ReleaseVersion>0.4.2</ReleaseVersion>
<AssemblyName>Common</AssemblyName>
</PropertyGroup>

<ItemGroup>
Expand Down
1 change: 1 addition & 0 deletions LiveQuery/LiveQuery-Unity/LiveQuery-Unity.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<TargetFramework>netstandard2.0</TargetFramework>
<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
<ReleaseVersion>0.4.2</ReleaseVersion>
<AssemblyName>LiveQuery</AssemblyName>
</PropertyGroup>

<ItemGroup>
Expand Down
1 change: 1 addition & 0 deletions Realtime/Realtime-Unity/Realtime-Unity.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<TargetFramework>netstandard2.0</TargetFramework>
<ReleaseVersion>0.4.2</ReleaseVersion>
<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
<AssemblyName>Realtime</AssemblyName>
</PropertyGroup>

<ItemGroup>
Expand Down
35 changes: 29 additions & 6 deletions Realtime/Realtime.Test/Client.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,22 @@

namespace Realtime.Test {
public class Client {
private const string USERNAME1 = "username1";
private const string PASSWORD1 = "password1";

private const string USERNAME2 = "username2";
private const string PASSWORD2 = "password2";

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

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

[Test]
Expand All @@ -34,12 +41,12 @@ public async Task OpenAndClose() {

[Test]
public async Task OpenAndCloseByLCUser() {
LCUser user = await LCUser.Login("hello", "world");
LCUser user = await LCUser.Login(USERNAME1, PASSWORD1);
LCIMClient client = new LCIMClient(user);
await client.Open();


LCUser game = await LCUser.Login("game", "play");
LCUser game = await LCUser.Login(USERNAME2, PASSWORD2);
LCIMClient client2 = new LCIMClient(game);
await client2.Open();

Expand Down Expand Up @@ -134,5 +141,21 @@ public async Task CreateTemporaryConversation() {

await tcs.Task;
}

private async Task NewUser(string username, string password) {
try {
await LCUser.Login(username, password);
} catch (LCException e) {
if (e.Code == 211) {
LCUser user1 = new LCUser {
Username = username,
Password = password
};
await user1.SignUp();
} else {
throw e;
}
}
}
}
}
5 changes: 2 additions & 3 deletions Realtime/Realtime.Test/Conversation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ public class Conversation {

[SetUp]
public async Task SetUp() {
LCLogger.LogDelegate += Utils.Print;
LCApplication.Initialize("ikGGdRE2YcVOemAaRbgp1xGJ-gzGzoHsz", "NUKmuRbdAhg1vrb2wexYo1jo", "https://ikggdre2.lc-cn-n1-shared.com");
Utils.SetUp();
c1 = new LCIMClient(Guid.NewGuid().ToString());
await c1.Open();
c2 = new LCIMClient(Guid.NewGuid().ToString());
Expand All @@ -32,7 +31,7 @@ public async Task TearDown() {
await c1.Close();
await c2.Close();
await lean.Close();
LCLogger.LogDelegate -= Utils.Print;
Utils.TearDown();
}

[Test]
Expand Down
40 changes: 33 additions & 7 deletions Realtime/Realtime.Test/ConversationQuery.cs
Original file line number Diff line number Diff line change
@@ -1,27 +1,24 @@
using NUnit.Framework;
using System.Collections.ObjectModel;
using System.Threading.Tasks;
using LeanCloud;
using LeanCloud.Common;
using LeanCloud.Realtime;

namespace Realtime.Test {
public class ConversationQuery {
private string clientId = "hello123";
private readonly string clientId = "m1";
private LCIMClient client;

[SetUp]
public async Task SetUp() {
LCLogger.LogDelegate += Utils.Print;
LCApplication.Initialize("ikGGdRE2YcVOemAaRbgp1xGJ-gzGzoHsz", "NUKmuRbdAhg1vrb2wexYo1jo", "https://ikggdre2.lc-cn-n1-shared.com");
Utils.SetUp();
client = new LCIMClient(clientId);
await client.Open();
}

[TearDown]
public async Task TearDown() {
LCLogger.LogDelegate -= Utils.Print;
await client.Close();
Utils.TearDown();
}

[Test]
Expand All @@ -36,7 +33,7 @@ public async Task QueryMyConversation() {

[Test]
public async Task QueryMemberConversation() {
string memberId = "cc1";
string memberId = "m1";
LCIMConversationQuery query = new LCIMConversationQuery(client);
query.WhereEqualTo("m", memberId);
ReadOnlyCollection<LCIMConversation> conversations = await query.Find();
Expand All @@ -45,5 +42,34 @@ public async Task QueryMemberConversation() {
Assert.True(conversation.MemberIds.Contains(memberId));
}
}

[Test]
public async Task QueryCompact() {
string memberId = "m1";
LCIMConversationQuery query = new LCIMConversationQuery(client)
.WhereEqualTo("m", memberId);
query.Compact = true;
ReadOnlyCollection<LCIMConversation> conversations = await query.Find();
foreach (LCIMConversation conversation in conversations) {
Assert.True(conversation.MemberIds.Count == 0);
await conversation.Fetch();
Assert.True(conversation.MemberIds.Count > 0);
}
}

[Test]
public async Task QueryWithLastMessage() {
string memberId = "m1";
LCIMConversationQuery query = new LCIMConversationQuery(client)
.WhereEqualTo("m", memberId);
query.WithLastMessageRefreshed = true;
ReadOnlyCollection<LCIMConversation> conversations = await query.Find();
foreach (LCIMConversation conversation in conversations) {
Assert.True(!string.IsNullOrEmpty(conversation.LastMessage.Id));
if (conversation.LastMessage is LCIMBinaryMessage binaryMessage) {
TestContext.WriteLine(System.Text.Encoding.UTF8.GetString(binaryMessage.Data));
}
}
}
}
}
13 changes: 7 additions & 6 deletions Realtime/Realtime.Test/Message.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@ public class Message {

[SetUp]
public async Task SetUp() {
LCLogger.LogDelegate += Utils.Print;
LCApplication.Initialize("ikGGdRE2YcVOemAaRbgp1xGJ-gzGzoHsz", "NUKmuRbdAhg1vrb2wexYo1jo", "https://ikggdre2.lc-cn-n1-shared.com");
Utils.SetUp();
m1 = new LCIMClient("m1");
m2 = new LCIMClient("m2");
await m1.Open();
Expand All @@ -45,7 +44,7 @@ public async Task SetUp() {
public async Task TearDown() {
await m1.Close();
await m2.Close();
LCLogger.LogDelegate -= Utils.Print;
Utils.TearDown();
}

[Test]
Expand Down Expand Up @@ -76,17 +75,19 @@ public async Task Send() {
Assert.NotNull(textMessage.Id);

LCFile image = new LCFile("hello", "../../../../../assets/hello.png");
await image.Save();
LCIMImageMessage imageMessage = new LCIMImageMessage(image);
await conversation.Send(imageMessage);
Assert.NotNull(imageMessage.Id);

LCFile file = new LCFile("apk", "../../../../../assets/test.apk");
await file.Save();
LCIMFileMessage fileMessage = new LCIMFileMessage(file);
await conversation.Send(fileMessage);
Assert.NotNull(fileMessage.Id);

LCIMBinaryMessage binaryMessage = new LCIMBinaryMessage(System.Text.Encoding.UTF8.GetBytes("LeanCloud"));
await conversation.Send(binaryMessage);
Assert.NotNull(binaryMessage.Id);

await tcs.Task;
}

Expand Down Expand Up @@ -152,7 +153,7 @@ public async Task Update() {
[Test]
[Order(4)]
public async Task Query() {
ReadOnlyCollection<LCIMMessage> messages = await conversation.QueryMessages();
ReadOnlyCollection<LCIMMessage> messages = await conversation.QueryMessages(messageType: -6);
Assert.Greater(messages.Count, 0);
foreach (LCIMMessage message in messages) {
Assert.AreEqual(message.ConversationId, conversation.Id);
Expand Down
6 changes: 2 additions & 4 deletions Realtime/Realtime.Test/Throttle.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using NUnit.Framework;
using System;
using System.Threading.Tasks;
using LeanCloud;
using LeanCloud.Realtime;
using LeanCloud.Realtime.Internal.Protocol;

Expand All @@ -14,8 +13,7 @@ public class Throttle {

[SetUp]
public async Task SetUp() {
LCLogger.LogDelegate += Utils.Print;
LCApplication.Initialize("ikGGdRE2YcVOemAaRbgp1xGJ-gzGzoHsz", "NUKmuRbdAhg1vrb2wexYo1jo", "https://ikggdre2.lc-cn-n1-shared.com");
Utils.SetUp();
c1 = new LCIMClient(Guid.NewGuid().ToString());
c2 = new LCIMClient(Guid.NewGuid().ToString());
await c1.Open();
Expand All @@ -28,7 +26,7 @@ public async Task SetUp() {
public async Task TearDown() {
await c1.Close();
await c2.Close();
LCLogger.LogDelegate -= Utils.Print;
Utils.TearDown();
}

[Test]
Expand Down
10 changes: 9 additions & 1 deletion Realtime/Realtime.Test/Utils.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
using System;
using LeanCloud;
using LeanCloud.Common;
using NUnit.Framework;

namespace Realtime.Test {
public static class Utils {
internal static void SetUp() {
LCLogger.LogDelegate += Print;
LCApplication.Initialize("3zWMOXuO9iSdnjXM942i6DdI-gzGzoHsz", "bkwiNq4Tj417eUaHlTWS5sPm", "https://3zwmoxuo.lc-cn-n1-shared.com");
}

internal static void TearDown() {
LCLogger.LogDelegate -= Print;
}

internal static void Print(LCLogLevel level, string info) {
switch (level) {
case LCLogLevel.Debug:
Expand Down
4 changes: 4 additions & 0 deletions Realtime/Realtime/Conversation/LCIMChatRoom.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,9 @@ public async Task<ReadOnlyCollection<string>> GetOnlineMembers(int limit = 50) {
public override Task<LCIMPartiallySuccessResult> AddMembers(IEnumerable<string> clientIds) {
throw new Exception("Add members is not allowed in chat room.");
}

public override Task Read() {
return Task.CompletedTask;
}
}
}
46 changes: 39 additions & 7 deletions Realtime/Realtime/Conversation/LCIMConversation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using System.Threading.Tasks;
using System.Linq;
using System.Collections.ObjectModel;
using LeanCloud.Storage;

namespace LeanCloud.Realtime {
/// <summary>
Expand Down Expand Up @@ -180,9 +179,8 @@ public async Task<int> GetMembersCount() {
/// <summary>
/// Mark the last message of this conversation as read.
/// </summary>
/// <param name="message"></param>
/// <returns></returns>
public async Task Read() {
public virtual async Task Read() {
if (LastMessage == null) {
return;
}
Expand Down Expand Up @@ -260,10 +258,13 @@ public async Task Quit() {
}
}



/// <summary>
/// Sends a message in this conversation.
/// </summary>
/// <param name="message">The message to send.</param>
/// <param name="options">The options of sending message.</param>
/// <returns></returns>
public async Task<LCIMMessage> Send(LCIMMessage message,
LCIMMessageSendOptions options = null) {
Expand All @@ -273,6 +274,7 @@ public async Task<LCIMMessage> Send(LCIMMessage message,
if (options == null) {
options = LCIMMessageSendOptions.Default;
}
await message.PrepareSend();
await Client.MessageController.Send(Id, message, options);
LastMessage = message;
return message;
Expand Down Expand Up @@ -450,7 +452,7 @@ public async Task<LCIMPageResult> QueryBlockedMembers(int limit = 10,
/// <param name="start">Start message ID.</param>
/// <param name="end">End message ID.</param>
/// <param name="direction">Query direction (defaults to NewToOld).</param>
/// <param name="limit">Limits the number of returned results. Its default value is 20.</param>
/// <param name="limit">Limits the number of returned results. Its default value is 100.</param>
/// <param name="messageType">The message type to query. The default value is 0 (text message).</param>
/// <returns></returns>
public async Task<ReadOnlyCollection<LCIMMessage>> QueryMessages(LCIMMessageQueryEndpoint start = null,
Expand All @@ -469,6 +471,17 @@ public async Task FetchReciptTimestamps() {
await Client.ConversationController.FetchReciptTimestamp(Id);
}

/// <summary>
/// Fetch conversation from server.
/// </summary>
/// <returns></returns>
public async Task<LCIMConversation> Fetch() {
LCIMConversationQuery query = new LCIMConversationQuery(Client);
query.WhereEqualTo("objectId", Id);
await query.Find();
return this;
}

internal static bool IsTemporayConversation(string convId) {
return convId.StartsWith("_tmp:");
}
Expand Down Expand Up @@ -503,9 +516,28 @@ internal void MergeFrom(Dictionary<string, object> conv) {
IEnumerable<string> ids = (muo as IList<object>).Cast<string>();
mutedIds = new HashSet<string>(ids);
}
//if (conv.TryGetValue("lm", out object lmo)) {
// LastMessageAt = (DateTime)LCDecoder.Decode(lmo);
//}
if (conv.TryGetValue("msg", out object msgo)) {
if (conv.TryGetValue("bin", out object bino)) {
string msg = msgo as string;
bool bin = (bool)bino;
if (bin) {
byte[] bytes = Convert.FromBase64String(msg);
LastMessage = LCIMBinaryMessage.Deserialize(bytes);
} else {
LastMessage = LCIMTypedMessage.Deserialize(msg);
}
}
LastMessage.ConversationId = Id;
if (conv.TryGetValue("msg_mid", out object msgId)) {
LastMessage.Id = msgId as string;
}
if (conv.TryGetValue("msg_from", out object msgFrom)) {
LastMessage.FromClientId = msgFrom as string;
}
if (conv.TryGetValue("msg_timestamp", out object timestamp)) {
LastMessage.SentTimestamp = (long)timestamp;
}
}
}

internal void MergeInfo(Dictionary<string, object> attr) {
Expand Down
Loading

0 comments on commit d263de1

Please sign in to comment.