diff options
Diffstat (limited to 'MatrixRoomUtils.Core/Extensions')
7 files changed, 0 insertions, 320 deletions
diff --git a/MatrixRoomUtils.Core/Extensions/ClassCollector.cs b/MatrixRoomUtils.Core/Extensions/ClassCollector.cs deleted file mode 100644 index d4ba838..0000000 --- a/MatrixRoomUtils.Core/Extensions/ClassCollector.cs +++ /dev/null @@ -1,22 +0,0 @@ -using System.Reflection; - -namespace MatrixRoomUtils.Core.Extensions; - -public class ClassCollector<T> where T : class { - static ClassCollector() { - if (!typeof(T).IsInterface) - throw new ArgumentException( - $"ClassCollector<T> must be used with an interface type. Passed type: {typeof(T).Name}"); - } - - public List<Type> ResolveFromAllAccessibleAssemblies() => AppDomain.CurrentDomain.GetAssemblies().SelectMany(ResolveFromAssembly).ToList(); - - public List<Type> ResolveFromObjectReference(object obj) => ResolveFromTypeReference(obj.GetType()); - - public List<Type> ResolveFromTypeReference(Type t) => Assembly.GetAssembly(t)?.GetReferencedAssemblies().SelectMany(ResolveFromAssemblyName).ToList() ?? new List<Type>(); - - public List<Type> ResolveFromAssemblyName(AssemblyName assemblyName) => ResolveFromAssembly(Assembly.Load(assemblyName)); - - public List<Type> ResolveFromAssembly(Assembly assembly) => assembly.GetTypes() - .Where(x => x is { IsClass: true, IsAbstract: false } && x.GetInterfaces().Contains(typeof(T))).ToList(); -} diff --git a/MatrixRoomUtils.Core/Extensions/DictionaryExtensions.cs b/MatrixRoomUtils.Core/Extensions/DictionaryExtensions.cs deleted file mode 100644 index 78bbdfa..0000000 --- a/MatrixRoomUtils.Core/Extensions/DictionaryExtensions.cs +++ /dev/null @@ -1,33 +0,0 @@ -namespace MatrixRoomUtils.Core.Extensions; - -public static class DictionaryExtensions { - public static bool ChangeKey<TKey, TValue>(this IDictionary<TKey, TValue> dict, - TKey oldKey, TKey newKey) { - TValue value; - if (!dict.Remove(oldKey, out value)) - return false; - - dict[newKey] = value; // or dict.Add(newKey, value) depending on ur comfort - return true; - } - - public static Y GetOrCreate<X, Y>(this IDictionary<X, Y> dict, X key) where Y : new() { - if (dict.TryGetValue(key, out var value)) { - return value; - } - - value = new Y(); - dict.Add(key, value); - return value; - } - - public static Y GetOrCreate<X, Y>(this IDictionary<X, Y> dict, X key, Func<X, Y> valueFactory) { - if (dict.TryGetValue(key, out var value)) { - return value; - } - - value = valueFactory(key); - dict.Add(key, value); - return value; - } -} diff --git a/MatrixRoomUtils.Core/Extensions/HttpClientExtensions.cs b/MatrixRoomUtils.Core/Extensions/HttpClientExtensions.cs deleted file mode 100644 index 009338a..0000000 --- a/MatrixRoomUtils.Core/Extensions/HttpClientExtensions.cs +++ /dev/null @@ -1,76 +0,0 @@ -using System.Net.Http.Headers; -using System.Reflection; -using System.Text.Json; - -namespace MatrixRoomUtils.Core.Extensions; - -public static class HttpClientExtensions { - public static async Task<bool> CheckSuccessStatus(this HttpClient hc, string url) { - //cors causes failure, try to catch - try { - var resp = await hc.GetAsync(url); - return resp.IsSuccessStatusCode; - } - catch (Exception e) { - Console.WriteLine($"Failed to check success status: {e.Message}"); - return false; - } - } -} - -public class MatrixHttpClient : HttpClient { - public override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, - CancellationToken cancellationToken) { - Console.WriteLine($"Sending request to {request.RequestUri}"); - try { - HttpRequestOptionsKey<bool> WebAssemblyEnableStreamingResponseKey = - new HttpRequestOptionsKey<bool>("WebAssemblyEnableStreamingResponse"); - request.Options.Set(WebAssemblyEnableStreamingResponseKey, true); - } - catch (Exception e) { - Console.WriteLine("Failed to set browser response streaming:"); - Console.WriteLine(e); - } - - var a = await base.SendAsync(request, cancellationToken); - if (!a.IsSuccessStatusCode) { - var content = await a.Content.ReadAsStringAsync(cancellationToken); - if (content.StartsWith('{')) { - var ex = JsonSerializer.Deserialize<MatrixException>(content); - ex.RawContent = content; - // Console.WriteLine($"Failed to send request: {ex}"); - if (ex?.RetryAfterMs is not null) { - await Task.Delay(ex.RetryAfterMs.Value, cancellationToken); - typeof(HttpRequestMessage).GetField("_sendStatus", BindingFlags.NonPublic | BindingFlags.Instance) - ?.SetValue(request, 0); - return await SendAsync(request, cancellationToken); - } - - throw ex!; - } - - throw new InvalidDataException("Encountered invalid data:\n" + content); - } - - return a; - } - - // GetFromJsonAsync - public async Task<T> GetFromJsonAsync<T>(string requestUri, CancellationToken cancellationToken = default) { - var request = new HttpRequestMessage(HttpMethod.Get, requestUri); - request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); - var response = await SendAsync(request, cancellationToken); - response.EnsureSuccessStatusCode(); - await using var responseStream = await response.Content.ReadAsStreamAsync(cancellationToken); - return await JsonSerializer.DeserializeAsync<T>(responseStream, cancellationToken: cancellationToken); - } - - // GetStreamAsync - public async Task<Stream> GetStreamAsync(string requestUri, CancellationToken cancellationToken = default) { - var request = new HttpRequestMessage(HttpMethod.Get, requestUri); - request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); - var response = await SendAsync(request, cancellationToken); - response.EnsureSuccessStatusCode(); - return await response.Content.ReadAsStreamAsync(cancellationToken); - } -} diff --git a/MatrixRoomUtils.Core/Extensions/IEnumerableExtensions.cs b/MatrixRoomUtils.Core/Extensions/IEnumerableExtensions.cs deleted file mode 100644 index 8994529..0000000 --- a/MatrixRoomUtils.Core/Extensions/IEnumerableExtensions.cs +++ /dev/null @@ -1,9 +0,0 @@ -using MatrixRoomUtils.Core.Interfaces; - -namespace MatrixRoomUtils.Core.Extensions; - -[AttributeUsage(AttributeTargets.Class, AllowMultiple = true)] -public class MatrixEventAttribute : Attribute { - public string EventName { get; set; } - public bool Legacy { get; set; } -} \ No newline at end of file diff --git a/MatrixRoomUtils.Core/Extensions/JsonElementExtensions.cs b/MatrixRoomUtils.Core/Extensions/JsonElementExtensions.cs deleted file mode 100644 index 7701c9e..0000000 --- a/MatrixRoomUtils.Core/Extensions/JsonElementExtensions.cs +++ /dev/null @@ -1,151 +0,0 @@ -using System.Diagnostics.CodeAnalysis; -using System.Reflection; -using System.Text.Json; -using System.Text.Json.Nodes; -using System.Text.Json.Serialization; -using MatrixRoomUtils.Core.Responses; - -namespace MatrixRoomUtils.Core.Extensions; - -public static class JsonElementExtensions { - public static bool FindExtraJsonElementFields(this JsonElement obj, Type objectType, string objectPropertyName) { - if (objectPropertyName == "content" && objectType == typeof(JsonObject)) - objectType = typeof(StateEventResponse); - // if (t == typeof(JsonNode)) - // return false; - - Console.WriteLine($"{objectType.Name} {objectPropertyName}"); - bool 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(StateEventResponse) || objectType == typeof(StateEvent))) { - unknownPropertyFound |= field.FindExtraJsonPropertyFieldsByValueKind( - StateEvent.GetStateEventType(obj.GetProperty("type").GetString()), - 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]; - } - - bool 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) - ); - } -} diff --git a/MatrixRoomUtils.Core/Extensions/ObjectExtensions.cs b/MatrixRoomUtils.Core/Extensions/ObjectExtensions.cs deleted file mode 100644 index a4b0791..0000000 --- a/MatrixRoomUtils.Core/Extensions/ObjectExtensions.cs +++ /dev/null @@ -1,16 +0,0 @@ -using System.Text.Encodings.Web; -using System.Text.Json; -using MatrixRoomUtils.Core.Interfaces; -using MatrixRoomUtils.Core.Responses; - -namespace MatrixRoomUtils.Core.Extensions; - -public static class ObjectExtensions { - public static string ToJson(this object obj, bool indent = true, bool ignoreNull = false, bool unsafeContent = false) { - var jso = new JsonSerializerOptions(); - if (indent) jso.WriteIndented = true; - if (ignoreNull) jso.IgnoreNullValues = true; - if (unsafeContent) jso.Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping; - return JsonSerializer.Serialize(obj, jso); - } -} \ No newline at end of file diff --git a/MatrixRoomUtils.Core/Extensions/StringExtensions.cs b/MatrixRoomUtils.Core/Extensions/StringExtensions.cs deleted file mode 100644 index b81d59f..0000000 --- a/MatrixRoomUtils.Core/Extensions/StringExtensions.cs +++ /dev/null @@ -1,13 +0,0 @@ -namespace MatrixRoomUtils.Core.Extensions; - -public static class StringExtensions { - // public static async Task<string> GetMediaUrl(this string MxcUrl) - // { - // //MxcUrl: mxc://rory.gay/ocRVanZoUTCcifcVNwXgbtTg - // //target: https://matrix.rory.gay/_matrix/media/v3/download/rory.gay/ocRVanZoUTCcifcVNwXgbtTg - // - // var server = MxcUrl.Split('/')[2]; - // var mediaId = MxcUrl.Split('/')[3]; - // return $"{(await new RemoteHomeServer(server).Configure()).FullHomeServerDomain}/_matrix/media/v3/download/{server}/{mediaId}"; - // } -} \ No newline at end of file |