diff --git a/src/Moonglade.Web/Controllers/SettingsController.cs b/src/Moonglade.Web/Controllers/SettingsController.cs index 857833cb7..38404c975 100644 --- a/src/Moonglade.Web/Controllers/SettingsController.cs +++ b/src/Moonglade.Web/Controllers/SettingsController.cs @@ -163,10 +163,43 @@ public async Task SocialLink(SocialLinkSettingsJsonModel model) return BadRequest(ModelState.CombineErrorMessages()); } + var links = model.JsonData.FromJson(); + + // Check each link, if any link is invalid, return BadRequest + foreach (var link in links) + { + if (string.IsNullOrWhiteSpace(link.Name)) + { + ModelState.AddModelError($"{nameof(Moonglade.Configuration.SocialLink)}.{nameof(Moonglade.Configuration.SocialLink.Name)}", "Name is required"); + return BadRequest(ModelState.CombineErrorMessages()); + } + + if (string.IsNullOrWhiteSpace(link.Icon)) + { + ModelState.AddModelError($"{nameof(Moonglade.Configuration.SocialLink)}.{nameof(Moonglade.Configuration.SocialLink.Icon)}", "Icon is required"); + return BadRequest(ModelState.CombineErrorMessages()); + } + + if (string.IsNullOrWhiteSpace(link.Url)) + { + ModelState.AddModelError($"{nameof(Moonglade.Configuration.SocialLink)}.{nameof(Moonglade.Configuration.SocialLink.Url)}", "Url is required"); + return BadRequest(ModelState.CombineErrorMessages()); + } + + if (!Uri.TryCreate(link.Url, UriKind.Absolute, out _)) + { + ModelState.AddModelError($"{nameof(Moonglade.Configuration.SocialLink)}.{nameof(Moonglade.Configuration.SocialLink.Url)}", "Url is invalid"); + return BadRequest(ModelState.CombineErrorMessages()); + } + + // Sterilize + link.Url = Helper.SterilizeLink(link.Url); + } + blogConfig.SocialLinkSettings = new() { IsEnabled = model.IsEnabled, - Links = model.JsonData.FromJson() + Links = links }; await SaveConfigAsync(blogConfig.SocialLinkSettings);