41 lines
1.5 KiB
C#
41 lines
1.5 KiB
C#
using SharpChat.EventStorage;
|
|
using SharpChat.Packet;
|
|
using System.Linq;
|
|
|
|
namespace SharpChat.Commands
|
|
{
|
|
public class MessageDeleteCommand : IUserCommand {
|
|
public bool IsMatch(UserCommandContext ctx) {
|
|
return ctx.NameEquals("delmsg") || (
|
|
ctx.NameEquals("delete")
|
|
&& ctx.Args.FirstOrDefault()?.All(char.IsDigit) == true
|
|
);
|
|
}
|
|
|
|
public void Dispatch(UserCommandContext ctx) {
|
|
bool deleteAnyMessage = ctx.User.Permissions.HasFlag(UserPermissions.DeleteAnyMessage);
|
|
|
|
if(!deleteAnyMessage && !ctx.User.Permissions.HasFlag(UserPermissions.DeleteOwnMessage)) {
|
|
ctx.Chat.SendTo(ctx.User, new CommandNotAllowedErrorPacket(ctx.Name));
|
|
return;
|
|
}
|
|
|
|
string? firstArg = ctx.Args.FirstOrDefault();
|
|
|
|
if(string.IsNullOrWhiteSpace(firstArg) || !firstArg.All(char.IsDigit) || !long.TryParse(firstArg, out long delSeqId)) {
|
|
ctx.Chat.SendTo(ctx.User, new CommandFormatErrorPacket());
|
|
return;
|
|
}
|
|
|
|
StoredEventInfo? delMsg = ctx.Chat.Events.GetEvent(delSeqId);
|
|
|
|
if(delMsg == null || delMsg.Sender?.Rank > ctx.User.Rank || (!deleteAnyMessage && delMsg.Sender?.UserId != ctx.User.UserId)) {
|
|
ctx.Chat.SendTo(ctx.User, new MessageDeleteNotAllowedErrorPacket());
|
|
return;
|
|
}
|
|
|
|
ctx.Chat.Events.RemoveEvent(delMsg);
|
|
ctx.Chat.Send(new MessageDeletePacket(delMsg.Id));
|
|
}
|
|
}
|
|
}
|