Add platform info to profile DTO with setting-based visibility

This commit is contained in:
Holden
2026-07-03 21:35:58 -05:00
parent 0abe65151c
commit d02172377b
5 changed files with 67 additions and 13 deletions

View File

@@ -3,6 +3,7 @@ using RecNet.Application.Profiles;
using RecNet.Application.Profiles.Avatar;
using RecNet.Application.Profiles.Relationships;
using RecNet.Application.Profiles.Settings;
using RecNet.Domain.Common;
namespace RecNet.Application.Common.Mapping;
@@ -10,7 +11,10 @@ public class ApplicationMappingProfile : Profile
{
public ApplicationMappingProfile()
{
CreateMap<Domain.Profiles.Profile, ProfileDTO>();
CreateMap<Domain.Profiles.Profile, ProfileDTO>()
.ForMember(
x => x.PlatformId,
opt => opt.MapFrom(x => x.Platform == PlatformType.Steamworks ? x.PlatformId : null));
CreateMap<Domain.Profiles.Avatar, AvatarDTO>();
CreateMap<Domain.Profiles.Relationship, RelationshipDTO>();
CreateMap<Domain.Profiles.PlayerSetting, PlayerSettingDTO>();

View File

@@ -1,4 +1,5 @@
using System.Text.Json.Serialization;
using RecNet.Domain.Common;
namespace RecNet.Application.Profiles;
@@ -12,4 +13,11 @@ public class ProfileDTO
[JsonPropertyName("Developer")]
public bool IsDeveloper { get; init; }
[JsonPropertyName("Platform")]
public PlatformType Platform { get; init; }
[JsonPropertyName("PlatformId")]
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public string? PlatformId { get; init; }
}

View File

@@ -21,13 +21,15 @@ public class ProfileService(
ITokenService tokenService,
IMapper mapper) : IProfileService
{
private const string ShowPlatformPreferenceKey = "SHOW_PLATFORM_PREF";
public async Task<ProfileDTO?> GetProfileAsync(Guid profileId, CancellationToken ct = default)
{
var profile = await profileRepository.GetByIdAsync(profileId, ct);
return profile is null
? null
: mapper.Map<ProfileDTO>(profile);
: await MapProfileAsync(profile, ct);
}
public async Task<IReadOnlyList<ProfileDTO>> GetProfilesAsync(
@@ -36,7 +38,11 @@ public class ProfileService(
{
var profiles = await profileRepository.GetByIdsAsync(profileIds, ct);
return mapper.Map<List<ProfileDTO>>(profiles);
var results = new List<ProfileDTO>(profiles.Count);
foreach (var profile in profiles)
results.Add(await MapProfileAsync(profile, ct));
return results;
}
public async Task<AvatarDTO?> GetAvatarAsync(Guid profileId, CancellationToken ct = default)
@@ -116,7 +122,7 @@ public class ProfileService(
var isValidGameVersion = await gameVersionRepository.IsValidAsync(command.AppVersion, ct);
if (!isValidGameVersion)
return LoginProfileResult.Fail("Invalid app version.");
return LoginProfileResult.Fail("A new update is available! Go to https://recroom.baby to download the latest update.");
// 2. Validate Platform Authentication
var validator = validators.FirstOrDefault(x => x.PlatformType == command.PlatformType);
@@ -166,9 +172,29 @@ public class ProfileService(
var token = tokenService.GenerateProfileToken(profile);
return LoginProfileResult.Success(
profile: mapper.Map<ProfileDTO>(profile),
profile: await MapProfileAsync(profile, ct),
accessToken: token.AccessToken,
expiresIn: token.ExpiresIn
);
}
}
private async Task<ProfileDTO> MapProfileAsync(ProfileEntity profile, CancellationToken ct)
{
var showPlatformPreference = profile.Platform == PlatformType.Steamworks &&
await profileRepository.GetSettingByProfileId<bool>(
profile.ProfileId,
ShowPlatformPreferenceKey,
ct);
return new ProfileDTO
{
ProfileId = profile.ProfileId,
Name = profile.Name,
IsDeveloper = profile.IsDeveloper,
Platform = profile.Platform,
PlatformId = profile.Platform == PlatformType.Steamworks && showPlatformPreference
? profile.PlatformId
: null
};
}
}

View File

@@ -7,6 +7,7 @@ public interface IProfileRepository
Task<Profile?> GetByIdAsync(Guid profileId, CancellationToken ct = default);
Task<Profile?> GetByIdWithSettingsAsync(Guid profileId, CancellationToken ct = default);
Task<Avatar?> GetAvatarByProfileIdAsync(Guid profileId, CancellationToken ct = default);
Task<T?> GetSettingByProfileId<T>(Guid profileId, string key, CancellationToken ct = default);
Task<IReadOnlyList<PlayerSetting>> GetSettingsByProfileIdAsync(Guid profileId, CancellationToken ct = default);
Task<IReadOnlyList<Profile>> GetByIdsAsync(List<Guid> profileIds, CancellationToken ct = default);

View File

@@ -1,3 +1,4 @@
using System.Text.Json;
using Microsoft.EntityFrameworkCore;
using RecNet.Domain.Common;
using RecNet.Domain.Profiles;
@@ -7,7 +8,7 @@ namespace RecNet.Infrastructure.Persistence.Repositories;
public class ProfileRepository(DatabaseContext dbContext) : IProfileRepository
{
public Task<Profile?> GetByIdAsync(
Guid profileId,
Guid profileId,
CancellationToken ct = default)
=> dbContext.Profiles
.FirstOrDefaultAsync(x => x.ProfileId == profileId, ct);
@@ -28,6 +29,20 @@ public class ProfileRepository(DatabaseContext dbContext) : IProfileRepository
.Select(x => x.Avatar)
.FirstOrDefaultAsync(ct);
public async Task<T?> GetSettingByProfileId<T>(Guid profileId, string key, CancellationToken ct = default)
{
var setting = await dbContext.PlayerSettings
.AsNoTracking()
.Where(x =>
x.UserId == profileId &&
x.Key == key)
.FirstOrDefaultAsync(ct);
if (setting == null)
return default;
return JsonSerializer.Deserialize<T>(setting.Value);
}
public async Task<IReadOnlyList<PlayerSetting>> GetSettingsByProfileIdAsync(
Guid profileId,
CancellationToken ct = default)
@@ -44,13 +59,13 @@ public class ProfileRepository(DatabaseContext dbContext) : IProfileRepository
.ToListAsync(ct);
public Task<Profile?> GetByPlatform(
PlatformType platform,
string platformId,
PlatformType platform,
string platformId,
CancellationToken ct = default)
=> dbContext.Profiles
.FirstOrDefaultAsync(x =>
x.Platform == platform &&
x.PlatformId == platformId,
x.Platform == platform &&
x.PlatformId == platformId,
ct);
public Task<Guid> GetProfileTokenVersion(
@@ -63,10 +78,10 @@ public class ProfileRepository(DatabaseContext dbContext) : IProfileRepository
.FirstOrDefaultAsync(ct);
public async Task AddAsync(
Profile profile,
Profile profile,
CancellationToken ct = default)
=> await dbContext.Profiles.AddAsync(profile, ct);
public Task SaveChangesAsync(CancellationToken ct = default)
=> dbContext.SaveChangesAsync(ct);
}
}