diff options
8 files changed, 184 insertions, 10 deletions
diff --git a/MatrixRoomUtils.Core/Helpers/SyncHelper.cs b/MatrixRoomUtils.Core/Helpers/SyncHelper.cs index f5c7d9d..ea9317e 100644 --- a/MatrixRoomUtils.Core/Helpers/SyncHelper.cs +++ b/MatrixRoomUtils.Core/Helpers/SyncHelper.cs @@ -1,5 +1,7 @@ using System.Diagnostics.CodeAnalysis; using System.Net.Http.Json; +using System.Text.Json; +using System.Text.Json.Nodes; using System.Text.Json.Serialization; using MatrixRoomUtils.Core.Extensions; using MatrixRoomUtils.Core.Filters; @@ -36,11 +38,25 @@ public class SyncHelper { // else url += "&full_state=true"; Console.WriteLine("Calling: " + url); try { - var res = await _homeServer._httpClient.GetFromJsonAsync<SyncResult>(url, - cancellationToken: cancellationToken ?? CancellationToken.None); - // await _storageService.CacheStorageProvider.SaveObjectAsync(outFileName, res); - // Console.WriteLine($"Wrote file: {outFileName}"); + var req = await _homeServer._httpClient.GetAsync(url, cancellationToken: cancellationToken ?? CancellationToken.None); + + // var res = await JsonSerializer.DeserializeAsync<SyncResult>(await req.Content.ReadAsStreamAsync()); + +#if DEBUG + var jsonObj = await req.Content.ReadFromJsonAsync<JsonElement>(); + try { + await _homeServer._httpClient.PostAsJsonAsync( + "http://localhost:5116/validate/" + typeof(SyncResult).AssemblyQualifiedName, jsonObj); + } + catch (Exception e) { + Console.WriteLine("[!!] Checking sync response failed: " + e); + } + + var res = jsonObj.Deserialize<SyncResult>(); return res; +#else + return await req.Content.ReadFromJsonAsync<SyncResult>(); +#endif } catch (TaskCanceledException) { Console.WriteLine("Sync cancelled!"); @@ -61,10 +77,15 @@ public class SyncHelper { SyncFilter? filter = null, CancellationToken? cancellationToken = null ) { + await Task.WhenAll((await _storageService.CacheStorageProvider.GetAllKeysAsync()) + .Where(x => x.StartsWith("sync")) + .ToList() + .Select(x => _storageService.CacheStorageProvider.DeleteObjectAsync(x))); SyncResult? sync = null; string? nextBatch = since; while (cancellationToken is null || !cancellationToken.Value.IsCancellationRequested) { - sync = await Sync(since: nextBatch, timeout: timeout, setPresence: setPresence, filter: filter, cancellationToken: cancellationToken); + sync = await Sync(since: nextBatch, timeout: timeout, setPresence: setPresence, filter: filter, + cancellationToken: cancellationToken); nextBatch = sync?.NextBatch ?? nextBatch; if (sync is null) continue; Console.WriteLine($"Got sync, next batch: {nextBatch}!"); @@ -126,17 +147,17 @@ public class SyncResult { [JsonPropertyName("rooms")] public RoomsDataStructure? Rooms { get; set; } - + [JsonPropertyName("to_device")] public EventList? ToDevice { get; set; } - + [JsonPropertyName("device_lists")] public DeviceListsDataStructure? DeviceLists { get; set; } public class DeviceListsDataStructure { [JsonPropertyName("changed")] public List<string>? Changed { get; set; } - + [JsonPropertyName("left")] public List<string>? Left { get; set; } } @@ -214,4 +235,4 @@ public class SyncResult { public class EventList { [JsonPropertyName("events")] public List<StateEventResponse> Events { get; set; } -} \ No newline at end of file +} diff --git a/MatrixRoomUtils.Core/RoomTypes/GenericRoom.cs b/MatrixRoomUtils.Core/RoomTypes/GenericRoom.cs index db4d4ce..eced379 100644 --- a/MatrixRoomUtils.Core/RoomTypes/GenericRoom.cs +++ b/MatrixRoomUtils.Core/RoomTypes/GenericRoom.cs @@ -1,5 +1,6 @@ using System.Net.Http.Json; using System.Text.Json; +using System.Text.Json.Nodes; using System.Web; using MatrixRoomUtils.Core.Extensions; using MatrixRoomUtils.Core.Responses; @@ -43,13 +44,27 @@ public class GenericRoom { if (!string.IsNullOrEmpty(type)) url += $"/{type}"; if (!string.IsNullOrEmpty(stateKey)) url += $"/{stateKey}"; try { +#if DEBUG && false + var resp = await _httpClient.GetFromJsonAsync<JsonObject>(url); + try { + _homeServer._httpClient.PostAsJsonAsync( + "http://localhost:5116/validate/" + typeof(T).AssemblyQualifiedName, resp); + } + catch (Exception e) { + Console.WriteLine("[!!] Checking state response failed: " + e); + } + + return resp.Deserialize<T>(); +#else var resp = await _httpClient.GetFromJsonAsync<T>(url); return resp; +#endif } catch (MatrixException e) { if (e is not { ErrorCode: "M_NOT_FOUND" }) { throw; } + Console.WriteLine(e); return default; } @@ -169,4 +184,4 @@ public class GenericRoom { } public readonly SpaceRoom AsSpace; -} \ No newline at end of file +} diff --git a/MatrixRoomUtils.DebugDataValidationApi/Controllers/ValidationController.cs b/MatrixRoomUtils.DebugDataValidationApi/Controllers/ValidationController.cs new file mode 100644 index 0000000..949871a --- /dev/null +++ b/MatrixRoomUtils.DebugDataValidationApi/Controllers/ValidationController.cs @@ -0,0 +1,26 @@ +using System.Text.Json; +using MatrixRoomUtils.Core.Extensions; +using Microsoft.AspNetCore.Mvc; + +namespace MatrixRoomUtils.DebugDataValidationApi.Controllers; + +[ApiController] +[Route("/")] +public class ValidationController : ControllerBase { + private readonly ILogger<ValidationController> _logger; + + public ValidationController(ILogger<ValidationController> logger) { + _logger = logger; + } + + [HttpPost("/validate/{type}")] + public async Task<bool> Get([FromRoute] string type, [FromBody] JsonElement content) { + Type t = Type.GetType(type); + if (t is null) { + Console.WriteLine($"Type `{type}` does not exist!"); + throw new ArgumentException($"Unknown type {type}!"); + } + Console.WriteLine($"Validating {type}..."); + return content.FindExtraJsonElementFields(t, "$"); + } +} diff --git a/MatrixRoomUtils.DebugDataValidationApi/MatrixRoomUtils.DebugDataValidationApi.csproj b/MatrixRoomUtils.DebugDataValidationApi/MatrixRoomUtils.DebugDataValidationApi.csproj new file mode 100644 index 0000000..4c23207 --- /dev/null +++ b/MatrixRoomUtils.DebugDataValidationApi/MatrixRoomUtils.DebugDataValidationApi.csproj @@ -0,0 +1,19 @@ +<Project Sdk="Microsoft.NET.Sdk.Web"> + + <PropertyGroup> + <TargetFramework>net7.0</TargetFramework> + <Nullable>enable</Nullable> + <ImplicitUsings>enable</ImplicitUsings> + <InvariantGlobalization>true</InvariantGlobalization> + </PropertyGroup> + + <ItemGroup> + <PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="7.0.9" /> + <PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" /> + </ItemGroup> + + <ItemGroup> + <ProjectReference Include="..\MatrixRoomUtils.Core\MatrixRoomUtils.Core.csproj" /> + </ItemGroup> + +</Project> diff --git a/MatrixRoomUtils.DebugDataValidationApi/Program.cs b/MatrixRoomUtils.DebugDataValidationApi/Program.cs new file mode 100644 index 0000000..047dbcf --- /dev/null +++ b/MatrixRoomUtils.DebugDataValidationApi/Program.cs @@ -0,0 +1,32 @@ +var builder = WebApplication.CreateBuilder(args); + +// Add services to the container. + +builder.Services.AddControllers(); +// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle +builder.Services.AddEndpointsApiExplorer(); +builder.Services.AddSwaggerGen(); +builder.Services.AddCors(options => +{ + options.AddPolicy( + "Open", + builder => builder.AllowAnyOrigin().AllowAnyHeader()); +}); + +var app = builder.Build(); + +// Configure the HTTP request pipeline. +if (app.Environment.IsDevelopment()) { + app.UseSwagger(); + app.UseSwaggerUI(); +} + +// app.UseHttpsRedirection(); + +app.UseCors("Open"); + +app.UseAuthorization(); + +app.MapControllers(); + +app.Run(); diff --git a/MatrixRoomUtils.DebugDataValidationApi/Properties/launchSettings.json b/MatrixRoomUtils.DebugDataValidationApi/Properties/launchSettings.json new file mode 100644 index 0000000..c33e091 --- /dev/null +++ b/MatrixRoomUtils.DebugDataValidationApi/Properties/launchSettings.json @@ -0,0 +1,41 @@ +{ + "$schema": "http://json.schemastore.org/launchsettings.json", + "iisSettings": { + "windowsAuthentication": false, + "anonymousAuthentication": true, + "iisExpress": { + "applicationUrl": "http://localhost:63687", + "sslPort": 44316 + } + }, + "profiles": { + "http": { + "commandName": "Project", + "dotnetRunMessages": true, + "launchBrowser": false, + "launchUrl": "swagger", + "applicationUrl": "http://localhost:5116", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + }, + "https": { + "commandName": "Project", + "dotnetRunMessages": true, + "launchBrowser": true, + "launchUrl": "swagger", + "applicationUrl": "https://localhost:7017;http://localhost:5116", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + }, + "IIS Express": { + "commandName": "IISExpress", + "launchBrowser": true, + "launchUrl": "swagger", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + } + } +} diff --git a/MatrixRoomUtils.DebugDataValidationApi/appsettings.Development.json b/MatrixRoomUtils.DebugDataValidationApi/appsettings.Development.json new file mode 100644 index 0000000..12c8ab9 --- /dev/null +++ b/MatrixRoomUtils.DebugDataValidationApi/appsettings.Development.json @@ -0,0 +1,11 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Information", + "Microsoft.AspNetCore.Routing": "Warning", + "Microsoft.AspNetCore.Mvc": "Warning", + "Microsoft.AspNetCore.Cors": "Warning" + } + } +} diff --git a/MatrixRoomUtils.DebugDataValidationApi/appsettings.json b/MatrixRoomUtils.DebugDataValidationApi/appsettings.json new file mode 100644 index 0000000..10f68b8 --- /dev/null +++ b/MatrixRoomUtils.DebugDataValidationApi/appsettings.json @@ -0,0 +1,9 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + }, + "AllowedHosts": "*" +} |