/// <summary> /// 标记消息为已读 /// </summary> /// <param name="message">消息</param> /// <returns>结果</returns> public async Task <bool> NoteRead(ChatMessage message) { //判断消息是否位于消息列表中 if (!instance.MessageList.Contains(message)) { //消息不存在返回假 return(false); } //调用消息控制器标记消息为已读 MessageController messageController = new MessageController(db.Messages); messageController.SetMessage(message); messageController.NoteRead(); //向服务器发送已读消息请求 client.NoteMessage(instance, message); //更新话题时间戳 instance.Read = message.SeqId; instance.LastUsed = ChatMessageBuilder.GetTimeStamp(); var dbContext = db.Topics.GetRepository(); await dbContext.UpsertTopic(instance); //标记成功返回真 return(true); }
/// <summary> /// 新增消息 /// </summary> /// <param name="message">消息</param> /// <returns>结果</returns> public async Task <bool> AddMessage(ChatMessage message) { //判断消息是否已存在 //if (instance.MessageList.Contains(message)) //{ //已存在则添加失败返回假 // return false; //} int newSeqId = message.SeqId; int oldSeqId = instance.MessageList.Count == 0 ? instance.MinLocalSeqId : instance.MessageList[0].SeqId; message.TopicName = instance.Name; //判断是否为初次添加话题 if (oldSeqId == 0) { instance.MessageList.Add(message); //更新本地消息序号 instance.MinLocalSeqId = newSeqId; instance.MaxLocalSeqId = newSeqId; //判断是否为历史消息 } else if (oldSeqId >= newSeqId) { instance.MessageList.Insert(0, message); instance.MinLocalSeqId = newSeqId; } else { instance.MessageList.Add(message); instance.MaxLocalSeqId = newSeqId; } //调用消息控制器添加消息 MessageController messageController = new MessageController(db.Messages); messageController.SetMessage(message); messageController.UpsertMessage(); //话题更新时间戳 instance.Recieve = message.SeqId; instance.LastUsed = ChatMessageBuilder.GetTimeStamp(); var dbContext = db.Topics.GetRepository(); await dbContext.UpsertTopic(instance); //添加成功返回真 return(true); }
/// <summary> /// 设置话题对象 /// </summary> /// <param name="topic">话题</param> /// <returns></returns> public async Task SetTopic(Topic topic) { instance = topic; //初始化消息列表 MessageController messageController = new MessageController(db.Messages); List <ChatMessage> messages = await messageController.GetMessages(instance, instance.Limit); messages.ForEach(async m => await AddMessage(m)); //初始化话题订阅者列表 SubscriberController subscriberController = new SubscriberController(db.Subscribers); instance.SubsriberList = await subscriberController.GetSubscribers(instance); }
/// <summary> /// 加载历史消息 /// </summary> /// <returns></returns> public async void LoadMessage() { //获得起始序号与截止序号 int before = instance.MessageList.Count == 0 ? instance.MinLocalSeqId : instance.MessageList[0].SeqId; int since = (before - instance.Limit) > 0 ? before - instance.Limit : 0; //判断范围 if (since >= instance.MinLocalSeqId) { //全部消息从数据库中加载 MessageController messageController = new MessageController(db.Messages); List <ChatMessage> messages = await messageController.GetMessages(instance, since, before) as List <ChatMessage>; messages.ForEach(async m => await AddMessage(m)); } else if (before > instance.MinLocalSeqId) { //从数据加载部分消息 MessageController messageController = new MessageController(db.Messages); List <ChatMessage> messages = await messageController.GetMessages(instance, since, before) as List <ChatMessage>; messages.ForEach(async m => await AddMessage(m)); //更新截止序号 before = instance.MinLocalSeqId; //剩余消息请求服务器 client.Load(instance, since, before); } else { //全部消息请求服务器 client.Load(instance, since, before); } //话题更新时间戳 instance.LastUsed = ChatMessageBuilder.GetTimeStamp(); var dbContext = db.Topics.GetRepository(); await dbContext.UpsertTopic(instance); }
/// <summary> /// 获取消息控制器 /// </summary> /// <returns>消息控制器</returns> public MessageController GetMessageController() { MessageController messageController = new MessageController(db.Messages); return messageController; }
/// <summary> /// 删除消息 /// </summary> /// <param name="message">消息</param> /// <returns>结果</returns> public async Task <bool> RemoveMessage(ChatMessage message) { //判断消息是否位于消息列表中 if (!instance.MessageList.Contains(message)) { //消息不存在返回假 return(false); } //判断是否删除最新消息 if (message.SeqId == instance.MaxLocalSeqId) { int size = instance.MessageList.Count; //判断是否删除完后消息列表为空 if (size <= 1) { //为空本地最大序号置为0 instance.MaxLocalSeqId = 0; } else { //不空更新本地最大序号 instance.MaxLocalSeqId = instance.MessageList[size - 2].SeqId; } } //判断是否删除最先消息 if (message.SeqId == instance.MinLocalSeqId) { int size = instance.MessageList.Count; //判断是否删除完后消息列表为空 if (size <= 1) { //为空本地最小序号置为0 instance.MinLocalSeqId = 0; } else { //不空更新本地最小序号 instance.MinLocalSeqId = instance.MessageList[1].SeqId; } } //调用消息控制器删除消息 MessageController messageController = new MessageController(db.Messages); messageController.SetMessage(message); messageController.DeleteMessage(); //更新话题时间戳 instance.Clear = message.SeqId; instance.MessageList.Remove(message); instance.LastUsed = ChatMessageBuilder.GetTimeStamp(); var dbContext = db.Topics.GetRepository(); await dbContext.UpsertTopic(instance); //向服务器发送删除消息请求 client.RemoveMessage(instance, message); //删除成功返回真 return(true); }