コード例 #1
0
        public List <MessageBasicInfoDTO> GetAllMessageByConservationId(int id, string userName)
        {
            List <MessageBasicInfoDTO> messageList = new List <MessageBasicInfoDTO>();
            int UserId = 0;

            using (var db = new UserDAL())
            {
                UserId = db.GetUserByUserNameOrEmail(userName).UserID;
            }
            using (var db = new Ws_DataContext())
            {
                var list = db.Message.Where(x => x.ConservationId == id).ToList();
                foreach (var item in list)
                {
                    MessageBasicInfoDTO current = new MessageBasicInfoDTO();
                    //Set time
                    if (DateTime.Now.Subtract(item.CreatedDate).TotalHours <= 24 && DateTime.Now.Subtract(item.CreatedDate).TotalHours >= 1)
                    {
                        current.CreatedDate = Math.Round(DateTime.Now.Subtract(item.CreatedDate).TotalHours, 0) + " Tiếng cách đây";
                    }
                    else if (DateTime.Now.Subtract(item.CreatedDate).TotalHours > 24)
                    {
                        current.CreatedDate = item.CreatedDate.ToString("H:mm:ss dd/MM/yy");
                    }
                    else if (DateTime.Now.Subtract(item.CreatedDate).Minutes == 0)
                    {
                        current.CreatedDate = "Vừa xong";
                    }
                    else
                    {
                        current.CreatedDate = DateTime.Now.Subtract(item.CreatedDate).Minutes + " Phút cách đây";
                    }
                    //Set other atributes
                    current.CreatorImage = item.User.User_Information.ProfileImage;
                    current.CreatorName  = item.User.UserName;
                    current.Content      = item.Content;
                    messageList.Add(current);
                }
                //Update Is Read for message.
                var currentConversation = db.Conversation.Where(x => x.ConservationId == id).SingleOrDefault();
                if (UserId == currentConversation.CreatorId)
                {
                    currentConversation.IsCreatorRead = true;
                }
                if (UserId == currentConversation.ReceiverId)
                {
                    currentConversation.IsReceiverRead = true;
                }
                db.Conversation.AddOrUpdate(currentConversation);
                db.SaveChanges();
            }
            return(messageList);
        }
コード例 #2
0
ファイル: GlobalHub.cs プロジェクト: sangnvus/201609JS01
        public void SendMessageInRoom(int eventId, string mess)
        {
            MessageBasicInfoDTO returnedMessage = new MessageBasicInfoDTO();
            int UserId = 0;

            using (var db = new UserDAL())
            {
                UserId = db.GetUserByUserNameOrEmail(HttpContext.Current.User.Identity.Name).UserID;
            }
            string currentConnection = Context.ConnectionId;

            using (var db = new Ws_DataContext())
            {
                //Add mesage to db
                PublicMessageDetail newMessage = new PublicMessageDetail();
                newMessage.UserId      = UserId;
                newMessage.EventId     = eventId;
                newMessage.Message     = mess;
                newMessage.CreatedDate = DateTime.Now;
                newMessage.Status      = true;
                newMessage             = db.PublicMessageDetails.Add(newMessage);
                db.SaveChanges();
                //Set retured message
                returnedMessage.CreatorImage = db.User_Information.SingleOrDefault(x => x.UserID == newMessage.UserId).ProfileImage;
                returnedMessage.Content      = newMessage.Message;
                returnedMessage.CreatedDate  = newMessage.CreatedDate.ToString("H:mm dd/MM");
                returnedMessage.CreatorName  = db.Ws_User.SingleOrDefault(x => x.UserID == newMessage.UserId).UserName;
            }
            //Send it to ALL clients is connecting this room
            using (var db = new Ws_DataContext())
            {
                var list = db.PublicRooms.Where(x => x.EventId == eventId)
                           .Select(x => x.ConnectionRoom.ConnectionString).ToList();
                foreach (var item in list)
                {
                    if (!item.Equals(currentConnection))
                    {
                        Clients.Client(item).ReceivePublicMessage(returnedMessage);
                    }
                }
            }
            //Send it to caller
            Clients.Caller.ReceivePublicMessage(returnedMessage);
        }
