1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
|
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;
}
public async Task<int> AddList(string[] itemsToAdd) {
int added = 0;
await Lock.WaitAsync();
var ignoreList = await homeserver.GetAccountDataOrNullAsync<IgnoredUserListEventContentWithDisabled>(IgnoredUserListEventContent.EventId) ?? new();
foreach (var item in itemsToAdd) {
if (ignoreList.IgnoredUsers.ContainsKey(item)) continue;
if (ignoreList.DisabledIgnoredUsers.Remove(item, out var value)) {
ignoreList.IgnoredUsers.Add(item, value);
added++;
continue;
}
ignoreList.IgnoredUsers.Add(item, new());
added++;
}
if (added > 0)
await homeserver.SetAccountDataAsync(IgnoredUserListEventContent.EventId, ignoreList);
Lock.Release();
return added;
}
}
|