Exemplo n.º 1
0
        public void Send(string name, string message, string recipientName)
        {
            ChatRoomMessageService messageService = new ChatRoomMessageService();
            UsersService userService = new UsersService();
            ChatRoomsService roomService = new ChatRoomsService();

            if (recipientName != null)
            {
                ChatRoomMessage chatRoomMessage = new ChatRoomMessage();
                User sender = userService.FindUserByUsername(name);
                User recipient = userService.FindUserByUsername(recipientName);
                if (sender != null && recipient != null)
                {
                    chatRoomMessage.CreatedBy = sender.UserId;
                    chatRoomMessage.DateCreated = DateTime.Now;
                    chatRoomMessage.Message = message;
                    chatRoomMessage.SentTo = recipient.UserId;
                    int roomId = roomService.FindRoomIdForPrivateChat(chatRoomMessage);
                    if (roomId != -1)
                    {
                        chatRoomMessage.RoomId = roomId;
                    }
                    else
                    {
                        ChatRoom chatRoom = new ChatRoom();
                        chatRoom.CreatedBy = sender.UserId;
                        chatRoom.DateCreated = DateTime.Now;
                        chatRoom.IsDeleted = false;
                        chatRoom.RoomDescription = "Private chat";
                        chatRoom.RoomName = "Private_Chat";
                        chatRoom.IsPrivate = true;
                        int currentRoomId = roomService.CreateWithId(chatRoom);
                        chatRoomMessage.RoomId = currentRoomId;
                    }
                    messageService.Create(chatRoomMessage);
                    previousMessages = messageService.FindAllMessagesForCurrentRoom(chatRoomMessage.RoomId);
                    Clients.User(recipientName).addNewMessageToPage(name, message, recipientName, previousMessages);
                }
            }
            else
            {
                ChatRoomMessage chatRoomMessage = new ChatRoomMessage();
                User sender = userService.FindUserByUsername(name);
                if (sender != null)
                {
                    chatRoomMessage.CreatedBy = sender.UserId;
                    chatRoomMessage.DateCreated = DateTime.Now;
                    chatRoomMessage.Message = message;
                    chatRoomMessage.SentTo = null;
                    chatRoomMessage.RoomId = 1; // MASTER ROOM

                    messageService.Create(chatRoomMessage);
                    previousMessages = messageService.FindAllMessagesForCurrentRoom(chatRoomMessage.RoomId);
                    Clients.All.addNewMessageToPage(name, message, recipientName, previousMessages);
                }
            }
        }
Exemplo n.º 2
0
        public void InitialCheckUp()
        {
            ChatRoomMessageService messageService = new ChatRoomMessageService();
            ChatRoomsService roomService = new ChatRoomsService();
            List<ChatMessageModel> previousMessages = new List<ChatMessageModel>();
            ChatRoomMessage chatRoomMessage = new ChatRoomMessage();

            previousMessages = messageService.FindAllMessagesForCurrentRoom(1);
            Clients.All.addNewMessageToPageInitial(previousMessages);
        }
 public object FindById(int messageId)
 {
     ChatRoomMessage message = new ChatRoomMessage();
     message = context.ChatRoomMessages.Find(messageId);
     return message;
 }
        /// <summary>
        /// Return a RoomId for new message.
        /// </summary>
        /// <param name="currentMessage"> Message for sending.</param>
        /// <returns>RoomId for currentMessage. If value is -1, need to create new room.</returns>
        public int FindRoomIdForPrivateChat(ChatRoomMessage currentMessage)
        {
            foreach (ChatRoomMessage chatMessage in context.ChatRoomMessages)
            {
                if (isExistPreviousConversation(currentMessage, chatMessage))
                {
                    return chatMessage.RoomId;
                }
            }

            return -1;
        }
 /// <summary>
 /// Check does already exist combination of Created-SentTo combination to get RoomId
 /// </summary>
 /// <param name="currentMessage">Message for sending.</param>
 /// <param name="chatMessage">Loop thought all messages in database.</param>
 /// <returns>True if exist, False if isn't exist.</returns>
 private Boolean isExistPreviousConversation(ChatRoomMessage currentMessage, ChatRoomMessage chatMessage)
 {
     if (chatMessage.CreatedBy.Equals(currentMessage.CreatedBy) && chatMessage.SentTo.Equals(currentMessage.SentTo)
         || chatMessage.CreatedBy.Equals(currentMessage.SentTo) && chatMessage.SentTo.Equals(currentMessage.CreatedBy))
     {
         return true;
     }
     else
     {
         return false;
     }
 }
        public IList<ChatMessageModel> LoadPrivateMessagesHistory(string userFrom, string userTo)
        {
            UsersService userService = new UsersService();
            ChatRoomMessageService msgService = new ChatRoomMessageService();
            User sender = userService.FindUserByUsername(userFrom);
            User recipient = userService.FindUserByUsername(userTo);

            ChatRoomMessage messageTemp = new ChatRoomMessage();
            messageTemp.CreatedBy = sender.UserId;
            messageTemp.SentTo = recipient.UserId;

            int roomId = FindRoomIdForPrivateChat(messageTemp);

            if (roomId != -1)
            {
                return msgService.FindAllMessagesForCurrentRoom(roomId).ToList();
            }

            return null;
        }