monograph: add support for webrisk api for analyzing urls for pro users

This commit is contained in:
Abdullah Atta
2025-09-15 11:22:37 +05:00
parent 0f43b3ee66
commit 97fbd3226d
6 changed files with 72 additions and 1 deletions

View File

@@ -33,6 +33,7 @@ using Notesnook.API.Authorization;
using Notesnook.API.Models;
using Notesnook.API.Services;
using Streetwriters.Common;
using Streetwriters.Common.Interfaces;
using Streetwriters.Common.Messages;
using Streetwriters.Data.Interfaces;
using Streetwriters.Data.Repositories;
@@ -46,12 +47,14 @@ namespace Notesnook.API.Controllers
{
const string SVG_PIXEL = "<svg xmlns='http://www.w3.org/2000/svg' width='1' height='1'><circle r='9'/></svg>";
private Repository<Monograph> Monographs { get; set; }
private readonly IURLAnalyzer urlAnalyzer;
private readonly IUnitOfWork unit;
private const int MAX_DOC_SIZE = 15 * 1024 * 1024;
public MonographsController(Repository<Monograph> monographs, IUnitOfWork unitOfWork)
public MonographsController(Repository<Monograph> monographs, IUnitOfWork unitOfWork, IURLAnalyzer analyzer)
{
Monographs = monographs;
unit = unitOfWork;
urlAnalyzer = analyzer;
}
private static FilterDefinition<Monograph> CreateMonographFilter(string userId, Monograph monograph)
@@ -338,6 +341,20 @@ namespace Notesnook.API.Controllers
}
return document.ToHtml();
}
if (ProUserRequirement.IsUserPro(User))
{
var config = Configuration.Default.WithDefaultLoader();
var context = BrowsingContext.New(config);
var document = await context.OpenAsync(r => r.Content(content));
foreach (var element in document.QuerySelectorAll("a"))
{
var href = element.GetAttribute("href");
if (string.IsNullOrEmpty(href)) continue;
if (!await urlAnalyzer.IsURLSafeAsync(href)) element.RemoveAttribute("href");
}
return document.ToHtml();
}
return content;
}
}

View File

@@ -57,8 +57,10 @@ using OpenTelemetry.Resources;
using Quartz;
using Streetwriters.Common;
using Streetwriters.Common.Extensions;
using Streetwriters.Common.Interfaces;
using Streetwriters.Common.Messages;
using Streetwriters.Common.Models;
using Streetwriters.Common.Services;
using Streetwriters.Data;
using Streetwriters.Data.DbContexts;
using Streetwriters.Data.Interfaces;
@@ -185,6 +187,7 @@ namespace Notesnook.API
services.AddScoped<ISyncItemsRepositoryAccessor, SyncItemsRepositoryAccessor>();
services.AddScoped<IUserService, UserService>();
services.AddScoped<IS3Service, S3Service>();
services.AddScoped<IURLAnalyzer, URLAnalyzer>();
services.AddControllers();