コード例 #3
0
ファイル: EventDAL.cs プロジェクト: sangnvus/201609JS01
        public List <MessageBasicInfoDTO> GetAllPublicMessage(int eventId)
        {
            List <MessageBasicInfoDTO> list = new List <MessageBasicInfoDTO>();

            using (var db = new Ws_DataContext())
            {
                var current = (from p in db.PublicMessageDetails
                               where p.EventId == eventId
                               orderby p.CreatedDate
                               select new { p, p.User.User_Information.ProfileImage, p.User.UserName }).ToList();


                foreach (var item in current)
                {
                    MessageBasicInfoDTO mess = new MessageBasicInfoDTO();
                    mess.Content      = item.p.Message;
                    mess.CreatedDate  = item.p.CreatedDate.ToString("H:mm dd/MM");
                    mess.CreatorImage = item.ProfileImage;
                    mess.CreatorName  = item.UserName;
                    list.Add(mess);
                }
            }
            return(list);
        }
コード例 #4
0
ファイル: GlobalHub.cs プロジェクト: sangnvus/201609JS01
        public void SendMessage(int ConservationId, string Message)
        {
            int UserId = 0;

            using (var db = new UserDAL())
            {
                UserId = db.GetUserByUserNameOrEmail(HttpContext.Current.User.Identity.Name).UserID;
            }
            Message             newMess = new Message();
            MessageBasicInfoDTO info    = new MessageBasicInfoDTO();

            using (var db = new Ws_DataContext())
            {
                newMess.ConservationId = ConservationId;
                newMess.Content        = Message;
                newMess.CreatedDate    = DateTime.Now;
                newMess.Status         = true;
                newMess.UserId         = UserId;
                newMess = db.Message.Add(newMess);
                db.SaveChanges();
                using (var dbCurrent = new ConservationDAL())
                {
                    Conversation current = dbCurrent.GetConservationById(ConservationId);
                    current.UpdatedTime = DateTime.Now;
                    if (UserId == current.CreatorId)
                    {
                        current.IsReceiverRead = false;
                    }
                    if (UserId == current.ReceiverId)
                    {
                        current.IsCreatorRead = false;
                    }
                    dbCurrent.UpdateTime(current);
                }
                var GetInfo = (from p in db.Message
                               where p.MessageId == newMess.MessageId
                               select new { p.User.UserName, p.User.User_Information.ProfileImage }).SingleOrDefault();
                info.CreatorImage = GetInfo.ProfileImage;
                info.CreatorName  = GetInfo.UserName;
                info.Content      = newMess.Content;
            }


            if (DateTime.Now.Subtract(newMess.CreatedDate).Hours <= 24 && DateTime.Now.Subtract(newMess.CreatedDate).Hours >= 1)
            {
                info.CreatedDate = DateTime.Now.Subtract(newMess.CreatedDate).Hours + " Tiếng cách đây";
            }
            else if (DateTime.Now.Subtract(newMess.CreatedDate).Hours > 24)
            {
                info.CreatedDate = newMess.CreatedDate.ToString("H:mm:ss dd/MM/yy");
            }
            else
            {
                info.CreatedDate = DateTime.Now.Subtract(newMess.CreatedDate).Minutes + " Phút cách đây";
            }
            //Get list connection Id specify by UserId
            List <string> ListConnetion = new List <string>();

            using (var db = new Ws_DataContext())
            {
                //Get Id of Receiver
                var Id = (from p in db.Conversation
                          where p.ConservationId == ConservationId
                          select new { p.CreatorId, p.ReceiverId }).SingleOrDefault();
                if (newMess.UserId == Id.CreatorId)
                {
                    ListConnetion = db.Connection.Where(x => x.UserId == Id.ReceiverId).Select(x => x.ConnectionString).ToList();
                }
                else
                {
                    ListConnetion = db.Connection.Where(x => x.UserId == Id.CreatorId).Select(x => x.ConnectionString).ToList();
                }
                if (ListConnetion.Count() != 0)
                {
                    var currentConversation = db.Conversation.Where(x => x.ConservationId == ConservationId).SingleOrDefault();
                    if (UserId == currentConversation.CreatorId)
                    {
                        currentConversation.IsReceiverRead = true;
                    }
                    if (UserId == currentConversation.ReceiverId)
                    {
                        currentConversation.IsCreatorRead = true;
                    }
                    db.Conversation.AddOrUpdate(currentConversation);
                    db.SaveChanges();
                }
            }
            //Send it to caller
            Clients.Caller.ReceiverMessage(info);
            //Send new Message to Receiver if Connecting

            foreach (var item in ListConnetion)
            {
                Clients.Client(item).NewMessageNotification("Bạn đã nhận 1 tin nhắn mới, xem tại Tin nhắn!");
                Clients.Client(item).ReceiverMessage(info);
            }
        }