Skip to content

Commit 32b24de

Browse files
authored
inbox: sync inbox items
1 parent 4779a21 commit 32b24de

File tree

6 files changed

+48
-17
lines changed

6 files changed

+48
-17
lines changed

Notesnook.API/Accessors/SyncItemsRepositoryAccessor.cs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public class SyncItemsRepositoryAccessor : ISyncItemsRepositoryAccessor
4444
public Repository<UserSettings> UsersSettings { get; }
4545
public Repository<Monograph> Monographs { get; }
4646
public Repository<InboxApiKey> InboxApiKey { get; }
47-
public SyncItemsRepository InboxItems { get; }
47+
public Repository<InboxSyncItem> InboxItems { get; }
4848

4949
public SyncItemsRepositoryAccessor(IDbContext dbContext,
5050

@@ -72,15 +72,14 @@ public SyncItemsRepositoryAccessor(IDbContext dbContext,
7272
IMongoCollection<SyncItem> vaults,
7373
[FromKeyedServices(Collections.TagsKey)]
7474
IMongoCollection<SyncItem> tags,
75-
[FromKeyedServices(Collections.InboxItems)]
76-
IMongoCollection<SyncItem> inboxItems,
7775

7876
Repository<UserSettings> usersSettings, Repository<Monograph> monographs,
79-
Repository<InboxApiKey> inboxApiKey)
77+
Repository<InboxApiKey> inboxApiKey, Repository<InboxSyncItem> inboxItems)
8078
{
8179
UsersSettings = usersSettings;
8280
Monographs = monographs;
8381
InboxApiKey = inboxApiKey;
82+
InboxItems = inboxItems;
8483
Notebooks = new SyncItemsRepository(dbContext, notebooks);
8584
Notes = new SyncItemsRepository(dbContext, notes);
8685
Contents = new SyncItemsRepository(dbContext, content);
@@ -93,7 +92,6 @@ public SyncItemsRepositoryAccessor(IDbContext dbContext,
9392
Colors = new SyncItemsRepository(dbContext, colors);
9493
Vaults = new SyncItemsRepository(dbContext, vaults);
9594
Tags = new SyncItemsRepository(dbContext, tags);
96-
InboxItems = new SyncItemsRepository(dbContext, inboxItems);
9795
}
9896
}
9997
}

Notesnook.API/Controllers/InboxController.cs

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,16 @@ You should have received a copy of the Affero GNU General Public License
1919

2020
using System;
2121
using System.Security.Claims;
22+
using System.Text.Json;
2223
using System.Threading.Tasks;
2324
using Microsoft.AspNetCore.Authorization;
2425
using Microsoft.AspNetCore.Mvc;
2526
using MongoDB.Bson;
2627
using Notesnook.API.Authorization;
27-
using Notesnook.API.Interfaces;
2828
using Notesnook.API.Models;
29-
using Notesnook.API.Repositories;
29+
using Notesnook.API.Services;
3030
using Streetwriters.Common;
31+
using Streetwriters.Common.Messages;
3132
using Streetwriters.Data.Repositories;
3233

3334
namespace Notesnook.API.Controllers
@@ -38,16 +39,16 @@ public class InboxController : ControllerBase
3839
{
3940
private readonly Repository<InboxApiKey> InboxApiKey;
4041
private readonly Repository<UserSettings> UserSetting;
41-
private SyncItemsRepository InboxItems;
42+
private Repository<InboxSyncItem> InboxItems;
4243

4344
public InboxController(
4445
Repository<InboxApiKey> inboxApiKeysRepository,
4546
Repository<UserSettings> userSettingsRepository,
46-
ISyncItemsRepositoryAccessor syncItemsRepositoryAccessor)
47+
Repository<InboxSyncItem> inboxItemsRepository)
4748
{
4849
InboxApiKey = inboxApiKeysRepository;
4950
UserSetting = userSettingsRepository;
50-
InboxItems = syncItemsRepositoryAccessor.InboxItems;
51+
InboxItems = inboxItemsRepository;
5152
}
5253

5354
[HttpGet("api-keys")]
@@ -189,6 +190,18 @@ public async Task<IActionResult> CreateInboxItemAsync([FromBody] InboxSyncItem r
189190
request.UserId = userId;
190191
request.ItemId = ObjectId.GenerateNewId().ToString();
191192
await InboxItems.InsertAsync(request);
193+
new SyncDeviceService(new SyncDevice(userId, string.Empty))
194+
.AddIdsToAllDevices([$"{request.ItemId}:inboxItems"]);
195+
await WampServers.MessengerServer.PublishMessageAsync(MessengerServerTopics.SendSSETopic, new SendSSEMessage
196+
{
197+
OriginTokenId = null,
198+
UserId = userId,
199+
Message = new Message
200+
{
201+
Type = "triggerSync",
202+
Data = JsonSerializer.Serialize(new { reason = "Inbox items updated." })
203+
}
204+
});
192205
return Ok();
193206
}
194207
catch (Exception ex)

