From 5affd9f061e75f6575a2fe6715f9e8757cfe87e8 Mon Sep 17 00:00:00 2001 From: "Emma [it/its]@Rory&" Date: Thu, 14 Dec 2023 07:20:46 +0100 Subject: Cleanup --- .../State/Policy/PolicyRuleStateEventContent.cs | 74 ++++++++++++++++++++ .../Spec/State/RoomInfo/RoomAliasEventContent.cs | 11 +++ .../Spec/State/RoomInfo/RoomAvatarEventContent.cs | 28 ++++++++ .../RoomInfo/RoomCanonicalAliasEventContent.cs | 14 ++++ .../Spec/State/RoomInfo/RoomCreateEventContent.cs | 31 +++++++++ .../State/RoomInfo/RoomEncryptionEventContent.cs | 13 ++++ .../State/RoomInfo/RoomGuestAccessEventContent.cs | 15 +++++ .../RoomInfo/RoomHistoryVisibilityEventContent.cs | 9 +++ .../State/RoomInfo/RoomJoinRulesEventContent.cs | 52 +++++++++++++++ .../Spec/State/RoomInfo/RoomMemberEventContent.cs | 29 ++++++++ .../Spec/State/RoomInfo/RoomNameEventContent.cs | 11 +++ .../Spec/State/RoomInfo/RoomPinnedEventContent.cs | 9 +++ .../State/RoomInfo/RoomPowerLevelEventContent.cs | 78 ++++++++++++++++++++++ .../State/RoomInfo/RoomServerACLEventContent.cs | 15 +++++ .../Spec/State/RoomInfo/RoomTopicEventContent.cs | 10 +++ .../Spec/State/Space/SpaceChildEventContent.cs | 13 ++++ .../Spec/State/Space/SpaceParentEventContent.cs | 12 ++++ 17 files changed, 424 insertions(+) create mode 100644 LibMatrix.EventTypes/Spec/State/Policy/PolicyRuleStateEventContent.cs create mode 100644 LibMatrix.EventTypes/Spec/State/RoomInfo/RoomAliasEventContent.cs create mode 100644 LibMatrix.EventTypes/Spec/State/RoomInfo/RoomAvatarEventContent.cs create mode 100644 LibMatrix.EventTypes/Spec/State/RoomInfo/RoomCanonicalAliasEventContent.cs create mode 100644 LibMatrix.EventTypes/Spec/State/RoomInfo/RoomCreateEventContent.cs create mode 100644 LibMatrix.EventTypes/Spec/State/RoomInfo/RoomEncryptionEventContent.cs create mode 100644 LibMatrix.EventTypes/Spec/State/RoomInfo/RoomGuestAccessEventContent.cs create mode 100644 LibMatrix.EventTypes/Spec/State/RoomInfo/RoomHistoryVisibilityEventContent.cs create mode 100644 LibMatrix.EventTypes/Spec/State/RoomInfo/RoomJoinRulesEventContent.cs create mode 100644 LibMatrix.EventTypes/Spec/State/RoomInfo/RoomMemberEventContent.cs create mode 100644 LibMatrix.EventTypes/Spec/State/RoomInfo/RoomNameEventContent.cs create mode 100644 LibMatrix.EventTypes/Spec/State/RoomInfo/RoomPinnedEventContent.cs create mode 100644 LibMatrix.EventTypes/Spec/State/RoomInfo/RoomPowerLevelEventContent.cs create mode 100644 LibMatrix.EventTypes/Spec/State/RoomInfo/RoomServerACLEventContent.cs create mode 100644 LibMatrix.EventTypes/Spec/State/RoomInfo/RoomTopicEventContent.cs create mode 100644 LibMatrix.EventTypes/Spec/State/Space/SpaceChildEventContent.cs create mode 100644 LibMatrix.EventTypes/Spec/State/Space/SpaceParentEventContent.cs (limited to 'LibMatrix.EventTypes/Spec/State') diff --git a/LibMatrix.EventTypes/Spec/State/Policy/PolicyRuleStateEventContent.cs b/LibMatrix.EventTypes/Spec/State/Policy/PolicyRuleStateEventContent.cs new file mode 100644 index 0000000..d3ab8cb --- /dev/null +++ b/LibMatrix.EventTypes/Spec/State/Policy/PolicyRuleStateEventContent.cs @@ -0,0 +1,74 @@ +using System.Text.Json.Serialization; + +namespace LibMatrix.EventTypes.Spec.State.Policy; + +//spec +[MatrixEvent(EventName = EventId)] //spec +[MatrixEvent(EventName = "m.room.rule.server")] //??? +[MatrixEvent(EventName = "org.matrix.mjolnir.rule.server")] //legacy +public class ServerPolicyRuleEventContent : PolicyRuleEventContent { + public const string EventId = "m.policy.rule.server"; +} + +[MatrixEvent(EventName = EventId)] //spec +[MatrixEvent(EventName = "m.room.rule.user")] //??? +[MatrixEvent(EventName = "org.matrix.mjolnir.rule.user")] //legacy +public class UserPolicyRuleEventContent : PolicyRuleEventContent { + public const string EventId = "m.policy.rule.user"; +} + +[MatrixEvent(EventName = EventId)] //spec +[MatrixEvent(EventName = "m.room.rule.room")] //??? +[MatrixEvent(EventName = "org.matrix.mjolnir.rule.room")] //legacy +public class RoomPolicyRuleEventContent : PolicyRuleEventContent { + public const string EventId = "m.policy.rule.room"; +} + +public abstract class PolicyRuleEventContent : EventContent { + /// + /// Entity this ban applies to, can use * and ? as globs. + /// Policy is invalid if entity is null + /// + [JsonPropertyName("entity")] + public string? Entity { get; set; } + + /// + /// Reason this user is banned + /// + [JsonPropertyName("reason")] + public string? Reason { get; set; } + + /// + /// Suggested action to take + /// + [JsonPropertyName("recommendation")] + public string? Recommendation { get; set; } + + /// + /// Expiry time in milliseconds since the unix epoch, or null if the ban has no expiry. + /// + [JsonPropertyName("support.feline.policy.expiry.rev.2")] //stable prefix: expiry, msc pending + public long? Expiry { get; set; } + + //utils + /// + /// Readable expiry time, provided for easy interaction + /// + [JsonPropertyName("gay.rory.matrix_room_utils.readable_expiry_time_utc")] + public DateTime? ExpiryDateTime { + get => Expiry == null ? null : DateTimeOffset.FromUnixTimeMilliseconds(Expiry.Value).DateTime; + set => Expiry = ((DateTimeOffset)value).ToUnixTimeMilliseconds(); + } +} + +public static class PolicyRecommendationTypes { + /// + /// Ban this user + /// + public static string Ban = "m.ban"; + + /// + /// Mute this user + /// + public static string Mute = "support.feline.policy.recommendation_mute"; //stable prefix: m.mute, msc pending +} diff --git a/LibMatrix.EventTypes/Spec/State/RoomInfo/RoomAliasEventContent.cs b/LibMatrix.EventTypes/Spec/State/RoomInfo/RoomAliasEventContent.cs new file mode 100644 index 0000000..53b85b8 --- /dev/null +++ b/LibMatrix.EventTypes/Spec/State/RoomInfo/RoomAliasEventContent.cs @@ -0,0 +1,11 @@ +using System.Text.Json.Serialization; + +namespace LibMatrix.EventTypes.Spec.State.RoomInfo; + +[MatrixEvent(EventName = EventId)] +public class RoomAliasEventContent : TimelineEventContent { + public const string EventId = "m.room.alias"; + + [JsonPropertyName("aliases")] + public List? Aliases { get; set; } +} diff --git a/LibMatrix.EventTypes/Spec/State/RoomInfo/RoomAvatarEventContent.cs b/LibMatrix.EventTypes/Spec/State/RoomInfo/RoomAvatarEventContent.cs new file mode 100644 index 0000000..d15e88e --- /dev/null +++ b/LibMatrix.EventTypes/Spec/State/RoomInfo/RoomAvatarEventContent.cs @@ -0,0 +1,28 @@ +using System.Text.Json.Serialization; + +namespace LibMatrix.EventTypes.Spec.State.RoomInfo; + +[MatrixEvent(EventName = EventId)] +public class RoomAvatarEventContent : TimelineEventContent { + public const string EventId = "m.room.avatar"; + + [JsonPropertyName("url")] + public string? Url { get; set; } + + [JsonPropertyName("info")] + public RoomAvatarInfo? Info { get; set; } + + public class RoomAvatarInfo { + [JsonPropertyName("h")] + public int? Height { get; set; } + + [JsonPropertyName("w")] + public int? Width { get; set; } + + [JsonPropertyName("mimetype")] + public string? MimeType { get; set; } + + [JsonPropertyName("size")] + public int? Size { get; set; } + } +} diff --git a/LibMatrix.EventTypes/Spec/State/RoomInfo/RoomCanonicalAliasEventContent.cs b/LibMatrix.EventTypes/Spec/State/RoomInfo/RoomCanonicalAliasEventContent.cs new file mode 100644 index 0000000..265775e --- /dev/null +++ b/LibMatrix.EventTypes/Spec/State/RoomInfo/RoomCanonicalAliasEventContent.cs @@ -0,0 +1,14 @@ +using System.Text.Json.Serialization; + +namespace LibMatrix.EventTypes.Spec.State; + +[MatrixEvent(EventName = EventId)] +public class RoomCanonicalAliasEventContent : TimelineEventContent { + public const string EventId = "m.room.canonical_alias"; + + [JsonPropertyName("alias")] + public string? Alias { get; set; } + + [JsonPropertyName("alt_aliases")] + public string[]? AltAliases { get; set; } +} diff --git a/LibMatrix.EventTypes/Spec/State/RoomInfo/RoomCreateEventContent.cs b/LibMatrix.EventTypes/Spec/State/RoomInfo/RoomCreateEventContent.cs new file mode 100644 index 0000000..7d25dc7 --- /dev/null +++ b/LibMatrix.EventTypes/Spec/State/RoomInfo/RoomCreateEventContent.cs @@ -0,0 +1,31 @@ +using System.Text.Json.Serialization; + +namespace LibMatrix.EventTypes.Spec.State; + +[MatrixEvent(EventName = EventId)] +public class RoomCreateEventContent : TimelineEventContent { + public const string EventId = "m.room.create"; + + [JsonPropertyName("room_version")] + public string? RoomVersion { get; set; } + + [JsonPropertyName("creator")] + public string? Creator { get; set; } + + [JsonPropertyName("m.federate")] + public bool? Federate { get; set; } + + [JsonPropertyName("predecessor")] + public RoomCreatePredecessor? Predecessor { get; set; } + + [JsonPropertyName("type")] + public string? Type { get; set; } + + public class RoomCreatePredecessor { + [JsonPropertyName("room_id")] + public string? RoomId { get; set; } + + [JsonPropertyName("event_id")] + public string? EventId { get; set; } + } +} diff --git a/LibMatrix.EventTypes/Spec/State/RoomInfo/RoomEncryptionEventContent.cs b/LibMatrix.EventTypes/Spec/State/RoomInfo/RoomEncryptionEventContent.cs new file mode 100644 index 0000000..8e9e05f --- /dev/null +++ b/LibMatrix.EventTypes/Spec/State/RoomInfo/RoomEncryptionEventContent.cs @@ -0,0 +1,13 @@ +using System.Text.Json.Serialization; + +namespace LibMatrix.EventTypes.Spec.State; + +[MatrixEvent(EventName = "m.room.encryption")] +public class RoomEncryptionEventContent : TimelineEventContent { + [JsonPropertyName("algorithm")] + public string? Algorithm { get; set; } + [JsonPropertyName("rotation_period_ms")] + public ulong? RotationPeriodMs { get; set; } + [JsonPropertyName("rotation_period_msgs")] + public ulong? RotationPeriodMsgs { get; set; } +} diff --git a/LibMatrix.EventTypes/Spec/State/RoomInfo/RoomGuestAccessEventContent.cs b/LibMatrix.EventTypes/Spec/State/RoomInfo/RoomGuestAccessEventContent.cs new file mode 100644 index 0000000..30f2def --- /dev/null +++ b/LibMatrix.EventTypes/Spec/State/RoomInfo/RoomGuestAccessEventContent.cs @@ -0,0 +1,15 @@ +using System.Text.Json.Serialization; + +namespace LibMatrix.EventTypes.Spec.State; + +[MatrixEvent(EventName = "m.room.guest_access")] +public class RoomGuestAccessEventContent : TimelineEventContent { + [JsonPropertyName("guest_access")] + public required string GuestAccess { get; set; } + + [JsonIgnore] + public bool IsGuestAccessEnabled { + get => GuestAccess == "can_join"; + set => GuestAccess = value ? "can_join" : "forbidden"; + } +} diff --git a/LibMatrix.EventTypes/Spec/State/RoomInfo/RoomHistoryVisibilityEventContent.cs b/LibMatrix.EventTypes/Spec/State/RoomInfo/RoomHistoryVisibilityEventContent.cs new file mode 100644 index 0000000..26d40e1 --- /dev/null +++ b/LibMatrix.EventTypes/Spec/State/RoomInfo/RoomHistoryVisibilityEventContent.cs @@ -0,0 +1,9 @@ +using System.Text.Json.Serialization; + +namespace LibMatrix.EventTypes.Spec.State; + +[MatrixEvent(EventName = "m.room.history_visibility")] +public class RoomHistoryVisibilityEventContent : TimelineEventContent { + [JsonPropertyName("history_visibility")] + public required string HistoryVisibility { get; set; } +} diff --git a/LibMatrix.EventTypes/Spec/State/RoomInfo/RoomJoinRulesEventContent.cs b/LibMatrix.EventTypes/Spec/State/RoomInfo/RoomJoinRulesEventContent.cs new file mode 100644 index 0000000..e300b5d --- /dev/null +++ b/LibMatrix.EventTypes/Spec/State/RoomInfo/RoomJoinRulesEventContent.cs @@ -0,0 +1,52 @@ +using System.Text.Json.Serialization; + +namespace LibMatrix.EventTypes.Spec.State; + +[MatrixEvent(EventName = "m.room.join_rules")] +public class RoomJoinRulesEventContent : TimelineEventContent { + /// + /// one of ["public", "invite", "knock", "restricted", "knock_restricted"] + /// "private" is reserved without implementation! + /// + [JsonPropertyName("join_rule")] + public string JoinRuleValue { get; set; } + + [JsonIgnore] + public required JoinRules JoinRule { + get => JoinRuleValue switch { + "public" => JoinRules.Public, + "invite" => JoinRules.Invite, + "knock" => JoinRules.Knock, + "restricted" => JoinRules.Restricted, + "knock_restricted" => JoinRules.KnockRestricted, + _ => throw new ArgumentOutOfRangeException() + }; + set => JoinRuleValue = value switch { + JoinRules.Public => "public", + JoinRules.Invite => "invite", + JoinRules.Knock => "knock", + JoinRules.Restricted => "restricted", + JoinRules.KnockRestricted => "knock_restricted", + _ => throw new ArgumentOutOfRangeException(nameof(value), value, null) + }; + } + + [JsonPropertyName("allow")] + public List? Allow { get; set; } + + public class AllowEntry { + [JsonPropertyName("type")] + public required string Type { get; set; } + + [JsonPropertyName("room_id")] + public required string RoomId { get; set; } + } + + public enum JoinRules { + Public, + Invite, + Knock, + Restricted, + KnockRestricted + } +} diff --git a/LibMatrix.EventTypes/Spec/State/RoomInfo/RoomMemberEventContent.cs b/LibMatrix.EventTypes/Spec/State/RoomInfo/RoomMemberEventContent.cs new file mode 100644 index 0000000..7e4f9b6 --- /dev/null +++ b/LibMatrix.EventTypes/Spec/State/RoomInfo/RoomMemberEventContent.cs @@ -0,0 +1,29 @@ +using System.Text.Json.Serialization; + +namespace LibMatrix.EventTypes.Spec.State; + +[MatrixEvent(EventName = EventId)] +public class RoomMemberEventContent : TimelineEventContent { + public const string EventId = "m.room.member"; + + [JsonPropertyName("reason")] + public string? Reason { get; set; } + + [JsonPropertyName("membership")] + public required string Membership { get; set; } + + [JsonPropertyName("displayname")] + public string? DisplayName { get; set; } + + [JsonPropertyName("is_direct")] + public bool? IsDirect { get; set; } + + [JsonPropertyName("avatar_url")] + public string? AvatarUrl { get; set; } + + [JsonPropertyName("kind")] + public string? Kind { get; set; } + + [JsonPropertyName("join_authorised_via_users_server")] + public string? JoinAuthorisedViaUsersServer { get; set; } +} diff --git a/LibMatrix.EventTypes/Spec/State/RoomInfo/RoomNameEventContent.cs b/LibMatrix.EventTypes/Spec/State/RoomInfo/RoomNameEventContent.cs new file mode 100644 index 0000000..00a1e8f --- /dev/null +++ b/LibMatrix.EventTypes/Spec/State/RoomInfo/RoomNameEventContent.cs @@ -0,0 +1,11 @@ +using System.Text.Json.Serialization; + +namespace LibMatrix.EventTypes.Spec.State; + +[MatrixEvent(EventName = EventId)] +public class RoomNameEventContent : TimelineEventContent { + public const string EventId = "m.room.name"; + + [JsonPropertyName("name")] + public string? Name { get; set; } +} diff --git a/LibMatrix.EventTypes/Spec/State/RoomInfo/RoomPinnedEventContent.cs b/LibMatrix.EventTypes/Spec/State/RoomInfo/RoomPinnedEventContent.cs new file mode 100644 index 0000000..9bbcd90 --- /dev/null +++ b/LibMatrix.EventTypes/Spec/State/RoomInfo/RoomPinnedEventContent.cs @@ -0,0 +1,9 @@ +using System.Text.Json.Serialization; + +namespace LibMatrix.EventTypes.Spec.State; + +[MatrixEvent(EventName = "m.room.pinned_events")] +public class RoomPinnedEventContent : TimelineEventContent { + [JsonPropertyName("pinned")] + public string[]? PinnedEvents { get; set; } +} diff --git a/LibMatrix.EventTypes/Spec/State/RoomInfo/RoomPowerLevelEventContent.cs b/LibMatrix.EventTypes/Spec/State/RoomInfo/RoomPowerLevelEventContent.cs new file mode 100644 index 0000000..1a09ab8 --- /dev/null +++ b/LibMatrix.EventTypes/Spec/State/RoomInfo/RoomPowerLevelEventContent.cs @@ -0,0 +1,78 @@ +using System.Text.Json.Serialization; + +namespace LibMatrix.EventTypes.Spec.State; + +[MatrixEvent(EventName = EventId)] +public class RoomPowerLevelEventContent : TimelineEventContent { + 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; + + [JsonPropertyName("invite")] + public long? Invite { get; set; } = 0; + + [JsonPropertyName("kick")] + public long? Kick { get; set; } = 50; + + [JsonPropertyName("notifications")] + public NotificationsPL? NotificationsPl { get; set; } // = null!; + + [JsonPropertyName("redact")] + public long? Redact { get; set; } = 50; + + [JsonPropertyName("state_default")] + public long? StateDefault { get; set; } = 50; + + [JsonPropertyName("events")] + public Dictionary? Events { get; set; } // = null!; + + [JsonPropertyName("users")] + public Dictionary? Users { get; set; } // = null!; + + [JsonPropertyName("users_default")] + public long? UsersDefault { get; set; } = 0; + + [Obsolete("Historical was a key related to MSC2716, a spec change on backfill that was dropped!", true)] + [JsonIgnore] + [JsonPropertyName("historical")] + public long Historical { get; set; } // = 50; + + public class NotificationsPL { + [JsonPropertyName("room")] + public long Room { get; set; } = 50; + } + + public bool IsUserAdmin(string userId) { + ArgumentNullException.ThrowIfNull(userId); + return Users.TryGetValue(userId, out var level) && level >= Events.Max(x => x.Value); + } + + public bool UserHasTimelinePermission(string userId, string eventType) { + ArgumentNullException.ThrowIfNull(userId); + return Users.TryGetValue(userId, out var level) && level >= Events.GetValueOrDefault(eventType, EventsDefault ?? 0); + } + + public bool UserHasStatePermission(string userId, string eventType) { + ArgumentNullException.ThrowIfNull(userId); + return Users.TryGetValue(userId, out var level) && level >= Events.GetValueOrDefault(eventType, StateDefault ?? 50); + } + + public long GetUserPowerLevel(string userId) { + ArgumentNullException.ThrowIfNull(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) { + ArgumentNullException.ThrowIfNull(userId); + Users ??= new(); + Users[userId] = powerLevel; + } +} diff --git a/LibMatrix.EventTypes/Spec/State/RoomInfo/RoomServerACLEventContent.cs b/LibMatrix.EventTypes/Spec/State/RoomInfo/RoomServerACLEventContent.cs new file mode 100644 index 0000000..75337f5 --- /dev/null +++ b/LibMatrix.EventTypes/Spec/State/RoomInfo/RoomServerACLEventContent.cs @@ -0,0 +1,15 @@ +using System.Text.Json.Serialization; + +namespace LibMatrix.EventTypes.Spec.State; + +[MatrixEvent(EventName = "m.room.server_acl")] +public class RoomServerACLEventContent : TimelineEventContent { + [JsonPropertyName("allow")] + public List? Allow { get; set; } // = null!; + + [JsonPropertyName("deny")] + public List? Deny { get; set; } // = null!; + + [JsonPropertyName("allow_ip_literals")] + public bool AllowIpLiterals { get; set; } // = false; +} diff --git a/LibMatrix.EventTypes/Spec/State/RoomInfo/RoomTopicEventContent.cs b/LibMatrix.EventTypes/Spec/State/RoomInfo/RoomTopicEventContent.cs new file mode 100644 index 0000000..3121c39 --- /dev/null +++ b/LibMatrix.EventTypes/Spec/State/RoomInfo/RoomTopicEventContent.cs @@ -0,0 +1,10 @@ +using System.Text.Json.Serialization; + +namespace LibMatrix.EventTypes.Spec.State; + +[MatrixEvent(EventName = "m.room.topic")] +[MatrixEvent(EventName = "org.matrix.msc3765.topic", Legacy = true)] +public class RoomTopicEventContent : TimelineEventContent { + [JsonPropertyName("topic")] + public string? Topic { get; set; } +} diff --git a/LibMatrix.EventTypes/Spec/State/Space/SpaceChildEventContent.cs b/LibMatrix.EventTypes/Spec/State/Space/SpaceChildEventContent.cs new file mode 100644 index 0000000..fb5c938 --- /dev/null +++ b/LibMatrix.EventTypes/Spec/State/Space/SpaceChildEventContent.cs @@ -0,0 +1,13 @@ +using System.Text.Json.Serialization; + +namespace LibMatrix.EventTypes.Spec.State; + +[MatrixEvent(EventName = "m.space.child")] +public class SpaceChildEventContent : TimelineEventContent { + [JsonPropertyName("auto_join")] + public bool? AutoJoin { get; set; } + [JsonPropertyName("via")] + public List? Via { get; set; } + [JsonPropertyName("suggested")] + public bool? Suggested { get; set; } +} diff --git a/LibMatrix.EventTypes/Spec/State/Space/SpaceParentEventContent.cs b/LibMatrix.EventTypes/Spec/State/Space/SpaceParentEventContent.cs new file mode 100644 index 0000000..0c23298 --- /dev/null +++ b/LibMatrix.EventTypes/Spec/State/Space/SpaceParentEventContent.cs @@ -0,0 +1,12 @@ +using System.Text.Json.Serialization; + +namespace LibMatrix.EventTypes.Spec.State; + +[MatrixEvent(EventName = "m.space.parent")] +public class SpaceParentEventContent : TimelineEventContent { + [JsonPropertyName("via")] + public string[]? Via { get; set; } + + [JsonPropertyName("canonical")] + public bool? Canonical { get; set; } +} -- cgit 1.4.1