From 13ab0ba0395f46ef6f38c522b69a350c41d14b66 Mon Sep 17 00:00:00 2001 From: Abdullah Atta Date: Wed, 5 Aug 2026 13:42:33 +0500 Subject: [PATCH] sync: update date synced on item push --- Notesnook.API/Hubs/SyncV2Hub.cs | 6 +- .../Repositories/SyncItemsRepository.cs | 56 +------------------ 2 files changed, 5 insertions(+), 57 deletions(-) diff --git a/Notesnook.API/Hubs/SyncV2Hub.cs b/Notesnook.API/Hubs/SyncV2Hub.cs index 33b6841..6f94e34 100644 --- a/Notesnook.API/Hubs/SyncV2Hub.cs +++ b/Notesnook.API/Hubs/SyncV2Hub.cs @@ -57,7 +57,7 @@ namespace Notesnook.API.Hubs private ISyncItemsRepositoryAccessor Repositories { get; } private SyncDeviceService SyncDeviceService { get; } private readonly IUnitOfWork unit; - private readonly FrozenDictionary, string, long>> UpsertActionsMap; + private readonly FrozenDictionary, string>> UpsertActionsMap; private readonly CollectionDef[] BaseCollectionDefs; private readonly CollectionDef[] V4CollectionDefs; ILogger Logger { get; } @@ -96,7 +96,7 @@ namespace Notesnook.API.Hubs new("inboxitemhistory", Repositories.InboxItemsHistory.FindItemsById), new("relation", Repositories.Relations.FindItemsById), // relations must sync at the end to prevent invalid state ]; - UpsertActionsMap = new Dictionary, string, long>> { + UpsertActionsMap = new Dictionary, string>> { { "settingitem", Repositories.Settings.UpsertMany }, { "attachment", Repositories.Attachments.UpsertMany }, { "note", Repositories.Notes.UpsertMany }, @@ -149,7 +149,7 @@ namespace Notesnook.API.Hubs try { var UpsertItems = UpsertActionsMap[pushItem.Type] ?? throw new Exception($"Invalid item type: {pushItem.Type}."); - UpsertItems(pushItem.Items, userId, 1); + UpsertItems(pushItem.Items, userId); if (!await unit.Commit()) return 0; diff --git a/Notesnook.API/Repositories/SyncItemsRepository.cs b/Notesnook.API/Repositories/SyncItemsRepository.cs index ed62fc6..d26d40d 100644 --- a/Notesnook.API/Repositories/SyncItemsRepository.cs +++ b/Notesnook.API/Repositories/SyncItemsRepository.cs @@ -55,24 +55,6 @@ namespace Notesnook.API.Repositories return ALGORITHMS.Contains(algorithm); } - public Task CountItemsSyncedAfterAsync(string userId, long timestamp) - { - var filter = Builders.Filter.And(Builders.Filter.Gt("DateSynced", timestamp), Builders.Filter.Eq("UserId", userId)); - return Collection.CountDocumentsAsync(filter); - } - public Task> FindItemsSyncedAfter(string userId, long timestamp, int batchSize) - { - var filter = Builders.Filter.And(Builders.Filter.Gt("DateSynced", timestamp), Builders.Filter.Eq("UserId", userId)); - return Collection.FindAsync(filter, new FindOptions - { - BatchSize = batchSize, - AllowDiskUse = true, - AllowPartialResults = false, - NoCursorTimeout = true, - Sort = new SortDefinitionBuilder().Ascending("_id") - }); - } - public Task> FindItemsById(string userId, IEnumerable ids, bool all, int batchSize) { var filters = new List>(new[] { Builders.Filter.Eq("UserId", userId) }); @@ -94,41 +76,7 @@ namespace Notesnook.API.Repositories dbContext.AddCommand((handle, ct) => Collection.DeleteManyAsync(handle, filter, null, ct)); } - public void Upsert(SyncItem item, string userId, long dateSynced) - { - if (item.Length > 15 * 1024 * 1024) - { - throw new Exception($"Size of item \"{item.ItemId}\" is too large. Maximum allowed size is 15 MB."); - } - - if (!IsValidAlgorithm(item.Algorithm)) - { - throw new Exception($"Invalid alg identifier {item.Algorithm}"); - } - - // Handle case where the cipher is corrupted. - if (!IsBase64String(item.Cipher)) - { - logger.LogError("Corrupted item {ItemId} in collection {CollectionName}. Length: {Length}, Cipher: {Cipher}", - item.ItemId, this.collectionName, item.Length, item.Cipher); - throw new Exception($"Corrupted item \"{item.ItemId}\" in collection \"{this.collectionName}\". Please report this error to support@streetwriters.co."); - } - - if (item.ItemId == null) - throw new Exception($"Item does not have an ItemId."); - - item.DateSynced = dateSynced; - item.UserId = userId; - - var filter = Builders.Filter.And( - Builders.Filter.Eq("UserId", userId), - Builders.Filter.Eq("ItemId", item.ItemId) - ); - - dbContext.AddCommand((handle, ct) => Collection.ReplaceOneAsync(handle, filter, item, new ReplaceOptions { IsUpsert = true }, ct)); - } - - public void UpsertMany(IEnumerable items, string userId, long dateSynced) + public void UpsertMany(IEnumerable items, string userId) { var userIdFilter = Builders.Filter.Eq("UserId", userId); var writes = new List>(); @@ -160,7 +108,7 @@ namespace Notesnook.API.Repositories Builders.Filter.Eq("ItemId", item.ItemId) ); - item.DateSynced = dateSynced; + item.DateSynced = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(); item.UserId = userId; writes.Add(new ReplaceOneModel(filter, item)