Ejemplo n.º 1
0
        private async Task <bool> TryBlockUnblockContent <T>(long itemId, string reason, long uid) where T : BaseModel, IBlockableContent
        {
            var modId = User.GetNumericId();

            if (modId is null)
            {
                return(false);
            }
            var luid = (long)modId;

            var item = await _context.Set <T>()
                       .Where(i => i.Id == itemId)
                       .Include(i => i.ContentBlock)
                       .FirstOrDefaultAsync();

            if (item is null)
            {
                return(false);
            }

            var title = item switch
            {
                Story s => s.Title,
                Chapter c => c.Title,
                Blogpost b => b.Title,
                _ => ""
            };

            if (item.ContentBlock is null)
            {
                item.ContentBlock = new Data.Models.ContentBlock
                {
                    Reason   = reason,
                    IssuerId = uid,
                    Type     = typeof(T).Name
                };

                // Log the action
                await _context.ModeratorActions.AddAsync(new ModeratorAction
                {
                    StaffMemberId = luid,
                    Description   = ModeratorActionTemplates.ContentBlocked(Type, title, itemId, User.GetUsername())
                });
            }
            else
            {
                _context.ContentBlocks.Remove(item.ContentBlock);

                await _context.ModeratorActions.AddAsync(new ModeratorAction
                {
                    StaffMemberId = luid,
                    Description   = ModeratorActionTemplates.ContentUnblocked(Type, title, itemId, User.GetUsername())
                });
            }

            await _context.SaveChangesAsync();

            return(true);
        }
Ejemplo n.º 2
0
        public async Task <ActionResult> ManageRoles(RoleData data)
        {
            var(userId, roles) = data;

            // Check if user is logged in
            if (_uid is null)
            {
                return(Unauthorized());
            }
            var uid = (long)_uid;

            var user = await _context.Users
                       .Where(u => u.Id == userId)
                       .Include(u => u.Roles)
                       .FirstOrDefaultAsync();

            var newRoles = await _context.OgmaRoles
                           .Where(ur => roles.Contains(ur.Id))
                           .ToListAsync();

            // Handle role removal
            var removedRoles = user.Roles.Except(newRoles, new OgmaRoleComparer()).ToList();

            foreach (var role in removedRoles)
            {
                user.Roles.Remove(role);
            }
            await _context.ModeratorActions.AddRangeAsync(removedRoles.Select(r => new ModeratorAction
            {
                StaffMemberId = uid,
                Description   = ModeratorActionTemplates.UserRoleRemoved(user, User.GetUsername(), r.Name)
            }));

            // Handle role adding
            var addedRoles = newRoles.Except(user.Roles, new OgmaRoleComparer()).ToList();

            foreach (var role in addedRoles)
            {
                user.Roles.Add(role);
            }
            await _context.ModeratorActions.AddRangeAsync(addedRoles.Select(r => new ModeratorAction
            {
                StaffMemberId = uid,
                Description   = ModeratorActionTemplates.UserRoleAdded(user, User.GetUsername(), r.Name)
            }));

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                return(BadRequest(e.Message));
            }
            return(Ok());
        }
Ejemplo n.º 3
0
        public async Task <ActionResult> BanUser(BanData data)
        {
            var(userId, days) = data;

            // Check if user is logged in
            if (_uid is null)
            {
                return(Unauthorized());
            }
            var uid = (long)_uid;

            // Get user to be banned
            var user = await _context.Users
                       .Where(u => u.Id == userId)
                       .FirstOrDefaultAsync();

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

            // Ban/unban user
            if (days.HasValue)
            {
                user.BannedUntil = DateTime.Now.AddDays((double)days);
                await _userManager.UpdateSecurityStampAsync(user);

                _context.ModeratorActions.Add(new ModeratorAction
                {
                    StaffMemberId = uid,
                    Description   = ModeratorActionTemplates.UserBan(user, User.GetUsername(), (DateTime)user.BannedUntil)
                });
            }
            else if (user.BannedUntil.HasValue)
            {
                _context.ModeratorActions.Add(new ModeratorAction
                {
                    StaffMemberId = uid,
                    Description   = ModeratorActionTemplates.UserUnban(user, User.GetUsername(), (DateTime)user.BannedUntil)
                });
                user.BannedUntil = null;
            }
            else
            {
                return(BadRequest());
            }

            await _context.SaveChangesAsync();

            return(Ok());
        }
Ejemplo n.º 4
0
        public async Task<ActionResult> MuteUser(BanData data)
        {
            var (userId, days) = data;
            
            // Check if user is logged in
            if (_uid is null) return Unauthorized();
            var uid = (long)_uid;
            
            // Get user to be muted
            var user = await _context.Users
                .Where(u => u.Id == userId)
                .FirstOrDefaultAsync();
            if (user == null) return NotFound();

            // Mute/unmute user
            if (days.HasValue && !user.MutedUntil.HasValue)
            {
                user.MutedUntil = DateTime.Now.AddDays((double) days);
                await _context.ModeratorActions.AddAsync(new ModeratorAction
                {
                    StaffMemberId = uid,
                    Description = ModeratorActionTemplates.UserMute(user, User.GetUsername(), (DateTime) user.MutedUntil)
                });
            }
            else if (user.MutedUntil.HasValue)
            {
                await _context.ModeratorActions.AddAsync(new ModeratorAction
                {
                    StaffMemberId = uid,
                    Description = ModeratorActionTemplates.UserUnmute(user, User.GetUsername(), (DateTime) user.MutedUntil)
                });
                user.MutedUntil = null;
            }
            else
            {
                return BadRequest();
            }
            
            await _context.SaveChangesAsync();
            return Ok();
        }