/// <summary> /// 读取未读信息列表 /// </summary> /// <param name="targetUser">对方的用户名</param> /// <param name="self">自己的用户名</param> /// <returns>返回未读信息的列表</returns> public static List<MessageResult> ReadUnreadMessages(string targetUser, string self) { using (Model2DataContext db = new Model2DataContext()) { List<MessageResult> result = new List<MessageResult>(); List<Message> tList = db.PondOfMessage.Where(x => x.From == targetUser && x.To == self && !x.Readed) .ToList(); tList.ForEach(x => { x.Readed = true; MessageResult mr = new MessageResult(x, x.From == self); result.Add(mr); }); db.SaveChanges(); return result; } }
/// <summary> /// 发送新消息 /// </summary> /// <param name="import">数据集</param> public void SendMessage(SendMessageImport import) { #region 判断发件人是否在线 if (!MyPond.Default.Tokens.Any(x => x.Token.Username == import.From)) { throw new Exception("您可以离线 请重新登陆"); } #endregion #region 判断收件人是否在线并且正在关注发件人 bool targetUserIsOnlineAndLookAtMe = MyPond.Default.Tokens.Any(x => x.Token.Username == import.To && x.TargetUser == import.From); #endregion string ip = WebHelper.GetEndpoint().Address; string address = WebHelper.GetAddress(ip); MessageResult mr = new MessageResult { From = import.From, To = import.To, IsSelf = true, Content = import.Content, IsOfficial = import.IsOfficial, SendTime = DateTime.Now, Ip = ip, Address = address }; #region 向发件人的聊天面板写入聊天信息 try { MyPond.Default.Tokens.First(x => x.Token.Username == import.From).Callback.WriteMessage(mr); } catch (Exception) { MyPond.Default.RemoveToken(import.From); } #endregion #region 想收件人通报新信息 if (targetUserIsOnlineAndLookAtMe) { try { mr.IsSelf = false; MyPond.Default.Tokens.First(x => x.Token.Username == import.To).Callback.WriteMessage(mr); } catch (Exception) { targetUserIsOnlineAndLookAtMe = false; MyPond.Default.RemoveToken(import.To); } } else if (MyPond.Default.Tokens.Any(x => x.Token.Username == import.To)) { try { MyPond.Default.Tokens.First(x => x.Token.Username == import.To).Callback.AddTheCountOfNewMessageForSomeone(import.From); } catch (Exception) { MyPond.Default.RemoveToken(import.To); } } #endregion MessageManager.Send(import.From, import.To, import.Content, ip, address , targetUserIsOnlineAndLookAtMe, import.IsOfficial); }