Example #1
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);
            //}
        }
Example #2
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);
        }