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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
|
@using System.Reflection
@using ArcaneLibs.Extensions
@using LibMatrix
@using LibMatrix.EventTypes.Spec.State.Policy
@using LibMatrix.RoomTypes
@using MatrixUtils.Web.Shared.PolicyEditorComponents
@if (_isInitialized && IsVisible) {
<tr id="@PolicyInfo.Policy.EventId">
<td>
<div style="display: flex; flex-direction: row; gap: 0.5em;">
@* @if (PowerLevels.UserHasStatePermission(Homeserver.WhoAmI.UserId, Policy.Type)) { *@
@if (true) {
<LinkButton OnClickAsync="@(() => {
IsEditing = true;
return Task.CompletedTask;
})">Edit
</LinkButton>
<LinkButton OnClickAsync="@RemovePolicyAsync">Remove</LinkButton>
@if (Policy.IsLegacyType) {
<LinkButton OnClickAsync="@RemovePolicyAsync">Update type</LinkButton>
}
@if (TypedContent.Entity?.StartsWith("@*:", StringComparison.Ordinal) == true) {
<LinkButton OnClickAsync="@ConvertToAclAsync">Convert to ACL</LinkButton>
}
@* @if (PolicyTypeIds[typeof(ServerPolicyRuleEventContent)].Contains(Policy.Type)) { *@
@* <LinkButton OnClickAsync="@(() => { *@
@* ServerPolicyToMakePermanent = Policy; *@
@* return Task.CompletedTask; *@
@* })">Make permanent *@
@* </LinkButton> *@
@* @if (CurrentUserIsDraupnir) { *@
@* <LinkButton Color="@(ActiveKicks.ContainsKey(Policy) ? "#FF0000" : null)" OnClick="@(() => DraupnirKickMatching(Policy))">Kick *@
@* users @(ActiveKicks.TryGetValue(Policy, out var kick) ? $"({kick})" : null) *@
@* </LinkButton> *@
@* } *@
// }
}
else {
<p>No permission to modify</p>
}
</div>
</td>
@foreach (var prop in PolicyCollection.PropertiesToDisplay.Values) {
if (prop.Name == "Entity") {
<td>
<span>@TruncateMxid(TypedContent.Entity)</span>
@foreach (var dup in PolicyInfo.DuplicatedBy) {
<br/>
<span>Duplicated by @dup.FriendlyTypeName.ToLower() <a href="@Anchor(dup.EventId!)">@TruncateMxid(dup.RawContent["entity"]?.GetValue<string>())</a></span>
}
@foreach (var dup in PolicyInfo.MadeRedundantBy) {
<br/>
<span>Also matched by @dup.FriendlyTypeName.ToLower() <a href="@Anchor(dup.EventId!)">@TruncateMxid(dup.RawContent["entity"]?.GetValue<string>())</a></span>
}
@if (RenderEventInfo) {
<br/>
<pre style="margin-bottom: unset;">
@PolicyInfo.Policy.Type/@PolicyInfo.Policy.StateKey by @PolicyInfo.Policy.Sender at @PolicyInfo.Policy.OriginServerTimestamp
</pre>
}
</td>
}
else {
<td>@prop.GetGetMethod()?.Invoke(TypedContent, null)</td>
}
}
</tr>
@if (IsEditing) {
<PolicyEditorModal PolicyEvent="@Policy" OnClose="@(() => IsEditing = false)" OnSaveAsync="@UpdatePolicyAsync"></PolicyEditorModal>
}
@* TODO: Implement ability to turn ACLs into wildcards *@
@*@if (ServerPolicyToMakePermanent is not null) {
<ModalWindow Title="Make policy permanent">
</ModalWindow>
}*@
}
@code {
[Parameter]
public PolicyList.PolicyCollection.PolicyInfo PolicyInfo { get; set; }
[Parameter]
public GenericRoom Room { get; set; } = null!;
[Parameter]
public required PolicyList.PolicyCollection PolicyCollection { get; set; }
[Parameter]
public bool RenderEventInfo { get; set; }
[Parameter]
public required Action PolicyCollectionStateHasChanged { get; set; }
private StateEventResponse Policy => PolicyInfo.Policy;
private bool IsEditing {
get;
set {
field = value;
_isDirty = true;
StateHasChanged();
}
}
public bool IsVisible {
get;
set {
field = value;
_isDirty = true;
}
} = true;
private PolicyRuleEventContent TypedContent { get; set; }
private bool _isDirty = true;
private bool _isInitialized;
protected override bool ShouldRender() => _isDirty;
protected override void OnParametersSet() {
TypedContent = Policy.TypedContent as PolicyRuleEventContent ?? throw new InvalidOperationException("Policy must have a typed content of type PolicyRuleEventContent.");
_isDirty = true;
_isInitialized = true;
// Console.WriteLine($"ParametersSet {Policy.StateKey}");
}
private static string TruncateMxid(string? mxid) {
if (string.IsNullOrWhiteSpace(mxid)) return mxid;
var parts = mxid.Split(':', 2);
if (parts[0].Length > 50)
parts[0] = parts[0][..50] + "[...]";
if (parts is [_, { Length: > 50 }])
parts[1] = parts[1][..50] + "[...]";
return parts.Length == 1 ? parts[0] : $"{parts[0]}:{parts[1]}";
}
private async Task RemovePolicyAsync() {
await Room.SendStateEventAsync(Policy.Type, Policy.StateKey, new { });
bool shouldUpdateVisibility = true;
PolicyCollection.ActivePolicies.Remove((Policy.Type, Policy.StateKey));
PolicyCollection.RemovedPolicies.Add((Policy.Type, Policy.StateKey), PolicyInfo);
if (PolicyInfo.DuplicatedBy.Count > 0) {
foreach (var evt in PolicyInfo.DuplicatedBy) {
var matchingEntry = PolicyCollection.ActivePolicies
.FirstOrDefault(x => StateEvent.Equals(x.Value.Policy, evt)).Value;
var removals = matchingEntry.DuplicatedBy.RemoveAll(x => StateEvent.Equals(x, Policy));
Console.WriteLine($"Removed {removals} duplicates from {evt.EventId}, matching entry: {matchingEntry.ToJson()}");
if (PolicyCollection.ViewType == PolicyList.PolicyCollection.SpecialViewType.Duplicates && matchingEntry.DuplicatedBy.Count == 0) {
PolicyCollection.ActivePolicies.Remove((matchingEntry.Policy.Type, matchingEntry.Policy.StateKey));
PolicyCollection.RemovedPolicies.Add((matchingEntry.Policy.Type, matchingEntry.Policy.StateKey), matchingEntry);
Console.WriteLine($"Also removed {matchingEntry.Policy.EventId} as it is now redundant");
}
}
PolicyCollectionStateHasChanged();
shouldUpdateVisibility = false;
}
if (PolicyInfo.MadeRedundantBy.Count > 0) {
foreach (var evt in PolicyInfo.MadeRedundantBy) {
var matchingEntry = PolicyCollection.ActivePolicies
.FirstOrDefault(x => StateEvent.Equals(x.Value.Policy, evt)).Value;
var removals = matchingEntry.MadeRedundantBy.RemoveAll(x => StateEvent.Equals(x, Policy));
Console.WriteLine($"Removed {removals} redundants from {evt.EventId}, matching entry: {matchingEntry.ToJson()}");
}
PolicyCollectionStateHasChanged();
shouldUpdateVisibility = false;
}
if (shouldUpdateVisibility) {
IsVisible = false;
StateHasChanged();
}
// PolicyEventsByType[policyEvent.MappedType].Remove(policyEvent);
// await LoadStatesAsync();
}
private async Task UpdatePolicyAsync(StateEventResponse evt) {
await Room.SendStateEventAsync(Policy.Type, Policy.StateKey, Policy.RawContent);
// CurrentlyEditingEvent = null;
// await LoadStatesAsync();
}
private async Task UpgradePolicyAsync() {
Policy.RawContent["gay.rory.matrixutils.upgraded_from_type"] = Policy.Type;
// await LoadStatesAsync();
}
private async Task ConvertToAclAsync() {
if (Policy.RawContent.ContainsKey("entity")) {
var newContent = Policy.ContentAs<ServerPolicyRuleEventContent>();
newContent!.Entity = newContent.Entity!.Replace("@*:", "");
await Room.SendStateEventAsync(ServerPolicyRuleEventContent.EventId, newContent.GetDraupnir2StateKey(), newContent);
await Room.SendStateEventAsync(Policy.Type, Policy.StateKey!, new { });
IsVisible = false;
StateHasChanged();
}
else {
throw new InvalidOperationException("Policy event must contain an 'entity' field to convert to ACL.");
}
}
private string Anchor(string anchor) {
return $"{NavigationManager.Uri.Split('#')[0]}#{anchor}";
}
}
|