about summary refs log tree commit diff
path: root/MiniUtils/Commands/RedactCommand.cs
diff options
context:
space:
mode:
Diffstat (limited to 'MiniUtils/Commands/RedactCommand.cs')
-rw-r--r--MiniUtils/Commands/RedactCommand.cs80
1 files changed, 80 insertions, 0 deletions
diff --git a/MiniUtils/Commands/RedactCommand.cs b/MiniUtils/Commands/RedactCommand.cs
new file mode 100644

index 0000000..cba06c9 --- /dev/null +++ b/MiniUtils/Commands/RedactCommand.cs
@@ -0,0 +1,80 @@ +using System.Collections.Frozen; +using ArcaneLibs.Extensions; +using LibMatrix.EventTypes.Spec; +using LibMatrix.EventTypes.Spec.State.RoomInfo; +using LibMatrix.Filters; +using LibMatrix.Helpers; +using LibMatrix.RoomTypes; +using LibMatrix.Utilities.Bot.Interfaces; +using MiniUtils.Classes; +using MiniUtils.Services; + +namespace MiniUtils.Commands; + +public class RedactCommand(IgnoreListManager ignoreListManager) : ICommand { + public string Name => "redact"; + + public string[]? Aliases => []; + + public string Description => "Redact all user's events"; + + public bool Unlisted => false; + private const string ThumbsUp = "\ud83d\udc4d\ufe0e"; + private const string Recycle = "\u267b\ufe0e"; + private const string Bullseye = "\u25ce\ufe0e"; + private const string RightArrowWithTail = "\u21a3\ufe0e"; + + public async Task Invoke(CommandContext ctx) { + if (ctx.Args is ["banned"]) + await RedactUsers(ctx, await ctx.Room.GetMemberIdsListAsync("ban")); + else if (ctx.Args is [.. var senders]) { + var sendersSet = senders.ToFrozenSet(); + await RedactUsers(ctx, sendersSet); + } + } + + private async Task RedactUsers(CommandContext ctx, FrozenSet<string> senders) { + var filter = new SyncFilter.EventFilter(senders: senders.ToList(), notTypes: ["m.room.redaction"]); + await ignoreListManager.MoveList(false, senders); + var count = 0; + List<Task> tasks = []; + await foreach (var resp in ctx.Room.GetManyMessagesAsync(filter: filter.ToJson(false, ignoreNull: true), chunkSize: 1000)) { + foreach (var chunk in resp.Chunk.Chunk(49)) { + foreach (var evt in chunk) { + if (!senders.Contains(evt.Sender!)) continue; + if (evt is { StateKey: not null, Type: not RoomMemberEventContent.EventId }) continue; + if (evt is { RawContent: null or { Count: 0 } }) continue; + tasks.Add(RedactEvent(ctx.Room, evt.EventId!)); + count++; + } + + if (tasks.Count > 0) { + await ctx.Room.SendMessageEventAsync(new MessageBuilder() + .WithBody( + $"[{Emojis.Hourglass}] {Emojis.Recycle} {count} ({Emojis.Checkmark} {tasks.Count(t => t.IsCompletedSuccessfully)} {Emojis.Prohibited} {tasks.Count(t => t.IsFaulted)} {Emojis.Hourglass} {tasks.Count(t => t.Status == TaskStatus.Running)})") + .Build()); + // await Task.WhenAll(tasks); + } + } + } + + await Task.WhenAll(tasks); + + await ctx.Room.SendMessageEventAsync(new MessageBuilder().WithBody($"{Emojis.Recycle} {count}").Build()); + // await ctx.Room.SendReactionAsync(ctx.MessageEvent.EventId!, $"{Emojis.Recycle} {count}"); + } + + private async Task RedactEvent(GenericRoom room, string eventId) { + bool success; + do { + try { + await room.RedactEventAsync(eventId); + success = true; + } + catch (Exception e) { + success = false; + Console.WriteLine($"Failed to redact event {eventId}: {e}"); + } + } while (!success); + } +} \ No newline at end of file