Refactor serialization system (#702)

* Initial work for serialization refactor

* Experiment with new naming conventions

* Mostly implement user and slot serialization.
Still needs to be fine tuned to match original implementation
Many things are left in a broken state like website features/api endpoints/lbp3 categories

* Fix release building

* Migrate scores, reviews, and more to new serialization system.
Many things are still broken but progress is steadily being made

* Fix Api responses and migrate serialization for most types

* Make serialization better and fix bugs
Fix recursive PrepareSerialization when recursive item is set during root item's PrepareSerialization, items, should be properly indexed in order but it's only tested to 1 level of recursion

* Fix review serialization

* Fix user serialization producing malformed SQL query

* Remove DefaultIfEmpty query

* MariaDB doesn't like double nested queries

* Fix LBP1 tag counter

* Implement lbp3 categories and add better deserialization handling

* Implement expression tree caching to speed up reflection and write new serializer tests

* Remove Game column from UserEntity and rename DatabaseContextModelSnapshot.cs back to DatabaseModelSnapshot.cs

* Make UserEntity username not required

* Fix recursive serialization of lists and add relevant unit tests

* Actually commit the migration

* Fix LocationTests to use new deserialization class

* Fix comments not serializing the right author username

* Replace all occurrences of StatusCode with their respective ASP.NET named result
instead of StatusCode(403) everything is now in the form of Forbid()

* Fix SlotBase.ConvertToEntity and LocationTests

* Fix compilation error

* Give Location a default value in GameUserSlot and GameUser

* Reimplement stubbed website functions

* Convert grief reports to new serialization system

* Update DatabaseModelSnapshot and bump dotnet tool version

* Remove unused directives

* Fix broken type reference

* Fix rated comments on website

* Don't include banned users in website comments

* Optimize score submission

* Fix slot id calculating in in-game comment posting

* Move serialization interfaces to types folder and add more documentation

* Allow uploading of versus scores
This commit is contained in:
Josh 2023-03-27 19:39:54 -05:00 committed by GitHub
parent 307b2135a3
commit 329ab66043
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
248 changed files with 4993 additions and 2896 deletions

View file

@ -3,13 +3,13 @@ using LBPUnion.ProjectLighthouse.Configuration;
using LBPUnion.ProjectLighthouse.Database;
using LBPUnion.ProjectLighthouse.Extensions;
using LBPUnion.ProjectLighthouse.Logging;
using LBPUnion.ProjectLighthouse.Serialization;
using LBPUnion.ProjectLighthouse.Servers.GameServer.Types.Categories;
using LBPUnion.ProjectLighthouse.Types.Entities.Level;
using LBPUnion.ProjectLighthouse.Types.Entities.Profile;
using LBPUnion.ProjectLighthouse.Types.Entities.Token;
using LBPUnion.ProjectLighthouse.Types.Levels;
using LBPUnion.ProjectLighthouse.Types.Logging;
using LBPUnion.ProjectLighthouse.Types.Serialization;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
@ -32,16 +32,18 @@ public class CollectionController : ControllerBase
[HttpGet("playlists/{playlistId:int}/slots")]
public async Task<IActionResult> GetPlaylistSlots(int playlistId)
{
Playlist? targetPlaylist = await this.database.Playlists.FirstOrDefaultAsync(p => p.PlaylistId == playlistId);
PlaylistEntity? targetPlaylist = await this.database.Playlists.FirstOrDefaultAsync(p => p.PlaylistId == playlistId);
if (targetPlaylist == null) return this.BadRequest();
IQueryable<Slot> slots = this.database.Slots.Include(s => s.Creator)
.Where(s => targetPlaylist.SlotIds.Contains(s.SlotId));
GameTokenEntity token = this.GetToken();
List<SlotBase> slots = await this.database.Slots.Where(s => targetPlaylist.SlotIds.Contains(s.SlotId))
.Select(s => SlotBase.CreateFromEntity(s, token))
.ToListAsync();
string response = Enumerable.Aggregate(slots, string.Empty, (current, slot) => current + slot.Serialize());
int total = targetPlaylist.SlotIds.Length;
return this.Ok(LbpSerializer.TaggedStringElement("slots", response, "total", total));
return this.Ok(new GenericSlotResponse(slots, total, 0));
}
[HttpPost("playlists/{playlistId:int}")]
@ -50,9 +52,9 @@ public class CollectionController : ControllerBase
[HttpPost("playlists/{playlistId:int}/order_slots")]
public async Task<IActionResult> UpdatePlaylist(int playlistId, int slotId)
{
GameToken token = this.GetToken();
GameTokenEntity token = this.GetToken();
Playlist? targetPlaylist = await this.database.Playlists.FirstOrDefaultAsync(p => p.PlaylistId == playlistId);
PlaylistEntity? targetPlaylist = await this.database.Playlists.FirstOrDefaultAsync(p => p.PlaylistId == playlistId);
if (targetPlaylist == null) return this.BadRequest();
if (token.UserId != targetPlaylist.CreatorId) return this.BadRequest();
@ -65,7 +67,7 @@ public class CollectionController : ControllerBase
return this.Ok(this.GetUserPlaylists(token.UserId));
}
Playlist? newPlaylist = await this.DeserializeBody<Playlist>("playlist", "levels");
GamePlaylist? newPlaylist = await this.DeserializeBody<GamePlaylist>("playlist", "levels");
if (newPlaylist == null) return this.BadRequest();
@ -97,41 +99,48 @@ public class CollectionController : ControllerBase
return this.Ok(this.GetUserPlaylists(token.UserId));
}
private string GetUserPlaylists(int userId)
private async Task<PlaylistResponse> GetUserPlaylists(int userId)
{
string response = Enumerable.Aggregate(
this.database.Playlists.Include(p => p.Creator).Where(p => p.CreatorId == userId),
string.Empty,
(current, slot) => current + slot.Serialize());
List<GamePlaylist> playlists = await this.database.Playlists.Include(p => p.Creator)
.Where(p => p.CreatorId == userId)
.Select(p => GamePlaylist.CreateFromEntity(p))
.ToListAsync();
int total = this.database.Playlists.Count(p => p.CreatorId == userId);
return LbpSerializer.TaggedStringElement("playlists", response, new Dictionary<string, object>
return new PlaylistResponse
{
{"total", total},
{"hint_start", total+1},
});
Playlists = playlists,
Total = total,
HintStart = total+1,
};
}
[HttpPost("playlists")]
public async Task<IActionResult> CreatePlaylist()
{
GameToken token = this.GetToken();
GameTokenEntity token = this.GetToken();
int playlistCount = await this.database.Playlists.CountAsync(p => p.CreatorId == token.UserId);
if (playlistCount > ServerConfiguration.Instance.UserGeneratedContentLimits.ListsQuota) return this.BadRequest();
Playlist? playlist = await this.DeserializeBody<Playlist>("playlist");
GamePlaylist? playlist = await this.DeserializeBody<GamePlaylist>("playlist");
if (playlist == null) return this.BadRequest();
playlist.CreatorId = token.UserId;
PlaylistEntity playlistEntity = new()
{
CreatorId = token.UserId,
Description = playlist.Description,
Name = playlist.Name,
SlotIds = playlist.SlotIds,
};
this.database.Playlists.Add(playlist);
this.database.Playlists.Add(playlistEntity);
await this.database.SaveChangesAsync();
return this.Ok(this.GetUserPlaylists(token.UserId));
return this.Ok(await this.GetUserPlaylists(token.UserId));
}
[HttpGet("user/{username}/playlists")]
@ -140,101 +149,60 @@ public class CollectionController : ControllerBase
int targetUserId = await this.database.UserIdFromUsername(username);
if (targetUserId == 0) return this.BadRequest();
return this.Ok(this.GetUserPlaylists(targetUserId));
return this.Ok(await this.GetUserPlaylists(targetUserId));
}
[HttpGet("searches")]
[HttpGet("genres")]
public async Task<IActionResult> GenresAndSearches()
{
GameToken token = this.GetToken();
GameTokenEntity token = this.GetToken();
User? user = await this.database.UserFromGameToken(token);
if (user == null) return this.StatusCode(403, "");
UserEntity? user = await this.database.UserFromGameToken(token);
if (user == null) return this.Forbid();
string categoriesSerialized = CategoryHelper.Categories.Aggregate
(
string.Empty,
(current, category) =>
{
string serialized;
List<GameCategory> categories = new();
if (category is CategoryWithUser categoryWithUser) serialized = categoryWithUser.Serialize(this.database, user);
else serialized = category.Serialize(this.database);
foreach (Category category in CategoryHelper.Categories.ToList())
{
if(category is CategoryWithUser categoryWithUser) categories.Add(categoryWithUser.Serialize(this.database, user));
else categories.Add(category.Serialize(this.database));
}
return current + serialized;
}
);
categoriesSerialized += LbpSerializer.StringElement("text_search", LbpSerializer.StringElement("url", "/slots/searchLBP3"));
return this.Ok
(
LbpSerializer.TaggedStringElement
(
"categories",
categoriesSerialized,
new Dictionary<string, object>
{
{
"hint", ""
},
{
"hint_start", 1
},
{
"total", CategoryHelper.Categories.Count
},
}
)
);
return this.Ok(new CategoryListResponse(categories, CategoryHelper.Categories.Count, 0, 1));
}
[HttpGet("searches/{endpointName}")]
public async Task<IActionResult> GetCategorySlots(string endpointName, [FromQuery] int pageStart, [FromQuery] int pageSize)
{
GameToken token = this.GetToken();
GameTokenEntity token = this.GetToken();
User? user = await this.database.UserFromGameToken(token);
if (user == null) return this.StatusCode(403, "");
UserEntity? user = await this.database.UserFromGameToken(token);
if (user == null) return this.Forbid();
Category? category = CategoryHelper.Categories.FirstOrDefault(c => c.Endpoint == endpointName);
if (category == null) return this.NotFound();
Logger.Debug("Found category " + category, LogArea.Category);
List<Slot> slots;
List<SlotBase> slots;
int totalSlots;
if (category is CategoryWithUser categoryWithUser)
{
slots = categoryWithUser.GetSlots(this.database, user, pageStart, pageSize).ToList();
slots = categoryWithUser.GetSlots(this.database, user, pageStart, pageSize)
.Select(s => SlotBase.CreateFromEntity(s, token))
.ToList();
totalSlots = categoryWithUser.GetTotalSlots(this.database, user);
}
else
{
slots = category.GetSlots(this.database, pageStart, pageSize).ToList();
slots = category.GetSlots(this.database, pageStart, pageSize)
.Select(s => SlotBase.CreateFromEntity(s, token))
.ToList();
totalSlots = category.GetTotalSlots(this.database);
}
string slotsSerialized = slots.Aggregate(string.Empty, (current, slot) => current + slot.Serialize(token.GameVersion));
return this.Ok
(
LbpSerializer.TaggedStringElement
(
"results",
slotsSerialized,
new Dictionary<string, object>
{
{
"total", totalSlots
},
{
"hint_start", pageStart + pageSize
},
}
)
);
return this.Ok(new GenericSlotResponse("results", slots, totalSlots, pageStart + pageSize));
}
}