mirror of
https://github.com/PluralKit/PluralKit.git
synced 2026-02-04 04:56:49 +00:00
feat: add abuse handling
This commit is contained in:
parent
4bf60a47d7
commit
2dfb851246
17 changed files with 405 additions and 16 deletions
|
|
@ -83,10 +83,12 @@ internal partial class Database: IDatabase
|
|||
SqlMapper.AddTypeHandler(new NumericIdHandler<MemberId, int>(i => new MemberId(i)));
|
||||
SqlMapper.AddTypeHandler(new NumericIdHandler<SwitchId, int>(i => new SwitchId(i)));
|
||||
SqlMapper.AddTypeHandler(new NumericIdHandler<GroupId, int>(i => new GroupId(i)));
|
||||
SqlMapper.AddTypeHandler(new NumericIdHandler<AbuseLogId, int>(i => new AbuseLogId(i)));
|
||||
SqlMapper.AddTypeHandler(new NumericIdArrayHandler<SystemId, int>(i => new SystemId(i)));
|
||||
SqlMapper.AddTypeHandler(new NumericIdArrayHandler<MemberId, int>(i => new MemberId(i)));
|
||||
SqlMapper.AddTypeHandler(new NumericIdArrayHandler<SwitchId, int>(i => new SwitchId(i)));
|
||||
SqlMapper.AddTypeHandler(new NumericIdArrayHandler<GroupId, int>(i => new GroupId(i)));
|
||||
SqlMapper.AddTypeHandler(new NumericIdArrayHandler<AbuseLogId, int>(i => new AbuseLogId(i)));
|
||||
|
||||
// Register our custom types to Npgsql
|
||||
// Without these it'll still *work* but break at the first launch + probably cause other small issues
|
||||
|
|
|
|||
|
|
@ -31,4 +31,5 @@ public class MessageContext
|
|||
public int? LatchTimeout { get; }
|
||||
public bool CaseSensitiveProxyTags { get; }
|
||||
public bool ProxyErrorMessageEnabled { get; }
|
||||
public bool DenyBotUsage { get; }
|
||||
}
|
||||
|
|
@ -17,7 +17,8 @@
|
|||
allow_autoproxy bool,
|
||||
latch_timeout integer,
|
||||
case_sensitive_proxy_tags bool,
|
||||
proxy_error_message_enabled bool
|
||||
proxy_error_message_enabled bool,
|
||||
deny_bot_usage bool
|
||||
)
|
||||
as $$
|
||||
-- CTEs to query "static" (accessible only through args) data
|
||||
|
|
@ -28,6 +29,7 @@ as $$
|
|||
left join systems on systems.id = accounts.system
|
||||
left join system_config on system_config.system = accounts.system
|
||||
left join system_guild on system_guild.system = accounts.system and system_guild.guild = guild_id
|
||||
left join abuse_logs on abuse_logs.id = accounts.abuse_log
|
||||
where accounts.uid = account_id),
|
||||
guild as (select * from servers where id = guild_id)
|
||||
select
|
||||
|
|
@ -50,14 +52,17 @@ as $$
|
|||
system.account_autoproxy as allow_autoproxy,
|
||||
system.latch_timeout as latch_timeout,
|
||||
system.case_sensitive_proxy_tags as case_sensitive_proxy_tags,
|
||||
system.proxy_error_message_enabled as proxy_error_message_enabled
|
||||
system.proxy_error_message_enabled as proxy_error_message_enabled,
|
||||
coalesce(abuse_logs.deny_bot_usage, false) as deny_bot_usage
|
||||
-- We need a "from" clause, so we just use some bogus data that's always present
|
||||
-- This ensure we always have exactly one row going forward, so we can left join afterwards and still get data
|
||||
from (select 1) as _placeholder
|
||||
left join system on true
|
||||
left join guild on true
|
||||
left join accounts on true
|
||||
left join system_last_switch on system_last_switch.system = system.id
|
||||
left join system_guild on system_guild.system = system.id and system_guild.guild = guild_id
|
||||
left join abuse_logs on abuse_logs.id = accounts.abuse_log
|
||||
$$ language sql stable rows 1;
|
||||
|
||||
|
||||
|
|
|
|||
23
PluralKit.Core/Database/Migrations/44.sql
Normal file
23
PluralKit.Core/Database/Migrations/44.sql
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
-- database version 44
|
||||
-- add abuse handling measures
|
||||
|
||||
create table abuse_logs (
|
||||
id serial primary key,
|
||||
uuid uuid default gen_random_uuid(),
|
||||
description text,
|
||||
deny_bot_usage bool not null default false,
|
||||
created timestamp not null default (current_timestamp at time zone 'utc')
|
||||
);
|
||||
|
||||
alter table accounts add column abuse_log integer default null references abuse_logs (id) on delete set null;
|
||||
create index abuse_logs_uuid_idx on abuse_logs (uuid);
|
||||
|
||||
-- we now need to handle a row in "accounts" table being created with no
|
||||
-- system (rather than just system being set to null after insert)
|
||||
--
|
||||
-- set default null and drop the sequence (from the column being created
|
||||
-- as type SERIAL)
|
||||
alter table accounts alter column system set default null;
|
||||
drop sequence accounts_system_seq;
|
||||
|
||||
update info set schema_version = 44;
|
||||
|
|
@ -0,0 +1,70 @@
|
|||
using Dapper;
|
||||
|
||||
using SqlKata;
|
||||
|
||||
namespace PluralKit.Core;
|
||||
|
||||
public partial class ModelRepository
|
||||
{
|
||||
public Task<AbuseLog?> GetAbuseLogByGuid(Guid id)
|
||||
{
|
||||
var query = new Query("abuse_logs").Where("uuid", id);
|
||||
return _db.QueryFirst<AbuseLog?>(query);
|
||||
}
|
||||
|
||||
public Task<AbuseLog?> GetAbuseLogByAccount(ulong accountId)
|
||||
{
|
||||
var query = new Query("accounts")
|
||||
.Select("abuse_logs.*")
|
||||
.LeftJoin("abuse_logs", "abuse_logs.id", "accounts.abuse_log")
|
||||
.Where("uid", accountId)
|
||||
.WhereNotNull("abuse_log");
|
||||
|
||||
return _db.QueryFirst<AbuseLog?>(query);
|
||||
}
|
||||
|
||||
public Task<IEnumerable<ulong>> GetAbuseLogAccounts(AbuseLogId id)
|
||||
{
|
||||
var query = new Query("accounts").Select("uid").Where("abuse_log", id);
|
||||
return _db.Query<ulong>(query);
|
||||
}
|
||||
|
||||
public async Task<AbuseLog> CreateAbuseLog(string? desc = null, bool? denyBotUsage = null, IPKConnection? conn = null)
|
||||
{
|
||||
var query = new Query("abuse_logs").AsInsert(new
|
||||
{
|
||||
description = desc,
|
||||
deny_bot_usage = denyBotUsage,
|
||||
});
|
||||
|
||||
var abuseLog = await _db.QueryFirst<AbuseLog>(conn, query, "returning *");
|
||||
_logger.Information("Created {AbuseLogId}", abuseLog.Id);
|
||||
return abuseLog;
|
||||
}
|
||||
|
||||
public async Task AddAbuseLogAccount(AbuseLogId abuseLog, ulong accountId, IPKConnection? conn = null)
|
||||
{
|
||||
var query = new Query("accounts").AsInsert(new
|
||||
{
|
||||
abuse_log = abuseLog,
|
||||
uid = accountId,
|
||||
});
|
||||
await _db.ExecuteQuery(conn, query, "on conflict (uid) do update set abuse_log = @p0");
|
||||
|
||||
_logger.Information("Linked account {UserId} to {AbuseLogId}", accountId, abuseLog);
|
||||
}
|
||||
|
||||
public async Task<AbuseLog> UpdateAbuseLog(AbuseLogId id, AbuseLogPatch patch, IPKConnection? conn = null)
|
||||
{
|
||||
_logger.Information("Updated {AbuseLogId}: {@AbuseLogPatch}", id, patch);
|
||||
var query = patch.Apply(new Query("abuse_logs").Where("id", id));
|
||||
return await _db.QueryFirst<AbuseLog>(conn, query, "returning *");
|
||||
}
|
||||
|
||||
public async Task DeleteAbuseLog(AbuseLogId id)
|
||||
{
|
||||
var query = new Query("abuse_logs").AsDelete().Where("id", id);
|
||||
await _db.ExecuteQuery(query);
|
||||
_logger.Information("Deleted {AbuseLogId}", id);
|
||||
}
|
||||
}
|
||||
|
|
@ -9,7 +9,7 @@ namespace PluralKit.Core;
|
|||
internal class DatabaseMigrator
|
||||
{
|
||||
private const string RootPath = "PluralKit.Core.Database"; // "resource path" root for SQL files
|
||||
private const int TargetSchemaVersion = 43;
|
||||
private const int TargetSchemaVersion = 44;
|
||||
private readonly ILogger _logger;
|
||||
|
||||
public DatabaseMigrator(ILogger logger)
|
||||
|
|
|
|||
36
PluralKit.Core/Models/AbuseLog.cs
Normal file
36
PluralKit.Core/Models/AbuseLog.cs
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
using NodaTime;
|
||||
|
||||
namespace PluralKit.Core;
|
||||
|
||||
public readonly struct AbuseLogId: INumericId<AbuseLogId, int>
|
||||
{
|
||||
public int Value { get; }
|
||||
|
||||
public AbuseLogId(int value)
|
||||
{
|
||||
Value = value;
|
||||
}
|
||||
|
||||
public bool Equals(AbuseLogId other) => Value == other.Value;
|
||||
|
||||
public override bool Equals(object obj) => obj is AbuseLogId other && Equals(other);
|
||||
|
||||
public override int GetHashCode() => Value;
|
||||
|
||||
public static bool operator ==(AbuseLogId left, AbuseLogId right) => left.Equals(right);
|
||||
|
||||
public static bool operator !=(AbuseLogId left, AbuseLogId right) => !left.Equals(right);
|
||||
|
||||
public int CompareTo(AbuseLogId other) => Value.CompareTo(other.Value);
|
||||
|
||||
public override string ToString() => $"AbuseLog #{Value}";
|
||||
}
|
||||
|
||||
public class AbuseLog
|
||||
{
|
||||
public AbuseLogId Id { get; private set; }
|
||||
public Guid Uuid { get; private set; }
|
||||
public string Description { get; private set; }
|
||||
public bool DenyBotUsage { get; private set; }
|
||||
public Instant Created { get; private set; }
|
||||
}
|
||||
16
PluralKit.Core/Models/Patch/AbuseLogPatch.cs
Normal file
16
PluralKit.Core/Models/Patch/AbuseLogPatch.cs
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
using Newtonsoft.Json.Linq;
|
||||
|
||||
using SqlKata;
|
||||
|
||||
namespace PluralKit.Core;
|
||||
|
||||
public class AbuseLogPatch: PatchObject
|
||||
{
|
||||
public Partial<string> Description { get; set; }
|
||||
public Partial<bool> DenyBotUsage { get; set; }
|
||||
|
||||
public override Query Apply(Query q) => q.ApplyPatch(wrapper => wrapper
|
||||
.With("description", Description)
|
||||
.With("deny_bot_usage", DenyBotUsage)
|
||||
);
|
||||
}
|
||||
|
|
@ -8,10 +8,12 @@ public class AccountPatch: PatchObject
|
|||
{
|
||||
public Partial<ulong> DmChannel { get; set; }
|
||||
public Partial<bool> AllowAutoproxy { get; set; }
|
||||
public Partial<AbuseLogId?> AbuseLog { get; set; }
|
||||
|
||||
public override Query Apply(Query q) => q.ApplyPatch(wrapper => wrapper
|
||||
.With("dm_channel", DmChannel)
|
||||
.With("allow_autoproxy", AllowAutoproxy)
|
||||
.With("abuse_log", AbuseLog)
|
||||
);
|
||||
|
||||
public JObject ToJson()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue