Fix Genre/Tag with spaces getting normalized (#3731)
This commit is contained in:
parent
55966f0b2a
commit
33221fee2b
10 changed files with 104 additions and 51 deletions
|
|
@ -1,5 +1,6 @@
|
|||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using API.Constants;
|
||||
using API.Data;
|
||||
using API.Data.ManualMigrations;
|
||||
using API.DTOs;
|
||||
|
|
@ -16,12 +17,10 @@ namespace API.Controllers;
|
|||
public class AdminController : BaseApiController
|
||||
{
|
||||
private readonly UserManager<AppUser> _userManager;
|
||||
private readonly IUnitOfWork _unitOfWork;
|
||||
|
||||
public AdminController(UserManager<AppUser> userManager, IUnitOfWork unitOfWork)
|
||||
public AdminController(UserManager<AppUser> userManager)
|
||||
{
|
||||
_userManager = userManager;
|
||||
_unitOfWork = unitOfWork;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
|
@ -32,18 +31,7 @@ public class AdminController : BaseApiController
|
|||
[HttpGet("exists")]
|
||||
public async Task<ActionResult<bool>> AdminExists()
|
||||
{
|
||||
var users = await _userManager.GetUsersInRoleAsync("Admin");
|
||||
var users = await _userManager.GetUsersInRoleAsync(PolicyConstants.AdminRole);
|
||||
return users.Count > 0;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Set the progress information for a particular user
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
[Authorize("RequireAdminRole")]
|
||||
[HttpPost("update-chapter-progress")]
|
||||
public async Task<ActionResult<bool>> UpdateChapterProgress(UpdateUserProgressDto dto)
|
||||
{
|
||||
return Ok(await Task.FromResult(false));
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,11 +0,0 @@
|
|||
using System;
|
||||
|
||||
namespace API.DTOs.Progress;
|
||||
#nullable enable
|
||||
|
||||
public class UpdateUserProgressDto
|
||||
{
|
||||
public int PageNum { get; set; }
|
||||
public DateTime LastModifiedUtc { get; set; }
|
||||
public DateTime CreatedUtc { get; set; }
|
||||
}
|
||||
20
API/EmailTemplates/KavitaPlusDebug.html
Normal file
20
API/EmailTemplates/KavitaPlusDebug.html
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
<tr>
|
||||
<td valign="top" style="text-align: center; padding: 20px 0 10px 20px;">
|
||||
<h1 style="margin: 0; font-family: 'Montserrat', sans-serif; font-size: 30px; line-height: 36px; font-weight: bold;">A User needs manual registration</h1> </td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td valign="top" align="center" style="text-align: center; padding: 15px 0px 20px 0px;">
|
||||
<center>
|
||||
<table role="presentation" align="center" cellspacing="0" cellpadding="0" border="0" class="center-on-narrow">
|
||||
<tr>
|
||||
<td class="button-td">
|
||||
InstallId: {{InstallId}}
|
||||
</td>
|
||||
<td class="button-td">
|
||||
Build: {{Build}}
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
</center>
|
||||
</td>
|
||||
</tr>
|
||||
|
|
@ -18,9 +18,9 @@ public static class StringExtensions
|
|||
|
||||
// Remove all newline and control characters
|
||||
var sanitized = input
|
||||
.Replace(Environment.NewLine, "")
|
||||
.Replace("\n", "")
|
||||
.Replace("\r", "");
|
||||
.Replace(Environment.NewLine, string.Empty)
|
||||
.Replace("\n", string.Empty)
|
||||
.Replace("\r", string.Empty);
|
||||
|
||||
// Optionally remove other potentially unwanted characters
|
||||
sanitized = Regex.Replace(sanitized, @"[^\u0020-\u007E]", string.Empty); // Removes non-printable ASCII
|
||||
|
|
|
|||
|
|
@ -16,14 +16,14 @@ public class GenreBuilder : IEntityBuilder<Genre>
|
|||
{
|
||||
Title = name.Trim().SentenceCase(),
|
||||
NormalizedTitle = name.ToNormalized(),
|
||||
Chapters = new List<Chapter>(),
|
||||
SeriesMetadatas = new List<SeriesMetadata>()
|
||||
Chapters = [],
|
||||
SeriesMetadatas = []
|
||||
};
|
||||
}
|
||||
|
||||
public GenreBuilder WithSeriesMetadata(SeriesMetadata seriesMetadata)
|
||||
{
|
||||
_genre.SeriesMetadatas ??= new List<SeriesMetadata>();
|
||||
_genre.SeriesMetadatas ??= [];
|
||||
_genre.SeriesMetadatas.Add(seriesMetadata);
|
||||
return this;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -16,8 +16,8 @@ public class TagBuilder : IEntityBuilder<Tag>
|
|||
{
|
||||
Title = name.Trim().SentenceCase(),
|
||||
NormalizedTitle = name.ToNormalized(),
|
||||
Chapters = new List<Chapter>(),
|
||||
SeriesMetadatas = new List<SeriesMetadata>()
|
||||
Chapters = [],
|
||||
SeriesMetadatas = []
|
||||
};
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -19,7 +19,12 @@ public static class GenreHelper
|
|||
public static async Task UpdateChapterGenres(Chapter chapter, IEnumerable<string> genreNames, IUnitOfWork unitOfWork)
|
||||
{
|
||||
// Normalize genre names once and store them in a hash set for quick lookups
|
||||
var normalizedGenresToAdd = new HashSet<string>(genreNames.Select(g => g.ToNormalized()));
|
||||
var normalizedToOriginal = genreNames
|
||||
.Select(g => new { Original = g, Normalized = g.ToNormalized() })
|
||||
.GroupBy(x => x.Normalized)
|
||||
.ToDictionary(g => g.Key, g => g.First().Original);
|
||||
|
||||
var normalizedGenresToAdd = new HashSet<string>(normalizedToOriginal.Keys);
|
||||
|
||||
// Remove genres that are no longer in the new list
|
||||
var genresToRemove = chapter.Genres
|
||||
|
|
@ -42,7 +47,7 @@ public static class GenreHelper
|
|||
// Find missing genres that are not in the database
|
||||
var missingGenres = normalizedGenresToAdd
|
||||
.Where(nt => !existingGenreTitles.ContainsKey(nt))
|
||||
.Select(title => new GenreBuilder(title).Build())
|
||||
.Select(nt => new GenreBuilder(normalizedToOriginal[nt]).Build())
|
||||
.ToList();
|
||||
|
||||
// Add missing genres to the database
|
||||
|
|
|
|||
|
|
@ -20,7 +20,13 @@ public static class TagHelper
|
|||
public static async Task UpdateChapterTags(Chapter chapter, IEnumerable<string> tagNames, IUnitOfWork unitOfWork)
|
||||
{
|
||||
// Normalize tag names once and store them in a hash set for quick lookups
|
||||
var normalizedTagsToAdd = new HashSet<string>(tagNames.Select(t => t.ToNormalized()));
|
||||
// Create a dictionary: normalized => original
|
||||
var normalizedToOriginal = tagNames
|
||||
.Select(t => new { Original = t, Normalized = t.ToNormalized() })
|
||||
.GroupBy(x => x.Normalized) // in case of duplicates
|
||||
.ToDictionary(g => g.Key, g => g.First().Original);
|
||||
|
||||
var normalizedTagsToAdd = new HashSet<string>(normalizedToOriginal.Keys);
|
||||
var existingTagsSet = new HashSet<string>(chapter.Tags.Select(t => t.NormalizedTitle));
|
||||
|
||||
var isModified = false;
|
||||
|
|
@ -30,7 +36,7 @@ public static class TagHelper
|
|||
.Where(t => !normalizedTagsToAdd.Contains(t.NormalizedTitle))
|
||||
.ToList();
|
||||
|
||||
if (tagsToRemove.Any())
|
||||
if (tagsToRemove.Count != 0)
|
||||
{
|
||||
foreach (var tagToRemove in tagsToRemove)
|
||||
{
|
||||
|
|
@ -47,7 +53,7 @@ public static class TagHelper
|
|||
// Find missing tags that are not already in the database
|
||||
var missingTags = normalizedTagsToAdd
|
||||
.Where(nt => !existingTagTitles.ContainsKey(nt))
|
||||
.Select(title => new TagBuilder(title).Build())
|
||||
.Select(nt => new TagBuilder(normalizedToOriginal[nt]).Build())
|
||||
.ToList();
|
||||
|
||||
// Add missing tags to the database if any
|
||||
|
|
@ -67,13 +73,11 @@ public static class TagHelper
|
|||
// Add the new or existing tags to the chapter
|
||||
foreach (var normalizedTitle in normalizedTagsToAdd)
|
||||
{
|
||||
var tag = existingTagTitles[normalizedTitle];
|
||||
if (existingTagsSet.Contains(normalizedTitle)) continue;
|
||||
|
||||
if (!existingTagsSet.Contains(normalizedTitle))
|
||||
{
|
||||
chapter.Tags.Add(tag);
|
||||
isModified = true;
|
||||
}
|
||||
var tag = existingTagTitles[normalizedTitle];
|
||||
chapter.Tags.Add(tag);
|
||||
isModified = true;
|
||||
}
|
||||
|
||||
// Commit changes if modifications were made to the chapter's tags
|
||||
|
|
|
|||
|
|
@ -4,6 +4,7 @@ using System.ComponentModel.DataAnnotations;
|
|||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Web;
|
||||
using API.Data;
|
||||
|
|
@ -11,6 +12,7 @@ using API.DTOs.Email;
|
|||
using API.Entities;
|
||||
using API.Services.Plus;
|
||||
using Kavita.Common;
|
||||
using Kavita.Common.EnvironmentInfo;
|
||||
using Kavita.Common.Extensions;
|
||||
using MailKit.Security;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
|
|
@ -52,6 +54,7 @@ public interface IEmailService
|
|||
|
||||
Task<bool> SendTokenExpiredEmail(int userId, ScrobbleProvider provider);
|
||||
Task<bool> SendTokenExpiringSoonEmail(int userId, ScrobbleProvider provider);
|
||||
Task<bool> SendKavitaPlusDebug();
|
||||
}
|
||||
|
||||
public class EmailService : IEmailService
|
||||
|
|
@ -72,6 +75,7 @@ public class EmailService : IEmailService
|
|||
public const string TokenExpiringSoonTemplate = "TokenExpiringSoon";
|
||||
public const string EmailConfirmTemplate = "EmailConfirm";
|
||||
public const string EmailPasswordResetTemplate = "EmailPasswordReset";
|
||||
public const string KavitaPlusDebugTemplate = "KavitaPlusDebug";
|
||||
|
||||
public EmailService(ILogger<EmailService> logger, IUnitOfWork unitOfWork, IDirectoryService directoryService,
|
||||
IHostEnvironment environment, ILocalizationService localizationService)
|
||||
|
|
@ -259,6 +263,40 @@ public class EmailService : IEmailService
|
|||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sends information about Kavita install for Kavita+ registration
|
||||
/// </summary>
|
||||
/// <example>Users in China can have issues subscribing, this flow will allow me to register their instance on their behalf</example>
|
||||
/// <returns></returns>
|
||||
public async Task<bool> SendKavitaPlusDebug()
|
||||
{
|
||||
var settings = await _unitOfWork.SettingsRepository.GetSettingsDtoAsync();
|
||||
if (!settings.IsEmailSetup()) return false;
|
||||
|
||||
var placeholders = new List<KeyValuePair<string, string>>
|
||||
{
|
||||
new ("{{InstallId}}", HashUtil.ServerToken()),
|
||||
new ("{{Build}}", BuildInfo.Version.ToString()),
|
||||
};
|
||||
|
||||
var emailOptions = new EmailOptionsDto()
|
||||
{
|
||||
Subject = UpdatePlaceHolders("Kavita+: A User needs manual registration", placeholders),
|
||||
Template = KavitaPlusDebugTemplate,
|
||||
Body = UpdatePlaceHolders(await GetEmailBody(KavitaPlusDebugTemplate), placeholders),
|
||||
Preheader = UpdatePlaceHolders("Kavita+: A User needs manual registration", placeholders),
|
||||
ToEmails =
|
||||
[
|
||||
// My kavita email
|
||||
Encoding.UTF8.GetString(Convert.FromBase64String("a2F2aXRhcmVhZGVyQGdtYWlsLmNvbQ=="))
|
||||
]
|
||||
};
|
||||
|
||||
await SendEmail(emailOptions);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sends an invite email to a user to setup their account
|
||||
/// </summary>
|
||||
|
|
@ -304,10 +342,10 @@ public class EmailService : IEmailService
|
|||
Template = EmailPasswordResetTemplate,
|
||||
Body = UpdatePlaceHolders(await GetEmailBody(EmailPasswordResetTemplate), placeholders),
|
||||
Preheader = "Email confirmation is required for continued access. Click the button to confirm your email.",
|
||||
ToEmails = new List<string>()
|
||||
{
|
||||
ToEmails =
|
||||
[
|
||||
dto.EmailAddress
|
||||
}
|
||||
]
|
||||
};
|
||||
|
||||
await SendEmail(emailOptions);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue