public async Task <IActionResult> DeleteMessage([FromRoute] ulong guildId, [FromRoute] int id) { await RequirePermission(guildId, DiscordPermission.Moderator); await GetRegisteredGuild(guildId); Identity identity = await GetIdentity(); ScheduledMessageRepository repo = ScheduledMessageRepository.CreateDefault(_serviceProvider, identity); ScheduledMessage message = await repo.GetMessage(id); if (message.GuildId != guildId) { throw new UnauthorizedException(); } // handled messages should only be deletable by an admin to prevent abuse if (message.Status != ScheduledMessageStatus.Pending && !(await identity.HasAdminRoleOnGuild(guildId))) { throw new ProtectedScheduledMessageException(); } await repo.DeleteMessage(message.Id); return(Ok()); }
public async Task <IActionResult> EditMessage([FromRoute] ulong guildId, [FromRoute] int id, [FromBody] ScheduledMessageForPutDto dto) { await RequirePermission(guildId, DiscordPermission.Moderator); await GetRegisteredGuild(guildId); ScheduledMessageRepository repo = ScheduledMessageRepository.CreateDefault(_serviceProvider, await GetIdentity()); ScheduledMessage message = await repo.GetMessage(id); if (message.GuildId != guildId) { throw new UnauthorizedException(); } if (message.Status != ScheduledMessageStatus.Pending) { throw new ProtectedScheduledMessageException(); } if (dto.ScheduledFor < DateTime.UtcNow.AddMinutes(1)) { throw new InvalidDateForScheduledMessageException(); } message.Name = dto.Name; message.Content = dto.Content; message.ScheduledFor = dto.ScheduledFor; message.ChannelId = dto.ChannelId; message = await repo.UpdateMessage(message); return(Ok(new ScheduledMessageView( message, await _discordAPI.FetchUserInfo(message.CreatorId, CacheBehavior.OnlyCache), await _discordAPI.FetchUserInfo(message.LastEditedById, CacheBehavior.OnlyCache), _discordAPI.FetchGuildChannels(guildId, CacheBehavior.OnlyCache).FirstOrDefault(x => x.Id == message.ChannelId) ))); }