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 UserAvatarUpdateInvocable(
|
|
|
|
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 UpdateUserAvatarAsync(Payload.Id, Payload.NewAvatar);
|
|
|
|
else
|
|
|
|
await ClearUserAvatarAsync(Payload.Id);
|
2024-09-03 16:29:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private async Task UpdateUserAvatarAsync(Snowflake id, string newAvatar)
|
|
|
|
{
|
|
|
|
_logger.Debug("Updating avatar for user {MemberId}", id);
|
2024-09-04 14:25:44 +02:00
|
|
|
|
2024-12-08 15:07:25 +01:00
|
|
|
User? user = await db.Users.FindAsync(id);
|
2024-09-03 16:29:51 +02:00
|
|
|
if (user == null)
|
|
|
|
{
|
2024-10-02 00:28:07 +02:00
|
|
|
_logger.Warning(
|
|
|
|
"Update avatar job queued for {UserId} but no user 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-09-03 16:29:51 +02:00
|
|
|
image.Seek(0, SeekOrigin.Begin);
|
2024-12-08 15:07:25 +01:00
|
|
|
string? prevHash = user.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
|
|
|
|
|
|
|
user.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 user {UserId}", id);
|
|
|
|
}
|
|
|
|
catch (ArgumentException ae)
|
|
|
|
{
|
2024-10-02 00:28:07 +02:00
|
|
|
_logger.Warning(
|
|
|
|
"Invalid data URI for new avatar for user {UserId}: {Reason}",
|
|
|
|
id,
|
|
|
|
ae.Message
|
|
|
|
);
|
2024-09-03 16:29:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private async Task ClearUserAvatarAsync(Snowflake id)
|
|
|
|
{
|
|
|
|
_logger.Debug("Clearing avatar for user {MemberId}", id);
|
2024-09-04 14:25:44 +02:00
|
|
|
|
2024-12-08 15:07:25 +01:00
|
|
|
User? user = await db.Users.FindAsync(id);
|
2024-09-03 16:29:51 +02:00
|
|
|
if (user == null)
|
|
|
|
{
|
2024-10-02 00:28:07 +02:00
|
|
|
_logger.Warning(
|
|
|
|
"Clear avatar job queued for {UserId} but no user with that ID exists",
|
|
|
|
id
|
|
|
|
);
|
2024-09-03 16:29:51 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (user.Avatar == null)
|
|
|
|
{
|
|
|
|
_logger.Warning("Clear avatar job queued for {UserId} with null avatar", id);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-09-09 14:50:00 +02:00
|
|
|
await objectStorageService.RemoveObjectAsync(Path(user.Id, user.Avatar));
|
2024-09-03 16:29:51 +02:00
|
|
|
|
|
|
|
user.Avatar = null;
|
|
|
|
await db.SaveChangesAsync();
|
|
|
|
}
|
|
|
|
|
|
|
|
public static string Path(Snowflake id, string hash) => $"users/{id}/avatars/{hash}.webp";
|
2024-10-02 00:28:07 +02:00
|
|
|
}
|