diff --git a/src/Moonglade.Configuration/BlogConfig.cs b/src/Moonglade.Configuration/BlogConfig.cs index e76004eb8..6ae3a5d40 100644 --- a/src/Moonglade.Configuration/BlogConfig.cs +++ b/src/Moonglade.Configuration/BlogConfig.cs @@ -14,6 +14,7 @@ public interface IBlogConfig CustomStyleSheetSettings CustomStyleSheetSettings { get; set; } CustomMenuSettings CustomMenuSettings { get; set; } LocalAccountSettings LocalAccountSettings { get; set; } + SocialLinkSettings SocialLinkSettings { get; set; } SystemManifestSettings SystemManifestSettings { get; set; } IEnumerable LoadFromConfig(IDictionary config); @@ -40,6 +41,8 @@ public class BlogConfig : IBlogConfig public LocalAccountSettings LocalAccountSettings { get; set; } + public SocialLinkSettings SocialLinkSettings { get; set; } + public SystemManifestSettings SystemManifestSettings { get; set; } public IEnumerable LoadFromConfig(IDictionary config) @@ -53,6 +56,7 @@ public IEnumerable LoadFromConfig(IDictionary config) CustomStyleSheetSettings = AssignValueForConfigItem(7, CustomStyleSheetSettings.DefaultValue, config); CustomMenuSettings = AssignValueForConfigItem(10, CustomMenuSettings.DefaultValue, config); LocalAccountSettings = AssignValueForConfigItem(11, LocalAccountSettings.DefaultValue, config); + SocialLinkSettings = AssignValueForConfigItem(12, SocialLinkSettings.DefaultValue, config); // Special case SystemManifestSettings = AssignValueForConfigItem(99, SystemManifestSettings.DefaultValue, config); diff --git a/src/Moonglade.Configuration/SocialLink.cs b/src/Moonglade.Configuration/SocialLink.cs index 4269825cc..984bbce6d 100644 --- a/src/Moonglade.Configuration/SocialLink.cs +++ b/src/Moonglade.Configuration/SocialLink.cs @@ -1,4 +1,20 @@ -namespace Moonglade.Configuration; +using System.ComponentModel.DataAnnotations; + +namespace Moonglade.Configuration; + +public class SocialLinkSettings : IBlogSettings +{ + public bool IsEnabled { get; set; } + + public SocialLink[] Links { get; set; } = []; + + public static SocialLinkSettings DefaultValue => + new() + { + IsEnabled = false, + Links = [] + }; +} public class SocialLink { @@ -7,4 +23,13 @@ public class SocialLink public string Icon { get; set; } public string Url { get; set; } +} + +public class SocialLinkSettingsJsonModel +{ + [Display(Name = "Enable Social Links")] + public bool IsEnabled { get; set; } + + [MaxLength(1024)] + public string JsonData { get; set; } } \ No newline at end of file diff --git a/src/Moonglade.Core/GetAllSocialLinksQuery.cs b/src/Moonglade.Core/GetAllSocialLinksQuery.cs index 77cdd0fb2..2302d6c71 100644 --- a/src/Moonglade.Core/GetAllSocialLinksQuery.cs +++ b/src/Moonglade.Core/GetAllSocialLinksQuery.cs @@ -1,22 +1,21 @@ -using Microsoft.Extensions.Configuration; -using Moonglade.Configuration; +using Moonglade.Configuration; namespace Moonglade.Core; -public record GetAllSocialLinksQuery : IRequest>; +public record GetAllSocialLinksQuery : IRequest; -public class GetAllSocialLinksQueryHandler(IConfiguration configuration) : IRequestHandler> +public class GetAllSocialLinksQueryHandler(IBlogConfig blogConfig) : IRequestHandler { - public Task> Handle(GetAllSocialLinksQuery request, CancellationToken ct) + public Task Handle(GetAllSocialLinksQuery request, CancellationToken ct) { - var section = configuration.GetSection("Experimental:SocialLinks"); + var section = blogConfig.SocialLinkSettings; - if (!section.Exists()) + if (!section.IsEnabled) { - return Task.FromResult(new List()); + return Task.FromResult(Array.Empty()); } - var links = section.Get>(); - return Task.FromResult(links ?? new List()); + var links = blogConfig.SocialLinkSettings.Links; + return Task.FromResult(links); } } \ No newline at end of file diff --git a/src/Moonglade.Setup/BlogConfigInitializer.cs b/src/Moonglade.Setup/BlogConfigInitializer.cs index 70945dcd5..fb7e225a6 100644 --- a/src/Moonglade.Setup/BlogConfigInitializer.cs +++ b/src/Moonglade.Setup/BlogConfigInitializer.cs @@ -59,6 +59,10 @@ await mediator.Send(new AddDefaultConfigurationCommand(key, nameof(CustomMenuSet await mediator.Send(new AddDefaultConfigurationCommand(key, nameof(LocalAccountSettings), LocalAccountSettings.DefaultValue.ToJson())); break; + case 12: + await mediator.Send(new AddDefaultConfigurationCommand(key, nameof(SocialLinkSettings), + SocialLinkSettings.DefaultValue.ToJson())); + break; case 99: await mediator.Send(new AddDefaultConfigurationCommand(key, nameof(SystemManifestSettings), isNew ? diff --git a/src/Moonglade.Web/Controllers/SettingsController.cs b/src/Moonglade.Web/Controllers/SettingsController.cs index 2e35972ae..2241a6a85 100644 --- a/src/Moonglade.Web/Controllers/SettingsController.cs +++ b/src/Moonglade.Web/Controllers/SettingsController.cs @@ -153,6 +153,26 @@ public async Task Advanced(AdvancedSettings model) return NoContent(); } + [HttpPost("social-link")] + [ProducesResponseType(StatusCodes.Status204NoContent)] + public async Task SocialLink(SocialLinkSettingsJsonModel model) + { + if (model.IsEnabled && string.IsNullOrWhiteSpace(model.JsonData)) + { + ModelState.AddModelError(nameof(SocialLinkSettingsJsonModel.JsonData), "JsonData is required"); + return BadRequest(ModelState.CombineErrorMessages()); + } + + blogConfig.SocialLinkSettings = new() + { + IsEnabled = model.IsEnabled, + Links = model.JsonData.FromJson() + }; + + await SaveConfigAsync(blogConfig.SocialLinkSettings); + return NoContent(); + } + [HttpPost("reset")] [ProducesResponseType(StatusCodes.Status202Accepted)] public async Task Reset(BlogDbContext context, IHostApplicationLifetime applicationLifetime) diff --git a/src/Moonglade.Web/Pages/Components/SocialLink/Default.cshtml b/src/Moonglade.Web/Pages/Components/SocialLink/Default.cshtml index 2ab2d3d27..26ce18ab1 100644 --- a/src/Moonglade.Web/Pages/Components/SocialLink/Default.cshtml +++ b/src/Moonglade.Web/Pages/Components/SocialLink/Default.cshtml @@ -1,5 +1,5 @@ @using Moonglade.Utils -@model List +@model SocialLink[] @if (Model.Any()) { diff --git a/src/Moonglade.Web/Pages/Settings/SocialLinks.cshtml b/src/Moonglade.Web/Pages/Settings/SocialLinks.cshtml new file mode 100644 index 000000000..a74cef2f6 --- /dev/null +++ b/src/Moonglade.Web/Pages/Settings/SocialLinks.cshtml @@ -0,0 +1,83 @@ +@page "/admin/settings/social-links" +@Html.AntiForgeryToken() +@{ + var bc = BlogConfig.SocialLinkSettings; + var settings = new SocialLinkSettingsJsonModel + { + IsEnabled = bc.IsEnabled, + JsonData = bc.Links.ToJson(true) + }; +} + +@section scripts { + + + +} + +@section head { + +} + +@section admintoolbar { + +} + +
+
+ +
+ This feature is under development. It is currently in preview. A GUI editor will be available in the future. +
+ +
+
+ +
+
+ +
+
+
+ + +
+
+
+ +
+
+
+ + + +
+ +
+
\ No newline at end of file diff --git a/src/Moonglade.Web/Pages/Settings/_SettingsHeader.cshtml b/src/Moonglade.Web/Pages/Settings/_SettingsHeader.cshtml index ba7999249..1c01cdb6b 100644 --- a/src/Moonglade.Web/Pages/Settings/_SettingsHeader.cshtml +++ b/src/Moonglade.Web/Pages/Settings/_SettingsHeader.cshtml @@ -22,6 +22,9 @@ + diff --git a/src/Moonglade.Web/Pages/Shared/_Aside.cshtml b/src/Moonglade.Web/Pages/Shared/_Aside.cshtml index e73ed8ebb..18c2c2726 100644 --- a/src/Moonglade.Web/Pages/Shared/_Aside.cshtml +++ b/src/Moonglade.Web/Pages/Shared/_Aside.cshtml @@ -40,7 +40,7 @@ } - @if (Configuration.GetSection("Experimental:SocialLinks").Exists()) + @if (BlogConfig.SocialLinkSettings.IsEnabled) {