fix(bot): add callback to update message count after proxying

also re-add -with-last-message flag :D
This commit is contained in:
spiral 2022-12-10 16:50:22 +00:00
parent 8e0454a13b
commit 24db9f28c2
No known key found for this signature in database
GPG Key ID: 244A11E4B0BCF40E
9 changed files with 32 additions and 17 deletions

View File

@ -63,8 +63,7 @@ public static class ContextListExt
if (ctx.MatchFlag("with-last-switch", "with-last-fronted", "with-last-front", "wls", "wlf"))
p.IncludeLastSwitch = true;
if (ctx.MatchFlag("with-last-message", "with-last-proxy", "wlm", "wlp"))
throw new PKError("Sorting by last message is temporarily disabled due to database issues, sorry.");
// p.IncludeLastMessage = true;
p.IncludeLastMessage = true;
if (ctx.MatchFlag("with-message-count", "wmc"))
p.IncludeMessageCount = true;
if (ctx.MatchFlag("with-created", "wc"))
@ -135,8 +134,8 @@ public static class ContextListExt
ret += $"({count} messages)";
else if (opts.IncludeLastSwitch && m.MetadataPrivacy.TryGet(lookupCtx, m.LastSwitchTime, out var lastSw))
ret += $"(last switched in: <t:{lastSw.Value.ToUnixTimeSeconds()}>)";
// else if (opts.IncludeLastMessage && m.MetadataPrivacy.TryGet(lookupCtx, m.LastMessage, out var lastMsg))
// ret += $"(last message: <t:{DiscordUtils.SnowflakeToInstant(lastMsg.Value).ToUnixTimeSeconds()}>)";
else if (opts.IncludeLastMessage && m.MetadataPrivacy.TryGet(lookupCtx, m.LastMessageTimestamp, out var lastMsg))
ret += $"(last message: <t:{m.LastMessageTimestamp.Value.ToUnixTimeSeconds()}>)";
else if (opts.IncludeCreated && m.MetadataPrivacy.TryGet(lookupCtx, m.Created, out var created))
ret += $"(created at <t:{created.ToUnixTimeSeconds()}>)";
else if (opts.IncludeAvatar && m.AvatarFor(lookupCtx) is { } avatarUrl)
@ -181,8 +180,8 @@ public static class ContextListExt
m.MessageCountFor(lookupCtx) is { } count && count > 0)
profile.Append($"\n**Message count:** {count}");
// if ((opts.IncludeLastMessage || opts.SortProperty == SortProperty.LastMessage) && m.MetadataPrivacy.TryGet(lookupCtx, m.LastMessage, out var lastMsg))
// profile.Append($"\n**Last message:** {DiscordUtils.SnowflakeToInstant(lastMsg.Value).FormatZoned(zone)}");
if ((opts.IncludeLastMessage || opts.SortProperty == SortProperty.LastMessage) && m.MetadataPrivacy.TryGet(lookupCtx, m.LastMessageTimestamp, out var lastMsg))
profile.Append($"\n**Last message:** {m.LastMessageTimestamp.Value.FormatZoned(ctx.Zone)}");
if ((opts.IncludeLastSwitch || opts.SortProperty == SortProperty.LastSwitch) &&
m.MetadataPrivacy.TryGet(lookupCtx, m.LastSwitchTime, out var lastSw))

View File

@ -130,11 +130,9 @@ public static class ListOptionsExt
SortProperty.Birthdate => input
.OrderByDescending(m => m.AnnualBirthday.HasValue && m.BirthdayPrivacy.CanAccess(ctx))
.ThenBy(m => m.BirthdayPrivacy.Get(ctx, m.AnnualBirthday), ReverseMaybe(Comparer<AnnualDate?>.Default)),
SortProperty.LastMessage => throw new PKError(
"Sorting by last message is temporarily disabled due to database issues, sorry."),
// SortProperty.LastMessage => input
// .OrderByDescending(m => m.LastMessage.HasValue)
// .ThenByDescending(m => m.LastMessage, ReverseMaybe(Comparer<ulong?>.Default)),
SortProperty.LastMessage => input
.OrderByDescending(m => m.LastMessageTimestamp.HasValue)
.ThenByDescending(m => m.LastMessageTimestamp!.Value, ReverseMaybe(Comparer<Instant?>.Default)),
SortProperty.LastSwitch => input
.OrderByDescending(m => m.LastSwitchTime.HasValue && m.MetadataPrivacy.CanAccess(ctx))
.ThenByDescending(m => m.MetadataPrivacy.Get(ctx, m.LastSwitchTime), ReverseMaybe(Comparer<Instant?>.Default)),

View File

@ -435,6 +435,9 @@ public class ProxyService
await _redis.SetOriginalMid(triggerMessage.Id, proxyMessage.Id);
}
Task UpdateMemberForSentMessage()
=> _repo.UpdateMemberForSentMessage(sentMessage.Member!.Value);
Task LogMessageToChannel() =>
_logChannel.LogMessage(sentMessage, triggerMessage, proxyMessage).AsTask();
@ -474,6 +477,7 @@ public class ProxyService
DeleteProxyTriggerMessage(),
SaveMessageInDatabase(),
SaveMessageInRedis(),
UpdateMemberForSentMessage(),
LogMessageToChannel(),
SaveLatchAutoproxy(),
DispatchWebhook()

View File

@ -0,0 +1,6 @@
-- database version 32
-- re-add last message timestamp to members
alter table members add column last_message_timestamp timestamp;
update info set schema_version = 32;

View File

@ -97,4 +97,16 @@ public partial class ModelRepository
new { member = id, account = userId }
);
}
public async Task UpdateMemberForSentMessage(MemberId id)
{
var query = new Query("members")
.AsUpdate(new {
last_message_timestamp = new UnsafeLiteral("now()"),
message_count = new UnsafeLiteral("message_count + 1")
})
.Where("id", id);
await _db.ExecuteQuery(query);
}
}

View File

@ -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 = 31;
private const int TargetSchemaVersion = 32;
private readonly ILogger _logger;
public DatabaseMigrator(ILogger logger)

View File

@ -6,7 +6,6 @@ namespace PluralKit.Core;
// TODO: is inheritance here correct?
public class ListedMember: PKMember
{
// public ulong? LastMessage { get; }
public Instant? LastSwitchTime { get; }
public AnnualDate? AnnualBirthday =>

View File

@ -9,10 +9,6 @@ from systems
create view member_list as
select members.*,
-- Find last message ID
-- max(mid) does full table scan, order by/limit uses index (dunno why, but it works!)
-- (select mid from messages where messages.member = members.id order by mid desc nulls last limit 1) as last_message,
-- Find last switch timestamp
(
select max(switches.timestamp)

View File

@ -50,6 +50,7 @@ public class PKMember
public bool KeepProxy { get; private set; }
public Instant Created { get; private set; }
public int MessageCount { get; private set; }
public Instant? LastMessageTimestamp { get; }
public bool AllowAutoproxy { get; private set; }
public PrivacyLevel MemberVisibility { get; private set; }