2024-12-09 21:11:46 +01:00
|
|
|
// Copyright (C) 2023-present sam/u1f320 (vulpine.solutions)
|
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Affero General Public License as published
|
|
|
|
// by the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Affero General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
|
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
2024-09-03 16:29:51 +02:00
|
|
|
using Coravel.Invocable;
|
|
|
|
using Foxnouns.Backend.Database;
|
2024-12-08 15:07:25 +01:00
|
|
|
using Foxnouns.Backend.Database.Models;
|
2024-09-03 16:29:51 +02:00
|
|
|
using Foxnouns.Backend.Extensions;
|
|
|
|
using Foxnouns.Backend.Services;
|
|
|
|
|
|
|
|
namespace Foxnouns.Backend.Jobs;
|
|
|
|
|
2024-10-02 00:28:07 +02:00
|
|
|
public class MemberAvatarUpdateInvocable(
|
|
|
|
DatabaseContext db,
|
|
|
|
ObjectStorageService objectStorageService,
|
|
|
|
ILogger logger
|
|
|
|
) : IInvocable, IInvocableWithPayload<AvatarUpdatePayload>
|
2024-09-03 16:29:51 +02:00
|
|
|
{
|
|
|
|
private readonly ILogger _logger = logger.ForContext<UserAvatarUpdateInvocable>();
|
|
|
|
public required AvatarUpdatePayload Payload { get; set; }
|
|
|
|
|
|
|
|
public async Task Invoke()
|
|
|
|
{
|
2024-10-02 00:28:07 +02:00
|
|
|
if (Payload.NewAvatar != null)
|
|
|
|
await UpdateMemberAvatarAsync(Payload.Id, Payload.NewAvatar);
|
|
|
|
else
|
|
|
|
await ClearMemberAvatarAsync(Payload.Id);
|
2024-09-03 16:29:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private async Task UpdateMemberAvatarAsync(Snowflake id, string newAvatar)
|
|
|
|
{
|
|
|
|
_logger.Debug("Updating avatar for member {MemberId}", id);
|
|
|
|
|
2024-12-08 15:07:25 +01:00
|
|
|
Member? member = await db.Members.FindAsync(id);
|
2024-09-03 16:29:51 +02:00
|
|
|
if (member == null)
|
|
|
|
{
|
2024-10-02 00:28:07 +02:00
|
|
|
_logger.Warning(
|
|
|
|
"Update avatar job queued for {MemberId} but no member with that ID exists",
|
|
|
|
id
|
|
|
|
);
|
2024-09-03 16:29:51 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
2024-12-08 15:07:25 +01:00
|
|
|
(string? hash, Stream? image) = await ImageObjectExtensions.ConvertBase64UriToImage(
|
2024-11-23 20:41:41 +01:00
|
|
|
newAvatar,
|
2024-12-08 15:07:25 +01:00
|
|
|
512,
|
|
|
|
true
|
2024-11-23 20:41:41 +01:00
|
|
|
);
|
2024-12-08 15:07:25 +01:00
|
|
|
string? prevHash = member.Avatar;
|
2024-09-03 16:29:51 +02:00
|
|
|
|
2024-09-09 14:50:00 +02:00
|
|
|
await objectStorageService.PutObjectAsync(Path(id, hash), image, "image/webp");
|
2024-09-03 16:29:51 +02:00
|
|
|
|
|
|
|
member.Avatar = hash;
|
|
|
|
await db.SaveChangesAsync();
|
|
|
|
|
|
|
|
if (prevHash != null && prevHash != hash)
|
2024-09-09 14:50:00 +02:00
|
|
|
await objectStorageService.RemoveObjectAsync(Path(id, prevHash));
|
2024-09-03 16:29:51 +02:00
|
|
|
|
|
|
|
_logger.Information("Updated avatar for member {MemberId}", id);
|
|
|
|
}
|
|
|
|
catch (ArgumentException ae)
|
|
|
|
{
|
2024-10-02 00:28:07 +02:00
|
|
|
_logger.Warning(
|
|
|
|
"Invalid data URI for new avatar for member {MemberId}: {Reason}",
|
|
|
|
id,
|
|
|
|
ae.Message
|
|
|
|
);
|
2024-09-03 16:29:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private async Task ClearMemberAvatarAsync(Snowflake id)
|
|
|
|
{
|
|
|
|
_logger.Debug("Clearing avatar for member {MemberId}", id);
|
|
|
|
|
2024-12-08 15:07:25 +01:00
|
|
|
Member? member = await db.Members.FindAsync(id);
|
2024-09-03 16:29:51 +02:00
|
|
|
if (member == null)
|
|
|
|
{
|
2024-10-02 00:28:07 +02:00
|
|
|
_logger.Warning(
|
|
|
|
"Clear avatar job queued for {MemberId} but no member with that ID exists",
|
|
|
|
id
|
|
|
|
);
|
2024-09-03 16:29:51 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (member.Avatar == null)
|
|
|
|
{
|
|
|
|
_logger.Warning("Clear avatar job queued for {MemberId} with null avatar", id);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-09-09 14:50:00 +02:00
|
|
|
await objectStorageService.RemoveObjectAsync(Path(member.Id, member.Avatar));
|
2024-09-03 16:29:51 +02:00
|
|
|
|
|
|
|
member.Avatar = null;
|
|
|
|
await db.SaveChangesAsync();
|
|
|
|
}
|
|
|
|
|
|
|
|
public static string Path(Snowflake id, string hash) => $"members/{id}/avatars/{hash}.webp";
|
2024-10-02 00:28:07 +02:00
|
|
|
}
|