Example #1
0
        public async Task <IActionResult> OnPostConfirmAsync(int?userId, int?groupId, int?membershipId)
        {
            if (groupId is null || userId is null)
            {
                return(BadRequest());
            }

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

            var chatUser = await _context.Users
                           .FirstOrDefaultAsync(u => u.Id == userId.Value);

            var currentUser = await _userManager.GetUserAsync(User);

            var currentMembership = await _context.Membership
                                    .OfType <MultiuserGroupMembership>()
                                    .FirstOrDefaultAsync(m => m.GroupID == group.GroupID && m.ChatUserID == currentUser.Id);

            if (currentMembership is null)
            {
                return(BadRequest());
            }

            var membership = await _context.Membership
                             .OfType <MultiuserGroupMembership>()
                             .FirstOrDefaultAsync(m => m.MembershipID == membershipId);

            if (membership.ChatUserID != chatUser.Id || membership.GroupID != group.GroupID || currentMembership.Rank <= membership.Rank)
            {
                return(BadRequest());
            }

            _context.Membership.Remove(membership);
            group.BannedUsers.Add(chatUser);

            Notification notif = new Notification()
            {
                ChatUserID = chatUser.Id,
                Date       = DateTime.Now,
                Title      = $"Banned from {group.Name}",
                Text       = $"You have been banned from {group.Name}.",
                ViewURL    = $"/Group?groupId={group.GroupID}"
            };

            await _context.Notification.AddAsync(notif);

            await _context.SaveChangesAsync();

            await _hubContext.SendNotificationAsync(notif);

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

            var request = await _context.GroupJoinRequest
                          .FirstOrDefaultAsync(r => r.GroupJoinRequestID == JoinRequestInput.RequestID);

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

            var membership = new MultiuserGroupMembership()
            {
                ChatUserID          = request.ChatUserID,
                GroupID             = request.GroupID,
                IsActiveInGroup     = false,
                IsOnlineInGroup     = false,
                Rank                = PermissionRank.USER.Ordinal,
                LastViewedMessageID = null,
                NumNew              = 0
            };

            await _context.Membership.AddAsync(membership);

            _context.GroupJoinRequest.Remove(request);

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

            var notif = new Notification()
            {
                ChatUserID = request.ChatUserID,
                Date       = DateTime.Now,
                Title      = "Join request accepted",
                Text       = $"Your join request for {group.Name} was accepted.",
                ViewURL    = $"/Group?groupId={group.GroupID}"
            };

            await _context.Notification.AddAsync(notif);

            await _context.SaveChangesAsync();

            await _hubContext.SendNotificationAsync(notif);

            return(LocalRedirect(returnUrl));
        }
Example #3
0
        public async Task <IActionResult> OnPostAcceptPersonalChatInvitationAsync(string returnUrl = null)
        {
            returnUrl ??= Url.Content("~/");
            var currentUser = await _userManager.GetUserAsync(User);

            var chatUser = await _context.Users
                           .FirstOrDefaultAsync(g => g.Id == PersonalChatInvitationInput.ChatUserID);

            if (chatUser is null)
            {
                return(BadRequest());
            }

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

            var invitation = await _context.PersonalChatInvitation
                             .FirstOrDefaultAsync(i => i.PersonalChatInvitationID == PersonalChatInvitationInput.InvitationID);

            if (invitation is null)
            {
                return(NotFound());
            }

            var group = new PersonalGroup()
            {
                DateCreated = DateTime.Now
            };

            await _context.Group.AddAsync(group);

            await _context.SaveChangesAsync();

            var membership1 = new PersonalGroupMembership()
            {
                ChatUserID          = invitation.ChatUserID,
                GroupID             = group.GroupID,
                IsActiveInGroup     = false,
                IsOnlineInGroup     = false,
                LastViewedMessageID = null,
                NumNew = 0
            };

            var membership2 = new PersonalGroupMembership()
            {
                ChatUserID          = invitation.SenderID,
                GroupID             = group.GroupID,
                IsActiveInGroup     = false,
                IsOnlineInGroup     = false,
                LastViewedMessageID = null,
                NumNew = 0
            };

            await _context.Membership.AddAsync(membership1);

            await _context.Membership.AddAsync(membership2);

            _context.PersonalChatInvitation.Remove(invitation);

            Notification notif = new Notification()
            {
                ChatUserID = invitation.SenderID,
                Date       = DateTime.Now,
                Title      = $"{chatUser.UserName} accepted your invite",
                Text       = $"{chatUser.UserName} has accepted your chat invitation.",
                ViewURL    = "/Chat"
            };

            await _context.Notification.AddAsync(notif);

            await _context.SaveChangesAsync();

            await _hubContext.SendNotificationAsync(notif);

            return(LocalRedirect(returnUrl));
        }