Changed get/set profile with more safe handling

This commit is contained in:
CakeAndBanana
2025-10-19 17:53:20 +02:00
parent bab81aaf51
commit c30190704f
2 changed files with 76 additions and 59 deletions

View File

@@ -745,23 +745,34 @@ public partial class LightlessHub
var cancellationToken = RequestAbortedToken; var cancellationToken = RequestAbortedToken;
var data = await DbContext.GroupProfiles if (dto?.Group == null)
.FirstOrDefaultAsync(g => g.Group.GID == dto.Group.GID || g.Group.Alias == dto.Group.AliasOrGID, cancellationToken)
.ConfigureAwait(false);
var profileDto = new GroupProfileDto(dto.Group, Description: null, Tags: null, PictureBase64: null, IsNsfw: false, IsDisabled: false);
if (data is not null)
{ {
_logger.LogCallInfo(LightlessHubLogger.Args($"GroupGetProfile: dto={dto?.GID}, groupProfile.Group={(data?.Group != null)}")); _logger.LogCallWarning(LightlessHubLogger.Args("GroupGetProfile: dto.Group is null"));
return new GroupProfileDto(Group: null, Description: null, Tags: null, PictureBase64: null, IsNsfw: false, IsDisabled: false);
if (data.Group != null)
{
profileDto = data.ToDTO();
}
} }
return profileDto; var data = await DbContext.GroupProfiles
.Include(gp => gp.Group)
.FirstOrDefaultAsync(
g => g.Group.GID == dto.Group.GID || g.Group.Alias == dto.Group.AliasOrGID,
cancellationToken
)
.ConfigureAwait(false);
if (data == null)
{
return new GroupProfileDto(dto.Group, Description: null, Tags: null, PictureBase64: null, IsNsfw: false, IsDisabled: false);
}
try
{
return data.ToDTO();
}
catch (Exception ex)
{
_logger.LogCallWarning(LightlessHubLogger.Args(ex, "GroupGetProfile: failed to map GroupProfileDto for {Group}", dto.Group.GID ?? dto.Group.AliasOrGID));
return new GroupProfileDto(dto.Group, Description: null, Tags: null, PictureBase64: null, IsNsfw: false, IsDisabled: false);
}
} }
[Authorize(Policy = "Identified")] [Authorize(Policy = "Identified")]
@@ -769,6 +780,8 @@ public partial class LightlessHub
{ {
_logger.LogCallInfo(LightlessHubLogger.Args(dto)); _logger.LogCallInfo(LightlessHubLogger.Args(dto));
var cancellationToken = RequestAbortedToken;
if (dto.Group == null) return; if (dto.Group == null) return;
var (hasRights, group) = await TryValidateGroupModeratorOrOwner(dto.Group.GID).ConfigureAwait(false); var (hasRights, group) = await TryValidateGroupModeratorOrOwner(dto.Group.GID).ConfigureAwait(false);
@@ -776,57 +789,39 @@ public partial class LightlessHub
var groupProfileDb = await DbContext.GroupProfiles var groupProfileDb = await DbContext.GroupProfiles
.FirstOrDefaultAsync(g => g.Group.GID == dto.Group.GID || g.Group.Alias == dto.Group.GID, .FirstOrDefaultAsync(g => g.Group.GID == dto.Group.GID || g.Group.Alias == dto.Group.GID,
RequestAbortedToken) cancellationToken)
.ConfigureAwait(false); .ConfigureAwait(false);
if (groupProfileDb != null) if (groupProfileDb == null)
{ {
var groupPairs = DbContext.GroupPairs.Where(p => p.GroupGID == groupProfileDb.GroupGID).Select(p => p.GroupUserUID).ToList(); groupProfileDb = new GroupProfile
if (string.Equals("", dto.PictureBase64, StringComparison.OrdinalIgnoreCase))
{ {
groupProfileDb.Base64GroupProfileImage = null; GroupGID = dto.Group.GID,
} ProfileDisabled = false,
else if (dto.PictureBase64 != null) IsNSFW = dto.IsNsfw ?? false,
{ };
groupProfileDb.Base64GroupProfileImage = dto.PictureBase64; groupProfileDb.UpdateProfileFromDto(dto);
} await DbContext.GroupProfiles.AddAsync(groupProfileDb, cancellationToken).ConfigureAwait(false);
if (dto.Tags != null)
{
groupProfileDb.Tags = dto.Tags;
}
if (dto.Description != null)
{
groupProfileDb.Description = dto.Description;
}
if (dto.IsNsfw != null)
{
groupProfileDb.IsNSFW = dto.IsNsfw.Value;
}
await Clients.Users(groupPairs).Client_GroupSendProfile(groupProfileDb.ToDTO()).ConfigureAwait(false);
} }
else else
{ {
var groupProfile = new GroupProfile groupProfileDb.UpdateProfileFromDto(dto);
{
GroupGID = dto.Group.GID,
Description = dto.Description,
Tags = dto.Tags,
Base64GroupProfileImage = dto.PictureBase64,
IsNSFW = false,
ProfileDisabled = false,
};
await DbContext.GroupProfiles.AddAsync(groupProfile, var userIds = await DbContext.GroupPairs
RequestAbortedToken) .Where(p => p.GroupGID == groupProfileDb.GroupGID)
.Select(p => p.GroupUserUID)
.ToListAsync(cancellationToken)
.ConfigureAwait(false); .ConfigureAwait(false);
if (userIds.Count > 0)
{
var profileDto = groupProfileDb.ToDTO();
await Clients.Users(userIds).Client_GroupSendProfile(profileDto)
.ConfigureAwait(false);
}
} }
await DbContext.SaveChangesAsync(RequestAbortedToken).ConfigureAwait(false); await DbContext.SaveChangesAsync(cancellationToken).ConfigureAwait(false);
} }
[Authorize(Policy = "Identified")] [Authorize(Policy = "Identified")]

