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 Newtonsoft.Json;
|
|
|
|
|
using LeanCloud.Realtime.Protocol;
|
2020-03-26 16:08:35 +08:00
|
|
|
|
using LeanCloud.Storage.Internal;
|
2020-03-25 16:42:30 +08:00
|
|
|
|
|
|
|
|
|
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 => {
|
|
|
|
|
LCIMConversation conversation = conversationDict[conv.Cid];
|
2020-03-25 16:42:30 +08:00
|
|
|
|
conversation.Unread = conv.Unread;
|
2020-03-26 16:08:35 +08:00
|
|
|
|
// 解析最后一条消息
|
|
|
|
|
Dictionary<string, object> msgData = JsonConvert.DeserializeObject<Dictionary<string, object>>(conv.Data,
|
|
|
|
|
new LCJsonConverter());
|
|
|
|
|
int msgType = (int)msgData["_lctype"];
|
|
|
|
|
LCIMMessage message = null;
|
|
|
|
|
switch (msgType) {
|
|
|
|
|
case -1:
|
|
|
|
|
message = new LCIMTextMessage();
|
|
|
|
|
break;
|
|
|
|
|
case -2:
|
|
|
|
|
message = new LCIMImageMessage();
|
|
|
|
|
break;
|
|
|
|
|
case -3:
|
|
|
|
|
message = new LCIMAudioMessage();
|
|
|
|
|
break;
|
|
|
|
|
case -4:
|
|
|
|
|
message = new LCIMVideoMessage();
|
|
|
|
|
break;
|
|
|
|
|
case -5:
|
|
|
|
|
message = new LCIMLocationMessage();
|
|
|
|
|
break;
|
|
|
|
|
case -6:
|
|
|
|
|
message = new LCIMFileMessage();
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
message.ConversationId = conv.Cid;
|
|
|
|
|
message.Id = conv.Mid;
|
|
|
|
|
message.FromClientId = conv.From;
|
|
|
|
|
message.SentTimestamp = conv.Timestamp;
|
|
|
|
|
conversation.LastMessage = message;
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
}
|