Ejemplo n.º 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);
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Send message to the chat room
        /// </summary>
        /// <param name="roomName">the name of the room</param>
        /// <param name="message">the message to send</param>
        /// <param name="senderID">The ID of the sender</param>
        /// <param name="senderName">The name of the sender</param>
        public async Task Send(string roomName, string message, string senderID, string senderName, int messageType)
        {
            // Get the time now (on server)
            DateTime time = DateTime.Now;

            // Get the sender details
            var sender = await(from u in db.Users
                               where u.Id.Equals(senderID)
                               select u).FirstAsync();

            // Get the room
            var room = ChatRooms.getRoom(roomName);

            // Get the receiver details
            var receiverID = room.User1DatabaseID.Equals(senderID) ? room.User2ID : room.User1ID;
            var receiver   = await(from u in db.Users
                                   where u.UserName.Equals(receiverID)
                                   select u).FirstAsync();

            // Get message type
            var mType = messageType == 0 ? MessageType.NORMAL :
                        messageType == 1 ? MessageType.SALEREQUEST : MessageType.SYSTEM;

            // get last message sent by receiver to sender and check if it was a sale request
            //var sendReplyEmail = false;
            //var receiverName = "";
            //var receiverEmail = "";
            //if (mType == MessageType.NORMAL)
            //{
            //    var conv = from c in db.Conversations
            //               where c.FromUser.UserName.Equals(receiverID) && c.ToUser.UserName.Equals(sender.UserName)
            //               orderby c.Time descending
            //               select c;

            //    var count = await conv.CountAsync();
            //    if (count > 0)
            //    {
            //        var con = await conv.FirstAsync();
            //        sendReplyEmail = con.MessageType == MessageType.SALEREQUEST;

            //        if (sendReplyEmail)
            //        {
            //            receiverName = receiver.TitleID + " " + receiver.Intials + " " + receiver.Surname;
            //            receiverEmail = receiver.Email;
            //        }
            //    }
            //}

            // Add conversation
            db.Conversations.Add(new Conversation()
            {
                FromUser      = sender,
                ToUser        = receiver,
                MessageStatus = MessageStatus.UNREAD,
                Text          = message,
                Time          = time,
                MessageType   = mType
            });

            // save changes
            await db.SaveChangesAsync();

            // Send message to the room
            time = time.AddHours(2);

            // send message to receiver
            await Clients.OthersInGroup(roomName).addMessage(roomName, new ChatMessage()
            {
                SenderID    = senderID,
                ReceiverID  = receiver.Id,
                SenderName  = senderName,
                Text        = message,
                Time        = time.ToShortDateString() + " @ " + time.ToShortTimeString(),
                Read        = MessageStatus.UNREAD,
                MessageType = mType
            });

            // update last seen
            UpdateLastSeen();

            // send reply email to receiver
            //if (sendReplyEmail)
            //{
            //    ChatRooms.SendSaleRequestReplyEmail(receiverEmail, receiverName, senderName);
            //}
        }
Ejemplo n.º 3
0
        public async Task CreateChatRoomForLiveChat(string requestMessage, string customerID, string customerName, string supplierID, int productId)
        {
            // (Task) Get more necessary supplier details
            var supplierDetails = await(from u in db.Users
                                        where u.Id.Equals(supplierID)
                                        select new { id = u.UserName, name = u.TitleID + " " + u.Intials + " " + u.Surname, Email = u.Email }).FirstAsync();

            // (Task) Get more details of product involved
            var product = await db.Products.FindAsync(productId);

            // (Task) Get the number of sale requests of the product
            var currentSaleRequestNumber = await(from sr in db.ConversationRoomProducts
                                                 where sr.ProductID == productId
                                                 select sr).CountAsync();

            // Get possible room names
            string roomName      = customerID + "_" + supplierID;
            string roomName2     = supplierID + "_" + customerID;
            var    user1         = db.Users.Find(customerID);
            string customerEmail = user1.Email;

            /*Create new room or add product to existing room*/
            if (ChatRooms.Exists(roomName)) // room exists
            {
                // Get room
                var room    = ChatRooms.getRoom(roomName);
                var conRoom = await(from cr in db.ConversationRooms
                                    where cr.Name.Equals(roomName)
                                    select cr).FirstAsync();

                // Add product to room
                db.ConversationRoomProducts.Add(new ConversationRoomProduct()
                {
                    ConversationRoom = conRoom, Product = product
                });

                // save changes
                await db.SaveChangesAsync();

                // Send sale request message
                await Send(roomName, requestMessage, customerID, customerName, 1);
            }
            else if (ChatRooms.Exists(roomName2)) // room exists
            {
                // Get room
                var room    = ChatRooms.getRoom(roomName2);
                var conRoom = await(from cr in db.ConversationRooms
                                    where cr.Name.Equals(roomName2)
                                    select cr).FirstAsync();

                // Add product to room
                db.ConversationRoomProducts.Add(new ConversationRoomProduct()
                {
                    ConversationRoom = conRoom, Product = product
                });

                // save changes
                await db.SaveChangesAsync();

                // Send sale request message
                await Send(roomName2, requestMessage, customerID, customerName, 1);
            }
            else // room does not exist
            {
                // Get users involved
                var user2 = db.Users.Find(supplierID);

                // Create new room
                var conRoom = new ConversationRoom()
                {
                    User1 = user1, User2 = user2, Name = roomName
                };
                db.ConversationRoomProducts.Add(new ConversationRoomProduct()
                {
                    ConversationRoom = conRoom, Product = product
                });
                Room room = new Room()
                {
                    Name            = roomName,
                    User1ID         = Context.User.Identity.Name,
                    User1Name       = customerName,
                    User1LastSeen   = user1.LastSeen,
                    User1DatabaseID = customerID,
                    User2ID         = supplierDetails.id,
                    User2Name       = supplierDetails.name,
                    User2LastSeen   = user2.LastSeen,
                    User2DatabaseID = supplierID,
                };

                // Add room to list of rooms
                ChatRooms.Add(room);

                // Join room
                await Join(room.Name);

                // Send room details to client (browser)
                await Clients.User(supplierDetails.id).addChatRoom(new
                {
                    Name          = room.Name,
                    OtherUserID   = room.User2DatabaseID,
                    OtherUserName = room.User2Name,
                    LastSeen      = room.User2LastSeen,
                    Messages      = new { },
                    Products      = new { }
                });

                // save changes
                await db.SaveChangesAsync();

                // Send sale request message
                await Send(roomName, requestMessage, customerID, customerName, 1);
            }

            // Send a sale request email to the supplier
            var supplierEmail = (string.IsNullOrEmpty(product.ContactEmail) || string.IsNullOrWhiteSpace(product.ContactEmail)) ? supplierDetails.Email : product.ContactEmail;

            ChatRooms.SendChatRequestEmail(supplierEmail, customerEmail, product.Name, requestMessage);
        }
Ejemplo n.º 4
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());
        }