using Domain.Entities;
using Features.ImageUploads.Services;
namespace Features.Users.Dtos;
/// Converts user domain entities into API response models.
public static class UserDtoMapper
{
///
/// How long a presigned avatar URL stays valid. Long enough to outlive the page that embeds it,
/// short enough that a leaked URL stops working.
///
public static readonly TimeSpan AvatarUrlLifetime = TimeSpan.FromHours(1);
/// Converts a user account into its public-profile representation.
/// The account to convert.
///
/// Signs the download URL for the account's avatar. Photo.Hyperlink holds an object key
/// rather than a URL, so it has to be signed before a client can fetch it.
///
public static PublicUserProfileDto ToPublicProfileDto(
this UserAccount user,
IFileStorageProvider fileStorageProvider
)
{
string? avatarKey = user.UserAvatar?.Photo?.Hyperlink;
return new PublicUserProfileDto(
UserAccountId : user.UserAccountId,
Username : user.Username,
FirstName : user.FirstName,
LastName : user.LastName,
Biography : user.UserProfile?.Biography ?? string.Empty,
AvatarUrl : string.IsNullOrWhiteSpace(avatarKey)
? string.Empty
: fileStorageProvider.GetPresignedUrl(avatarKey, AvatarUrlLifetime),
CreatedAt : user.CreatedAt
);
}
}