2020-03-26 16:08:35 +08:00
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading.Tasks;
|
2020-03-25 16:42:30 +08:00
|
|
|
|
using System.Collections.Generic;
|
2020-03-27 15:52:34 +08:00
|
|
|
|
using System.Collections.ObjectModel;
|
2020-03-25 16:42:30 +08:00
|
|
|
|
using LeanCloud.Realtime.Protocol;
|
|
|
|
|
|
|
|
|
|
namespace LeanCloud.Realtime.Internal.Controller {
|
|
|
|
|
internal class LCIMUnreadController : LCIMController {
|
|
|
|
|
internal LCIMUnreadController(LCIMClient client) : base(client) {
|
2020-03-27 15:52:34 +08:00
|
|
|
|
|
2020-03-25 16:42:30 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-03-27 15:52:34 +08:00
|
|
|
|
#region 消息处理
|
|
|
|
|
|
2020-03-25 16:42:30 +08:00
|
|
|
|
internal override async Task OnNotification(GenericCommand notification) {
|
|
|
|
|
UnreadCommand unread = notification.UnreadMessage;
|
2020-03-26 16:08:35 +08:00
|
|
|
|
|
|
|
|
|
IEnumerable<string> convIds = unread.Convs
|
|
|
|
|
.Select(conv => conv.Cid);
|
2020-03-27 15:52:34 +08:00
|
|
|
|
Dictionary<string, LCIMConversation> conversationDict = (await Client.GetConversationList(convIds))
|
2020-03-26 16:08:35 +08:00
|
|
|
|
.ToDictionary(item => item.Id);
|
2020-03-27 15:52:34 +08:00
|
|
|
|
ReadOnlyCollection<LCIMConversation> conversations = unread.Convs.Select(conv => {
|
2020-03-27 17:30:18 +08:00
|
|
|
|
// 设置对话中的未读数据
|
2020-03-27 15:52:34 +08:00
|
|
|
|
LCIMConversation conversation = conversationDict[conv.Cid];
|
2020-03-25 16:42:30 +08:00
|
|
|
|
conversation.Unread = conv.Unread;
|
2020-04-10 16:32:33 +08:00
|
|
|
|
if (conv.HasData || conv.HasBinaryMsg) {
|
|
|
|
|
// 如果有消息,则反序列化
|
|
|
|
|
LCIMMessage message = null;
|
|
|
|
|
if (conv.HasBinaryMsg) {
|
|
|
|
|
// 二进制消息
|
|
|
|
|
byte[] bytes = conv.BinaryMsg.ToByteArray();
|
|
|
|
|
message = LCIMBinaryMessage.Deserialize(bytes);
|
|
|
|
|
} else {
|
|
|
|
|
// 类型消息
|
|
|
|
|
message = LCIMTypedMessage.Deserialize(conv.Data);
|
|
|
|
|
}
|
|
|
|
|
// 填充消息数据
|
|
|
|
|
message.ConversationId = conv.Cid;
|
|
|
|
|
message.Id = conv.Mid;
|
|
|
|
|
message.FromClientId = conv.From;
|
|
|
|
|
message.SentTimestamp = conv.Timestamp;
|
|
|
|
|
conversation.LastMessage = message;
|
2020-03-26 16:08:35 +08:00
|
|
|
|
}
|
|
|
|
|
return conversation;
|
2020-03-27 15:52:34 +08:00
|
|
|
|
}).ToList().AsReadOnly();
|
|
|
|
|
Client.OnUnreadMessagesCountUpdated?.Invoke(conversations);
|
2020-03-25 16:42:30 +08:00
|
|
|
|
}
|
2020-03-27 15:52:34 +08:00
|
|
|
|
|
|
|
|
|
#endregion
|
2020-03-25 16:42:30 +08:00
|
|
|
|
}
|
|
|
|
|
}
|