fix: don't delete rows from messages table when the associated member is deleted
This commit is contained in:
parent
54830fd264
commit
34df19c551
@ -148,7 +148,7 @@ public class DiscordControllerV2: PKControllerBase
|
|||||||
if (msg == null)
|
if (msg == null)
|
||||||
throw Errors.MessageNotFound;
|
throw Errors.MessageNotFound;
|
||||||
|
|
||||||
var ctx = ContextFor(msg.System);
|
var ctx = msg.System == null ? LookupContext.ByNonOwner : ContextFor(msg.System);
|
||||||
return msg.ToJson(ctx, APIVersion.V2);
|
return msg.ToJson(ctx, APIVersion.V2);
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -51,7 +51,7 @@ public class ProxiedMessage
|
|||||||
|
|
||||||
var msg = await GetMessageToEdit(ctx);
|
var msg = await GetMessageToEdit(ctx);
|
||||||
|
|
||||||
if (ctx.System.Id != msg.System.Id)
|
if (ctx.System.Id != msg.System?.Id)
|
||||||
throw new PKError("Can't edit a message sent by a different system.");
|
throw new PKError("Can't edit a message sent by a different system.");
|
||||||
|
|
||||||
var newContent = ctx.RemainderOrNull().NormalizeLineEndSpacing();
|
var newContent = ctx.RemainderOrNull().NormalizeLineEndSpacing();
|
||||||
@ -209,7 +209,7 @@ public class ProxiedMessage
|
|||||||
if (!showContent)
|
if (!showContent)
|
||||||
throw new PKError(noShowContentError);
|
throw new PKError(noShowContentError);
|
||||||
|
|
||||||
if (message.System.Id != ctx.System.Id)
|
if (message.System?.Id != ctx.System.Id && message.Message.Sender != ctx.Author.Id)
|
||||||
throw new PKError("You can only delete your own messages.");
|
throw new PKError("You can only delete your own messages.");
|
||||||
|
|
||||||
await ctx.Rest.DeleteMessage(message.Message.Channel, message.Message.Mid);
|
await ctx.Rest.DeleteMessage(message.Message.Channel, message.Message.Mid);
|
||||||
|
@ -128,7 +128,7 @@ public class ReactionAdded: IEventHandler<MessageReactionAddEvent>
|
|||||||
var system = await _repo.GetSystemByAccount(evt.UserId);
|
var system = await _repo.GetSystemByAccount(evt.UserId);
|
||||||
|
|
||||||
// Can only delete your own message
|
// Can only delete your own message
|
||||||
if (msg.System.Id != system?.Id) return;
|
if (msg.System?.Id != system?.Id && msg.Message.Sender != evt.UserId) return;
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
@ -170,16 +170,17 @@ public class ReactionAdded: IEventHandler<MessageReactionAddEvent>
|
|||||||
try
|
try
|
||||||
{
|
{
|
||||||
var dm = await _cache.GetOrCreateDmChannel(_rest, evt.UserId);
|
var dm = await _cache.GetOrCreateDmChannel(_rest, evt.UserId);
|
||||||
await _rest.CreateMessage(dm.Id, new MessageRequest
|
if (msg.Member != null)
|
||||||
{
|
await _rest.CreateMessage(dm.Id, new MessageRequest
|
||||||
Embed = await _embeds.CreateMemberEmbed(
|
{
|
||||||
msg.System,
|
Embed = await _embeds.CreateMemberEmbed(
|
||||||
msg.Member,
|
msg.System,
|
||||||
guild,
|
msg.Member,
|
||||||
LookupContext.ByNonOwner,
|
guild,
|
||||||
DateTimeZone.Utc
|
LookupContext.ByNonOwner,
|
||||||
)
|
DateTimeZone.Utc
|
||||||
});
|
)
|
||||||
|
});
|
||||||
|
|
||||||
await _rest.CreateMessage(
|
await _rest.CreateMessage(
|
||||||
dm.Id,
|
dm.Id,
|
||||||
@ -202,6 +203,8 @@ public class ReactionAdded: IEventHandler<MessageReactionAddEvent>
|
|||||||
var requiredPerms = PermissionSet.ViewChannel | PermissionSet.SendMessages;
|
var requiredPerms = PermissionSet.ViewChannel | PermissionSet.SendMessages;
|
||||||
if (member == null || !(await _cache.PermissionsFor(evt.ChannelId, member)).HasFlag(requiredPerms)) return;
|
if (member == null || !(await _cache.PermissionsFor(evt.ChannelId, member)).HasFlag(requiredPerms)) return;
|
||||||
|
|
||||||
|
if (msg.Member == null) return;
|
||||||
|
|
||||||
var config = await _repo.GetSystemConfig(msg.System.Id);
|
var config = await _repo.GetSystemConfig(msg.System.Id);
|
||||||
|
|
||||||
if (config.PingsEnabled)
|
if (config.PingsEnabled)
|
||||||
|
@ -355,13 +355,20 @@ public class EmbedService
|
|||||||
|
|
||||||
// Put it all together
|
// Put it all together
|
||||||
var eb = new EmbedBuilder()
|
var eb = new EmbedBuilder()
|
||||||
.Author(new Embed.EmbedAuthor(msg.Member.NameFor(ctx),
|
.Author(new Embed.EmbedAuthor(msg.Member?.NameFor(ctx) ?? "(deleted member)",
|
||||||
IconUrl: msg.Member.AvatarFor(ctx).TryGetCleanCdnUrl()))
|
IconUrl: msg.Member?.AvatarFor(ctx).TryGetCleanCdnUrl()))
|
||||||
.Description(content)
|
.Description(content)
|
||||||
.Image(showContent ? new Embed.EmbedImage(serverMsg?.Attachments?.FirstOrDefault()?.Url) : null)
|
.Image(showContent ? new Embed.EmbedImage(serverMsg?.Attachments?.FirstOrDefault()?.Url) : null)
|
||||||
.Field(new Embed.Field("System",
|
.Field(new Embed.Field("System",
|
||||||
msg.System.Name != null ? $"{msg.System.Name} (`{msg.System.Hid}`)" : $"`{msg.System.Hid}`", true))
|
msg.System == null
|
||||||
.Field(new Embed.Field("Member", $"{msg.Member.NameFor(ctx)} (`{msg.Member.Hid}`)", true))
|
? "*(deleted or unknown system)*"
|
||||||
|
: msg.System.Name != null ? $"{msg.System.Name} (`{msg.System.Hid}`)" : $"`{msg.System.Hid}`"
|
||||||
|
, true))
|
||||||
|
.Field(new Embed.Field("Member",
|
||||||
|
msg.Member == null
|
||||||
|
? "*(deleted member)*"
|
||||||
|
: $"{msg.Member.NameFor(ctx)} (`{msg.Member.Hid}`)"
|
||||||
|
, true))
|
||||||
.Field(new Embed.Field("Sent by", userStr, true))
|
.Field(new Embed.Field("Sent by", userStr, true))
|
||||||
.Timestamp(DiscordUtils.SnowflakeToInstant(msg.Message.Mid).ToDateTimeOffset().ToString("O"));
|
.Timestamp(DiscordUtils.SnowflakeToInstant(msg.Message.Mid).ToDateTimeOffset().ToString("O"));
|
||||||
|
|
||||||
|
@ -44,7 +44,7 @@ public class LogChannelService
|
|||||||
var triggerChannel = await _cache.GetChannel(proxiedMessage.Channel);
|
var triggerChannel = await _cache.GetChannel(proxiedMessage.Channel);
|
||||||
|
|
||||||
var system = await _repo.GetSystem(ctx.SystemId.Value);
|
var system = await _repo.GetSystem(ctx.SystemId.Value);
|
||||||
var member = await _repo.GetMember(proxiedMessage.Member);
|
var member = await _repo.GetMember(proxiedMessage.Member!.Value);
|
||||||
|
|
||||||
// Send embed!
|
// Send embed!
|
||||||
var embed = _embed.CreateLoggedMessageEmbed(trigger, hookMessage, system.Hid, member, triggerChannel.Name,
|
var embed = _embed.CreateLoggedMessageEmbed(trigger, hookMessage, system.Hid, member, triggerChannel.Name,
|
||||||
|
10
PluralKit.Core/Database/Migrations/24.sql
Normal file
10
PluralKit.Core/Database/Migrations/24.sql
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
-- schema version 24
|
||||||
|
-- don't drop message rows when system/member are deleted
|
||||||
|
|
||||||
|
alter table messages alter column member drop not null;
|
||||||
|
alter table messages drop contstraint messages_member_fkey;
|
||||||
|
alter table messages
|
||||||
|
add constraint messages_member_fkey
|
||||||
|
foreign key (member) references members(id) on delete set null;
|
||||||
|
|
||||||
|
update info set schema_version = 24;
|
@ -29,9 +29,13 @@ public partial class ModelRepository
|
|||||||
FullMessage Mapper(PKMessage msg, PKMember member, PKSystem system) =>
|
FullMessage Mapper(PKMessage msg, PKMember member, PKSystem system) =>
|
||||||
new() { Message = msg, System = system, Member = member };
|
new() { Message = msg, System = system, Member = member };
|
||||||
|
|
||||||
|
var query = "select * from messages"
|
||||||
|
+ " left join members on messages.member = members.id"
|
||||||
|
+ " left join systems on members.system = systems.id"
|
||||||
|
+ " where (mid = @Id or original_mid = @Id)";
|
||||||
|
|
||||||
var result = await conn.QueryAsync<PKMessage, PKMember, PKSystem, FullMessage>(
|
var result = await conn.QueryAsync<PKMessage, PKMember, PKSystem, FullMessage>(
|
||||||
"select messages.*, members.*, systems.* from messages, members, systems where (mid = @Id or original_mid = @Id) and messages.member = members.id and systems.id = members.system",
|
query, Mapper, new { Id = id });
|
||||||
Mapper, new { Id = id });
|
|
||||||
return result.FirstOrDefault();
|
return result.FirstOrDefault();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -133,7 +133,7 @@ public class DispatchService
|
|||||||
if (system.WebhookUrl == null)
|
if (system.WebhookUrl == null)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
var member = await repo.GetMember(newMessage.Member);
|
var member = await repo.GetMember(newMessage.Member!.Value);
|
||||||
|
|
||||||
var fullMessage = new FullMessage
|
var fullMessage = new FullMessage
|
||||||
{
|
{
|
||||||
|
@ -9,7 +9,7 @@ public class PKMessage
|
|||||||
public ulong Mid { get; set; }
|
public ulong Mid { get; set; }
|
||||||
public ulong? Guild { get; set; } // null value means "no data" (ie. from before this field being added)
|
public ulong? Guild { get; set; } // null value means "no data" (ie. from before this field being added)
|
||||||
public ulong Channel { get; set; }
|
public ulong Channel { get; set; }
|
||||||
public MemberId Member { get; set; }
|
public MemberId? Member { get; set; }
|
||||||
public ulong Sender { get; set; }
|
public ulong Sender { get; set; }
|
||||||
public ulong? OriginalMid { get; set; }
|
public ulong? OriginalMid { get; set; }
|
||||||
}
|
}
|
||||||
@ -17,8 +17,8 @@ public class PKMessage
|
|||||||
public class FullMessage
|
public class FullMessage
|
||||||
{
|
{
|
||||||
public PKMessage Message;
|
public PKMessage Message;
|
||||||
public PKMember Member;
|
public PKMember? Member;
|
||||||
public PKSystem System;
|
public PKSystem? System;
|
||||||
|
|
||||||
public JObject ToJson(LookupContext ctx, APIVersion v)
|
public JObject ToJson(LookupContext ctx, APIVersion v)
|
||||||
{
|
{
|
||||||
@ -30,8 +30,8 @@ public class FullMessage
|
|||||||
o.Add("sender", Message.Sender.ToString());
|
o.Add("sender", Message.Sender.ToString());
|
||||||
o.Add("channel", Message.Channel.ToString());
|
o.Add("channel", Message.Channel.ToString());
|
||||||
o.Add("guild", Message.Guild?.ToString());
|
o.Add("guild", Message.Guild?.ToString());
|
||||||
o.Add("system", System.ToJson(ctx, v));
|
o.Add("system", System?.ToJson(ctx, v));
|
||||||
o.Add("member", Member.ToJson(ctx, v: v));
|
o.Add("member", Member?.ToJson(ctx, v: v));
|
||||||
|
|
||||||
return o;
|
return o;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user