public async Task <IActionResult> RemoveUserFromGroup(string adminId, int groupId, string userId) { AppUser user = await userManager.FindByIdAsync(adminId); Group group = repository.Groups.FirstOrDefault(g => g.GroupId == groupId); if (user != null) { repository.RemoveUserFromGroup(groupId, userId); //remove user from chat if (group.GroupChatId != 0) { if (repository.GroupChatUsers .FirstOrDefault(g => g.GroupChatId == group.GroupChatId && g.UserId == userId) != null) { repository.RemoveUserFromGroupChat(group.GroupChatId, userId); } } //remove notification for user Notification notification = repository.Notifications .FirstOrDefault(n => n.For == "Group" && n.ForId == groupId && n.Subject == "NewGroupUser" && n.ForUserId == userId); if (notification != null) { repository.DeleteNotification(notification.NotificationId); } return(RedirectToAction("GroupDetails", new { id = adminId, groupId })); } else { return(RedirectToAction("UserNotFound", "Error")); } }
public async Task GroupChatRemoveUser(int chatId, string userId) { AppUser user = userManager.Users.FirstOrDefault(u => u.Id == userId); GroupChat chat = repository.GroupChats.FirstOrDefault(c => c.ChatId == chatId); AppUser chatAdmin = userManager.Users.FirstOrDefault(u => u.Id == chat.CreatedBy); GroupChatUsers chatUser = repository.GroupChatUsers.FirstOrDefault(cu => cu.GroupChatId == chatId && cu.UserId == userId); repository.RemoveUserFromGroupChat(chatId, user.Id); string msg = ((user.FirstName == null && user.LastName == null) ? user.UserName : $"{user.FirstName} {user.LastName}") + " has left the conversation"; await NewGroupMessage(chatAdmin.Id, chatId, msg, "SystemMsg"); await Clients.All.SendAsync("DeleteChatFromHeader", chatId, userId); await Clients.All.SendAsync("DeleteChatFromIndex", chatId, userId); await Clients.All.SendAsync("DeleteUserFromList", chatId, userId); await Clients.All.SendAsync("ExitChat", chatId, userId); }