@using LibMatrix.EventTypes.Spec.State.Policy @using System.Reflection @using ArcaneLibs.Attributes @using LibMatrix @using System.Collections.Frozen @using LibMatrix.EventTypes @using LibMatrix.RoomTypes Policy type: Select a value @foreach (var (type, mappedType) in PolicyTypes) { @mappedType.GetFriendlyName().ToLower() } Reason: Recommendation: Entities: @* *@ @* JSON data *@ @* *@ @* $1$ @PolicyEvent.ToJson(true, true) #1# *@ @* *@ @* *@ Cancel Save @code { [Parameter] public required Action OnClose { get; set; } [Parameter] public required Action OnSaved { get; set; } [Parameter] public required GenericRoom Room { get; set; } public string Recommendation { get; set; } = "m.ban"; public string Reason { get; set; } = "spam"; public string Users { get; set; } = ""; private static FrozenSet KnownPolicyTypes = StateEvent.KnownStateEventTypes.Where(x => x.IsAssignableTo(typeof(PolicyRuleEventContent))).ToFrozenSet(); private static Dictionary PolicyTypes = KnownPolicyTypes .ToDictionary(x => x.GetCustomAttributes().First(y => !string.IsNullOrWhiteSpace(y.EventName)).EventName, x => x); private string? MappedType { get; set; } private async Task Save() { try { await DoActualSave(); } catch (Exception e) { Console.WriteLine($"Failed to save: {e}"); } } private async Task DoActualSave() { Console.WriteLine($"Saving ---"); Console.WriteLine($"Users = {Users}"); var users = Users.Split("\n").Select(x => x.Trim()).Where(x => x.StartsWith('@')).ToList(); var tasks = users.Select(x => ExecuteBan(Room, x)).ToList(); await Task.WhenAll(tasks); OnSaved.Invoke(); } private async Task ExecuteBan(GenericRoom room, string entity) { bool success = false; while (!success) { try { var content = Activator.CreateInstance(PolicyTypes[MappedType!]) as PolicyRuleEventContent; content.Recommendation = Recommendation; content.Reason = Reason; content.Entity = entity; await room.SendStateEventAsync(MappedType!, content.GetDraupnir2StateKey(), content); success = true; } catch (MatrixException e) { if (e is not { ErrorCode: MatrixException.ErrorCodes.M_FORBIDDEN }) throw; Console.WriteLine(e); } catch (Exception e) { //ignored Console.WriteLine(e); } } } }
*@ @* $1$ @PolicyEvent.ToJson(true, true) #1# *@ @*