diff options
author | TheArcaneBrony <myrainbowdash949@gmail.com> | 2023-10-14 23:19:25 +0200 |
---|---|---|
committer | TheArcaneBrony <myrainbowdash949@gmail.com> | 2023-10-14 23:19:25 +0200 |
commit | 4340b1899470c06f170817dbc1200040619fbf8d (patch) | |
tree | d1d6049d7bc8185c4222238a49846f964bfa7848 /LibMatrix/EventTypes/Spec | |
parent | Error handling (diff) | |
download | LibMatrix-4340b1899470c06f170817dbc1200040619fbf8d.tar.xz |
Handle floats etc in requests
Diffstat (limited to '')
-rw-r--r-- | LibMatrix/EventTypes/Spec/State/RoomInfo/RoomPowerLevelEventContent.cs | 30 |
1 files changed, 27 insertions, 3 deletions
diff --git a/LibMatrix/EventTypes/Spec/State/RoomInfo/RoomPowerLevelEventContent.cs b/LibMatrix/EventTypes/Spec/State/RoomInfo/RoomPowerLevelEventContent.cs index 7f37271..a1bf517 100644 --- a/LibMatrix/EventTypes/Spec/State/RoomInfo/RoomPowerLevelEventContent.cs +++ b/LibMatrix/EventTypes/Spec/State/RoomInfo/RoomPowerLevelEventContent.cs @@ -3,13 +3,15 @@ using LibMatrix.Interfaces; namespace LibMatrix.EventTypes.Spec.State; -[MatrixEvent(EventName = "m.room.power_levels")] +[MatrixEvent(EventName = EventId)] public class RoomPowerLevelEventContent : EventContent { + public const string EventId = "m.room.power_levels"; + [JsonPropertyName("ban")] public long? Ban { get; set; } = 50; [JsonPropertyName("events_default")] - public long EventsDefault { get; set; } = 0; + public long? EventsDefault { get; set; } = 0; [JsonPropertyName("events")] public Dictionary<string, long>? Events { get; set; } // = null!; @@ -46,10 +48,32 @@ public class RoomPowerLevelEventContent : EventContent { } public bool IsUserAdmin(string userId) { + if(userId is null) throw new ArgumentNullException(nameof(userId)); return Users.TryGetValue(userId, out var level) && level >= Events.Max(x => x.Value); } public bool UserHasPermission(string userId, string eventType) { - return Users.TryGetValue(userId, out var level) && level >= Events.GetValueOrDefault(eventType, EventsDefault); + if(userId is null) throw new ArgumentNullException(nameof(userId)); + return Users.TryGetValue(userId, out var level) && level >= Events.GetValueOrDefault(eventType, EventsDefault ?? 0); + } + + public long GetUserPowerLevel(string userId) { + if(userId is null) throw new ArgumentNullException(nameof(userId)); + return Users.TryGetValue(userId, out var level) ? level : UsersDefault ?? UsersDefault ?? 0; + } + + public long GetEventPowerLevel(string eventType) { + return Events.TryGetValue(eventType, out var level) ? level : EventsDefault ?? EventsDefault ?? 0; + } + + public void SetUserPowerLevel(string userId, long powerLevel) { + if(userId is null) throw new ArgumentNullException(nameof(userId)); + Users ??= new(); + if (Users.TryGetValue(userId, out var level)) { + Users[userId] = powerLevel; + } + else { + Users.Add(userId, powerLevel); + } } } |