95 lines
No EOL
3.8 KiB
Text
95 lines
No EOL
3.8 KiB
Text
@page "/admin/metadata"
|
|
@using System.Text.RegularExpressions
|
|
@using Iceshrimp.Backend.Components.Admin
|
|
@using Iceshrimp.Backend.Core.Extensions
|
|
@using Iceshrimp.Backend.Core.Services
|
|
@using Microsoft.AspNetCore.Components.Forms
|
|
@inherits AdminComponentBase
|
|
|
|
<AdminPageHeader Title="Instance metadata"/>
|
|
|
|
<p>Here you can adjust basic instance metadata. It gets displayed to all users, including guests.</p>
|
|
|
|
<EditForm FormName="update-metadata" Model="Model" OnSubmit="@OnSubmit" enctype="multipart/form-data">
|
|
<label for="icon">Icon</label>
|
|
<InputFile name="Model.IconFile" id="icon" accept="image/*"/>
|
|
<label for="name">Instance name</label>
|
|
<InputText @bind-Value="@Model.Name" id="name" placeholder="Shrimp & friends"/>
|
|
<label for="desc">Instance description</label>
|
|
<InputTextArea @bind-Value="@Model.Description" id="desc" placeholder="Welcome to my instance!"/>
|
|
<label for="theme-color">Theme color</label>
|
|
<InputText @bind-Value="@Model.ThemeColor" id="theme-color" placeholder="#7b74cc"/>
|
|
<label for="contact">Admin contact email</label>
|
|
<InputText @bind-Value="@Model.AdminContact" type="email" id="contact" placeholder="webmaster@example.org"/>
|
|
<button type="submit">Submit</button>
|
|
</EditForm>
|
|
|
|
@code {
|
|
[Inject] public required DriveService Drive { get; set; }
|
|
[Inject] public required MetaService Meta { get; set; }
|
|
[Inject] public required SystemUserService SystemUser { get; set; }
|
|
|
|
[SupplyParameterFromForm] private MetadataModel Model { get; set; } = null!;
|
|
|
|
private class MetadataModel
|
|
{
|
|
public string? Name { get; set; }
|
|
public string? Description { get; set; }
|
|
public string? AdminContact { get; set; }
|
|
public string? ThemeColor { get; set; }
|
|
public IFormFile? IconFile { get; set; }
|
|
|
|
public void Canonicalize()
|
|
{
|
|
var colorRegex = new Regex(@"^#(?:[0-9a-f]{3}){1,2}$", RegexOptions.IgnoreCase);
|
|
|
|
Name = Name?.Trim();
|
|
Description = Description?.Trim();
|
|
AdminContact = AdminContact?.Trim();
|
|
ThemeColor = ThemeColor?.Trim();
|
|
|
|
if (Name?.Length == 0 || Name == "Iceshrimp.NET")
|
|
Name = null;
|
|
if (Description?.Length == 0)
|
|
Description = null;
|
|
if (AdminContact?.Length == 0)
|
|
AdminContact = null;
|
|
if (!colorRegex.IsMatch(ThemeColor ?? ""))
|
|
ThemeColor = null;
|
|
if (!IconFile?.ContentType.StartsWith("image/") ?? false)
|
|
IconFile = null;
|
|
}
|
|
}
|
|
|
|
protected override void OnParametersSet()
|
|
{
|
|
// ReSharper disable once NullCoalescingConditionIsAlwaysNotNullAccordingToAPIContract
|
|
Model ??= new MetadataModel();
|
|
}
|
|
|
|
protected override async Task OnGet()
|
|
{
|
|
(Model.Name, Model.Description, Model.AdminContact, Model.ThemeColor) =
|
|
await Meta.GetManyAsync(MetaEntity.InstanceName, MetaEntity.InstanceDescription, MetaEntity.AdminContactEmail, MetaEntity.ThemeColor);
|
|
}
|
|
|
|
async Task OnSubmit()
|
|
{
|
|
Model.Canonicalize();
|
|
await Meta.SetAsync(MetaEntity.InstanceName, Model.Name);
|
|
await Meta.SetAsync(MetaEntity.InstanceDescription, Model.Description);
|
|
await Meta.SetAsync(MetaEntity.AdminContactEmail, Model.AdminContact);
|
|
await Meta.SetAsync(MetaEntity.ThemeColor, Model.ThemeColor);
|
|
|
|
if (Model.IconFile != null)
|
|
{
|
|
var ia = await SystemUser.GetInstanceActorAsync();
|
|
var req = new DriveFileCreationRequest { Filename = Model.IconFile.Name, IsSensitive = false, MimeType = Model.IconFile.ContentType };
|
|
|
|
var file = await Drive.StoreFileAsync(Model.IconFile.OpenReadStream(), ia, req);
|
|
await Meta.SetAsync(MetaEntity.IconFileId, file.Id);
|
|
}
|
|
|
|
ReloadPage();
|
|
}
|
|
} |