diff --git a/LibMatrix/Extensions/EnumerableExtensions.cs b/LibMatrix/Extensions/EnumerableExtensions.cs
index b5193ab..7a810ac 100644
--- a/LibMatrix/Extensions/EnumerableExtensions.cs
+++ b/LibMatrix/Extensions/EnumerableExtensions.cs
@@ -1,6 +1,7 @@
namespace LibMatrix.Extensions;
public static class EnumerableExtensions {
+#if !DISABLE_LEGACY_EVENTS
public static void MergeStateEventLists(this IList<LegacyMatrixEvent> oldState, IList<LegacyMatrixEvent> newState) {
foreach (var stateEvent in newState) {
var old = oldState.FirstOrDefault(x => x.Type == stateEvent.Type && x.StateKey == stateEvent.StateKey);
@@ -26,4 +27,5 @@ public static class EnumerableExtensions {
oldState.Add(stateEvent);
}
}
+#endif
}
\ No newline at end of file
diff --git a/LibMatrix/Extensions/JsonElementExtensions.cs b/LibMatrix/Extensions/JsonElementExtensions.cs
deleted file mode 100644
index a2faddc..0000000
--- a/LibMatrix/Extensions/JsonElementExtensions.cs
+++ /dev/null
@@ -1,147 +0,0 @@
-using System.Reflection;
-using System.Text.Json;
-using System.Text.Json.Nodes;
-using System.Text.Json.Serialization;
-
-namespace LibMatrix.Extensions;
-
-public static class JsonElementExtensions {
- public static bool FindExtraJsonElementFields(this JsonElement obj, Type objectType, string objectPropertyName) {
- if (objectPropertyName == "content" && objectType == typeof(JsonObject))
- objectType = typeof(LegacyMatrixEventResponse);
- // if (t == typeof(JsonNode))
- // return false;
-
- Console.WriteLine($"{objectType.Name} {objectPropertyName}");
- var unknownPropertyFound = false;
- var mappedPropsDict = objectType.GetProperties()
- .Where(x => x.GetCustomAttribute<JsonPropertyNameAttribute>() is not null)
- .ToDictionary(x => x.GetCustomAttribute<JsonPropertyNameAttribute>()!.Name, x => x);
- objectType.GetProperties().Where(x => !mappedPropsDict.ContainsKey(x.Name))
- .ToList().ForEach(x => mappedPropsDict.TryAdd(x.Name, x));
-
- foreach (var field in obj.EnumerateObject()) {
- if (mappedPropsDict.TryGetValue(field.Name, out var mappedProperty)) {
- //dictionary
- if (mappedProperty.PropertyType.IsGenericType &&
- mappedProperty.PropertyType.GetGenericTypeDefinition() == typeof(Dictionary<,>)) {
- unknownPropertyFound |= _checkDictionary(field, objectType, mappedProperty.PropertyType);
- continue;
- }
-
- if (mappedProperty.PropertyType.IsGenericType &&
- mappedProperty.PropertyType.GetGenericTypeDefinition() == typeof(List<>)) {
- unknownPropertyFound |= _checkList(field, objectType, mappedProperty.PropertyType);
- continue;
- }
-
- if (field.Name == "content" && (objectType == typeof(LegacyMatrixEventResponse) || objectType == typeof(LegacyMatrixEvent))) {
- unknownPropertyFound |= field.FindExtraJsonPropertyFieldsByValueKind(
- LegacyMatrixEvent.GetStateEventType(obj.GetProperty("type").GetString()!), // We expect type to always be present
- mappedProperty.PropertyType);
- continue;
- }
-
- unknownPropertyFound |=
- field.FindExtraJsonPropertyFieldsByValueKind(objectType, mappedProperty.PropertyType);
- continue;
- }
-
- Console.WriteLine($"[!!] Unknown property {field.Name} in {objectType.Name}!");
- unknownPropertyFound = true;
- }
-
- return unknownPropertyFound;
- }
-
- private static bool FindExtraJsonPropertyFieldsByValueKind(this JsonProperty field, Type containerType,
- Type propertyType) {
- if (propertyType.IsGenericType && propertyType.GetGenericTypeDefinition() == typeof(Nullable<>)) propertyType = propertyType.GetGenericArguments()[0];
-
- var switchResult = false;
- switch (field.Value.ValueKind) {
- case JsonValueKind.Array:
- switchResult = field.Value.EnumerateArray().Aggregate(switchResult,
- (current, element) => current | element.FindExtraJsonElementFields(propertyType, field.Name));
- break;
- case JsonValueKind.Object:
- switchResult |= field.Value.FindExtraJsonElementFields(propertyType, field.Name);
- break;
- case JsonValueKind.True:
- case JsonValueKind.False:
- return _checkBool(field, containerType, propertyType);
- case JsonValueKind.String:
- return _checkString(field, containerType, propertyType);
- case JsonValueKind.Number:
- return _checkNumber(field, containerType, propertyType);
- case JsonValueKind.Undefined:
- case JsonValueKind.Null:
- break;
- default:
- throw new ArgumentOutOfRangeException();
- }
-
- return switchResult;
- }
-
- private static bool _checkBool(this JsonProperty field, Type containerType, Type propertyType) {
- if (propertyType == typeof(bool)) return true;
- Console.WriteLine(
- $"[!!] Encountered bool for {field.Name} in {containerType.Name}, the class defines {propertyType.Name}!");
- return false;
- }
-
- private static bool _checkString(this JsonProperty field, Type containerType, Type propertyType) {
- if (propertyType == typeof(string)) return true;
- // ReSharper disable once BuiltInTypeReferenceStyle
- if (propertyType == typeof(String)) return true;
- Console.WriteLine(
- $"[!!] Encountered string for {field.Name} in {containerType.Name}, the class defines {propertyType.Name}!");
- return false;
- }
-
- private static bool _checkNumber(this JsonProperty field, Type containerType, Type propertyType) {
- if (propertyType == typeof(int) ||
- propertyType == typeof(double) ||
- propertyType == typeof(float) ||
- propertyType == typeof(decimal) ||
- propertyType == typeof(long) ||
- propertyType == typeof(short) ||
- propertyType == typeof(uint) ||
- propertyType == typeof(ulong) ||
- propertyType == typeof(ushort) ||
- propertyType == typeof(byte) ||
- propertyType == typeof(sbyte))
- return true;
- Console.WriteLine(
- $"[!!] Encountered number for {field.Name} in {containerType.Name}, the class defines {propertyType.Name}!");
- return false;
- }
-
- private static bool _checkDictionary(this JsonProperty field, Type containerType, Type propertyType) {
- var keyType = propertyType.GetGenericArguments()[0];
- var valueType = propertyType.GetGenericArguments()[1];
- valueType = Nullable.GetUnderlyingType(valueType) ?? valueType;
- Console.WriteLine(
- $"Encountered dictionary {field.Name} with key type {keyType.Name} and value type {valueType.Name}!");
-
- return field.Value.EnumerateObject()
- .Where(key => !valueType.IsPrimitive && valueType != typeof(string))
- .Aggregate(false, (current, key) =>
- current | key.FindExtraJsonPropertyFieldsByValueKind(containerType, valueType)
- );
- }
-
- private static bool _checkList(this JsonProperty field, Type containerType, Type propertyType) {
- var valueType = propertyType.GetGenericArguments()[0];
- valueType = Nullable.GetUnderlyingType(valueType) ?? valueType;
- Console.WriteLine(
- $"Encountered list {field.Name} with value type {valueType.Name}!");
-
- return field.Value.EnumerateArray()
- .Where(key => !valueType.IsPrimitive && valueType != typeof(string))
- .Aggregate(false, (current, key) =>
- current | key.FindExtraJsonElementFields(valueType, field.Name)
- );
- }
-}
\ No newline at end of file
diff --git a/LibMatrix/Filters/SyncFilter.cs b/LibMatrix/Filters/MatrixFilter.cs
index 787ffa7..08dc2fc 100644
--- a/LibMatrix/Filters/SyncFilter.cs
+++ b/LibMatrix/Filters/MatrixFilter.cs
@@ -2,7 +2,7 @@ using System.Text.Json.Serialization;
namespace LibMatrix.Filters;
-public class SyncFilter {
+public class MatrixFilter {
[JsonPropertyName("account_data")]
public EventFilter? AccountData { get; set; }
diff --git a/LibMatrix/Helpers/MessageBuilder.cs b/LibMatrix/Helpers/MessageBuilder.cs
index 0ed0339..43999b3 100644
--- a/LibMatrix/Helpers/MessageBuilder.cs
+++ b/LibMatrix/Helpers/MessageBuilder.cs
@@ -1,3 +1,4 @@
+#if !DISABLE_LEGACY_EVENTS
using LibMatrix.LegacyEvents.EventTypes.Spec;
namespace LibMatrix.Helpers;
@@ -118,4 +119,6 @@ public class MessageBuilder(string msgType = "m.text", string format = "org.matr
}
}
}
-}
\ No newline at end of file
+}
+
+#endif
\ No newline at end of file
diff --git a/LibMatrix/Helpers/SyncHelper.cs b/LibMatrix/Helpers/SyncHelper.cs
index 5635955..09bcf8e 100644
--- a/LibMatrix/Helpers/SyncHelper.cs
+++ b/LibMatrix/Helpers/SyncHelper.cs
@@ -9,7 +9,7 @@ using Microsoft.Extensions.Logging;
namespace LibMatrix.Helpers;
public class SyncHelper(AuthenticatedHomeserverGeneric homeserver, ILogger? logger = null) {
- private SyncFilter? _filter;
+ private MatrixFilter? _filter;
private string? _namedFilterName;
private bool _filterIsDirty = false;
private string? _filterId = null;
@@ -36,7 +36,7 @@ public class SyncHelper(AuthenticatedHomeserverGeneric homeserver, ILogger? logg
}
}
- public SyncFilter? Filter {
+ public MatrixFilter? Filter {
get => _filter;
set {
_filter = value;
diff --git a/LibMatrix/Helpers/SyncStateResolver.cs b/LibMatrix/Helpers/SyncStateResolver.cs
index b1f83ff..511819a 100644
--- a/LibMatrix/Helpers/SyncStateResolver.cs
+++ b/LibMatrix/Helpers/SyncStateResolver.cs
@@ -10,7 +10,7 @@ public class SyncStateResolver(AuthenticatedHomeserverGeneric homeserver, ILogge
public string? Since { get; set; }
public int Timeout { get; set; } = 30000;
public string? SetPresence { get; set; } = "online";
- public SyncFilter? Filter { get; set; }
+ public MatrixFilter? Filter { get; set; }
public bool FullState { get; set; } = false;
public SyncResponse? MergedState { get; set; }
diff --git a/LibMatrix/Homeservers/AuthenticatedHomeserverGeneric.cs b/LibMatrix/Homeservers/AuthenticatedHomeserverGeneric.cs
index db76985..96e20e0 100644
--- a/LibMatrix/Homeservers/AuthenticatedHomeserverGeneric.cs
+++ b/LibMatrix/Homeservers/AuthenticatedHomeserverGeneric.cs
@@ -192,8 +192,8 @@ public class AuthenticatedHomeserverGeneric : RemoteHomeserver {
var oldProfile = await GetProfileAsync(WhoAmI.UserId!);
Dictionary<string, RoomMemberLegacyEventContent> expectedRoomProfiles = new();
var syncHelper = new SyncHelper(this) {
- Filter = new SyncFilter {
- AccountData = new SyncFilter.EventFilter() {
+ Filter = new MatrixFilter {
+ AccountData = new MatrixFilter.EventFilter() {
Types = new List<string> {
"m.room.member"
}
@@ -312,7 +312,7 @@ public class AuthenticatedHomeserverGeneric : RemoteHomeserver {
/// <param name="filter"></param>
/// <returns></returns>
/// <exception cref="Exception"></exception>
- public async Task<FilterIdResponse> UploadFilterAsync(SyncFilter filter) {
+ public async Task<FilterIdResponse> UploadFilterAsync(MatrixFilter filter) {
List<List<string>?> senderLists = [
filter.AccountData?.Senders,
filter.AccountData?.NotSenders,
@@ -338,10 +338,10 @@ public class AuthenticatedHomeserverGeneric : RemoteHomeserver {
return await resp.Content.ReadFromJsonAsync<FilterIdResponse>() ?? throw new Exception("Failed to upload filter?");
}
- public async Task<SyncFilter> GetFilterAsync(string filterId) {
+ public async Task<MatrixFilter> GetFilterAsync(string filterId) {
if (_filterCache.TryGetValue(filterId, out var filter)) return filter;
var resp = await ClientHttpClient.GetAsync("/_matrix/client/v3/user/" + UserId + "/filter/" + filterId);
- return _filterCache[filterId] = await resp.Content.ReadFromJsonAsync<SyncFilter>() ?? throw new Exception("Failed to get filter?");
+ return _filterCache[filterId] = await resp.Content.ReadFromJsonAsync<MatrixFilter>() ?? throw new Exception("Failed to get filter?");
}
public class FilterIdResponse {
@@ -385,7 +385,7 @@ public class AuthenticatedHomeserverGeneric : RemoteHomeserver {
}
private Dictionary<string, string>? _namedFilterCache;
- private Dictionary<string, SyncFilter> _filterCache = new();
+ private Dictionary<string, MatrixFilter> _filterCache = new();
public async Task<JsonObject?> GetCapabilitiesAsync() {
var res = await ClientHttpClient.GetAsync("/_matrix/client/v3/capabilities");
diff --git a/LibMatrix/Homeservers/Extensions/NamedCaches/NamedFilterCache.cs b/LibMatrix/Homeservers/Extensions/NamedCaches/NamedFilterCache.cs
index 76533a4..551ac39 100644
--- a/LibMatrix/Homeservers/Extensions/NamedCaches/NamedFilterCache.cs
+++ b/LibMatrix/Homeservers/Extensions/NamedCaches/NamedFilterCache.cs
@@ -13,7 +13,7 @@ public class NamedFilterCache(AuthenticatedHomeserverGeneric hs) : NamedCache<st
/// <param name="filter">Filter to upload if not cached, otherwise defaults to common filters if that exists.</param>
/// <returns></returns>
/// <exception cref="ArgumentNullException"></exception>
- public async Task<string> GetOrSetValueAsync(string key, SyncFilter? filter = null) {
+ public async Task<string> GetOrSetValueAsync(string key, MatrixFilter? filter = null) {
var existingValue = await GetValueAsync(key);
if (existingValue != null) {
return existingValue;
diff --git a/LibMatrix/LegacyMatrixEvent.cs b/LibMatrix/LegacyMatrixEvent.cs
index 5433da3..a1ac5db 100644
--- a/LibMatrix/LegacyMatrixEvent.cs
+++ b/LibMatrix/LegacyMatrixEvent.cs
@@ -1,3 +1,4 @@
+#if !DISABLE_LEGACY_EVENTS
using System.Collections.Frozen;
using System.Diagnostics.CodeAnalysis;
using System.Reflection;
@@ -222,4 +223,5 @@ public class StateEventContentPolymorphicTypeInfoResolver : DefaultJsonTypeInfoR
}
*/
-#endregion
\ No newline at end of file
+#endregion
+#endif
\ No newline at end of file
diff --git a/LibMatrix/LibMatrix.csproj b/LibMatrix/LibMatrix.csproj
index 42ef17d..69f2bf4 100644
--- a/LibMatrix/LibMatrix.csproj
+++ b/LibMatrix/LibMatrix.csproj
@@ -8,6 +8,10 @@
<Optimize>true</Optimize>
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
+
+ <!-- event rewrite dev... -->
+ <DefineConstants Condition="!$(ExtraDefineConstants.Contains('WITH_LEGACY_EVENTS'))">$(DefineConstants);DISABLE_LEGACY_EVENTS</DefineConstants>
+ <DefineConstants Condition=" '$(ExtraDefineConstants)' != '' ">$(DefineConstants);$(ExtraDefineConstants)</DefineConstants>
</PropertyGroup>
<ItemGroup>
diff --git a/LibMatrix/RoomTypes/GenericRoom.cs b/LibMatrix/RoomTypes/GenericRoom.cs
index 8fa46d3..aafe29f 100644
--- a/LibMatrix/RoomTypes/GenericRoom.cs
+++ b/LibMatrix/RoomTypes/GenericRoom.cs
@@ -105,16 +105,16 @@ public class GenericRoom {
if (!fallbackToSync) throw;
Console.WriteLine("WARNING: Homeserver does not support getting event ID from state events, falling back to sync");
var sh = new SyncHelper(Homeserver);
- var emptyFilter = new SyncFilter.EventFilter(types: [], limit: 1, senders: [], notTypes: ["*"]);
- var emptyStateFilter = new SyncFilter.RoomFilter.StateFilter(types: [], limit: 1, senders: [], notTypes: ["*"], rooms:[]);
+ var emptyFilter = new MatrixFilter.EventFilter(types: [], limit: 1, senders: [], notTypes: ["*"]);
+ var emptyStateFilter = new MatrixFilter.RoomFilter.StateFilter(types: [], limit: 1, senders: [], notTypes: ["*"], rooms:[]);
sh.Filter = new() {
Presence = emptyFilter,
AccountData = emptyFilter,
- Room = new SyncFilter.RoomFilter() {
+ Room = new MatrixFilter.RoomFilter() {
AccountData = emptyStateFilter,
Timeline = emptyStateFilter,
Ephemeral = emptyStateFilter,
- State = new SyncFilter.RoomFilter.StateFilter(),
+ State = new MatrixFilter.RoomFilter.StateFilter(),
Rooms = [RoomId]
}
};
diff --git a/LibMatrix/Utilities/CommonSyncFilters.cs b/LibMatrix/Utilities/CommonSyncFilters.cs
index d07c225..68836f5 100644
--- a/LibMatrix/Utilities/CommonSyncFilters.cs
+++ b/LibMatrix/Utilities/CommonSyncFilters.cs
@@ -13,29 +13,29 @@ public static class CommonSyncFilters {
public const string GetSpaceRelations = "gay.rory.matrixutils.get_space_relations.v0";
public const string GetOwnMemberEvents = "gay.rory.matrixutils.get_own_member_events.v0";
- public static SyncFilter GetAccountDataFilter => new() {
- Presence = new SyncFilter.EventFilter(notTypes: ["*"]),
- Room = new SyncFilter.RoomFilter() {
+ public static MatrixFilter GetAccountDataFilter => new() {
+ Presence = new MatrixFilter.EventFilter(notTypes: ["*"]),
+ Room = new MatrixFilter.RoomFilter() {
Rooms = []
}
};
- public static SyncFilter GetAccountDataWithRoomsFilter => new() {
- Presence = new SyncFilter.EventFilter(notTypes: ["*"]),
- Room = new SyncFilter.RoomFilter() {
- State = new SyncFilter.RoomFilter.StateFilter(notTypes: ["*"]),
- Ephemeral = new SyncFilter.RoomFilter.StateFilter(notTypes: ["*"]),
- Timeline = new SyncFilter.RoomFilter.StateFilter(notTypes: ["*"])
+ public static MatrixFilter GetAccountDataWithRoomsFilter => new() {
+ Presence = new MatrixFilter.EventFilter(notTypes: ["*"]),
+ Room = new MatrixFilter.RoomFilter() {
+ State = new MatrixFilter.RoomFilter.StateFilter(notTypes: ["*"]),
+ Ephemeral = new MatrixFilter.RoomFilter.StateFilter(notTypes: ["*"]),
+ Timeline = new MatrixFilter.RoomFilter.StateFilter(notTypes: ["*"])
}
};
- public static SyncFilter GetBasicRoomDataFilter => new() {
- AccountData = new SyncFilter.EventFilter(notTypes: ["*"], limit: 1),
- Presence = new SyncFilter.EventFilter(notTypes: ["*"], limit: 1),
- Room = new SyncFilter.RoomFilter {
- AccountData = new SyncFilter.RoomFilter.StateFilter(rooms: []),
- Ephemeral = new SyncFilter.RoomFilter.StateFilter(rooms: []),
- State = new SyncFilter.RoomFilter.StateFilter {
+ public static MatrixFilter GetBasicRoomDataFilter => new() {
+ AccountData = new MatrixFilter.EventFilter(notTypes: ["*"], limit: 1),
+ Presence = new MatrixFilter.EventFilter(notTypes: ["*"], limit: 1),
+ Room = new MatrixFilter.RoomFilter {
+ AccountData = new MatrixFilter.RoomFilter.StateFilter(rooms: []),
+ Ephemeral = new MatrixFilter.RoomFilter.StateFilter(rooms: []),
+ State = new MatrixFilter.RoomFilter.StateFilter {
Types = new List<string> {
RoomCreateLegacyEventContent.EventId,
RoomNameLegacyEventContent.EventId,
@@ -45,46 +45,46 @@ public static class CommonSyncFilters {
},
LazyLoadMembers = true, IncludeRedundantMembers = false
},
- Timeline = new SyncFilter.RoomFilter.StateFilter(rooms: [])
+ Timeline = new MatrixFilter.RoomFilter.StateFilter(rooms: [])
}
};
- public static SyncFilter GetSpaceRelationsFilter => new() {
- AccountData = new SyncFilter.EventFilter(notTypes: ["*"], limit: 1),
- Presence = new SyncFilter.EventFilter(notTypes: ["*"], limit: 1),
- Room = new SyncFilter.RoomFilter {
- AccountData = new SyncFilter.RoomFilter.StateFilter(rooms: []),
- Ephemeral = new SyncFilter.RoomFilter.StateFilter(rooms: []),
- State = new SyncFilter.RoomFilter.StateFilter {
+ public static MatrixFilter GetSpaceRelationsFilter => new() {
+ AccountData = new MatrixFilter.EventFilter(notTypes: ["*"], limit: 1),
+ Presence = new MatrixFilter.EventFilter(notTypes: ["*"], limit: 1),
+ Room = new MatrixFilter.RoomFilter {
+ AccountData = new MatrixFilter.RoomFilter.StateFilter(rooms: []),
+ Ephemeral = new MatrixFilter.RoomFilter.StateFilter(rooms: []),
+ State = new MatrixFilter.RoomFilter.StateFilter {
Types = new List<string> {
SpaceChildLegacyEventContent.EventId,
SpaceParentLegacyEventContent.EventId
},
LazyLoadMembers = true, IncludeRedundantMembers = false
},
- Timeline = new SyncFilter.RoomFilter.StateFilter(rooms: [])
+ Timeline = new MatrixFilter.RoomFilter.StateFilter(rooms: [])
}
};
- public static SyncFilter GetOwnMemberEventsFilter => new() {
- AccountData = new SyncFilter.EventFilter(types: ["m.room.member"], limit: 1),
- Presence = new SyncFilter.EventFilter(notTypes: ["*"], limit: 1),
- Room = new SyncFilter.RoomFilter {
- AccountData = new SyncFilter.RoomFilter.StateFilter(rooms: []),
- Ephemeral = new SyncFilter.RoomFilter.StateFilter(rooms: []),
- State = new SyncFilter.RoomFilter.StateFilter {
+ public static MatrixFilter GetOwnMemberEventsFilter => new() {
+ AccountData = new MatrixFilter.EventFilter(types: ["m.room.member"], limit: 1),
+ Presence = new MatrixFilter.EventFilter(notTypes: ["*"], limit: 1),
+ Room = new MatrixFilter.RoomFilter {
+ AccountData = new MatrixFilter.RoomFilter.StateFilter(rooms: []),
+ Ephemeral = new MatrixFilter.RoomFilter.StateFilter(rooms: []),
+ State = new MatrixFilter.RoomFilter.StateFilter {
Types = new List<string> {
RoomMemberLegacyEventContent.EventId
},
LazyLoadMembers = true, IncludeRedundantMembers = false,
Senders = ["@me"]
},
- Timeline = new SyncFilter.RoomFilter.StateFilter(rooms: [])
+ Timeline = new MatrixFilter.RoomFilter.StateFilter(rooms: [])
}
};
// This must be down here, due to statics load order
- public static FrozenDictionary<string, SyncFilter> FilterMap => new Dictionary<string, SyncFilter>() {
+ public static FrozenDictionary<string, MatrixFilter> FilterMap => new Dictionary<string, MatrixFilter>() {
[GetAccountData] = GetAccountDataFilter,
[GetAccountDataWithRooms] = GetAccountDataWithRoomsFilter,
[GetBasicRoomInfo] = GetBasicRoomDataFilter,
|