-
Notifications
You must be signed in to change notification settings - Fork 53
monographs: add slug field which regenerates on update #72
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -18,27 +18,25 @@ You should have received a copy of the Affero GNU General Public License | |
| */ | ||
|
|
||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Linq; | ||
| using System.Security.Claims; | ||
| using System.Text.Json; | ||
| using System.Threading.Tasks; | ||
| using AngleSharp; | ||
| using AngleSharp.Dom; | ||
| using Microsoft.AspNetCore.Authorization; | ||
| using Microsoft.AspNetCore.Http; | ||
| using Microsoft.AspNetCore.Mvc; | ||
| using Microsoft.Extensions.Logging; | ||
| using MongoDB.Bson; | ||
| using MongoDB.Driver; | ||
| using Notesnook.API.Authorization; | ||
| using NanoidDotNet; | ||
| using Notesnook.API.Extensions; | ||
| using Notesnook.API.Models; | ||
| using Notesnook.API.Services; | ||
| using Streetwriters.Common; | ||
| using Streetwriters.Common.Helpers; | ||
| using Streetwriters.Common.Interfaces; | ||
| using Streetwriters.Common.Messages; | ||
| using Streetwriters.Data.Interfaces; | ||
| using Streetwriters.Data.Repositories; | ||
|
|
||
| namespace Notesnook.API.Controllers | ||
|
|
@@ -95,6 +93,22 @@ private async Task<Monograph> FindMonographAsync(string itemId) | |
| return await result.FirstOrDefaultAsync(); | ||
| } | ||
|
|
||
| private async Task<Monograph> FindMonographBySlugAsync(string slug) | ||
| { | ||
| var result = await monographs.Collection.FindAsync( | ||
| Builders<Monograph>.Filter.Eq("Slug", slug), | ||
| new FindOptions<Monograph> | ||
| { | ||
| Limit = 1 | ||
| }); | ||
| return await result.FirstOrDefaultAsync(); | ||
| } | ||
|
|
||
| private static string GenerateSlug() | ||
| { | ||
| return Nanoid.Generate(size: 24); | ||
| } | ||
|
|
||
| [HttpPost] | ||
| public async Task<IActionResult> PublishAsync([FromQuery] string? deviceId, [FromBody] Monograph monograph) | ||
| { | ||
|
|
@@ -120,6 +134,7 @@ public async Task<IActionResult> PublishAsync([FromQuery] string? deviceId, [Fro | |
| } | ||
| monograph.Deleted = false; | ||
| monograph.ViewCount = 0; | ||
| monograph.Slug = GenerateSlug(); | ||
| await monographs.Collection.ReplaceOneAsync( | ||
| CreateMonographFilter(userId, monograph), | ||
| monograph, | ||
|
|
@@ -131,7 +146,8 @@ await monographs.Collection.ReplaceOneAsync( | |
| return Ok(new | ||
| { | ||
| id = monograph.ItemId, | ||
| datePublished = monograph.DatePublished | ||
| datePublished = monograph.DatePublished, | ||
| publishUrl = monograph.ConstructPublishUrl() | ||
| }); | ||
| } | ||
| catch (Exception e) | ||
|
|
@@ -164,6 +180,7 @@ public async Task<IActionResult> UpdateAsync([FromQuery] string? deviceId, [From | |
| monograph.Content = null; | ||
|
|
||
| monograph.DatePublished = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(); | ||
| monograph.Slug = GenerateSlug(); | ||
| var result = await monographs.Collection.UpdateOneAsync( | ||
| CreateMonographFilter(userId, monograph), | ||
| Builders<Monograph>.Update | ||
|
|
@@ -172,6 +189,7 @@ public async Task<IActionResult> UpdateAsync([FromQuery] string? deviceId, [From | |
| .Set(m => m.EncryptedContent, monograph.EncryptedContent) | ||
| .Set(m => m.SelfDestruct, monograph.SelfDestruct) | ||
| .Set(m => m.Title, monograph.Title) | ||
| .Set(m => m.Slug, monograph.Slug) | ||
| .Set(m => m.Password, monograph.Password) | ||
| ); | ||
| if (!result.IsAcknowledged) return BadRequest(); | ||
|
|
@@ -181,7 +199,8 @@ public async Task<IActionResult> UpdateAsync([FromQuery] string? deviceId, [From | |
| return Ok(new | ||
| { | ||
| id = monograph.ItemId, | ||
| datePublished = monograph.DatePublished | ||
| datePublished = monograph.DatePublished, | ||
| publishUrl = monograph.ConstructPublishUrl() | ||
| }); | ||
| } | ||
| catch (Exception e) | ||
|
|
@@ -208,11 +227,25 @@ public async Task<IActionResult> GetUserMonographsAsync() | |
| return Ok(userMonographs.Select((m) => m.ItemId ?? m.Id)); | ||
| } | ||
|
|
||
| [HttpGet("{id}")] | ||
| [HttpGet("{slugOrId}")] | ||
| [AllowAnonymous] | ||
| public async Task<IActionResult> GetMonographAsync([FromRoute] string id) | ||
| public async Task<IActionResult> GetMonographAsync([FromRoute] string slugOrId) | ||
| { | ||
| var monograph = await FindMonographAsync(id); | ||
| var monograph = await FindMonographBySlugAsync(slugOrId); | ||
|
|
||
| if (monograph == null) | ||
| { | ||
| monograph = await FindMonographAsync(slugOrId); | ||
| if (!string.IsNullOrEmpty(monograph?.Slug)) | ||
| { | ||
| return NotFound(new | ||
| { | ||
| error = "invalid_id", | ||
| error_description = $"No such monograph found." | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| if (monograph == null || monograph.Deleted) | ||
| { | ||
| return NotFound(new | ||
|
|
@@ -317,6 +350,22 @@ await monographs.Collection.ReplaceOneAsync( | |
| return Ok(); | ||
| } | ||
|
|
||
| [HttpGet("{id}/publish-url")] | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. maybe we can combine analytics and publish-url endpoints into one? |
||
| public async Task<IActionResult> GetPublishUrlAsync([FromRoute] string id) | ||
| { | ||
| var userId = this.User.GetUserId(); | ||
| var monograph = await FindMonographAsync(id); | ||
| if (monograph == null || monograph.Deleted || monograph.UserId != userId) | ||
| { | ||
| return NotFound(); | ||
| } | ||
|
|
||
| return Ok(new | ||
| { | ||
| publishUrl = monograph.ConstructPublishUrl() | ||
| }); | ||
| } | ||
|
|
||
| private static async Task MarkMonographForSyncAsync(string userId, string monographId, string? deviceId, string? jti) | ||
| { | ||
| if (deviceId == null) return; | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| /* | ||
| 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 <http://www.gnu.org/licenses/>. | ||
| */ | ||
|
|
||
| using Notesnook.API.Models; | ||
| using Streetwriters.Common; | ||
|
|
||
| namespace Notesnook.API.Extensions | ||
| { | ||
| public static class MonographExtensions | ||
| { | ||
| public static string ConstructPublishUrl(this Monograph monograph) | ||
| { | ||
| var baseUrl = Constants.MONOGRAPH_PUBLIC_URL; | ||
| return $"{baseUrl}/{monograph.Slug ?? monograph.ItemId}"; | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -31,6 +31,7 @@ You should have received a copy of the Affero GNU General Public License | |
| using Microsoft.AspNetCore.SignalR; | ||
| using MongoDB.Driver; | ||
| using Notesnook.API.Authorization; | ||
| using Notesnook.API.Extensions; | ||
| using Notesnook.API.Interfaces; | ||
| using Notesnook.API.Models; | ||
| using Notesnook.API.Services; | ||
|
|
@@ -275,18 +276,20 @@ private async Task<SyncV2Metadata> HandleRequestFetch(string deviceId, bool incl | |
| Builders<Monograph>.Filter.In("_id", unsyncedMonographIds) | ||
| ) | ||
| ); | ||
| var userMonographs = await Repositories.Monographs.Collection.Find(filter).Project((m) => new MonographMetadata | ||
| var userMonographs = await Repositories.Monographs.Collection.Find(filter).ToListAsync(); | ||
| var userMonographMetadatas = userMonographs.Select((m) => new MonographMetadata | ||
| { | ||
| DatePublished = m.DatePublished, | ||
| Deleted = m.Deleted, | ||
| Password = m.Password, | ||
| SelfDestruct = m.SelfDestruct, | ||
| Title = m.Title, | ||
| PublishUrl = m.ConstructPublishUrl(), | ||
| ItemId = m.ItemId ?? m.Id.ToString(), | ||
| ViewCount = m.ViewCount | ||
| }).ToListAsync(); | ||
| }).ToList(); | ||
|
|
||
| if (userMonographs.Count > 0 && !await Clients.Caller.SendMonographs(userMonographs).WaitAsync(TimeSpan.FromMinutes(10))) | ||
| if (userMonographMetadatas.Count > 0 && !await Clients.Caller.SendMonographs(userMonographMetadatas).WaitAsync(TimeSpan.FromMinutes(10))) | ||
|
Comment on lines
-278
to
+292
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was unable to use |
||
| throw new HubException("Client rejected monographs."); | ||
|
|
||
| device.HasInitialMonographsSync = true; | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm still contemplating if we need to add this check. On one hand, I think it makes sense to restrict fetching monograph by id if the slug exists. On another hand, if a user's client app is a bit behind (i.e. doesn't have the slug property yet), they'll be shown the monograph link with the id which will be a 404 causing confusion