From 32b24dead2f6d227b181230a664dc047158d3ea9 Mon Sep 17 00:00:00 2001 From: 01zulfi <85733202+01zulfi@users.noreply.github.com> Date: Wed, 8 Oct 2025 12:49:41 +0500 Subject: [PATCH] inbox: sync inbox items --- .../Accessors/SyncItemsRepositoryAccessor.cs | 8 +++--- Notesnook.API/Controllers/InboxController.cs | 23 +++++++++++++---- Notesnook.API/Hubs/SyncV2Hub.cs | 25 ++++++++++++++++--- .../ISyncItemsRepositoryAccessor.cs | 2 +- Notesnook.API/Models/SyncItem.cs | 4 +-- Notesnook.API/Startup.cs | 3 ++- 6 files changed, 48 insertions(+), 17 deletions(-) diff --git a/Notesnook.API/Accessors/SyncItemsRepositoryAccessor.cs b/Notesnook.API/Accessors/SyncItemsRepositoryAccessor.cs index 294b00b..fbc264d 100644 --- a/Notesnook.API/Accessors/SyncItemsRepositoryAccessor.cs +++ b/Notesnook.API/Accessors/SyncItemsRepositoryAccessor.cs @@ -44,7 +44,7 @@ namespace Notesnook.API.Accessors public Repository UsersSettings { get; } public Repository Monographs { get; } public Repository InboxApiKey { get; } - public SyncItemsRepository InboxItems { get; } + public Repository InboxItems { get; } public SyncItemsRepositoryAccessor(IDbContext dbContext, @@ -72,15 +72,14 @@ namespace Notesnook.API.Accessors IMongoCollection vaults, [FromKeyedServices(Collections.TagsKey)] IMongoCollection tags, - [FromKeyedServices(Collections.InboxItems)] - IMongoCollection inboxItems, Repository usersSettings, Repository monographs, - Repository inboxApiKey) + Repository inboxApiKey, Repository inboxItems) { UsersSettings = usersSettings; Monographs = monographs; InboxApiKey = inboxApiKey; + InboxItems = inboxItems; Notebooks = new SyncItemsRepository(dbContext, notebooks); Notes = new SyncItemsRepository(dbContext, notes); Contents = new SyncItemsRepository(dbContext, content); @@ -93,7 +92,6 @@ namespace Notesnook.API.Accessors Colors = new SyncItemsRepository(dbContext, colors); Vaults = new SyncItemsRepository(dbContext, vaults); Tags = new SyncItemsRepository(dbContext, tags); - InboxItems = new SyncItemsRepository(dbContext, inboxItems); } } } \ No newline at end of file diff --git a/Notesnook.API/Controllers/InboxController.cs b/Notesnook.API/Controllers/InboxController.cs index d3595a0..cdb277e 100644 --- a/Notesnook.API/Controllers/InboxController.cs +++ b/Notesnook.API/Controllers/InboxController.cs @@ -19,15 +19,16 @@ along with this program. If not, see . using System; using System.Security.Claims; +using System.Text.Json; using System.Threading.Tasks; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using MongoDB.Bson; using Notesnook.API.Authorization; -using Notesnook.API.Interfaces; using Notesnook.API.Models; -using Notesnook.API.Repositories; +using Notesnook.API.Services; using Streetwriters.Common; +using Streetwriters.Common.Messages; using Streetwriters.Data.Repositories; namespace Notesnook.API.Controllers @@ -38,16 +39,16 @@ namespace Notesnook.API.Controllers { private readonly Repository InboxApiKey; private readonly Repository UserSetting; - private SyncItemsRepository InboxItems; + private Repository InboxItems; public InboxController( Repository inboxApiKeysRepository, Repository userSettingsRepository, - ISyncItemsRepositoryAccessor syncItemsRepositoryAccessor) + Repository inboxItemsRepository) { InboxApiKey = inboxApiKeysRepository; UserSetting = userSettingsRepository; - InboxItems = syncItemsRepositoryAccessor.InboxItems; + InboxItems = inboxItemsRepository; } [HttpGet("api-keys")] @@ -189,6 +190,18 @@ namespace Notesnook.API.Controllers request.UserId = userId; request.ItemId = ObjectId.GenerateNewId().ToString(); await InboxItems.InsertAsync(request); + new SyncDeviceService(new SyncDevice(userId, string.Empty)) + .AddIdsToAllDevices([$"{request.ItemId}:inboxItems"]); + await WampServers.MessengerServer.PublishMessageAsync(MessengerServerTopics.SendSSETopic, new SendSSEMessage + { + OriginTokenId = null, + UserId = userId, + Message = new Message + { + Type = "triggerSync", + Data = JsonSerializer.Serialize(new { reason = "Inbox items updated." }) + } + }); return Ok(); } catch (Exception ex) diff --git a/Notesnook.API/Hubs/SyncV2Hub.cs b/Notesnook.API/Hubs/SyncV2Hub.cs index 42ac5c4..1ef7e8d 100644 --- a/Notesnook.API/Hubs/SyncV2Hub.cs +++ b/Notesnook.API/Hubs/SyncV2Hub.cs @@ -43,6 +43,7 @@ namespace Notesnook.API.Hubs Task SendItems(SyncTransferItemV2 transferItem); Task SendVaultKey(EncryptedData vaultKey); Task SendMonographs(IEnumerable monographs); + Task SendInboxItems(IEnumerable inboxItems); Task PushCompleted(); } @@ -197,15 +198,20 @@ namespace Notesnook.API.Hubs public async Task RequestFetch(string deviceId) { - return await HandleRequestFetch(deviceId, false); + return await HandleRequestFetch(deviceId, false, false); } public async Task RequestFetchV2(string deviceId) { - return await HandleRequestFetch(deviceId, true); + return await HandleRequestFetch(deviceId, true, false); } - private async Task HandleRequestFetch(string deviceId, bool includeMonographs) + public async Task RequestFetchV3(string deviceId) + { + return await HandleRequestFetch(deviceId, true, true); + } + + private async Task HandleRequestFetch(string deviceId, bool includeMonographs, bool includeInboxItems) { var userId = Context.User?.FindFirstValue("sub") ?? throw new HubException("Please login to sync."); @@ -285,6 +291,19 @@ namespace Notesnook.API.Hubs device.HasInitialMonographsSync = true; } + if (includeInboxItems) + { + var unsyncedInboxItems = ids.Where((id) => id.EndsWith(":inboxItems")).ToHashSet(); + var unsyncedInboxItemIds = unsyncedInboxItems.Select((id) => id.Split(":")[0]).ToArray(); + var userInboxItems = isResetSync + ? await Repositories.InboxItems.FindAsync(m => m.UserId == userId) + : await Repositories.InboxItems.FindAsync(m => m.UserId == userId && unsyncedInboxItemIds.Contains(m.ItemId)); + if (userInboxItems.Any() && !await Clients.Caller.SendInboxItems(userInboxItems).WaitAsync(TimeSpan.FromMinutes(10))) + { + throw new HubException("Client rejected inbox items."); + } + } + deviceService.Reset(); return new SyncV2Metadata diff --git a/Notesnook.API/Interfaces/ISyncItemsRepositoryAccessor.cs b/Notesnook.API/Interfaces/ISyncItemsRepositoryAccessor.cs index 507f2c1..295c353 100644 --- a/Notesnook.API/Interfaces/ISyncItemsRepositoryAccessor.cs +++ b/Notesnook.API/Interfaces/ISyncItemsRepositoryAccessor.cs @@ -38,9 +38,9 @@ namespace Notesnook.API.Interfaces SyncItemsRepository Colors { get; } SyncItemsRepository Vaults { get; } SyncItemsRepository Tags { get; } - SyncItemsRepository InboxItems { get; } Repository UsersSettings { get; } Repository Monographs { get; } Repository InboxApiKey { get; } + Repository InboxItems { get; } } } \ No newline at end of file diff --git a/Notesnook.API/Models/SyncItem.cs b/Notesnook.API/Models/SyncItem.cs index 7d04537..b4fdaef 100644 --- a/Notesnook.API/Models/SyncItem.cs +++ b/Notesnook.API/Models/SyncItem.cs @@ -44,7 +44,7 @@ namespace Notesnook.API.Models [DataMember(Name = "userId")] [JsonPropertyName("userId")] [MessagePack.Key("userId")] - public string UserId + public string? UserId { get; set; } @@ -71,7 +71,7 @@ namespace Notesnook.API.Models [DataMember(Name = "id")] [JsonPropertyName("id")] [MessagePack.Key("id")] - public string ItemId + public string? ItemId { get; set; } diff --git a/Notesnook.API/Startup.cs b/Notesnook.API/Startup.cs index 4ad8b4e..55b843e 100644 --- a/Notesnook.API/Startup.cs +++ b/Notesnook.API/Startup.cs @@ -173,7 +173,8 @@ namespace Notesnook.API services.AddRepository("user_settings", "notesnook") .AddRepository("monographs", "notesnook") .AddRepository("announcements", "notesnook") - .AddRepository(Collections.InboxApiKeysKey, "notesnook"); + .AddRepository(Collections.InboxApiKeysKey, "notesnook") + .AddRepository(Collections.InboxItems, "notesnook"); services.AddMongoCollection(Collections.SettingsKey) .AddMongoCollection(Collections.AttachmentsKey)