From 497f9ca0b1d0326c20b93b56326a4fe0d814070e Mon Sep 17 00:00:00 2001 From: sbwalker Date: Sat, 20 Jul 2024 19:17:47 -0400 Subject: [PATCH] improve validation of seach content --- .../Infrastructure/Jobs/SearchIndexJob.cs | 64 +++++++++---------- .../Admin/Files/Manager/FileManager.cs | 3 - .../HtmlText/Manager/HtmlTextManager.cs | 2 - Oqtane.Shared/Models/SearchContent.cs | 14 ++-- 4 files changed, 38 insertions(+), 45 deletions(-) diff --git a/Oqtane.Server/Infrastructure/Jobs/SearchIndexJob.cs b/Oqtane.Server/Infrastructure/Jobs/SearchIndexJob.cs index 8d8e987a..0374cd2d 100644 --- a/Oqtane.Server/Infrastructure/Jobs/SearchIndexJob.cs +++ b/Oqtane.Server/Infrastructure/Jobs/SearchIndexJob.cs @@ -132,7 +132,7 @@ namespace Oqtane.Infrastructure { if (!ignoreEntities.Contains(searchContent.EntityName)) { - SaveModuleMetaData(searchContent, pageModule, tenantId, removed); + ValidateSearchContent(searchContent, pageModule, tenantId, removed); searchContents.Add(searchContent); } } @@ -148,23 +148,8 @@ namespace Oqtane.Infrastructure if (!searchable && changed && !ignoreEntities.Contains(EntityNames.Module)) { // module does not implement ISearchable - var searchContent = new SearchContent - { - SiteId = page.SiteId, - EntityName = EntityNames.Module, - EntityId = pageModule.ModuleId.ToString(), - Title = pageModule.Title, - Description = string.Empty, - Body = string.Empty, - Url = $"{(!string.IsNullOrEmpty(page.Path) && !page.Path.StartsWith("/") ? "/" : "")}{page.Path}", - Permissions = $"{EntityNames.Module}:{pageModule.ModuleId},{EntityNames.Page}:{pageModule.PageId}", - ContentModifiedBy = pageModule.ModifiedBy, - ContentModifiedOn = pageModule.ModifiedOn, - AdditionalContent = string.Empty, - CreatedOn = DateTime.UtcNow, - IsDeleted = (removed || pageModule.IsDeleted || !Utilities.IsEffectiveOrExpired(pageModule.EffectiveDate, pageModule.ExpiryDate)), - TenantId = tenantId - }; + var searchContent = new SearchContent(); + ValidateSearchContent(searchContent, pageModule, tenantId, removed); searchContents.Add(searchContent); } } @@ -182,10 +167,12 @@ namespace Oqtane.Infrastructure } - private void SaveModuleMetaData(SearchContent searchContent, PageModule pageModule, int tenantId, bool removed) + private void ValidateSearchContent(SearchContent searchContent, PageModule pageModule, int tenantId, bool removed) { + // set default values searchContent.SiteId = pageModule.Module.SiteId; searchContent.TenantId = tenantId; + searchContent.CreatedOn = DateTime.UtcNow; if (string.IsNullOrEmpty(searchContent.EntityName)) { @@ -197,6 +184,31 @@ namespace Oqtane.Infrastructure searchContent.EntityId = pageModule.ModuleId.ToString(); } + if (string.IsNullOrEmpty(searchContent.Title)) + { + searchContent.Title = string.Empty; + if (!string.IsNullOrEmpty(pageModule.Title)) + { + searchContent.Title = pageModule.Title; + } + else if (pageModule.Page != null) + { + searchContent.Title = !string.IsNullOrEmpty(pageModule.Page.Title) ? pageModule.Page.Title : pageModule.Page.Name; + } + } + + if (searchContent.Description == null) { searchContent.Description = string.Empty;} + if (searchContent.Body == null) { searchContent.Body = string.Empty; } + + if (string.IsNullOrEmpty(searchContent.Url)) + { + searchContent.Url = string.Empty; + if (pageModule.Page != null) + { + searchContent.Url = $"{(!string.IsNullOrEmpty(pageModule.Page.Path) && !pageModule.Page.Path.StartsWith("/") ? "/" : "")}{pageModule.Page.Path}"; + } + } + if (string.IsNullOrEmpty(searchContent.Permissions)) { searchContent.Permissions = $"{EntityNames.Module}:{pageModule.ModuleId},{EntityNames.Page}:{pageModule.PageId}"; @@ -217,24 +229,10 @@ namespace Oqtane.Infrastructure searchContent.AdditionalContent = string.Empty; } - if (pageModule.Page != null) - { - if (string.IsNullOrEmpty(searchContent.Url)) - { - searchContent.Url = $"{(!string.IsNullOrEmpty(pageModule.Page.Path) && !pageModule.Page.Path.StartsWith("/") ? "/" : "")}{pageModule.Page.Path}"; - } - - if (string.IsNullOrEmpty(searchContent.Title)) - { - searchContent.Title = !string.IsNullOrEmpty(pageModule.Page.Title) ? pageModule.Page.Title : pageModule.Page.Name; - } - } - if (removed || pageModule.IsDeleted || !Utilities.IsEffectiveOrExpired(pageModule.EffectiveDate, pageModule.ExpiryDate)) { searchContent.IsDeleted = true; } - } private void SaveSearchLastIndexedOn(ISettingRepository settingRepository, int siteId, DateTime lastIndexedOn) diff --git a/Oqtane.Server/Modules/Admin/Files/Manager/FileManager.cs b/Oqtane.Server/Modules/Admin/Files/Manager/FileManager.cs index 03926ea8..06cc90e6 100644 --- a/Oqtane.Server/Modules/Admin/Files/Manager/FileManager.cs +++ b/Oqtane.Server/Modules/Admin/Files/Manager/FileManager.cs @@ -57,14 +57,11 @@ namespace Oqtane.Modules.Admin.Files.Manager EntityName = EntityNames.File, EntityId = file.FileId.ToString(), Title = path, - Description = string.Empty, Body = body, Url = $"{Constants.FileUrl}{folder.Path}{file.Name}", Permissions = $"{EntityNames.Folder}:{folder.FolderId}", ContentModifiedBy = file.ModifiedBy, ContentModifiedOn = file.ModifiedOn, - AdditionalContent = string.Empty, - CreatedOn = DateTime.UtcNow, IsDeleted = (removed || file.IsDeleted.Value) }; searchContents.Add(searchContent); diff --git a/Oqtane.Server/Modules/HtmlText/Manager/HtmlTextManager.cs b/Oqtane.Server/Modules/HtmlText/Manager/HtmlTextManager.cs index 744fb7b5..fac72001 100644 --- a/Oqtane.Server/Modules/HtmlText/Manager/HtmlTextManager.cs +++ b/Oqtane.Server/Modules/HtmlText/Manager/HtmlTextManager.cs @@ -61,8 +61,6 @@ namespace Oqtane.Modules.HtmlText.Manager { searchContents.Add(new SearchContent { - Title = pageModule.Module.Title, - Description = string.Empty, Body = htmltext.Content, ContentModifiedBy = htmltext.CreatedBy, ContentModifiedOn = htmltext.CreatedOn diff --git a/Oqtane.Shared/Models/SearchContent.cs b/Oqtane.Shared/Models/SearchContent.cs index 03c97712..a5e3a295 100644 --- a/Oqtane.Shared/Models/SearchContent.cs +++ b/Oqtane.Shared/Models/SearchContent.cs @@ -2,6 +2,7 @@ using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations.Schema; using System.Text.Json; + namespace Oqtane.Models { public class SearchContent @@ -30,25 +31,25 @@ namespace Oqtane.Models public string AdditionalContent { get; set; } + [NotMapped] + public bool IsDeleted { get; set; } + public DateTime CreatedOn { get; set; } public List SearchContentProperties { get; set; } public List SearchContentWords { get; set; } - [NotMapped] - public string UniqueKey => $"{TenantId}:{SiteId}:{EntityName}:{EntityId}"; - [NotMapped] public int TenantId { get; set; } [NotMapped] - public bool IsDeleted { get; set; } + public string UniqueKey => $"{TenantId}:{SiteId}:{EntityName}:{EntityId}"; // constructors public SearchContent() { } - public SearchContent(int siteId, string entityName, string entityId, string title, string description, string body, string url, string permissions, string contentModifiedBy, DateTime contentModifiedOn) + public SearchContent(int siteId, string entityName, string entityId, string title, string description, string body, string url, string permissions, string contentModifiedBy, DateTime contentModifiedOn, bool isDeleted) { SiteId = siteId; EntityName = entityName; @@ -60,8 +61,7 @@ namespace Oqtane.Models Permissions = permissions; ContentModifiedBy = contentModifiedBy; ContentModifiedOn = contentModifiedOn; - AdditionalContent = ""; - CreatedOn = DateTime.UtcNow; + IsDeleted = isDeleted; } public override string ToString()