From fb8284b5e848d705b24a2e2a42d3d1c9d24728d2 Mon Sep 17 00:00:00 2001 From: Mario Steele Date: Thu, 24 Jul 2025 04:30:54 -0500 Subject: [PATCH] Updated Syncer Updated Syncer class to properly sync all classes, even with special requierments needed for Settings. Now properly handles all classes, and ensures that the data is properly stored in the database, and can be written out correctly to the FreeTube database files. --- FreeTubeSyncer/REST/Syncer.cs | 52 ++++++++++++++++++++++++++--------- 1 file changed, 39 insertions(+), 13 deletions(-) diff --git a/FreeTubeSyncer/REST/Syncer.cs b/FreeTubeSyncer/REST/Syncer.cs index b0fc24d..13758f9 100644 --- a/FreeTubeSyncer/REST/Syncer.cs +++ b/FreeTubeSyncer/REST/Syncer.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.IO; using System.Linq; using System.Text.Json; +using System.Text.Json.Nodes; using System.Threading.Tasks; using FreeTubeSyncer.Library; using FreeTubeSyncer.Models.DatabaseModels; @@ -10,11 +11,12 @@ using RestSharp; namespace FreeTubeSyncer.REST; -public class Syncer where T : class, IDataModel +public class Syncer where T : class, IDataModel, new() { private List _entries = new List(); private RestClient _client; private string _dbPath; + private string _dbName; private string _restEndpoint; public bool IsDirty = false; @@ -25,6 +27,7 @@ public class Syncer where T : class, IDataModel watcher.OnDatabaseChange += HandleDatabaseChange; _client = new RestClient(new RestClientOptions("http://localhost:5183")); _dbPath = dbPath; + _dbName = dbName; _restEndpoint = restEndpoint; ReadDatabase().Wait(); FetchDatabase().Wait(); @@ -36,20 +39,24 @@ public class Syncer where T : class, IDataModel foreach (var entry in lines) { if (entry == "") continue; + T? item; try { - var item = JsonSerializer.Deserialize(entry, GlobalJsonOptions.Options); - if (item == null) continue; - if (_entries.Any(x => x.EqualId(item.Id()))) - _entries.RemoveAll(x => x.EqualId(item.Id())); - _entries.Add(item); - Console.WriteLine($"Posting {item.Id()}"); - await _client.PostJsonAsync(_restEndpoint, item); + item = JsonSerializer.Deserialize(entry, GlobalJsonOptions.Options); } catch (Exception ex) { - Console.WriteLine($"Failed to parse: {entry}"); + var jobj = JsonSerializer.Deserialize(entry, GlobalJsonOptions.Options); + item = new T(); + item.MarshalData(jobj["_id"].GetValue(), entry); } + if (item == null) continue; + item.MarshalData(item.Id(), entry); + if (_entries.Any(x => x.EqualId(item.Id()))) + _entries.RemoveAll(x => x.EqualId(item.Id())); + _entries.Add(item); + Console.WriteLine($"Posting {item.Id()}"); + await _client.PostJsonAsync(_restEndpoint, item); } } @@ -66,9 +73,25 @@ public class Syncer where T : class, IDataModel } } - private async void HandleDatabaseChange(string dbName, object entryObject) + private async void HandleDatabaseChange(string dbName, string entryObject) { - var entry = (T)entryObject; + if (dbName != _dbName) + return; + + T? entry; + try + { + entry = JsonSerializer.Deserialize(entryObject, GlobalJsonOptions.Options); + } + catch (Exception ex) + { + var jobj = JsonSerializer.Deserialize(entryObject, GlobalJsonOptions.Options); + entry = new T(); + entry.MarshalData(jobj["_id"].GetValue(), entryObject); + } + + if (entry == null) return; + entry.MarshalData(entry.Id(), entryObject); if (_entries.Any(x => x.EqualId(entry.Id()))) _entries.RemoveAll(x => x.EqualId(entry.Id())); _entries.Add(entry); @@ -78,11 +101,14 @@ public class Syncer where T : class, IDataModel public void Sync() { + if (!IsDirty) + return; + Console.WriteLine($"Syncing {_dbPath}..."); var json = new List(); foreach (var entry in _entries) - json.Add(JsonSerializer.Serialize(entry)); + json.Add(entry.JsonData()); File.WriteAllLines(_dbPath, json); - Console.WriteLine($"Updated {_dbPath}"); + Console.WriteLine($"Updated {_dbPath}."); IsDirty = false; } } \ No newline at end of file