[backend/razor] Set instance icon on pages if configured

This commit is contained in:
pancakes 2025-03-29 13:20:07 +10:00
parent 054fb66a00
commit 28a04c64d0
No known key found for this signature in database
2 changed files with 18 additions and 3 deletions

View file

@ -12,7 +12,8 @@ namespace Iceshrimp.Backend.Core.Services;
public class InstanceService(
DatabaseContext db,
HttpClient httpClient,
ILogger<InstanceService> logger
ILogger<InstanceService> logger,
MetaService meta
) : IScopedService
{
private static readonly AsyncKeyedLocker<string> KeyedLocker = new(o =>
@ -275,4 +276,10 @@ public class InstanceService(
return rule;
}
public async Task<string?> GetInstanceImageAsync()
{
var iconId = await meta.GetAsync(MetaEntity.IconFileId);
return await db.DriveFiles.Where(p => p.Id == iconId).Select(p => p.RawAccessUrl).FirstOrDefaultAsync();
}
}

View file

@ -18,6 +18,10 @@
<VersionedLink rel="stylesheet" href="/Iceshrimp.Backend.styles.css"/>
<VersionedLink rel="stylesheet" href="/css/default.css"/>
<VersionedLink rel="icon" type="image/png" href="/_content/Iceshrimp.Assets.Branding/favicon.png"/>
@if (_instanceIcon != null)
{
<link rel="icon" href="@_instanceIcon">
}
<HeadOutlet/>
<PageTitle>@(_instanceName ?? "Iceshrimp.NET")</PageTitle>
</head>
@ -54,18 +58,22 @@
</html>
@code {
[Inject] public required MetaService Meta { get; set; }
[Inject] public required InstanceService InstanceSvc { get; set; }
[Inject] public required MetaService Meta { get; set; }
// This is separate from AuthorizationMiddleware.AuthorizeAttribute, as that middleware is meant for API calls.
public class RequireAuthorizationAttribute : Attribute;
private string? _instanceName;
private User? _user;
private string? _instanceIcon;
protected override async Task OnParametersSetAsync()
{
_user = Context.GetUser();
_instanceName = await Meta.GetAsync(MetaEntity.InstanceName);
_user = Context.GetUser();
_instanceIcon = await InstanceSvc.GetInstanceImageAsync();
if (Context.GetEndpoint()?.Metadata.GetMetadata<RequireAuthorizationAttribute>() == null)
return;