示例#1
0
        public async Task <ActionResult> Leave(int chatId)
        {
            var username = _userManager.GetUserName(HttpContext.User);

            _logger.LogInformation(LoggingEvents.DeleteItem, "{username} leaving chat ({id}).", username, chatId);
            var user = await _userManager.GetUserAsync(User);

            if (user == null)
            {
                _logger.LogWarning(LoggingEvents.DeleteItem, "{username} failed leaving chat ({id}).", username, chatId);
                return(BadRequest());
            }

            var result = (await _chatService.RemoveUsersFromChatAsync(chatId, user.Id));

            if (result)
            {
                await _chatHub.Clients.Group(chatId.ToString()).Leave(chatId, new User(user));
                await SendUpdateMessage(chatId, user, UpdateMessageType.LEAVE);

                await _chatHubState.RemoveUserFromGroupAsync(_chatHub, user.Id, chatId.ToString());

                return(NoContent());
            }

            _logger.LogWarning(LoggingEvents.DeleteItem, "{username} failed leaving chat ({id}).", username, chatId);
            return(BadRequest());
        }
示例#2
0
        public async Task <IActionResult> RemoveUserFromDepartment(int departmentId, [FromBody] string userId)
        {
            var username = _userManager.GetUserName(HttpContext.User);

            _logger.LogInformation(LoggingEvents.InsertItem, "{username} removing user from department ({id}).", username, departmentId);

            var user = await _userManager.FindByIdAsync(userId);

            if (user == null)
            {
                _logger.LogWarning(LoggingEvents.InsertItemNotFound, "{usernamed} failed to remove user from department ({id}), since the user does not exist.", username, departmentId);
                return(NotFound("User"));
            }

            var result = await _departmentService.RemoveUsersFromDepartmentAsync(departmentId, user);

            if (result)
            {
                var chatIds = (await _chatService.GetChatsAsync(userId, departmentId)).Select(c => c.Id);

                foreach (var chatId in chatIds)
                {
                    await _chatService.RemoveUsersFromChatAsync(chatId, userId);

                    await _chatHub.Clients.Group(chatId.ToString()).Remove(chatId, new User(user));

                    await _chatHubState.RemoveUserFromGroupAsync(_chatHub, userId, chatId.ToString());
                }

                _logger.LogInformation(LoggingEvents.InsertItem, "{username} removing {other} from department ({id}).", username, user.UserName, departmentId);
                return(NoContent());
            }
            _logger.LogWarning(LoggingEvents.InsertItem, "{username} failed removing {other} from department ({id}).", username, user.UserName, departmentId);
            return(BadRequest());
        }