[backend/masto-client] Add instance banner and fallback icon

This commit is contained in:
pancakes 2025-03-29 14:50:02 +10:00
parent 93df8755ac
commit 72bb4123f8
No known key found for this signature in database
3 changed files with 41 additions and 6 deletions

View file

@ -36,18 +36,23 @@ public class InstanceController(
var noteCount = await db.Notes.LongCountAsync(p => p.UserHost == null); var noteCount = await db.Notes.LongCountAsync(p => p.UserHost == null);
var instanceCount = await db.Instances.LongCountAsync(); var instanceCount = await db.Instances.LongCountAsync();
var (instanceName, instanceDescription, adminContact) = var (instanceName, instanceDescription, adminContact, bannerId) =
await meta.GetManyAsync(MetaEntity.InstanceName, MetaEntity.InstanceDescription, await meta.GetManyAsync(MetaEntity.InstanceName, MetaEntity.InstanceDescription,
MetaEntity.AdminContactEmail); MetaEntity.AdminContactEmail, MetaEntity.BannerFileId);
// can't merge with above call since they're all nullable and this is not. // can't merge with above call since they're all nullable and this is not.
var vapidKey = await meta.GetAsync(MetaEntity.VapidPublicKey); var vapidKey = await meta.GetAsync(MetaEntity.VapidPublicKey);
var banner = await db.DriveFiles.Where(p => p.Id == bannerId)
.Select(p => p.PublicUrl ?? p.RawAccessUrl)
.FirstOrDefaultAsync();
return new InstanceInfoV1Response(config.Value, instanceName, instanceDescription, adminContact) return new InstanceInfoV1Response(config.Value, instanceName, instanceDescription, adminContact)
{ {
Stats = new InstanceStats(userCount, noteCount, instanceCount), Stats = new InstanceStats(userCount, noteCount, instanceCount),
Pleroma = new PleromaInstanceExtensions { VapidPublicKey = vapidKey, Metadata = new InstanceMetadata() }, Pleroma = new PleromaInstanceExtensions { VapidPublicKey = vapidKey, Metadata = new InstanceMetadata() },
Rules = await GetRules() Rules = await GetRules(),
ThumbnailUrl = banner
}; };
} }
@ -61,20 +66,40 @@ public class InstanceController(
&& !Constants.SystemUsers.Contains(p.UsernameLower) && !Constants.SystemUsers.Contains(p.UsernameLower)
&& p.LastActiveDate > cutoff); && p.LastActiveDate > cutoff);
var (instanceName, instanceDescription, adminContact, iconId) = var (instanceName, instanceDescription, adminContact, iconId, bannerId) =
await meta.GetManyAsync(MetaEntity.InstanceName, MetaEntity.InstanceDescription, await meta.GetManyAsync(MetaEntity.InstanceName, MetaEntity.InstanceDescription,
MetaEntity.AdminContactEmail, MetaEntity.IconFileId); MetaEntity.AdminContactEmail, MetaEntity.IconFileId, MetaEntity.BannerFileId);
var favicon = await db.DriveFiles.Where(p => p.Id == iconId) var favicon = await db.DriveFiles.Where(p => p.Id == iconId)
.Select(p => new InstanceIcon(p.PublicUrl ?? p.RawAccessUrl, p.Properties.Width ?? 128, .Select(p => new InstanceIcon(p.PublicUrl ?? p.RawAccessUrl, p.Properties.Width ?? 128,
p.Properties.Height ?? 128)) p.Properties.Height ?? 128))
.FirstOrDefaultAsync(); .FirstOrDefaultAsync();
List<InstanceIcon> icons = favicon != null
? [favicon]
:
[
new
InstanceIcon($"https://{config.Value.Instance.WebDomain}/_content/Iceshrimp.Assets.Branding/192.png",
192, 192),
new
InstanceIcon($"https://{config.Value.Instance.WebDomain}/_content/Iceshrimp.Assets.Branding/512.png",
512, 512)
];
// Mastodon expects an instance thumbnail, the mail wordmark isn't ideal but it's the closest thing ww have for a fallback
var banner = await db.DriveFiles.Where(p => p.Id == bannerId)
.Select(p => new InstanceThumbnail(p.PublicUrl ?? p.RawAccessUrl, p.Blurhash))
.FirstOrDefaultAsync()
?? new
InstanceThumbnail($"https://{config.Value.Instance.WebDomain}/_content/Iceshrimp.Assets.Branding/mail-wordmark.png",
null);
return new InstanceInfoV2Response(config.Value, instanceName, instanceDescription, adminContact) return new InstanceInfoV2Response(config.Value, instanceName, instanceDescription, adminContact)
{ {
Usage = new InstanceUsage { Users = new InstanceUsersUsage { ActiveMonth = activeMonth } }, Usage = new InstanceUsage { Users = new InstanceUsersUsage { ActiveMonth = activeMonth } },
Rules = await GetRules(), Rules = await GetRules(),
Icons = favicon != null ? [favicon] : [] Icons = icons,
Thumbnail = banner
}; };
} }

View file

@ -40,6 +40,8 @@ public class InstanceInfoV1Response(
[J("rules")] public required List<RuleEntity> Rules { get; set; } [J("rules")] public required List<RuleEntity> Rules { get; set; }
[J("thumbnail")] public required string? ThumbnailUrl { get; set; }
//TODO: add the rest //TODO: add the rest
} }

View file

@ -31,6 +31,8 @@ public class InstanceInfoV2Response(
[J("icon")] public required List<InstanceIcon> Icons { get; set; } [J("icon")] public required List<InstanceIcon> Icons { get; set; }
[J("thumbnail")] public required InstanceThumbnail Thumbnail { get; set; }
//TODO: add the rest //TODO: add the rest
} }
@ -85,3 +87,9 @@ public class InstanceIcon(string src, int width, int height)
[J("src")] public string Url => src; [J("src")] public string Url => src;
[J("size")] public string Size => $"{width}x{height}"; [J("size")] public string Size => $"{width}x{height}";
} }
public class InstanceThumbnail(string url, string? blurhash)
{
[J("url")] public string Url => url;
[J("blurhash")] public string? Blurhash => blurhash;
}