Пример #1
0
        private void UpdateLastSeen()
        {
            using (var context = new ApplicationDbContext())
            {
                // get user name
                var userName = Context.User.Identity.Name;

                // get time now
                var lastSeen = DateTime.Now;

                // update chat rooms in memory
                var rooms = from r in ChatRooms.GetAll()
                            where r.User1ID.Equals(userName) || r.User2ID.Equals(userName)
                            select r;
                foreach (var room in rooms)
                {
                    if (room.User1ID.Equals(userName))
                    {
                        room.User1LastSeen = lastSeen;
                    }
                    else
                    {
                        room.User2LastSeen = lastSeen;
                    }
                    Clients.OthersInGroup(room.Name).updateLastSeen(new { Name = room.Name, LastSeen = lastSeen });
                }

                // update user last seen time in database
                var userManager = new UserManager <ApplicationUser>(new UserStore <ApplicationUser>(context));
                var user        = userManager.FindByName(userName);
                user.LastSeen = lastSeen;
                userManager.Update(user);
            }
        }
Пример #2
0
        public override System.Threading.Tasks.Task OnConnected()
        {
            /* Get all the chat rooms and their conversations */
            foreach (var room in ChatRooms.GetAll(Context.User.Identity.Name))
            {
                // Get all the messages in this room
                var mis = (from c in db.Conversations
                           where (c.FromUser.UserName.Equals(room.User1ID) && c.ToUser.UserName.Equals(room.User2ID) ||
                                  (c.FromUser.UserName.Equals(room.User2ID) && c.ToUser.UserName.Equals(room.User1ID)))
                           orderby c.Time ascending
                           select new
                {
                    SenderID = c.FromUser.Id,
                    ReceiverID = c.ToUser.Id,
                    SenderName = c.FromUser.TitleID + " " + c.FromUser.Intials + " " + c.FromUser.Surname,
                    Text = c.Text,
                    Time = c.Time,
                    Read = c.MessageStatus,
                    MessageType = c.MessageType
                }).ToList();
                var messages = (from m in mis
                                select new ChatMessage
                {
                    SenderID = m.SenderID,
                    ReceiverID = (room.User1ID.Equals(Context.User.Identity.Name) ? room.User1DatabaseID : room.User2DatabaseID),
                    SenderName = m.SenderName,
                    Text = m.Text,
                    Time = m.Time.AddHours(2).ToShortDateString() + " @ " + m.Time.AddHours(2).ToShortTimeString(),
                    Read = m.Read,
                    MessageType = m.MessageType
                }).ToArray();

                // Get all products in this room
                var products = (from p in db.ConversationRoomProducts
                                where p.ConversationRoom.Name.Equals(room.Name)
                                select new NegotiationProductModel()
                {
                    ProductID = p.ProductID,
                    Name = p.Product.Name
                }).ToArray();

                // Send everything to the user
                if (room.User1ID.Equals(Context.User.Identity.Name))
                {
                    Clients.Caller.addChatRoom(new
                    {
                        Name          = room.Name,
                        OtherUserID   = room.User2DatabaseID,
                        OtherUserName = room.User2Name,
                        LastSeen      = room.User2LastSeen,
                        Messages      = messages,
                        Products      = products
                    });
                }
                else
                {
                    Clients.Caller.addChatRoom(new
                    {
                        Name          = room.Name,
                        OtherUserID   = room.User1DatabaseID,
                        OtherUserName = room.User1Name,
                        LastSeen      = room.User1LastSeen,
                        Messages      = messages,
                        Products      = products
                    });
                }
            }

            // Update last seen time
            UpdateLastSeen();

            return(base.OnConnected());
        }