Ejemplo n.º 1
0
        public async Task <IActionResult> DeleteSpecificItem([FromRoute] string guildid, [FromRoute] string modcaseid, [FromQuery] bool sendNotification = true, [FromQuery] bool handlePunishment = true)
        {
            logger.LogInformation($"{HttpContext.Request.Method} {HttpContext.Request.Path} | Incoming request.");
            Identity currentIdentity = await identityManager.GetIdentity(HttpContext);

            User currentUser = await currentIdentity.GetCurrentDiscordUser();

            if (currentUser == null)
            {
                logger.LogInformation($"{HttpContext.Request.Method} {HttpContext.Request.Path} | 401 Unauthorized.");
                return(Unauthorized());
            }
            if (!await currentIdentity.HasModRoleOrHigherOnGuild(guildid, this.database) && !config.Value.SiteAdminDiscordUserIds.Contains(currentUser.Id))
            {
                logger.LogInformation($"{HttpContext.Request.Method} {HttpContext.Request.Path} | 401 Unauthorized.");
                return(Unauthorized());
            }
            // ========================================================

            if (await database.SelectSpecificGuildConfig(guildid) == null)
            {
                logger.LogInformation($"{HttpContext.Request.Method} {HttpContext.Request.Path} | 400 Guild not registered.");
                return(BadRequest("Guild not registered."));
            }

            ModCase modCase = await database.SelectSpecificModCase(guildid, modcaseid);

            if (modCase == null)
            {
                logger.LogInformation($"{HttpContext.Request.Method} {HttpContext.Request.Path} | 404 ModCase not found.");
                return(NotFound());
            }

            try {
                filesHandler.DeleteDirectory(Path.Combine(config.Value.AbsolutePathToFileUpload, guildid, modcaseid));
            } catch (Exception e) {
                logger.LogError(e, "Failed to delete files directory for modcase.");
            }

            database.DeleteSpecificModCase(modCase);
            await database.SaveChangesAsync();

            if (handlePunishment)
            {
                try {
                    logger.LogInformation($"{HttpContext.Request.Method} {HttpContext.Request.Path} | Handling punishment.");
                    await punishmentHandler.UndoPunishment(modCase, database);
                }
                catch (Exception e) {
                    logger.LogError(e, "Failed to handle punishment for modcase.");
                }
            }

            logger.LogInformation($"{HttpContext.Request.Method} {HttpContext.Request.Path} | Sending notification.");
            try {
                await discordAnnouncer.AnnounceModCase(modCase, RestAction.Deleted, currentUser, sendNotification);
            }
            catch (Exception e) {
                logger.LogError(e, "Failed to announce modcase.");
            }

            logger.LogInformation($"{HttpContext.Request.Method} {HttpContext.Request.Path} | 200 Deleted ModCase.");
            return(Ok(new { id = modCase.Id, caseid = modCase.CaseId }));
        }
        public async Task <IActionResult> CreateItem([FromRoute] string guildid, [FromBody] AutoModerationEventForCreateDto dto)
        {
            logger.LogInformation($"{HttpContext.Request.Method} {HttpContext.Request.Path} | Incoming request.");
            if (string.IsNullOrEmpty(this.config.Value.DiscordBotToken))
            {
                logger.LogInformation($"{HttpContext.Request.Method} {HttpContext.Request.Path} | 401 Authorization header not defined.");
                return(Unauthorized());
            }

            string auth = String.Empty;

            try {
                auth = Request.Headers["Authorization"];
            } catch (Exception e) {
                logger.LogInformation($"{HttpContext.Request.Method} {HttpContext.Request.Path} | 401 Authorization header not defined.", e);
                return(Unauthorized());
            }
            if (this.config.Value.DiscordBotToken == auth)
            {
                AutoModerationConfig modConfig = await this.database.SelectModerationConfigForGuildAndType(guildid, dto.AutoModerationType);

                if (modConfig == null)
                {
                    logger.LogInformation($"{HttpContext.Request.Method} {HttpContext.Request.Path} | 400 No config found for this type.");
                    return(BadRequest("No config found for this type"));
                }

                AutoModerationEvent modEvent = new AutoModerationEvent();
                if (modConfig.AutoModerationAction == AutoModerationAction.CaseCreated || modConfig.AutoModerationAction == AutoModerationAction.ContentDeletedAndCaseCreated)
                {
                    ModCase newModCase = new ModCase();

                    User discordBot = await discord.FetchCurrentBotInfo();

                    newModCase.CaseId = await database.GetHighestCaseIdForGuild(guildid) + 1;

                    newModCase.GuildId           = guildid;
                    newModCase.UserId            = dto.UserId;
                    newModCase.Username          = dto.Username;
                    newModCase.Discriminator     = dto.Discriminator;
                    newModCase.Nickname          = dto.Nickname;
                    newModCase.ModId             = discordBot.Id;
                    newModCase.CreatedAt         = DateTime.UtcNow;
                    newModCase.LastEditedAt      = newModCase.CreatedAt;
                    newModCase.LastEditedByModId = discordBot.Id;
                    newModCase.OccuredAt         = newModCase.CreatedAt;
                    newModCase.Title             = $"AutoModeration: {dto.AutoModerationType.ToString()}";
                    newModCase.Description       = $"User triggered AutoModeration.\nEvent: {dto.AutoModerationType.ToString()}.\nAction: {modConfig.AutoModerationAction.ToString()}\nMessageId: {dto.MessageId}.\nMessage content: {dto.MessageContent}.";
                    newModCase.Labels            = new List <string>()
                    {
                        "automoderation", dto.AutoModerationType.ToString()
                    }.ToArray();
                    newModCase.Valid = true;

                    if (modConfig.PunishmentType != null && modConfig.PunishmentType != PunishmentType.None)
                    {
                        newModCase.PunishmentType   = modConfig.PunishmentType.Value;
                        newModCase.PunishmentActive = true;

                        if (modConfig.PunishmentDurationMinutes == null)
                        {
                            newModCase.Punishment    = newModCase.PunishmentType.ToString();
                            newModCase.PunishedUntil = null;
                        }
                        else
                        {
                            newModCase.Punishment    = "Temp" + newModCase.PunishmentType.ToString();
                            newModCase.PunishedUntil = DateTime.UtcNow.AddMinutes(modConfig.PunishmentDurationMinutes.Value);
                        }

                        try {
                            logger.LogInformation($"{HttpContext.Request.Method} {HttpContext.Request.Path} | Handling punishment.");
                            await punishmentHandler.ExecutePunishment(newModCase, database);
                        }
                        catch (Exception e) {
                            logger.LogError(e, "Failed to handle punishment for modcase.");
                        }
                    }
                    else
                    {
                        newModCase.Punishment       = "Warn";
                        newModCase.PunishmentType   = PunishmentType.None;
                        newModCase.PunishedUntil    = null;
                        newModCase.PunishmentActive = false;
                    }

                    await database.SaveModCase(newModCase);

                    await database.SaveChangesAsync();

                    modEvent.AssociatedCaseId = newModCase.CaseId;

                    logger.LogInformation($"{HttpContext.Request.Method} {HttpContext.Request.Path} | Sending notification.");
                    try {
                        await discordAnnouncer.AnnounceModCase(newModCase, RestAction.Created, discordBot, modConfig.SendPublicNotification);
                    }
                    catch (Exception e) {
                        logger.LogError(e, "Failed to announce modcase.");
                    }
                }

                modEvent.GuildId              = guildid;
                modEvent.CreatedAt            = DateTime.UtcNow;
                modEvent.UserId               = dto.UserId;
                modEvent.Username             = dto.Username;
                modEvent.Nickname             = dto.Nickname;
                modEvent.Discriminator        = dto.Discriminator;
                modEvent.MessageContent       = dto.MessageContent;
                modEvent.MessageId            = dto.MessageId;
                modEvent.AutoModerationType   = dto.AutoModerationType;
                modEvent.AutoModerationAction = modConfig.AutoModerationAction;

                await database.SaveModerationEvent(modEvent);

                await database.SaveChangesAsync();

                return(Ok(new { Id = modEvent.Id }));
            }
            else
            {
                logger.LogInformation($"{HttpContext.Request.Method} {HttpContext.Request.Path} | 401 Unauthorized.");
                return(Unauthorized());
            }
        }