Add MCDO (#31)
* rudimentary charadata first impl * update submodule * update properly * some fixes etc * add cascade and some other fixes * most of charadata impl * dotnet 9 * net9 and migration to k4os * bum papi --------- Co-authored-by: Stanley Dimant <root.darkarchon@outlook.com>
This commit is contained in:
@@ -1,5 +1,4 @@
|
||||
using MareSynchronos.API.Data.Enum;
|
||||
using MareSynchronos.API.SignalR;
|
||||
using MareSynchronos.API.SignalR;
|
||||
using MareSynchronosServer.Hubs;
|
||||
using MareSynchronosShared.Utils;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
|
||||
@@ -0,0 +1,554 @@
|
||||
using MareSynchronos.API.Data;
|
||||
using MareSynchronos.API.Dto.CharaData;
|
||||
using MareSynchronosServer.Utils;
|
||||
using MareSynchronosShared.Models;
|
||||
using MareSynchronosShared.Utils;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using System.Text;
|
||||
using System.Text.Json;
|
||||
|
||||
namespace MareSynchronosServer.Hubs;
|
||||
|
||||
public partial class MareHub
|
||||
{
|
||||
[Authorize(Policy = "Identified")]
|
||||
public async Task<CharaDataFullDto?> CharaDataCreate()
|
||||
{
|
||||
_logger.LogCallInfo();
|
||||
|
||||
int uploadCount = DbContext.CharaData.Count(c => c.UploaderUID == UserUID);
|
||||
User user = DbContext.Users.Single(u => u.UID == UserUID);
|
||||
int maximumUploads = string.IsNullOrEmpty(user.Alias) ? _maxCharaDataByUser : _maxCharaDataByUserVanity;
|
||||
if (uploadCount >= maximumUploads)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
string charaDataId = null;
|
||||
while (charaDataId == null)
|
||||
{
|
||||
charaDataId = StringUtils.GenerateRandomString(10, "abcdefghijklmnopqrstuvwxyzABCDEFHIJKLMNOPQRSTUVWXYZ");
|
||||
bool idExists = await DbContext.CharaData.AnyAsync(c => c.UploaderUID == UserUID && c.Id == charaDataId).ConfigureAwait(false);
|
||||
if (idExists)
|
||||
{
|
||||
charaDataId = null;
|
||||
}
|
||||
}
|
||||
|
||||
DateTime createdDate = DateTime.UtcNow;
|
||||
CharaData charaData = new()
|
||||
{
|
||||
Id = charaDataId,
|
||||
UploaderUID = UserUID,
|
||||
CreatedDate = createdDate,
|
||||
UpdatedDate = createdDate,
|
||||
AccessType = CharaDataAccess.Individuals,
|
||||
ShareType = CharaDataShare.Private,
|
||||
CustomizeData = string.Empty,
|
||||
GlamourerData = string.Empty,
|
||||
ExpiryDate = DateTime.MaxValue,
|
||||
Description = string.Empty,
|
||||
};
|
||||
|
||||
await DbContext.CharaData.AddAsync(charaData).ConfigureAwait(false);
|
||||
await DbContext.SaveChangesAsync().ConfigureAwait(false);
|
||||
|
||||
_logger.LogCallInfo(MareHubLogger.Args("SUCCESS", charaDataId));
|
||||
|
||||
return GetCharaDataFullDto(charaData);
|
||||
}
|
||||
|
||||
[Authorize(Policy = "Identified")]
|
||||
public async Task<bool> CharaDataDelete(string id)
|
||||
{
|
||||
var existingData = await DbContext.CharaData.SingleOrDefaultAsync(u => u.Id == id && u.UploaderUID == UserUID).ConfigureAwait(false);
|
||||
if (existingData == null)
|
||||
return false;
|
||||
|
||||
try
|
||||
{
|
||||
_logger.LogCallInfo(MareHubLogger.Args("SUCCESS", id));
|
||||
|
||||
DbContext.Remove(existingData);
|
||||
await DbContext.SaveChangesAsync().ConfigureAwait(false);
|
||||
return true;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogCallWarning(MareHubLogger.Args("FAILURE", id, ex.Message));
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
[Authorize(Policy = "Identified")]
|
||||
public async Task<CharaDataDownloadDto?> CharaDataDownload(string id)
|
||||
{
|
||||
CharaData charaData = await GetCharaDataById(id, nameof(CharaDataDownload)).ConfigureAwait(false);
|
||||
|
||||
if (!string.Equals(charaData.UploaderUID, UserUID, StringComparison.Ordinal))
|
||||
{
|
||||
charaData.DownloadCount++;
|
||||
await DbContext.SaveChangesAsync().ConfigureAwait(false);
|
||||
}
|
||||
|
||||
_logger.LogCallInfo(MareHubLogger.Args("SUCCESS", id));
|
||||
|
||||
return GetCharaDataDownloadDto(charaData);
|
||||
}
|
||||
|
||||
[Authorize(Policy = "Identified")]
|
||||
public async Task<CharaDataMetaInfoDto?> CharaDataGetMetainfo(string id)
|
||||
{
|
||||
var charaData = await GetCharaDataById(id, nameof(CharaDataGetMetainfo)).ConfigureAwait(false);
|
||||
|
||||
_logger.LogCallInfo(MareHubLogger.Args("SUCCESS", id));
|
||||
|
||||
return GetCharaDataMetaInfoDto(charaData);
|
||||
}
|
||||
|
||||
[Authorize(Policy = "Identified")]
|
||||
public async Task<List<CharaDataFullDto>> CharaDataGetOwn()
|
||||
{
|
||||
var ownCharaData = await DbContext.CharaData
|
||||
.Include(u => u.Files)
|
||||
.Include(u => u.FileSwaps)
|
||||
.Include(u => u.OriginalFiles)
|
||||
.Include(u => u.AllowedIndividiuals)
|
||||
.ThenInclude(u => u.AllowedUser)
|
||||
.Include(u => u.Poses)
|
||||
.AsSplitQuery()
|
||||
.Where(c => c.UploaderUID == UserUID).ToListAsync().ConfigureAwait(false);
|
||||
_logger.LogCallInfo(MareHubLogger.Args("SUCCESS"));
|
||||
return [.. ownCharaData.Select(GetCharaDataFullDto)];
|
||||
}
|
||||
|
||||
[Authorize(Policy = "Identified")]
|
||||
public async Task<List<CharaDataMetaInfoDto>> CharaDataGetShared()
|
||||
{
|
||||
_logger.LogCallInfo();
|
||||
|
||||
var allPairs = await GetAllPairInfo(UserUID).ConfigureAwait(false);
|
||||
List<CharaData> sharedCharaData = [];
|
||||
foreach (var pair in allPairs.Where(p => !p.Value.OwnPermissions.IsPaused && !p.Value.OtherPermissions.IsPaused))
|
||||
{
|
||||
var allSharedDataByPair = await DbContext.CharaData
|
||||
.Include(u => u.Files)
|
||||
.Include(u => u.OriginalFiles)
|
||||
.Include(u => u.AllowedIndividiuals)
|
||||
.Include(u => u.Poses)
|
||||
.Include(u => u.Uploader)
|
||||
.Where(p => p.ShareType == CharaDataShare.Shared && p.UploaderUID == pair.Key)
|
||||
.AsSplitQuery()
|
||||
.AsNoTracking()
|
||||
.ToListAsync()
|
||||
.ConfigureAwait(false);
|
||||
|
||||
foreach (var charaData in allSharedDataByPair)
|
||||
{
|
||||
if (await CheckCharaDataAllowance(charaData).ConfigureAwait(false))
|
||||
{
|
||||
sharedCharaData.Add(charaData);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var charaDataDirectlyShared = await DbContext.CharaData.Include(u => u.Files)
|
||||
.Include(u => u.OriginalFiles)
|
||||
.Include(u => u.AllowedIndividiuals)
|
||||
.Include(u => u.Poses)
|
||||
.Include(u => u.Uploader)
|
||||
.Where(p => p.ShareType == CharaDataShare.Shared && p.AllowedIndividiuals.Any(u => u.AllowedUserUID == UserUID))
|
||||
.AsSplitQuery()
|
||||
.AsNoTracking()
|
||||
.ToListAsync()
|
||||
.ConfigureAwait(false);
|
||||
|
||||
foreach (var data in charaDataDirectlyShared)
|
||||
{
|
||||
if (sharedCharaData.Exists(d => string.Equals(d.Id, data.Id, StringComparison.Ordinal)
|
||||
&& string.Equals(d.UploaderUID, d.UploaderUID, StringComparison.Ordinal)))
|
||||
continue;
|
||||
sharedCharaData.Add(data);
|
||||
}
|
||||
|
||||
_logger.LogCallInfo(MareHubLogger.Args("SUCCESS", sharedCharaData.Count));
|
||||
|
||||
return [.. sharedCharaData.Select(GetCharaDataMetaInfoDto)];
|
||||
}
|
||||
|
||||
[Authorize(Policy = "Identified")]
|
||||
public async Task<CharaDataFullDto?> CharaDataUpdate(CharaDataUpdateDto updateDto)
|
||||
{
|
||||
var charaData = await DbContext.CharaData
|
||||
.Include(u => u.Files)
|
||||
.Include(u => u.OriginalFiles)
|
||||
.Include(u => u.AllowedIndividiuals)
|
||||
.ThenInclude(u => u.AllowedUser)
|
||||
.Include(u => u.FileSwaps)
|
||||
.Include(u => u.Poses)
|
||||
.AsSplitQuery()
|
||||
.SingleOrDefaultAsync(u => u.Id == updateDto.Id && u.UploaderUID == UserUID).ConfigureAwait(false);
|
||||
|
||||
if (charaData == null)
|
||||
return null;
|
||||
|
||||
bool anyChanges = false;
|
||||
|
||||
if (updateDto.Description != null)
|
||||
{
|
||||
charaData.Description = updateDto.Description;
|
||||
anyChanges = true;
|
||||
}
|
||||
|
||||
if (updateDto.ExpiryDate != null)
|
||||
{
|
||||
charaData.ExpiryDate = updateDto.ExpiryDate;
|
||||
anyChanges = true;
|
||||
}
|
||||
|
||||
if (updateDto.GlamourerData != null)
|
||||
{
|
||||
charaData.GlamourerData = updateDto.GlamourerData;
|
||||
anyChanges = true;
|
||||
}
|
||||
|
||||
if (updateDto.CustomizeData != null)
|
||||
{
|
||||
charaData.CustomizeData = updateDto.CustomizeData;
|
||||
anyChanges = true;
|
||||
}
|
||||
|
||||
if (updateDto.ManipulationData != null)
|
||||
{
|
||||
charaData.ManipulationData = updateDto.ManipulationData;
|
||||
anyChanges = true;
|
||||
}
|
||||
|
||||
if (updateDto.AccessType != null)
|
||||
{
|
||||
charaData.AccessType = GetAccessType(updateDto.AccessType.Value);
|
||||
anyChanges = true;
|
||||
}
|
||||
|
||||
if (updateDto.ShareType != null)
|
||||
{
|
||||
charaData.ShareType = GetShareType(updateDto.ShareType.Value);
|
||||
anyChanges = true;
|
||||
}
|
||||
|
||||
if (updateDto.AllowedUsers != null)
|
||||
{
|
||||
var individuals = charaData.AllowedIndividiuals.ToList();
|
||||
charaData.AllowedIndividiuals.Clear();
|
||||
DbContext.RemoveRange(individuals);
|
||||
var allowedUserList = updateDto.AllowedUsers.ToList();
|
||||
foreach (var user in updateDto.AllowedUsers)
|
||||
{
|
||||
var dbUser = await DbContext.Users.SingleOrDefaultAsync(u => u.UID == user || u.Alias == user).ConfigureAwait(false);
|
||||
if (dbUser != null)
|
||||
{
|
||||
if (!allowedUserList.Contains(dbUser.UID, StringComparer.Ordinal) && !allowedUserList.Contains(dbUser.Alias, StringComparer.Ordinal))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
allowedUserList.RemoveAll(u => string.Equals(u, dbUser.UID, StringComparison.Ordinal));
|
||||
allowedUserList.RemoveAll(u => string.Equals(u, dbUser.Alias, StringComparison.Ordinal));
|
||||
|
||||
charaData.AllowedIndividiuals.Add(new CharaDataAllowance()
|
||||
{
|
||||
AllowedUser = dbUser,
|
||||
Parent = charaData
|
||||
});
|
||||
}
|
||||
}
|
||||
anyChanges = true;
|
||||
}
|
||||
|
||||
if (updateDto.FileGamePaths != null)
|
||||
{
|
||||
var originalFiles = charaData.OriginalFiles.ToList();
|
||||
charaData.OriginalFiles.Clear();
|
||||
DbContext.RemoveRange(originalFiles);
|
||||
var files = charaData.Files.ToList();
|
||||
charaData.Files.Clear();
|
||||
DbContext.RemoveRange(files);
|
||||
foreach (var file in updateDto.FileGamePaths)
|
||||
{
|
||||
charaData.Files.Add(new CharaDataFile()
|
||||
{
|
||||
FileCacheHash = file.HashOrFileSwap,
|
||||
GamePath = file.GamePath,
|
||||
Parent = charaData
|
||||
});
|
||||
|
||||
charaData.OriginalFiles.Add(new CharaDataOriginalFile()
|
||||
{
|
||||
Hash = file.HashOrFileSwap,
|
||||
Parent = charaData,
|
||||
GamePath = file.GamePath
|
||||
});
|
||||
}
|
||||
|
||||
anyChanges = true;
|
||||
}
|
||||
|
||||
if (updateDto.FileSwaps != null)
|
||||
{
|
||||
var fileSwaps = charaData.FileSwaps.ToList();
|
||||
charaData.FileSwaps.Clear();
|
||||
DbContext.RemoveRange(fileSwaps);
|
||||
foreach (var file in updateDto.FileSwaps)
|
||||
{
|
||||
charaData.FileSwaps.Add(new CharaDataFileSwap()
|
||||
{
|
||||
FilePath = file.HashOrFileSwap,
|
||||
GamePath = file.GamePath,
|
||||
Parent = charaData
|
||||
});
|
||||
}
|
||||
|
||||
anyChanges = true;
|
||||
}
|
||||
|
||||
if (updateDto.Poses != null)
|
||||
{
|
||||
foreach (var pose in updateDto.Poses)
|
||||
{
|
||||
if (pose.Id == null)
|
||||
{
|
||||
charaData.Poses.Add(new CharaDataPose()
|
||||
{
|
||||
Description = pose.Description,
|
||||
Parent = charaData,
|
||||
ParentUploaderUID = UserUID,
|
||||
PoseData = pose.PoseData,
|
||||
WorldData = pose.WorldData == null ? string.Empty : JsonSerializer.Serialize(pose.WorldData),
|
||||
});
|
||||
|
||||
anyChanges = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
var associatedPose = charaData.Poses.FirstOrDefault(p => p.Id == pose.Id);
|
||||
if (associatedPose == null)
|
||||
continue;
|
||||
|
||||
if (pose.Description == null && pose.PoseData == null && pose.WorldData == null)
|
||||
{
|
||||
charaData.Poses.Remove(associatedPose);
|
||||
DbContext.Remove(associatedPose);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (pose.Description != null)
|
||||
associatedPose.Description = pose.Description;
|
||||
if (pose.WorldData != null)
|
||||
{
|
||||
if (pose.WorldData.Value == default) associatedPose.WorldData = string.Empty;
|
||||
else associatedPose.WorldData = JsonSerializer.Serialize(pose.WorldData.Value);
|
||||
}
|
||||
if (pose.PoseData != null)
|
||||
associatedPose.PoseData = pose.PoseData;
|
||||
}
|
||||
|
||||
anyChanges = true;
|
||||
}
|
||||
|
||||
var overflowingPoses = charaData.Poses.Skip(10).ToList();
|
||||
foreach (var overflowing in overflowingPoses)
|
||||
{
|
||||
charaData.Poses.Remove(overflowing);
|
||||
DbContext.Remove(overflowing);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (anyChanges)
|
||||
{
|
||||
charaData.UpdatedDate = DateTime.UtcNow;
|
||||
await DbContext.SaveChangesAsync().ConfigureAwait(false);
|
||||
_logger.LogCallInfo(MareHubLogger.Args("SUCCESS", anyChanges));
|
||||
}
|
||||
|
||||
return GetCharaDataFullDto(charaData);
|
||||
}
|
||||
|
||||
private static CharaDataAccess GetAccessType(AccessTypeDto dataAccess) => dataAccess switch
|
||||
{
|
||||
AccessTypeDto.Public => CharaDataAccess.Public,
|
||||
AccessTypeDto.AllPairs => CharaDataAccess.AllPairs,
|
||||
AccessTypeDto.ClosePairs => CharaDataAccess.ClosePairs,
|
||||
AccessTypeDto.Individuals => CharaDataAccess.Individuals,
|
||||
_ => throw new NotSupportedException(),
|
||||
};
|
||||
|
||||
private static AccessTypeDto GetAccessTypeDto(CharaDataAccess dataAccess) => dataAccess switch
|
||||
{
|
||||
CharaDataAccess.Public => AccessTypeDto.Public,
|
||||
CharaDataAccess.AllPairs => AccessTypeDto.AllPairs,
|
||||
CharaDataAccess.ClosePairs => AccessTypeDto.ClosePairs,
|
||||
CharaDataAccess.Individuals => AccessTypeDto.Individuals,
|
||||
_ => throw new NotSupportedException(),
|
||||
};
|
||||
|
||||
private static CharaDataDownloadDto GetCharaDataDownloadDto(CharaData charaData)
|
||||
{
|
||||
return new CharaDataDownloadDto(charaData.Id, charaData.Uploader.ToUserData())
|
||||
{
|
||||
CustomizeData = charaData.CustomizeData,
|
||||
Description = charaData.Description,
|
||||
FileGamePaths = charaData.Files.Select(k => new GamePathEntry(k.FileCacheHash, k.GamePath)).ToList(),
|
||||
GlamourerData = charaData.GlamourerData,
|
||||
FileSwaps = charaData.FileSwaps.Select(k => new GamePathEntry(k.FilePath, k.GamePath)).ToList(),
|
||||
ManipulationData = charaData.ManipulationData,
|
||||
};
|
||||
}
|
||||
|
||||
private CharaDataFullDto GetCharaDataFullDto(CharaData charaData)
|
||||
{
|
||||
return new CharaDataFullDto(charaData.Id, new(UserUID))
|
||||
{
|
||||
AccessType = GetAccessTypeDto(charaData.AccessType),
|
||||
ShareType = GetShareTypeDto(charaData.ShareType),
|
||||
AllowedUsers = [.. charaData.AllowedIndividiuals.Select(u => new UserData(u.AllowedUser.UID, u.AllowedUser.Alias))],
|
||||
CustomizeData = charaData.CustomizeData,
|
||||
Description = charaData.Description,
|
||||
ExpiryDate = charaData.ExpiryDate ?? DateTime.MaxValue,
|
||||
OriginalFiles = charaData.OriginalFiles.Select(k => new GamePathEntry(k.Hash, k.GamePath)).ToList(),
|
||||
FileGamePaths = charaData.Files.Select(k => new GamePathEntry(k.FileCacheHash, k.GamePath)).ToList(),
|
||||
FileSwaps = charaData.FileSwaps.Select(k => new GamePathEntry(k.FilePath, k.GamePath)).ToList(),
|
||||
GlamourerData = charaData.GlamourerData,
|
||||
CreatedDate = charaData.CreatedDate,
|
||||
UpdatedDate = charaData.UpdatedDate,
|
||||
ManipulationData = charaData.ManipulationData,
|
||||
DownloadCount = charaData.DownloadCount,
|
||||
PoseData = [.. charaData.Poses.OrderBy(p => p.Id).Select(k =>
|
||||
{
|
||||
WorldData data = default;
|
||||
|
||||
if(!string.IsNullOrEmpty(k.WorldData)) data = JsonSerializer.Deserialize<WorldData>(k.WorldData);
|
||||
return new PoseEntry(k.Id)
|
||||
{
|
||||
Description = k.Description,
|
||||
PoseData = k.PoseData,
|
||||
WorldData = data
|
||||
};
|
||||
})],
|
||||
};
|
||||
}
|
||||
|
||||
private static CharaDataMetaInfoDto GetCharaDataMetaInfoDto(CharaData charaData)
|
||||
{
|
||||
var allOrigHashes = charaData.OriginalFiles.Select(k => k.Hash).ToList();
|
||||
var allFileHashes = charaData.Files.Select(f => f.FileCacheHash).ToList();
|
||||
var allHashesPresent = allOrigHashes.TrueForAll(h => allFileHashes.Contains(h, StringComparer.Ordinal));
|
||||
var canBeDownloaded = allHashesPresent &= !string.IsNullOrEmpty(charaData.GlamourerData);
|
||||
return new CharaDataMetaInfoDto(charaData.Id, charaData.Uploader.ToUserData())
|
||||
{
|
||||
CanBeDownloaded = canBeDownloaded,
|
||||
Description = charaData.Description,
|
||||
UpdatedDate = charaData.UpdatedDate,
|
||||
PoseData = [.. charaData.Poses.OrderBy(p => p.Id).Select(k =>
|
||||
{
|
||||
WorldData data = default;
|
||||
if(!string.IsNullOrEmpty(k.WorldData)) data = JsonSerializer.Deserialize<WorldData>(k.WorldData);
|
||||
return new PoseEntry(k.Id)
|
||||
{
|
||||
Description = k.Description,
|
||||
PoseData = k.PoseData,
|
||||
WorldData = data
|
||||
};
|
||||
})],
|
||||
};
|
||||
}
|
||||
|
||||
private static CharaDataShare GetShareType(ShareTypeDto dataShare) => dataShare switch
|
||||
{
|
||||
ShareTypeDto.Shared => CharaDataShare.Shared,
|
||||
ShareTypeDto.Private => CharaDataShare.Private,
|
||||
_ => throw new NotSupportedException(),
|
||||
};
|
||||
|
||||
private static ShareTypeDto GetShareTypeDto(CharaDataShare dataShare) => dataShare switch
|
||||
{
|
||||
CharaDataShare.Shared => ShareTypeDto.Shared,
|
||||
CharaDataShare.Private => ShareTypeDto.Private,
|
||||
_ => throw new NotSupportedException(),
|
||||
};
|
||||
|
||||
private async Task<bool> CheckCharaDataAllowance(CharaData charaData)
|
||||
{
|
||||
// check for self
|
||||
if (string.Equals(charaData.UploaderUID, UserUID, StringComparison.Ordinal))
|
||||
return true;
|
||||
|
||||
// check for public access
|
||||
if (charaData.AccessType == CharaDataAccess.Public)
|
||||
return true;
|
||||
|
||||
// check for individuals
|
||||
if (charaData.AllowedIndividiuals.Any(u => string.Equals(u.AllowedUserUID, UserUID, StringComparison.Ordinal)))
|
||||
return true;
|
||||
|
||||
var pairInfoUploader = await GetAllPairInfo(charaData.UploaderUID).ConfigureAwait(false);
|
||||
|
||||
// check for all pairs
|
||||
if (charaData.AccessType == CharaDataAccess.AllPairs)
|
||||
{
|
||||
if (pairInfoUploader.TryGetValue(UserUID, out var userInfo) && userInfo.IsSynced && !userInfo.OwnPermissions.IsPaused && !userInfo.OtherPermissions.IsPaused)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
// check for individual pairs
|
||||
if (charaData.AccessType == CharaDataAccess.ClosePairs)
|
||||
{
|
||||
if (pairInfoUploader.TryGetValue(UserUID, out var userInfo) && userInfo.IsSynced && !userInfo.OwnPermissions.IsPaused && !userInfo.OtherPermissions.IsPaused
|
||||
&& userInfo.IndividuallyPaired)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private async Task<CharaData> GetCharaDataById(string id, string methodName)
|
||||
{
|
||||
var splitid = id.Split(":", StringSplitOptions.None);
|
||||
if (splitid.Length != 2)
|
||||
{
|
||||
_logger.LogCallWarning(MareHubLogger.Args("INVALID", id));
|
||||
throw new InvalidOperationException($"Id {id} not in expected format");
|
||||
}
|
||||
|
||||
var charaData = await DbContext.CharaData
|
||||
.Include(u => u.Files)
|
||||
.Include(u => u.FileSwaps)
|
||||
.Include(u => u.AllowedIndividiuals)
|
||||
.Include(u => u.Poses)
|
||||
.Include(u => u.Uploader)
|
||||
.AsSplitQuery()
|
||||
.SingleOrDefaultAsync(c => c.Id == splitid[1] && c.UploaderUID == splitid[0]).ConfigureAwait(false);
|
||||
|
||||
if (charaData == null)
|
||||
{
|
||||
_logger.LogCallWarning(MareHubLogger.Args("NOT FOUND", id));
|
||||
throw new InvalidDataException($"No chara data with {id} found");
|
||||
}
|
||||
|
||||
if (!await CheckCharaDataAllowance(charaData).ConfigureAwait(false))
|
||||
{
|
||||
_logger.LogCallWarning(MareHubLogger.Args("UNAUTHORIZED", id));
|
||||
throw new UnauthorizedAccessException($"User is not allowed to download {id}");
|
||||
}
|
||||
|
||||
return charaData;
|
||||
}
|
||||
}
|
||||
@@ -2,12 +2,10 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using MareSynchronosServer.Utils;
|
||||
using MareSynchronosShared.Utils;
|
||||
using Microsoft.IdentityModel.Tokens;
|
||||
using MareSynchronos.API.Data;
|
||||
using MareSynchronos.API.Dto.Group;
|
||||
using MareSynchronosShared.Metrics;
|
||||
using Microsoft.AspNetCore.SignalR;
|
||||
using MareSynchronosShared.Data;
|
||||
|
||||
namespace MareSynchronosServer.Hubs;
|
||||
|
||||
|
||||
@@ -37,6 +37,8 @@ public partial class MareHub : Hub<IMareHub>, IMareHub
|
||||
private readonly Version _expectedClientVersion;
|
||||
private readonly Lazy<MareDbContext> _dbContextLazy;
|
||||
private MareDbContext DbContext => _dbContextLazy.Value;
|
||||
private readonly int _maxCharaDataByUser;
|
||||
private readonly int _maxCharaDataByUserVanity;
|
||||
|
||||
public MareHub(MareMetrics mareMetrics,
|
||||
IDbContextFactory<MareDbContext> mareDbContextFactory, ILogger<MareHub> logger, SystemInfoService systemInfoService,
|
||||
@@ -51,6 +53,8 @@ public partial class MareHub : Hub<IMareHub>, IMareHub
|
||||
_maxGroupUserCount = configuration.GetValueOrDefault(nameof(ServerConfiguration.MaxGroupUserCount), 100);
|
||||
_fileServerAddress = configuration.GetValue<Uri>(nameof(ServerConfiguration.CdnFullUrl));
|
||||
_expectedClientVersion = configuration.GetValueOrDefault(nameof(ServerConfiguration.ExpectedClientVersion), new Version(0, 0, 0));
|
||||
_maxCharaDataByUser = configuration.GetValueOrDefault(nameof(ServerConfiguration.MaxCharaDataByUser), 10);
|
||||
_maxCharaDataByUserVanity = configuration.GetValueOrDefault(nameof(ServerConfiguration.MaxCharaDataByUserVanity), 50);
|
||||
_contextAccessor = contextAccessor;
|
||||
_redis = redisDb;
|
||||
_onlineSyncedPairCacheService = onlineSyncedPairCacheService;
|
||||
@@ -109,6 +113,8 @@ public partial class MareHub : Hub<IMareHub>, IMareHub
|
||||
MaxGroupsJoinedByUser = _maxJoinedGroupsByUser,
|
||||
MaxGroupUserCount = _maxGroupUserCount,
|
||||
FileServerAddress = _fileServerAddress,
|
||||
MaxCharaData = _maxCharaDataByUser,
|
||||
MaxCharaDataVanity = _maxCharaDataByUserVanity,
|
||||
},
|
||||
DefaultPreferredPermissions = new DefaultPermissionsDto()
|
||||
{
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk.Web">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<TargetFramework>net9.0</TargetFramework>
|
||||
<UserSecretsId>aspnet-MareSynchronosServer-BA82A12A-0B30-463C-801D-B7E81318CD50</UserSecretsId>
|
||||
<AssemblyVersion>1.1.0.0</AssemblyVersion>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
@@ -25,14 +25,12 @@
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="lz4net" Version="1.0.15.93" />
|
||||
<PackageReference Include="Meziantou.Analyzer" Version="2.0.176">
|
||||
<PackageReference Include="Meziantou.Analyzer" Version="2.0.184">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="Microsoft.Extensions.Hosting.Systemd" Version="8.0.1" />
|
||||
<PackageReference Include="Microsoft.IdentityModel.Tokens" Version="8.1.2" />
|
||||
<PackageReference Include="SixLabors.ImageSharp" Version="3.1.5" />
|
||||
<PackageReference Include="Microsoft.Extensions.Hosting.Systemd" Version="9.0.0" />
|
||||
<PackageReference Include="SixLabors.ImageSharp" Version="3.1.6" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
||||
@@ -0,0 +1,50 @@
|
||||
using MareSynchronosShared.Data;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace MareSynchronosServer.Services;
|
||||
|
||||
public class CharaDataCleanupService : IHostedService
|
||||
{
|
||||
private readonly ILogger<CharaDataCleanupService> _logger;
|
||||
private readonly IDbContextFactory<MareDbContext> _dbContextFactory;
|
||||
private readonly CancellationTokenSource _cleanupCts = new();
|
||||
|
||||
public CharaDataCleanupService(ILogger<CharaDataCleanupService> logger, IDbContextFactory<MareDbContext> dbContextFactory)
|
||||
{
|
||||
_logger = logger;
|
||||
_dbContextFactory = dbContextFactory;
|
||||
}
|
||||
|
||||
public Task StartAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
_ = Cleanup(cancellationToken);
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
private async Task Cleanup(CancellationToken ct)
|
||||
{
|
||||
_logger.LogInformation("CharaData Cleanup Service started");
|
||||
while (!ct.IsCancellationRequested)
|
||||
{
|
||||
using (var db = await _dbContextFactory.CreateDbContextAsync(ct).ConfigureAwait(false))
|
||||
{
|
||||
var dateTime = DateTime.UtcNow;
|
||||
var expiredData = await db.CharaData.Where(c => c.ExpiryDate <= DateTime.UtcNow).ToListAsync(cancellationToken: ct).ConfigureAwait(false);
|
||||
|
||||
_logger.LogInformation("Removing {count} expired Chara Data entries", expiredData.Count);
|
||||
|
||||
db.RemoveRange(expiredData);
|
||||
await db.SaveChangesAsync(ct).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
await Task.Delay(TimeSpan.FromHours(12), ct).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
|
||||
public Task StopAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
_cleanupCts?.Cancel();
|
||||
_cleanupCts?.Dispose();
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
}
|
||||
@@ -3,7 +3,6 @@ using Microsoft.VisualBasic.FileIO;
|
||||
using Prometheus;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Globalization;
|
||||
using System.Text.Json;
|
||||
|
||||
namespace MareSynchronosServer.Services;
|
||||
|
||||
|
||||
@@ -12,15 +12,15 @@ public class UserCleanupService : IHostedService
|
||||
{
|
||||
private readonly MareMetrics metrics;
|
||||
private readonly ILogger<UserCleanupService> _logger;
|
||||
private readonly IServiceProvider _services;
|
||||
private readonly IDbContextFactory<MareDbContext> _mareDbContextFactory;
|
||||
private readonly IConfigurationService<ServerConfiguration> _configuration;
|
||||
private CancellationTokenSource _cleanupCts;
|
||||
|
||||
public UserCleanupService(MareMetrics metrics, ILogger<UserCleanupService> logger, IServiceProvider services, IConfigurationService<ServerConfiguration> configuration)
|
||||
public UserCleanupService(MareMetrics metrics, ILogger<UserCleanupService> logger, IDbContextFactory<MareDbContext> mareDbContextFactory, IConfigurationService<ServerConfiguration> configuration)
|
||||
{
|
||||
this.metrics = metrics;
|
||||
_logger = logger;
|
||||
_services = services;
|
||||
_mareDbContextFactory = mareDbContextFactory;
|
||||
_configuration = configuration;
|
||||
}
|
||||
|
||||
@@ -38,16 +38,17 @@ public class UserCleanupService : IHostedService
|
||||
{
|
||||
while (!ct.IsCancellationRequested)
|
||||
{
|
||||
using var scope = _services.CreateScope();
|
||||
using var dbContext = scope.ServiceProvider.GetService<MareDbContext>()!;
|
||||
using (var dbContext = await _mareDbContextFactory.CreateDbContextAsync(ct).ConfigureAwait(false))
|
||||
{
|
||||
|
||||
CleanUpOutdatedLodestoneAuths(dbContext);
|
||||
CleanUpOutdatedLodestoneAuths(dbContext);
|
||||
|
||||
await PurgeUnusedAccounts(dbContext).ConfigureAwait(false);
|
||||
await PurgeUnusedAccounts(dbContext).ConfigureAwait(false);
|
||||
|
||||
await PurgeTempInvites(dbContext).ConfigureAwait(false);
|
||||
await PurgeTempInvites(dbContext).ConfigureAwait(false);
|
||||
|
||||
dbContext.SaveChanges();
|
||||
dbContext.SaveChanges();
|
||||
}
|
||||
|
||||
var now = DateTime.Now;
|
||||
TimeOnly currentTime = new(now.Hour, now.Minute, now.Second);
|
||||
|
||||
@@ -101,6 +101,8 @@ public class Startup
|
||||
{
|
||||
services.AddSingleton<UserCleanupService>();
|
||||
services.AddHostedService(provider => provider.GetService<UserCleanupService>());
|
||||
services.AddSingleton<CharaDataCleanupService>();
|
||||
services.AddHostedService(provider => provider.GetService<CharaDataCleanupService>());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user