From 2952dd2c63023a8e17302955e6fd0a722471a575 Mon Sep 17 00:00:00 2001 From: Abdullah Atta Date: Fri, 3 Oct 2025 18:17:33 +0500 Subject: [PATCH] sync: fix monograph sync --- .../Controllers/MonographsController.cs | 2 +- Notesnook.API/Hubs/SyncV2Hub.cs | 20 +++++-- Notesnook.API/Models/Monograph.cs | 2 +- Notesnook.API/Models/MonographMetadata.cs | 52 +++++++++++++++++++ 4 files changed, 69 insertions(+), 7 deletions(-) create mode 100644 Notesnook.API/Models/MonographMetadata.cs diff --git a/Notesnook.API/Controllers/MonographsController.cs b/Notesnook.API/Controllers/MonographsController.cs index 81bd18d..d78bad1 100644 --- a/Notesnook.API/Controllers/MonographsController.cs +++ b/Notesnook.API/Controllers/MonographsController.cs @@ -294,7 +294,7 @@ namespace Notesnook.API.Controllers return Ok(); } - private async Task MarkMonographForSyncAsync(string monographId, string deviceId) + private async Task MarkMonographForSyncAsync(string monographId, string? deviceId) { if (deviceId == null) return; var userId = this.User.FindFirstValue("sub"); diff --git a/Notesnook.API/Hubs/SyncV2Hub.cs b/Notesnook.API/Hubs/SyncV2Hub.cs index ce84038..97d739e 100644 --- a/Notesnook.API/Hubs/SyncV2Hub.cs +++ b/Notesnook.API/Hubs/SyncV2Hub.cs @@ -22,6 +22,7 @@ using System.Collections.Frozen; using System.Collections.Generic; using System.Diagnostics; using System.Linq; +using System.Linq.Expressions; using System.Security.Claims; using System.Text.Json.Serialization; using System.Threading.Tasks; @@ -41,7 +42,7 @@ namespace Notesnook.API.Hubs { Task SendItems(SyncTransferItemV2 transferItem); Task SendVaultKey(EncryptedData vaultKey); - Task SendMonographs(IEnumerable monographs); + Task SendMonographs(IEnumerable monographs); Task PushCompleted(); } @@ -259,11 +260,20 @@ namespace Notesnook.API.Hubs var isSyncingMonographsForFirstTime = !device.HasInitialMonographsSync; var unsyncedMonographs = ids.Where((id) => id.EndsWith(":monograph")).ToHashSet(); var unsyncedMonographIds = unsyncedMonographs.Select((id) => id.Split(":")[0]).ToArray(); - var userMonographs = isResetSync || isSyncingMonographsForFirstTime - ? await Repositories.Monographs.FindAsync(m => m.UserId == userId) - : await Repositories.Monographs.FindAsync(m => m.UserId == userId && unsyncedMonographIds.Contains(m.ItemId)); + Expression> filter = isResetSync || isSyncingMonographsForFirstTime + ? (m => m.UserId == userId) + : (m => m.UserId == userId && unsyncedMonographIds.Contains(m.ItemId)); + var userMonographs = await Repositories.Monographs.Collection.Find(filter).Project((m) => new MonographMetadata + { + DatePublished = m.DatePublished, + Deleted = m.Deleted, + Password = m.Password, + SelfDestruct = m.SelfDestruct, + Title = m.Title, + ItemId = m.ItemId, + }).ToListAsync(); - if (userMonographs.Any() && !await Clients.Caller.SendMonographs(userMonographs).WaitAsync(TimeSpan.FromMinutes(10))) + if (userMonographs.Count > 0 && !await Clients.Caller.SendMonographs(userMonographs).WaitAsync(TimeSpan.FromMinutes(10))) throw new HubException("Client rejected monographs."); device.HasInitialMonographsSync = true; diff --git a/Notesnook.API/Models/Monograph.cs b/Notesnook.API/Models/Monograph.cs index bdc5e3f..bfe2d62 100644 --- a/Notesnook.API/Models/Monograph.cs +++ b/Notesnook.API/Models/Monograph.cs @@ -69,7 +69,7 @@ namespace Notesnook.API.Models public string Title { get; set; } [JsonPropertyName("userId")] - public string UserId { get; set; } + public string? UserId { get; set; } [JsonPropertyName("selfDestruct")] public bool SelfDestruct { get; set; } diff --git a/Notesnook.API/Models/MonographMetadata.cs b/Notesnook.API/Models/MonographMetadata.cs new file mode 100644 index 0000000..85e700a --- /dev/null +++ b/Notesnook.API/Models/MonographMetadata.cs @@ -0,0 +1,52 @@ +/* +This file is part of the Notesnook Sync Server project (https://notesnook.com/) + +Copyright (C) 2023 Streetwriters (Private) Limited + +This program is free software: you can redistribute it and/or modify +it under the terms of the Affero GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +Affero GNU General Public License for more details. + +You should have received a copy of the Affero GNU General Public License +along with this program. If not, see . +*/ + +using System.Text.Json.Serialization; +using MongoDB.Bson; +using MongoDB.Bson.Serialization.Attributes; +using System.Runtime.Serialization; + +namespace Notesnook.API.Models +{ + public class MonographMetadata + { + [DataMember(Name = "id")] + [JsonPropertyName("id")] + [MessagePack.Key("id")] + public required string ItemId + { + get; set; + } + + [JsonPropertyName("title")] + public required string Title { get; set; } + + [JsonPropertyName("selfDestruct")] + public bool SelfDestruct { get; set; } + + [JsonPropertyName("datePublished")] + public long DatePublished { get; set; } + + [JsonPropertyName("password")] + public EncryptedData? Password { get; set; } + + [JsonPropertyName("deleted")] + public bool Deleted { get; set; } + } +} \ No newline at end of file