<commit_before>﻿using System;
using System.Linq;
using System.Threading.Tasks;
using DSharpPlus;
using DSharpPlus.Entities;
using DSharpPlus.Exceptions;
using Microsoft.EntityFrameworkCore;

namespace CompatBot.Database.Providers
{
    internal static class InviteWhitelistProvider
    {
        public static bool IsWhitelisted(ulong guildId)
        {
            using var db = new BotDb();
            return db.WhitelistedInvites.Any(i => i.GuildId == guildId);
        }

        public static async Task<bool> IsWhitelistedAsync(DiscordInvite invite)
        {
            var code = string.IsNullOrWhiteSpace(invite.Code) ? null : invite.Code;
            var name = string.IsNullOrWhiteSpace(invite.Guild.Name) ? null : invite.Guild.Name;
            using var db = new BotDb();
            var whitelistedInvite = await db.WhitelistedInvites.FirstOrDefaultAsync(i => i.GuildId == invite.Guild.Id);
            if (whitelistedInvite == null)
                return false;

            if (name != null && name != whitelistedInvite.Name)
                whitelistedInvite.Name = invite.Guild.Name;
            if (string.IsNullOrEmpty(whitelistedInvite.InviteCode) && code != null)
                whitelistedInvite.InviteCode = code;
            await db.SaveChangesAsync().ConfigureAwait(false);
            return true;
        }

        public static async Task<bool> AddAsync(DiscordInvite invite)
        {
            if (await IsWhitelistedAsync(invite).ConfigureAwait(false))
                return false;

            var code = invite.IsRevoked || string.IsNullOrWhiteSpace(invite.Code) ? null : invite.Code;
            var name = string.IsNullOrWhiteSpace(invite.Guild.Name) ? null : invite.Guild.Name;
            using (var db = new BotDb())
            {
                await db.WhitelistedInvites.AddAsync(new WhitelistedInvite { GuildId = invite.Guild.Id, Name = name, InviteCode = code }).ConfigureAwait(false);
                await db.SaveChangesAsync().ConfigureAwait(false);
            }
            return true;
        }

        public static async Task<bool> AddAsync(ulong guildId)
        {
            if (IsWhitelisted(guildId))
                return false;

            using (var db = new BotDb())
            {
                await db.WhitelistedInvites.AddAsync(new WhitelistedInvite {GuildId = guildId}).ConfigureAwait(false);
                await db.SaveChangesAsync().ConfigureAwait(false);
            }
            return true;
        }

        public static async Task<bool> RemoveAsync(int id)
        {
            using var db = new BotDb();
            var dbItem = await db.WhitelistedInvites.FirstOrDefaultAsync(i => i.Id == id).ConfigureAwait(false);
            if (dbItem == null)
                return false;

            db.WhitelistedInvites.Remove(dbItem);
            await db.SaveChangesAsync().ConfigureAwait(false);
            return true;
        }

        public static async Task CleanupAsync(DiscordClient client)
        {
            while (!Config.Cts.IsCancellationRequested)
            {
                using (var db = new BotDb())
                {
                    foreach (var invite in db.WhitelistedInvites.Where(i => i.InviteCode != null))
                    {
                        try
                        {
                            var result = await client.GetInviteByCodeAsync(invite.InviteCode).ConfigureAwait(false);
                            if (result?.IsRevoked == false)
                                invite.InviteCode = null;
                        }
                        catch (NotFoundException)
                        {
                            invite.InviteCode = null;
                        }
                        catch (Exception e)
                        {
                            Config.Log.Debug(e);
                        }
                    }
                    await db.SaveChangesAsync(Config.Cts.Token).ConfigureAwait(false);
                }<commit_msg>ah yes, I'm very smart<commit_after>﻿using System;
using System.Linq;
using System.Threading.Tasks;
using DSharpPlus;
using DSharpPlus.Entities;
using DSharpPlus.Exceptions;
using Microsoft.EntityFrameworkCore;

namespace CompatBot.Database.Providers
{
    internal static class InviteWhitelistProvider
    {
        public static bool IsWhitelisted(ulong guildId)
        {
            using var db = new BotDb();
            return db.WhitelistedInvites.Any(i => i.GuildId == guildId);
        }

        public static async Task<bool> IsWhitelistedAsync(DiscordInvite invite)
        {
            var code = string.IsNullOrWhiteSpace(invite.Code) ? null : invite.Code;
            var name = string.IsNullOrWhiteSpace(invite.Guild.Name) ? null : invite.Guild.Name;
            using var db = new BotDb();
            var whitelistedInvite = await db.WhitelistedInvites.FirstOrDefaultAsync(i => i.GuildId == invite.Guild.Id);
            if (whitelistedInvite == null)
                return false;

            if (name != null && name != whitelistedInvite.Name)
                whitelistedInvite.Name = invite.Guild.Name;
            if (string.IsNullOrEmpty(whitelistedInvite.InviteCode) && code != null)
                whitelistedInvite.InviteCode = code;
            await db.SaveChangesAsync().ConfigureAwait(false);
            return true;
        }

        public static async Task<bool> AddAsync(DiscordInvite invite)
        {
            if (await IsWhitelistedAsync(invite).ConfigureAwait(false))
                return false;

            var code = invite.IsRevoked || string.IsNullOrWhiteSpace(invite.Code) ? null : invite.Code;
            var name = string.IsNullOrWhiteSpace(invite.Guild.Name) ? null : invite.Guild.Name;
            using (var db = new BotDb())
            {
                await db.WhitelistedInvites.AddAsync(new WhitelistedInvite { GuildId = invite.Guild.Id, Name = name, InviteCode = code }).ConfigureAwait(false);
                await db.SaveChangesAsync().ConfigureAwait(false);
            }
            return true;
        }

        public static async Task<bool> AddAsync(ulong guildId)
        {
            if (IsWhitelisted(guildId))
                return false;

            using (var db = new BotDb())
            {
                await db.WhitelistedInvites.AddAsync(new WhitelistedInvite {GuildId = guildId}).ConfigureAwait(false);
                await db.SaveChangesAsync().ConfigureAwait(false);
            }
            return true;
        }

        public static async Task<bool> RemoveAsync(int id)
        {
            using var db = new BotDb();
            var dbItem = await db.WhitelistedInvites.FirstOrDefaultAsync(i => i.Id == id).ConfigureAwait(false);
            if (dbItem == null)
                return false;

            db.WhitelistedInvites.Remove(dbItem);
            await db.SaveChangesAsync().ConfigureAwait(false);
            return true;
        }

        public static async Task CleanupAsync(DiscordClient client)
        {
            while (!Config.Cts.IsCancellationRequested)
            {
                using (var db = new BotDb())
                {
                    foreach (var invite in db.WhitelistedInvites.Where(i => i.InviteCode != null))
                    {
                        try
                        {
                            var result = await client.GetInviteByCodeAsync(invite.InviteCode).ConfigureAwait(false);
                            if (result?.IsRevoked == true)
                                invite.InviteCode = null;
                        }
                        catch (NotFoundException)
                        {
                            invite.InviteCode = null;
                            Config.Log.Info($"Removed invite code {invite.InviteCode} for server {invite.Name}");
                        }
                        catch (Exception e)
                        {
                            Config.Log.Debug(e);
                        }
                    }
                    await db.SaveChangesAsync(Config.Cts.Token).ConfigureAwait(false);
                }
{'fraction_non_alphanumeric': 0.08640157093765341, 'fraction_numerical': 0.0, 'mean_word_length': 1.6035143769968052, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3518021', 'n_tokens_mistral': 2181, 'n_tokens_neox': 2124, 'n_words': 441}