feat: add system_id parameter to avatar pull requests

This commit is contained in:
Iris System 2024-02-14 06:30:51 +13:00
parent 6547f1ca20
commit 71ea11e97e
6 changed files with 13 additions and 12 deletions

View file

@ -263,7 +263,7 @@ public class Groups
{ {
ctx.CheckOwnGroup(target); ctx.CheckOwnGroup(target);
img = await _avatarHosting.TryRehostImage(img, AvatarHostingService.RehostedImageType.Avatar, ctx.Author.Id); img = await _avatarHosting.TryRehostImage(img, AvatarHostingService.RehostedImageType.Avatar, ctx.Author.Id, ctx.System);
await AvatarUtils.VerifyAvatarOrThrow(_client, img.Url); await AvatarUtils.VerifyAvatarOrThrow(_client, img.Url);
await ctx.Repository.UpdateGroup(target.Id, new GroupPatch { Icon = img.CleanUrl ?? img.Url }); await ctx.Repository.UpdateGroup(target.Id, new GroupPatch { Icon = img.CleanUrl ?? img.Url });
@ -330,7 +330,7 @@ public class Groups
{ {
ctx.CheckOwnGroup(target); ctx.CheckOwnGroup(target);
img = await _avatarHosting.TryRehostImage(img, AvatarHostingService.RehostedImageType.Banner, ctx.Author.Id); img = await _avatarHosting.TryRehostImage(img, AvatarHostingService.RehostedImageType.Banner, ctx.Author.Id, ctx.System);
await AvatarUtils.VerifyAvatarOrThrow(_client, img.Url, true); await AvatarUtils.VerifyAvatarOrThrow(_client, img.Url, true);
await ctx.Repository.UpdateGroup(target.Id, new GroupPatch { BannerImage = img.CleanUrl ?? img.Url }); await ctx.Repository.UpdateGroup(target.Id, new GroupPatch { BannerImage = img.CleanUrl ?? img.Url });

View file

@ -81,7 +81,7 @@ public class Member
uriBuilder.Query = ""; uriBuilder.Query = "";
img.CleanUrl = uriBuilder.Uri.AbsoluteUri; img.CleanUrl = uriBuilder.Uri.AbsoluteUri;
img = await _avatarHosting.TryRehostImage(img, AvatarHostingService.RehostedImageType.Avatar, ctx.Author.Id); img = await _avatarHosting.TryRehostImage(img, AvatarHostingService.RehostedImageType.Avatar, ctx.Author.Id, ctx.System);
await AvatarUtils.VerifyAvatarOrThrow(_client, img.Url); await AvatarUtils.VerifyAvatarOrThrow(_client, img.Url);
await ctx.Repository.UpdateMember(member.Id, new MemberPatch { AvatarUrl = img.CleanUrl ?? img.Url }, conn); await ctx.Repository.UpdateMember(member.Id, new MemberPatch { AvatarUrl = img.CleanUrl ?? img.Url }, conn);

View file

@ -141,7 +141,7 @@ public class MemberAvatar
ctx.CheckSystem().CheckOwnMember(target); ctx.CheckSystem().CheckOwnMember(target);
avatarArg = await _avatarHosting.TryRehostImage(avatarArg.Value, AvatarHostingService.RehostedImageType.Avatar, ctx.Author.Id); avatarArg = await _avatarHosting.TryRehostImage(avatarArg.Value, AvatarHostingService.RehostedImageType.Avatar, ctx.Author.Id, ctx.System);
await AvatarUtils.VerifyAvatarOrThrow(_client, avatarArg.Value.Url); await AvatarUtils.VerifyAvatarOrThrow(_client, avatarArg.Value.Url);
await UpdateAvatar(location, ctx, target, avatarArg.Value.CleanUrl ?? avatarArg.Value.Url); await UpdateAvatar(location, ctx, target, avatarArg.Value.CleanUrl ?? avatarArg.Value.Url);
await PrintResponse(location, ctx, target, avatarArg.Value, guildData); await PrintResponse(location, ctx, target, avatarArg.Value, guildData);

View file

@ -182,7 +182,7 @@ public class MemberEdit
async Task SetBannerImage(ParsedImage img) async Task SetBannerImage(ParsedImage img)
{ {
img = await _avatarHosting.TryRehostImage(img, AvatarHostingService.RehostedImageType.Banner, ctx.Author.Id); img = await _avatarHosting.TryRehostImage(img, AvatarHostingService.RehostedImageType.Banner, ctx.Author.Id, ctx.System);
await AvatarUtils.VerifyAvatarOrThrow(_client, img.Url, true); await AvatarUtils.VerifyAvatarOrThrow(_client, img.Url, true);
await ctx.Repository.UpdateMember(target.Id, new MemberPatch { BannerImage = img.CleanUrl ?? img.Url }); await ctx.Repository.UpdateMember(target.Id, new MemberPatch { BannerImage = img.CleanUrl ?? img.Url });

View file

@ -475,7 +475,7 @@ public class SystemEdit
{ {
ctx.CheckOwnSystem(target); ctx.CheckOwnSystem(target);
img = await _avatarHosting.TryRehostImage(img, AvatarHostingService.RehostedImageType.Avatar, ctx.Author.Id); img = await _avatarHosting.TryRehostImage(img, AvatarHostingService.RehostedImageType.Avatar, ctx.Author.Id, ctx.System);
await AvatarUtils.VerifyAvatarOrThrow(_client, img.Url); await AvatarUtils.VerifyAvatarOrThrow(_client, img.Url);
await ctx.Repository.UpdateSystem(target.Id, new SystemPatch { AvatarUrl = img.CleanUrl ?? img.Url }); await ctx.Repository.UpdateSystem(target.Id, new SystemPatch { AvatarUrl = img.CleanUrl ?? img.Url });
@ -545,7 +545,7 @@ public class SystemEdit
{ {
ctx.CheckOwnSystem(target); ctx.CheckOwnSystem(target);
img = await _avatarHosting.TryRehostImage(img, AvatarHostingService.RehostedImageType.Avatar, ctx.Author.Id); img = await _avatarHosting.TryRehostImage(img, AvatarHostingService.RehostedImageType.Avatar, ctx.Author.Id, ctx.System);
await AvatarUtils.VerifyAvatarOrThrow(_client, img.Url); await AvatarUtils.VerifyAvatarOrThrow(_client, img.Url);
await ctx.Repository.UpdateSystemGuild(target.Id, ctx.Guild.Id, new SystemGuildPatch { AvatarUrl = img.CleanUrl ?? img.Url }); await ctx.Repository.UpdateSystemGuild(target.Id, ctx.Guild.Id, new SystemGuildPatch { AvatarUrl = img.CleanUrl ?? img.Url });
@ -644,7 +644,7 @@ public class SystemEdit
else if (await ctx.MatchImage() is { } img) else if (await ctx.MatchImage() is { } img)
{ {
img = await _avatarHosting.TryRehostImage(img, AvatarHostingService.RehostedImageType.Banner, ctx.Author.Id); img = await _avatarHosting.TryRehostImage(img, AvatarHostingService.RehostedImageType.Banner, ctx.Author.Id, ctx.System);
await AvatarUtils.VerifyAvatarOrThrow(_client, img.Url, true); await AvatarUtils.VerifyAvatarOrThrow(_client, img.Url, true);
await ctx.Repository.UpdateSystem(target.Id, new SystemPatch { BannerImage = img.CleanUrl ?? img.Url }); await ctx.Repository.UpdateSystem(target.Id, new SystemPatch { BannerImage = img.CleanUrl ?? img.Url });

View file

@ -1,3 +1,4 @@
using PluralKit.Core;
using System.Net; using System.Net;
using System.Net.Http.Json; using System.Net.Http.Json;
@ -14,9 +15,9 @@ public class AvatarHostingService
_client = client; _client = client;
} }
public async Task<ParsedImage> TryRehostImage(ParsedImage input, RehostedImageType type, ulong userId) public async Task<ParsedImage> TryRehostImage(ParsedImage input, RehostedImageType type, ulong userId, PKSystem? system)
{ {
var uploaded = await TryUploadAvatar(input.Url, type, userId); var uploaded = await TryUploadAvatar(input.Url, type, userId, system);
if (uploaded != null) if (uploaded != null)
{ {
// todo: make new image type called Cdn? // todo: make new image type called Cdn?
@ -26,7 +27,7 @@ public class AvatarHostingService
return input; return input;
} }
public async Task<string?> TryUploadAvatar(string? avatarUrl, RehostedImageType type, ulong userId) public async Task<string?> TryUploadAvatar(string? avatarUrl, RehostedImageType type, ulong userId, PKSystem? system)
{ {
if (!AvatarUtils.IsDiscordCdnUrl(avatarUrl)) if (!AvatarUtils.IsDiscordCdnUrl(avatarUrl))
return null; return null;
@ -42,7 +43,7 @@ public class AvatarHostingService
}; };
var response = await _client.PostAsJsonAsync(_config.AvatarServiceUrl + "/pull", var response = await _client.PostAsJsonAsync(_config.AvatarServiceUrl + "/pull",
new { url = avatarUrl, kind, uploaded_by = userId }); new { url = avatarUrl, kind, uploaded_by = userId, system_id = system?.Uuid.ToString() });
if (response.StatusCode != HttpStatusCode.OK) if (response.StatusCode != HttpStatusCode.OK)
{ {
var error = await response.Content.ReadFromJsonAsync<ErrorResponse>(); var error = await response.Content.ReadFromJsonAsync<ErrorResponse>();