Too many refactors in one:

- Allowed adding ephemeral(ish) views and functions
- Moved message_count to a concrete database field
- Moved most proxy logic to a stored procedure
- Moved database files around and refactored schema manager
This commit is contained in:
Ske
2020-06-12 20:29:50 +02:00
parent 24f1363bb0
commit ba441a15cc
37 changed files with 554 additions and 398 deletions

View File

@@ -1,93 +0,0 @@
#nullable enable
using System;
using System.Linq;
using System.Threading.Tasks;
using NodaTime;
using PluralKit.Core;
namespace PluralKit.Bot
{
public class Autoproxier
{
public static readonly string EscapeString = @"\";
public static readonly Duration AutoproxyExpiryTime = Duration.FromHours(6);
private IClock _clock;
private IDataStore _data;
public Autoproxier(IDataStore data, IClock clock)
{
_data = data;
_clock = clock;
}
public async ValueTask<ProxyMatch?> TryAutoproxy(AutoproxyContext ctx)
{
if (IsEscaped(ctx.Content))
return null;
var member = await FindAutoproxyMember(ctx);
if (member == null) return null;
return new ProxyMatch
{
Content = ctx.Content,
Member = member,
ProxyTags = ProxyTagsFor(member)
};
}
private async ValueTask<PKMember?> FindAutoproxyMember(AutoproxyContext ctx)
{
switch (ctx.Mode)
{
case AutoproxyMode.Off:
return null;
case AutoproxyMode.Front:
return await _data.GetFirstFronter(ctx.Account.System);
case AutoproxyMode.Latch:
// Latch mode: find last proxied message, use *that* member
var msg = await _data.GetLastMessageInGuild(ctx.SenderId, ctx.GuildId);
if (msg == null) return null; // No message found
// If the message is older than 6 hours, ignore it and force the sender to "refresh" a proxy
// This can be revised in the future, it's a preliminary value.
var timestamp = DiscordUtils.SnowflakeToInstant(msg.Message.Mid);
if (_clock.GetCurrentInstant() - timestamp > AutoproxyExpiryTime) return null;
return msg.Member;
case AutoproxyMode.Member:
// We already have the member list cached, so:
// O(n) lookup since n is small (max 1500 de jure) and we're more constrained by memory (for a dictionary) here
return ctx.Account.Members.FirstOrDefault(m => m.Id == ctx.AutoproxyMember);
default:
throw new ArgumentOutOfRangeException($"Unknown autoproxy mode {ctx.Mode}");
}
}
private ProxyTag? ProxyTagsFor(PKMember member)
{
if (member.ProxyTags.Count == 0) return null;
return member.ProxyTags.First();
}
private bool IsEscaped(string message) => message.TrimStart().StartsWith(EscapeString);
public struct AutoproxyContext
{
public CachedAccount Account;
public string Content;
public AutoproxyMode Mode;
public int? AutoproxyMember;
public ulong SenderId;
public ulong GuildId;
}
}
}

View File