Notesnook.API/Hubs/SyncV2Hub.cs

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ public interface ISyncV2HubClient
4343
Task<bool> SendItems(SyncTransferItemV2 transferItem);
4444
Task<bool> SendVaultKey(EncryptedData vaultKey);
4545
Task<bool> SendMonographs(IEnumerable<MonographMetadata> monographs);
46+
Task<bool> SendInboxItems(IEnumerable<InboxSyncItem> inboxItems);
4647
Task PushCompleted();
4748
}
4849

@@ -197,15 +198,20 @@ private async IAsyncEnumerable<SyncTransferItemV2> PrepareChunks(string userId,
197198

198199
public async Task<SyncV2Metadata> RequestFetch(string deviceId)
199200
{
200-
return await HandleRequestFetch(deviceId, false);
201+
return await HandleRequestFetch(deviceId, false, false);
201202
}
202203

203204
public async Task<SyncV2Metadata> RequestFetchV2(string deviceId)
204205
{
205-
return await HandleRequestFetch(deviceId, true);
206+
return await HandleRequestFetch(deviceId, true, false);
206207
}
207208

208-
private async Task<SyncV2Metadata> HandleRequestFetch(string deviceId, bool includeMonographs)
209+
public async Task<SyncV2Metadata> RequestFetchV3(string deviceId)
210+
{
211+
return await HandleRequestFetch(deviceId, true, true);
212+
}
213+
214+
private async Task<SyncV2Metadata> HandleRequestFetch(string deviceId, bool includeMonographs, bool includeInboxItems)
209215
{
210216
var userId = Context.User?.FindFirstValue("sub") ?? throw new HubException("Please login to sync.");
211217

@@ -285,6 +291,19 @@ private async Task<SyncV2Metadata> HandleRequestFetch(string deviceId, bool incl
285291
device.HasInitialMonographsSync = true;
286292
}
287293

294+
if (includeInboxItems)
295+
{
296+
var unsyncedInboxItems = ids.Where((id) => id.EndsWith(":inboxItems")).ToHashSet();
297+
var unsyncedInboxItemIds = unsyncedInboxItems.Select((id) => id.Split(":")[0]).ToArray();
298+
var userInboxItems = isResetSync
299+
? await Repositories.InboxItems.FindAsync(m => m.UserId == userId)
300+
: await Repositories.InboxItems.FindAsync(m => m.UserId == userId && unsyncedInboxItemIds.Contains(m.ItemId));
301+
if (userInboxItems.Any() && !await Clients.Caller.SendInboxItems(userInboxItems).WaitAsync(TimeSpan.FromMinutes(10)))
302+
{
303+
throw new HubException("Client rejected inbox items.");
304+
}
305+
}
306+
288307
deviceService.Reset();
289308

290309
return new SyncV2Metadata

Notesnook.API/Interfaces/ISyncItemsRepositoryAccessor.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,9 @@ public interface ISyncItemsRepositoryAccessor
3838
SyncItemsRepository Colors { get; }
3939
SyncItemsRepository Vaults { get; }
4040
SyncItemsRepository Tags { get; }
41-
SyncItemsRepository InboxItems { get; }
4241
Repository<UserSettings> UsersSettings { get; }
4342
Repository<Monograph> Monographs { get; }
4443
Repository<InboxApiKey> InboxApiKey { get; }
44+
Repository<InboxSyncItem> InboxItems { get; }
4545
}
4646
}

Notesnook.API/Models/SyncItem.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public long DateSynced
4444
[DataMember(Name = "userId")]
4545
[JsonPropertyName("userId")]
4646
[MessagePack.Key("userId")]
47-
public string UserId
47+
public string? UserId
4848
{
4949
get; set;
5050
}
@@ -71,7 +71,7 @@ public string Cipher
7171
[DataMember(Name = "id")]
7272
[JsonPropertyName("id")]
7373
[MessagePack.Key("id")]
74-
public string ItemId
74+
public string? ItemId
7575
{
7676
get; set;
7777
}

Notesnook.API/Startup.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,8 @@ public void ConfigureServices(IServiceCollection services)
173173
services.AddRepository<UserSettings>("user_settings", "notesnook")
174174
.AddRepository<Monograph>("monographs", "notesnook")
175175
.AddRepository<Announcement>("announcements", "notesnook")
176-
.AddRepository<InboxApiKey>(Collections.InboxApiKeysKey, "notesnook");
176+
.AddRepository<InboxApiKey>(Collections.InboxApiKeysKey, "notesnook")
177+
.AddRepository<InboxSyncItem>(Collections.InboxItems, "notesnook");
177178

178179
services.AddMongoCollection(Collections.SettingsKey)
179180
.AddMongoCollection(Collections.AttachmentsKey)

0 commit comments

Comments
 (0)