about summary refs log tree commit diff
path: root/MiniUtils/Services/IgnoreListManager.cs
diff options
context:
space:
mode:
authorRory& <root@rory.gay>2025-05-13 08:17:26 +0200
committerRory& <root@rory.gay>2025-05-13 08:17:26 +0200
commitddb5a61b0fd234b436c0406ab57e38d67429c6b8 (patch)
treebd2bc4c20aab27135ebaa55e1f9193f22e7d73cc /MiniUtils/Services/IgnoreListManager.cs
downloadMiniUtils-ddb5a61b0fd234b436c0406ab57e38d67429c6b8.tar.xz
Initial commit
Diffstat (limited to 'MiniUtils/Services/IgnoreListManager.cs')
-rw-r--r--MiniUtils/Services/IgnoreListManager.cs67
1 files changed, 67 insertions, 0 deletions
diff --git a/MiniUtils/Services/IgnoreListManager.cs b/MiniUtils/Services/IgnoreListManager.cs
new file mode 100644

index 0000000..c42dd02 --- /dev/null +++ b/MiniUtils/Services/IgnoreListManager.cs
@@ -0,0 +1,67 @@ +using LibMatrix.EventTypes.Spec; +using LibMatrix.Homeservers; +using MiniUtils.Classes; + +namespace MiniUtils.Services; + +public class IgnoreListManager(AuthenticatedHomeserverGeneric homeserver) { + private static readonly SemaphoreSlim Lock = new(1, 1); + + public async Task<int> DisableAll() { + await Lock.WaitAsync(); + var ignoreList = await homeserver.GetAccountDataOrNullAsync<IgnoredUserListEventContentWithDisabled>(IgnoredUserListEventContent.EventId) ?? new(); + if (ignoreList.IgnoredUsers.Count == 0) { + Lock.Release(); + return 0; + } + + foreach (var ignore in ignoreList.IgnoredUsers) { + ignoreList.DisabledIgnoredUsers.Add(ignore.Key, ignore.Value); + } + + int count = ignoreList.DisabledIgnoredUsers.Count; + + ignoreList.IgnoredUsers.Clear(); + await homeserver.SetAccountDataAsync(IgnoredUserListEventContent.EventId, ignoreList); + Lock.Release(); + return count; + } + + public async Task<int> EnableAll() { + await Lock.WaitAsync(); + var ignoreList = await homeserver.GetAccountDataOrNullAsync<IgnoredUserListEventContentWithDisabled>(IgnoredUserListEventContent.EventId) ?? new(); + if (ignoreList.DisabledIgnoredUsers.Count == 0) { + Lock.Release(); + return 0; + } + + foreach (var ignore in ignoreList.DisabledIgnoredUsers) { + ignoreList.IgnoredUsers.Add(ignore.Key, ignore.Value); + } + + var count = ignoreList.IgnoredUsers.Count; + ignoreList.DisabledIgnoredUsers.Clear(); + await homeserver.SetAccountDataAsync(IgnoredUserListEventContent.EventId, ignoreList); + Lock.Release(); + return count; + } + + public async Task<int> MoveList(bool enable, IEnumerable<string> items) { + await Lock.WaitAsync(); + var ignoreList = await homeserver.GetAccountDataOrNullAsync<IgnoredUserListEventContentWithDisabled>(IgnoredUserListEventContent.EventId) ?? new(); + var fromDict = enable ? ignoreList.DisabledIgnoredUsers : ignoreList.IgnoredUsers; + var toDict = enable ? ignoreList.IgnoredUsers : ignoreList.DisabledIgnoredUsers; + var moved = 0; + foreach (var item in items) { + if (fromDict.Remove(item, out var value)) { + toDict.Add(item, value); + moved++; + } + } + + if (moved > 0) + await homeserver.SetAccountDataAsync(IgnoredUserListEventContent.EventId, ignoreList); + Lock.Release(); + return moved; + } +} \ No newline at end of file