Ejemplo n.º 1
0
        /// <summary>
        /// 发送消息逻辑, 如果用户不在线就记录未读
        /// </summary>
        /// <param name="msg"></param>
        public async Task SendMessage(TextChatMessageViewModel msg)
        {
            var userOnline = socialMsgManager.HasOnline(msg.ToId.ToString());

            var chatRelationShip = await chatLogDataAccessor.OneAsync <ChatLog>(x =>
                                                                                x.ChatCollection.Any(c => c.UserId == msg.FromId) &&
                                                                                x.ChatCollection.Any(c => c.UserId == msg.ToId), "ChatCollection", "ChatMessageList");

            if (chatRelationShip == null)
            {
                chatRelationShip = new ChatLog
                {
                    Id             = Guid.NewGuid(),
                    ChatCollection = new List <ChatCollection>
                    {
                        new ChatCollection {
                            Id         = Guid.NewGuid(),
                            CreateTime = DateTime.Now,
                            UserId     = msg.FromId
                        },
                        new ChatCollection {
                            Id         = Guid.NewGuid(),
                            CreateTime = DateTime.Now,
                            UserId     = msg.ToId
                        }
                    }
                };

                await chatLogDataAccessor.Add(chatRelationShip);
            }

            var msgModel = new ChatMessage();

            msgModel.MessageContent = msg.Content;
            msgModel.TimeStamp      = TimeStamp.Get();
            msgModel.MsgType        = MessageType.Text;
            msgModel.ChageLog       = chatRelationShip;
            msgModel.FromUserId     = msg.FromId;
            msgModel.ToUserId       = msg.ToId;
            msgModel.CreateTime     = DateTime.Now;
            if (userOnline)
            {
                msgModel.IsReaded = true;
            }
            else
            {
                msgModel.IsReaded = false;
            }
            await chatLogDataAccessor.Add(msgModel);

            //当前
            var jsonSetting = new JsonSerializerSettings();

            jsonSetting.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
            var contentJson = JsonConvert.SerializeObject(msg, jsonSetting);

            socialMsgManager.SendMssage(msg.ToId.ToString(), msg.Content);
        }