@@ -5,10 +5,10 @@ namespace PluralKit.Bot
{
public struct ProxyMatch
{
public PKMember Member;
public ProxyMember Member;
public string? Content;
public ProxyTag? ProxyTags;
public string? ProxyContent
{
get

View File

@@ -0,0 +1,69 @@
using System.Collections.Generic;
using System.Linq;
using NodaTime;
using PluralKit.Core;
namespace PluralKit.Bot
{
public class ProxyMatcher
{
public static readonly Duration LatchExpiryTime = Duration.FromHours(6);
private IClock _clock;
private ProxyTagParser _parser;
public ProxyMatcher(ProxyTagParser parser, IClock clock)
{
_parser = parser;
_clock = clock;
}
public bool TryMatch(IReadOnlyCollection<ProxyMember> members, out ProxyMatch match, string messageContent,
bool hasAttachments, bool allowAutoproxy)
{
if (TryMatchTags(members, messageContent, hasAttachments, out match)) return true;
if (allowAutoproxy && TryMatchAutoproxy(members, messageContent, out match)) return true;
return false;
}
private bool TryMatchTags(IReadOnlyCollection<ProxyMember> members, string messageContent, bool hasAttachments, out ProxyMatch match)
{
if (!_parser.TryMatch(members, messageContent, out match)) return false;
// Edge case: If we got a match with blank inner text, we'd normally just send w/ attachments
// However, if there are no attachments, the user probably intended something else, so we "un-match" and proceed to autoproxy
return hasAttachments || match.Content.Length > 0;
}
private bool TryMatchAutoproxy(IReadOnlyCollection<ProxyMember> members, string messageContent,
out ProxyMatch match)
{
match = default;
// We handle most autoproxy logic in the database function, so we just look for the member that's marked
var info = members.FirstOrDefault(i => i.IsAutoproxyMember);
if (info == null) return false;
// If we're in latch mode and the latch message is too old, fail the match too
if (info.AutoproxyMode == AutoproxyMode.Latch && info.LatchMessage != null)
{
var timestamp = DiscordUtils.SnowflakeToInstant(info.LatchMessage.Value);
if (_clock.GetCurrentInstant() - timestamp > LatchExpiryTime) return false;
}
// Match succeeded, build info object and return
match = new ProxyMatch
{
Content = messageContent,
Member = info,
// We're autoproxying, so not using any proxy tags here
// we just find the first pair of tags (if any), otherwise null
ProxyTags = info.ProxyTags.FirstOrDefault()
};
return true;
}
}
}

View File

@@ -1,6 +1,11 @@
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using Dapper;
using DSharpPlus;
using DSharpPlus.Entities;
using DSharpPlus.Exceptions;
@@ -11,112 +16,80 @@ using Serilog;
namespace PluralKit.Bot
{
public class ProxyService {
public static readonly TimeSpan MessageDeletionDelay = TimeSpan.FromMilliseconds(1000);
public class ProxyService
{
public static readonly TimeSpan MessageDeletionDelay = TimeSpan.FromMilliseconds(1000);
private LogChannelService _logChannel;
private DbConnectionFactory _db;
private IDataStore _data;
private ILogger _logger;
private WebhookExecutorService _webhookExecutor;
private ProxyTagParser _parser;
private Autoproxier _autoproxier;
public ProxyService(LogChannelService logChannel, IDataStore data, ILogger logger, WebhookExecutorService webhookExecutor, ProxyTagParser parser, Autoproxier autoproxier)
private readonly ProxyMatcher _matcher;
public ProxyService(LogChannelService logChannel, IDataStore data, ILogger logger,
WebhookExecutorService webhookExecutor, DbConnectionFactory db, ProxyMatcher matcher)
{
_logChannel = logChannel;
_data = data;
_webhookExecutor = webhookExecutor;
_parser = parser;
_autoproxier = autoproxier;
_db = db;
_matcher = matcher;
_logger = logger.ForContext<ProxyService>();
}
public async Task<ProxyMatch?> TryGetMatch(DiscordMessage message, SystemGuildSettings systemGuildSettings, CachedAccount account, bool allowAutoproxy)
public async Task HandleIncomingMessage(DiscordMessage message, bool allowAutoproxy)
{
// First, try parsing by tags
if (_parser.TryParse(message.Content, account.Members, out var tagMatch))
{
// If the content is blank (and we don't have any attachments), someone just sent a message that happens
// to be equal to someone else's tags. This doesn't count! Proceed to autoproxy in that case.
var isEdgeCase = tagMatch.Content.Trim().Length == 0 && message.Attachments.Count == 0;
if (!isEdgeCase) return tagMatch;
}
// Then, if AP is enabled, try finding an autoproxy match
if (allowAutoproxy)
return await _autoproxier.TryAutoproxy(new Autoproxier.AutoproxyContext
{
Account = account,
AutoproxyMember = systemGuildSettings.AutoproxyMember,
Content = message.Content,
GuildId = message.Channel.GuildId,
Mode = systemGuildSettings.AutoproxyMode,
SenderId = message.Author.Id
});
// Didn't find anything :(
return null;
// Quick context checks to quit early
if (!IsMessageValid(message)) return;
// Fetch members and try to match to a specific member
var members = await FetchProxyMembers(message.Author.Id, message.Channel.GuildId);
if (!_matcher.TryMatch(members, out var match, message.Content, message.Attachments.Count > 0,
allowAutoproxy)) return;
// Do some quick permission checks before going through with the proxy
// (do channel checks *after* checking other perms to make sure we don't spam errors when eg. channel is blacklisted)
if (!IsProxyValid(message, match)) return;
if (!await CheckBotPermissionsOrError(message.Channel)) return;
if (!CheckProxyNameBoundsOrError(match)) return;
// Everything's in order, we can execute the proxy!
await ExecuteProxy(message, match);
}
public async Task HandleMessageAsync(DiscordClient client, GuildConfig guild, CachedAccount account, DiscordMessage message, bool allowAutoproxy)
private async Task ExecuteProxy(DiscordMessage trigger, ProxyMatch match)
{
// Early checks
if (message.Channel.Guild == null) return;
if (guild.Blacklist.Contains(message.ChannelId)) return;
var systemSettingsForGuild = account.SettingsForGuild(message.Channel.GuildId);
if (!systemSettingsForGuild.ProxyEnabled) return;
if (!await EnsureBotPermissions(message.Channel)) return;
// Find a proxy match (either with tags or autoproxy), bail if we couldn't find any
if (!(await TryGetMatch(message, systemSettingsForGuild, account, allowAutoproxy) is { } match))
return;
// Send the webhook
var id = await _webhookExecutor.ExecuteWebhook(trigger.Channel, match.Member.ProxyName, match.Member.ProxyAvatar,
match.Content, trigger.Attachments);
// Can't proxy a message with no content and no attachment
if (match.Content.Trim().Length == 0 && message.Attachments.Count == 0)
return;
var memberSettingsForGuild = account.SettingsForMemberGuild(match.Member.Id, message.Channel.GuildId);
// Find and check proxied name
var proxyName = match.Member.ProxyName(account.System.Tag, memberSettingsForGuild.DisplayName);
if (proxyName.Length < 2) throw Errors.ProxyNameTooShort(proxyName);
if (proxyName.Length > Limits.MaxProxyNameLength) throw Errors.ProxyNameTooLong(proxyName);
// Find proxy avatar (server avatar -> member avatar -> system avatar)
var proxyAvatar = memberSettingsForGuild.AvatarUrl ?? match.Member.AvatarUrl ?? account.System.AvatarUrl;
// Execute the webhook!
var hookMessage = await _webhookExecutor.ExecuteWebhook(message.Channel, proxyName, proxyAvatar,
await SanitizeEveryoneMaybe(message, match.ProxyContent),
message.Attachments
);
// Store the message in the database, and log it in the log channel (if applicable)
await _data.AddMessage(message.Author.Id, hookMessage, message.Channel.GuildId, message.Channel.Id, message.Id, match.Member);
await _logChannel.LogMessage(client, account.System, match.Member, hookMessage, message.Id, message.Channel, message.Author, match.Content, guild);
// Handle post-proxy actions
await _data.AddMessage(trigger.Author.Id, trigger.Channel.GuildId, trigger.Channel.Id, id, trigger.Id, match.Member.MemberId);
await _logChannel.LogMessage(match, trigger, id);
// Wait a second or so before deleting the original message
await Task.Delay(MessageDeletionDelay);
try
{
await message.DeleteAsync();
await trigger.DeleteAsync();
}
catch (NotFoundException)
{
// If it's already deleted, we just log and swallow the exception
_logger.Warning("Attempted to delete already deleted proxy trigger message {Message}", message.Id);
_logger.Warning("Attempted to delete already deleted proxy trigger message {Message}", trigger.Id);
}
}
private static async Task<string> SanitizeEveryoneMaybe(DiscordMessage message,
string messageContents)
private async Task<IReadOnlyCollection<ProxyMember>> FetchProxyMembers(ulong account, ulong guild)
{
var permissions = await message.Channel.PermissionsIn(message.Author);
return (permissions & Permissions.MentionEveryone) == 0 ? messageContents.SanitizeEveryone() : messageContents;
await using var conn = await _db.Obtain();
var members = await conn.QueryAsync<ProxyMember>("proxy_info",
new {account_id = account, guild_id = guild}, commandType: CommandType.StoredProcedure);
return members.ToList();
}
private async Task<bool> EnsureBotPermissions(DiscordChannel channel)
private async Task<bool> CheckBotPermissionsOrError(DiscordChannel channel)
{
var permissions = channel.BotPermissions();
@@ -141,5 +114,43 @@ namespace PluralKit.Bot
return true;
}
private bool CheckProxyNameBoundsOrError(ProxyMatch match)
{
var proxyName = match.Member.ProxyName;
if (proxyName.Length < 2) throw Errors.ProxyNameTooShort(proxyName);
if (proxyName.Length > Limits.MaxProxyNameLength) throw Errors.ProxyNameTooLong(proxyName);
// TODO: this never returns false as it throws instead, should this happen?
return true;
}
private bool IsMessageValid(DiscordMessage message)
{
return
// Must be a guild text channel
message.Channel.Type == ChannelType.Text &&
// Must not be a system message
message.MessageType == MessageType.Default &&
!(message.Author.IsSystem ?? false) &&
// Must not be a bot or webhook message
!message.WebhookMessage &&
!message.Author.IsBot &&
// Must have either an attachment or content (or both, but not neither)
(message.Attachments.Count > 0 || (message.Content != null && message.Content.Trim().Length > 0));
}
private bool IsProxyValid(DiscordMessage message, ProxyMatch match)
{
return
// System and member must have proxying enabled in this guild
match.Member.ProxyEnabled &&
// Channel must not be blacklisted here
!match.Member.ChannelBlacklist.Contains(message.ChannelId);
}
}
}
}

View File

@@ -1,4 +1,5 @@
#nullable enable
using System;
using System.Collections.Generic;
using System.Linq;
@@ -8,7 +9,7 @@ namespace PluralKit.Bot
{
public class ProxyTagParser
{
public bool TryParse(string input, IEnumerable<PKMember> members, out ProxyMatch result)
public bool TryMatch(IEnumerable<ProxyMember> members, string input, out ProxyMatch result)
{
result = default;
@@ -19,7 +20,7 @@ namespace PluralKit.Bot
// "Flatten" list of members to a list of tag-member pairs
// Then order them by "tag specificity"
// (ProxyString length desc = prefix+suffix length desc = inner message asc = more specific proxy first)
// (prefix+suffix length desc = inner message asc = more specific proxy first)
var tags = members
.SelectMany(member => member.ProxyTags.Select(tag => (tag, member)))
.OrderByDescending(p => p.tag.ProxyString.Length);
@@ -34,15 +35,10 @@ namespace PluralKit.Bot
if (tag.Prefix == null && tag.Suffix == null) continue;
// Can we match with these tags?
if (TryMatchTags(input, tag, out result.Content))
if (TryMatchTagsInner(input, tag, out result.Content))
{
// (see https://github.com/xSke/PluralKit/pull/181)
if (result.Content == "\U0000fe0f") return false;
// If we extracted a leading mention before, add that back now
if (leadingMention != null) result.Content = $"{leadingMention} {result.Content}";
// We're done!
return true;
}
@@ -53,8 +49,24 @@ namespace PluralKit.Bot
return false;
}
private bool TryMatchTags(string input, ProxyTag tag, out string content)
public bool TryMatchTags(string input, ProxyTag tag, out string inner)
{
// This just wraps TryMatchTagsInner w/ support for leading mentions
var leadingMention = ExtractLeadingMention(ref input);
inner = "";
if (!TryMatchTagsInner(input, tag, out var innerRaw)) return false;
// Add leading mentions back
inner = leadingMention == null ? innerRaw : $"{leadingMention} {innerRaw}";
return true;
}
private bool TryMatchTagsInner(string input, ProxyTag tag, out string inner)
{
inner = "";
// Normalize null tags to empty strings
var prefix = tag.Prefix ?? "";
var suffix = tag.Suffix ?? "";
@@ -66,19 +78,14 @@ namespace PluralKit.Bot
// Special case: image-only proxies + proxy tags with spaces
// Trim everything, then see if we have a "contentless tag pair" (normally disallowed, but OK if we have an attachment)
if (!isMatch && input.Trim() == prefix.TrimEnd() + suffix.TrimStart())
{
content = "";
return true;
}
if (isMatch)
{
content = input.Substring(prefix.Length, input.Length - prefix.Length - suffix.Length);
return true;
}
content = "";
return false;
if (!isMatch) return false;
// We got a match, extract inner text
inner = input.Substring(prefix.Length, input.Length - prefix.Length - suffix.Length).Trim();
// (see https://github.com/xSke/PluralKit/pull/181)
return inner != "\U0000fe0f";
}
private string? ExtractLeadingMention(ref string input)