View File

@@ -9,14 +9,27 @@ namespace LightlessSyncServer.Utils;
public static class Extensions public static class Extensions
{ {
public static void UpdateProfileFromDto(this GroupProfile profile, GroupProfileDto dto)
{
if (profile == null || dto == null) return;
profile.Base64GroupProfileImage = string.IsNullOrWhiteSpace(dto.PictureBase64) ? null : dto.PictureBase64;
if (dto.Tags != null) profile.Tags = dto.Tags;
if (dto.Description != null) profile.Description = dto.Description;
if (dto.IsNsfw.HasValue) profile.IsNSFW = dto.IsNsfw.Value;
}
public static GroupProfileDto ToDTO(this GroupProfile groupProfile) public static GroupProfileDto ToDTO(this GroupProfile groupProfile)
{ {
ArgumentNullException.ThrowIfNull(groupProfile); if (groupProfile == null)
{
return new GroupProfileDto(Group: null, Description: null, Tags: null, PictureBase64: null, IsNsfw: false, IsDisabled: false);
}
return groupProfile.Group == null var groupData = groupProfile.Group?.ToGroupData();
? throw new InvalidOperationException("GroupProfile.Group is null when converting to DTO.")
: new GroupProfileDto( return new GroupProfileDto(
groupProfile.Group.ToGroupData(), groupData,
groupProfile.Description, groupProfile.Description,
groupProfile.Tags, groupProfile.Tags,
groupProfile.Base64GroupProfileImage, groupProfile.Base64GroupProfileImage,
@@ -27,16 +40,25 @@ public static class Extensions
public static GroupData ToGroupData(this Group group) public static GroupData ToGroupData(this Group group)
{ {
if (group == null)
return null;
return new GroupData(group.GID, group.Alias, group.CreatedDate); return new GroupData(group.GID, group.Alias, group.CreatedDate);
} }
public static UserData ToUserData(this GroupPair pair) public static UserData ToUserData(this GroupPair pair)
{ {
if (pair == null)
return null;
return new UserData(pair.GroupUser.UID, pair.GroupUser.Alias); return new UserData(pair.GroupUser.UID, pair.GroupUser.Alias);
} }
public static UserData ToUserData(this User user) public static UserData ToUserData(this User user)
{ {
if (user == null)
return null;
return new UserData(user.UID, user.Alias); return new UserData(user.UID, user.Alias);
} }