예제 #1
0
        /// <summary>
        /// Get all active chats for user.
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        public async Task <ChatResponse> GetActiveChats(ApplicationUser user)
        {
            var chats = new List <Chat>();

            var userChats = await _repository.GetActiveChatsByUserId(user.Id);

            foreach (var userChat in userChats)
            {
                var chat = await _repository.FindChatById(userChat.ChatId);

                if (chat == null)
                {
                    return(ChatResponse.Unsuccessful("Error finding chat."));
                }

                chat.Name = await SetChatName(chat, user);

                chats.Add(chat);
            }

            chats = chats
                    .OrderByDescending(x => x.LastActivityAt)
                    .ToList();

            return(ChatResponse.Successfull(chats));
        }
예제 #2
0
        /// <summary>
        /// Join user to chat that
        /// already exists.
        /// </summary>
        /// <param name="chatId"></param>
        /// <param name="userId"></param>
        /// <returns></returns>
        public async Task <ChatResponse> JoinUserToChat(string chatId, string userId)
        {
            var realChatId = Guid.Parse(chatId);

            var userChat = new ApplicationUserChat
            {
                ChatId = realChatId,
                UserId = userId
            };

            var alreadyJoined = _repository.CheckUserChatExistance(userChat);

            if (alreadyJoined)
            {
                return(ChatResponse.Successfull());
            }

            var success = await _repository.SaveUserChat(userChat);

            if (!success)
            {
                return(ChatResponse.Unsuccessful("Error saving userchat relation."));
            }

            return(ChatResponse.Successfull());
        }
예제 #3
0
        /// <summary>
        /// Find chat by chatId.
        /// </summary>
        /// <param name="chatId"></param>
        /// <returns></returns>
        public async Task <ChatResponse> FindChatByChatId(Guid chatId)
        {
            var chat = await _db.Chats
                       .Include(x => x.Messages).ThenInclude(x => x.ApplicationUser)
                       .Include(x => x.ApplicationUserChats)
                       .FirstOrDefaultAsync(x => x.ChatId == chatId);

            if (chat == null)
            {
                return(ChatResponse.Unsuccessful("Chat not found."));
            }

            return(ChatResponse.Successfull(chat));
        }
예제 #4
0
        /// <summary>
        /// Process new chat.
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public async Task <ChatResponse> ProcessNewChat(NewChatDto data)
        {
            var users = await GetChatUsers(data.Users, data.AdminId);

            data.ApplicationUsers = users;

            var chat = _mapper.Map <Chat>(data);

            chat.ApplicationUserChats = SetChatUsers(chat, users);

            var success = await _repository.SaveChat(chat);

            if (!success)
            {
                return(ChatResponse.Unsuccessful("Error saving chat."));
            }

            return(ChatResponse.Successfull(chat));
        }
예제 #5
0
        /// <summary>
        /// Remove user from
        /// related chat.
        /// </summary>
        /// <param name="chatId"></param>
        /// <param name="userId"></param>
        /// <returns></returns>
        public async Task <ChatResponse> RemoveUserFromChat(string chatId, string userId)
        {
            var realChatId = Guid.Parse(chatId);

            var userChat = new ApplicationUserChat
            {
                ChatId = realChatId,
                UserId = userId
            };

            var success = await _repository.DeleteUserChat(userChat);

            if (!success)
            {
                return(ChatResponse.Unsuccessful("Error removing userchat relation."));
            }

            return(ChatResponse.Successfull());
        }
예제 #6
0
        /// <summary>
        /// Get archived chats
        /// for logged user.
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public async Task <ChatResponse> GetArchivedChats(string userId)
        {
            var chats = new List <Chat>();

            var userChats = await _repository.GetArchivedChatsByUserId(userId);

            foreach (var userChat in userChats)
            {
                var chat = await _repository.FindChatById(userChat.ChatId);

                if (chat == null)
                {
                    return(ChatResponse.Unsuccessful("Error finding chat."));
                }

                chats.Add(chat);
            }

            return(ChatResponse.Successfull(chats));
        }
예제 #7
0
        /// <summary>
        /// Update last activity
        /// for chat after new
        /// received message.
        /// </summary>
        /// <param name="chatId"></param>
        /// <returns></returns>
        public async Task <ChatResponse> RefreshChatActivity(Guid chatId)
        {
            var chat = await _repository.FindChatById(chatId);

            if (chat == null)
            {
                return(ChatResponse.Unsuccessful("Chat not found."));
            }

            chat.LastActivityAt = DateTime.Now;

            var success = await _repository.UpdateChat(chat);

            if (!success)
            {
                return(ChatResponse.Unsuccessful("Error updating chat."));
            }

            return(ChatResponse.Successfull());
        }
예제 #8
0
        /// <summary>
        /// Archive chat for user.
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="chatId"></param>
        /// <returns></returns>
        public async Task <ChatResponse> ArchiveChat(string userId, string chatId)
        {
            var realChatId = Guid.Parse(chatId);

            var relation = await _repository.FindChatUserRelation(userId, realChatId);

            if (relation == null)
            {
                return(ChatResponse.Unsuccessful("Chat not found"));
            }

            relation.Archived = true;

            var success = await _repository.UpdateUserChat(relation);

            if (!success)
            {
                return(ChatResponse.Unsuccessful("Error updating user chat relation."));
            }

            return(ChatResponse.Successfull());
        }
예제 #9
0
        /// <summary>
        /// Get chat by
        /// sent chatId.
        /// </summary>
        /// <param name="chatId"></param>
        /// <param name="loggedUser"></param>
        /// <returns></returns>
        public async Task <ChatResponse> GetChat(Guid chatId, ApplicationUser loggedUser)
        {
            var response = await _repository.FindChatByChatId(chatId);

            if (!response.Success)
            {
                return(ChatResponse.Unsuccessful(response.ErrorMessage));
            }

            response.Chat.Name = await SetChatName(response.Chat, loggedUser);

            var chatDetails = _mapper.Map <ChatDetailsDto>(response.Chat);

            var chatUsers = _repository.GetChatUsers(response.Chat.ApplicationUserChats);

            chatUsers.Remove(loggedUser);

            var users = _mapper.Map <List <UserDetailsDto> >(chatUsers);

            chatDetails.Users = users;

            return(ChatResponse.Successfull(chatDetails));
        }