Example #1
0
        public async Task <IActionResult> OnPostSendChatInvitationAsync(string returnUrl = null)
        {
            returnUrl ??= Url.Content("~/");

            var currentUser = await _userManager.GetUserAsync(User);

            var chatUser = await _context.Users
                           .FirstOrDefaultAsync(u => u.Id == SendChatInvitationInput.ChatUserID);

            if (currentUser.Id == chatUser.Id)
            {
                return(BadRequest());
            }

            bool hasChat = await _context.Membership
                           .OfType <PersonalGroupMembership>()
                           .Include(m => m.Group)
                           .ThenInclude(g => g.Memberships)
                           .Where(m => m.ChatUserID == currentUser.Id)
                           .AnyAsync(m => m.Group.Memberships.FirstOrDefault(_m => _m.ChatUserID != currentUser.Id).ChatUserID == chatUser.Id);

            if (hasChat)
            {
                return(BadRequest());
            }

            bool eitherInviteSent = await _context.PersonalChatInvitation
                                    .AnyAsync(i => (i.ChatUserID == chatUser.Id && i.SenderID == currentUser.Id) ||
                                              (i.ChatUserID == currentUser.Id && i.SenderID == chatUser.Id));

            if (eitherInviteSent)
            {
                return(BadRequest());
            }

            var invitation = new PersonalChatInvitation()
            {
                ChatUserID = chatUser.Id,
                SenderID   = currentUser.Id,
                DateSent   = DateTime.Now,
                Message    = SendChatInvitationInput.Message
            };

            await _context.PersonalChatInvitation.AddAsync(invitation);

            await _context.SaveChangesAsync();

            await _hubContext.SendNewNotificationAsync(chatUser.Id);

            return(LocalRedirect(returnUrl));
        }
Example #2
0
        public async Task <IActionResult> OnPostInviteToGroupAsync(string returnUrl = null)
        {
            returnUrl ??= Url.Content("~/");

            var chatUser = await _context.Users
                           .Include(u => u.GroupJoinInvitations)
                           .Include(u => u.Memberships)
                           .FirstOrDefaultAsync(u => u.Id == InviteToGroupInput.UserID);

            if (chatUser == null)
            {
                return(LocalRedirect(returnUrl));
            }

            if (!chatUser.UserName.Equals(InviteToGroupInput.UserName))
            {
                return(LocalRedirect(returnUrl));
            }

            var group = await _context.Group
                        .OfType <MultiuserGroup>()
                        .Include(g => g.BannedUsers)
                        .FirstOrDefaultAsync(g => g.GroupID == InviteToGroupInput.GroupID);

            if (group == null)
            {
                return(NotFound());
            }

            if (chatUser.GroupJoinInvitations.FirstOrDefault(i => i.GroupID == group.GroupID) != null ||
                chatUser.Memberships.FirstOrDefault(m => m.GroupID == group.GroupID) != null ||
                group.BannedUsers.FirstOrDefault(u => u.Id == chatUser.Id) != null)
            {
                return(LocalRedirect(returnUrl));
            }

            GroupJoinInvitation invitation = new GroupJoinInvitation()
            {
                GroupID  = group.GroupID,
                DateSent = DateTime.Now,
                Message  = InviteToGroupInput.Message
            };

            chatUser.GroupJoinInvitations.Add(invitation);
            await _context.SaveChangesAsync();

            await _hubContext.SendNewNotificationAsync(chatUser.Id);

            return(LocalRedirect(returnUrl));
        }