Lots of bug fixes and performance improvements (#410)

* Many bug fixes and performance enhancements

* Fix warnings and speed up photos with me

* Finish refactoring user serialization

* Finish refactoring user serialization
Use GameTokens instead of User when possible
Prevent negative page sizes

* Fix debug compilation

* Add gzip compression to example nginx config

* Remove deflate changes

* Add UsernameFromWebToken

Co-authored-by: Jayden <jvyden@jvyden.xyz>
This commit is contained in:
Josh 2022-08-12 19:56:17 -05:00 committed by GitHub
commit d23a264b8a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
43 changed files with 625 additions and 505 deletions

View file

@ -23,6 +23,7 @@ public class RoomVisualizerController : ControllerBase
public async Task<IActionResult> CreateFakeRoom()
{
#if !DEBUG
await Task.FromResult(0);
return this.NotFound();
#else
List<int> users = await this.database.Users.OrderByDescending(_ => EF.Functions.Random()).Take(2).Select(u => u.UserId).ToListAsync();
@ -51,6 +52,7 @@ public class RoomVisualizerController : ControllerBase
public async Task<IActionResult> CreateRoomsWithDuplicatePlayers()
{
#if !DEBUG
await Task.FromResult(0);
return this.NotFound();
#else
List<int> users = await this.database.Users.OrderByDescending(_ => EF.Functions.Random()).Take(1).Select(u => u.UserId).ToListAsync();

View file

@ -2,7 +2,7 @@
using LBPUnion.ProjectLighthouse.Helpers;
using LBPUnion.ProjectLighthouse.Levels;
using LBPUnion.ProjectLighthouse.Logging;
using LBPUnion.ProjectLighthouse.PlayerData.Profiles;
using LBPUnion.ProjectLighthouse.PlayerData;
using LBPUnion.ProjectLighthouse.Types;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
@ -28,10 +28,10 @@ public class SlotPageController : ControllerBase
[HttpGet("rateComment")]
public async Task<IActionResult> RateComment([FromRoute] int id, [FromQuery] int commentId, [FromQuery] int rating)
{
User? user = this.database.UserFromWebRequest(this.Request);
if (user == null) return this.Redirect("~/login");
WebToken? token = this.database.WebTokenFromRequest(this.Request);
if (token == null) return this.Redirect("~/login");
await this.database.RateComment(user, commentId, rating);
await this.database.RateComment(token.UserId, commentId, rating);
return this.Redirect($"~/slot/{id}#{commentId}");
}
@ -39,19 +39,19 @@ public class SlotPageController : ControllerBase
[HttpPost("postComment")]
public async Task<IActionResult> PostComment([FromRoute] int id, [FromForm] string? msg)
{
User? user = this.database.UserFromWebRequest(this.Request);
if (user == null) return this.Redirect("~/login");
WebToken? token = this.database.WebTokenFromRequest(this.Request);
if (token == null) return this.Redirect("~/login");
if (msg == null)
{
Logger.Error($"Refusing to post comment from {user.UserId} on user {id}, {nameof(msg)} is null", LogArea.Comments);
Logger.Error($"Refusing to post comment from {token.UserId} on user {id}, {nameof(msg)} is null", LogArea.Comments);
return this.Redirect("~/slot/" + id);
}
msg = SanitizationHelper.SanitizeString(msg);
await this.database.PostComment(user, id, CommentType.Level, msg);
Logger.Success($"Posted comment from {user.UserId}: \"{msg}\" on user {id}", LogArea.Comments);
await this.database.PostComment(token.UserId, id, CommentType.Level, msg);
Logger.Success($"Posted comment from {token.UserId}: \"{msg}\" on user {id}", LogArea.Comments);
return this.Redirect("~/slot/" + id);
}
@ -61,13 +61,13 @@ public class SlotPageController : ControllerBase
{
if (string.IsNullOrEmpty(callbackUrl)) callbackUrl = "~/slot/" + id;
User? user = this.database.UserFromWebRequest(this.Request);
if (user == null) return this.Redirect("~/login");
WebToken? token = this.database.WebTokenFromRequest(this.Request);
if (token == null) return this.Redirect("~/login");
Slot? heartedSlot = await this.database.Slots.FirstOrDefaultAsync(s => s.SlotId == id);
if (heartedSlot == null) return this.NotFound();
await this.database.HeartLevel(user, heartedSlot);
await this.database.HeartLevel(token.UserId, heartedSlot);
return this.Redirect(callbackUrl);
}
@ -77,13 +77,13 @@ public class SlotPageController : ControllerBase
{
if (string.IsNullOrEmpty(callbackUrl)) callbackUrl = "~/slot/" + id;
User? user = this.database.UserFromWebRequest(this.Request);
if (user == null) return this.Redirect("~/login");
WebToken? token = this.database.WebTokenFromRequest(this.Request);
if (token == null) return this.Redirect("~/login");
Slot? heartedSlot = await this.database.Slots.FirstOrDefaultAsync(s => s.SlotId == id);
if (heartedSlot == null) return this.NotFound();
await this.database.UnheartLevel(user, heartedSlot);
await this.database.UnheartLevel(token.UserId, heartedSlot);
return this.Redirect(callbackUrl);
}
@ -93,13 +93,13 @@ public class SlotPageController : ControllerBase
{
if (string.IsNullOrEmpty(callbackUrl)) callbackUrl = "~/slot/" + id;
User? user = this.database.UserFromWebRequest(this.Request);
if (user == null) return this.Redirect("~/login");
WebToken? token = this.database.WebTokenFromRequest(this.Request);
if (token == null) return this.Redirect("~/login");
Slot? queuedSlot = await this.database.Slots.FirstOrDefaultAsync(s => s.SlotId == id);
if (queuedSlot == null) return this.NotFound();
await this.database.QueueLevel(user, queuedSlot);
await this.database.QueueLevel(token.UserId, queuedSlot);
return this.Redirect(callbackUrl);
}
@ -109,13 +109,13 @@ public class SlotPageController : ControllerBase
{
if (string.IsNullOrEmpty(callbackUrl)) callbackUrl = "~/slot/" + id;
User? user = this.database.UserFromWebRequest(this.Request);
if (user == null) return this.Redirect("~/login");
WebToken? token = this.database.WebTokenFromRequest(this.Request);
if (token == null) return this.Redirect("~/login");
Slot? queuedSlot = await this.database.Slots.FirstOrDefaultAsync(s => s.SlotId == id);
if (queuedSlot == null) return this.NotFound();
await this.database.UnqueueLevel(user, queuedSlot);
await this.database.UnqueueLevel(token.UserId, queuedSlot);
return this.Redirect(callbackUrl);
}

View file

@ -1,6 +1,7 @@
#nullable enable
using LBPUnion.ProjectLighthouse.Helpers;
using LBPUnion.ProjectLighthouse.Logging;
using LBPUnion.ProjectLighthouse.PlayerData;
using LBPUnion.ProjectLighthouse.PlayerData.Profiles;
using LBPUnion.ProjectLighthouse.Types;
using Microsoft.AspNetCore.Mvc;
@ -22,10 +23,10 @@ public class UserPageController : ControllerBase
[HttpGet("rateComment")]
public async Task<IActionResult> RateComment([FromRoute] int id, [FromQuery] int? commentId, [FromQuery] int? rating)
{
User? user = this.database.UserFromWebRequest(this.Request);
if (user == null) return this.Redirect("~/login");
WebToken? token = this.database.WebTokenFromRequest(this.Request);
if (token == null) return this.Redirect("~/login");
await this.database.RateComment(user, commentId.GetValueOrDefault(), rating.GetValueOrDefault());
await this.database.RateComment(token.UserId, commentId.GetValueOrDefault(), rating.GetValueOrDefault());
return this.Redirect($"~/user/{id}#{commentId}");
}
@ -33,19 +34,19 @@ public class UserPageController : ControllerBase
[HttpPost("postComment")]
public async Task<IActionResult> PostComment([FromRoute] int id, [FromForm] string? msg)
{
User? user = this.database.UserFromWebRequest(this.Request);
if (user == null) return this.Redirect("~/login");
WebToken? token = this.database.WebTokenFromRequest(this.Request);
if (token == null) return this.Redirect("~/login");
if (msg == null)
{
Logger.Error($"Refusing to post comment from {user.UserId} on user {id}, {nameof(msg)} is null", LogArea.Comments);
Logger.Error($"Refusing to post comment from {token.UserId} on user {id}, {nameof(msg)} is null", LogArea.Comments);
return this.Redirect("~/user/" + id);
}
msg = SanitizationHelper.SanitizeString(msg);
await this.database.PostComment(user, id, CommentType.Profile, msg);
Logger.Success($"Posted comment from {user.UserId}: \"{msg}\" on user {id}", LogArea.Comments);
await this.database.PostComment(token.UserId, id, CommentType.Profile, msg);
Logger.Success($"Posted comment from {token.UserId}: \"{msg}\" on user {id}", LogArea.Comments);
return this.Redirect("~/user/" + id);
}
@ -53,13 +54,13 @@ public class UserPageController : ControllerBase
[HttpGet("heart")]
public async Task<IActionResult> HeartUser([FromRoute] int id)
{
User? user = this.database.UserFromWebRequest(this.Request);
if (user == null) return this.Redirect("~/login");
WebToken? token = this.database.WebTokenFromRequest(this.Request);
if (token == null) return this.Redirect("~/login");
User? heartedUser = await this.database.Users.FirstOrDefaultAsync(u => u.UserId == id);
if (heartedUser == null) return this.NotFound();
await this.database.HeartUser(user, heartedUser);
await this.database.HeartUser(token.UserId, heartedUser);
return this.Redirect("~/user/" + id);
}
@ -67,13 +68,13 @@ public class UserPageController : ControllerBase
[HttpGet("unheart")]
public async Task<IActionResult> UnheartUser([FromRoute] int id)
{
User? user = this.database.UserFromWebRequest(this.Request);
if (user == null) return this.Redirect("~/login");
WebToken? token = this.database.WebTokenFromRequest(this.Request);
if (token == null) return this.Redirect("~/login");
User? heartedUser = await this.database.Users.FirstOrDefaultAsync(u => u.UserId == id);
if (heartedUser == null) return this.NotFound();
await this.database.UnheartUser(user, heartedUser);
await this.database.UnheartUser(token.UserId, heartedUser);
return this.Redirect("~/user/" + id);
}

View file

@ -13,7 +13,7 @@ public class CasePage : BaseLayout
public CasePage(Database database) : base(database)
{}
public List<ModerationCase> Cases;
public List<ModerationCase> Cases = new();
public int CaseCount;
public int DismissedCaseCount;

View file

@ -12,16 +12,13 @@ public class FilterTestPage : BaseLayout
public string? FilteredText;
public string? Text;
#if DEBUG
public IActionResult OnGet(string? text = null)
{
#if !DEBUG
return this.NotFound();
#endif
if (text != null) this.FilteredText = CensorHelper.ScanMessage(text);
this.Text = text;
return this.Page();
}
#endif
}

View file

@ -42,14 +42,14 @@ public class LandingPage : BaseLayout
const int maxShownLevels = 5;
this.LatestTeamPicks = await this.Database.Slots.Where(s => s.Type == SlotType.User && s.Type == SlotType.User)
this.LatestTeamPicks = await this.Database.Slots.Where(s => s.Type == SlotType.User && !s.SubLevel)
.Where(s => s.TeamPick)
.OrderByDescending(s => s.FirstUploaded)
.Take(maxShownLevels)
.Include(s => s.Creator)
.ToListAsync();
this.NewestLevels = await this.Database.Slots.Where(s => s.Type == SlotType.User && s.Type == SlotType.User)
this.NewestLevels = await this.Database.Slots.Where(s => s.Type == SlotType.User && !s.SubLevel)
.OrderByDescending(s => s.FirstUploaded)
.Take(maxShownLevels)
.Include(s => s.Creator)

View file

@ -1,4 +1,5 @@
@using System.Globalization
@using System.Web
@using LBPUnion.ProjectLighthouse.Levels
@using LBPUnion.ProjectLighthouse.PlayerData
@model LBPUnion.ProjectLighthouse.PlayerData.Photo
@ -25,7 +26,7 @@
{
case SlotType.User:
<span>
in level <b><a href="/slot/@Model.SlotId">@Model.Slot.Name</a></b>
in level <b><a href="/slot/@Model.SlotId">@HttpUtility.HtmlDecode(Model.Slot.Name)</a></b>
</span>
break;
case SlotType.Developer:

View file

@ -11,7 +11,7 @@ public class PirateSignupPage : BaseLayout
public PirateSignupPage(Database database) : base(database)
{}
public async Task<IActionResult> OnGet()
public IActionResult OnGet()
{
User? user = this.Database.UserFromWebRequest(this.Request);
if (user == null) return this.RedirectToPage("/login");

View file

@ -17,7 +17,7 @@
bool isMobile = this.Request.IsMobile();
}
@if (Model.Slot.Hidden)
@if (Model.Slot!.Hidden)
{
<div class="ui inverted red segment">
<h2>This level is currently hidden.</h2>

View file

@ -54,7 +54,8 @@ public class SlotPage : BaseLayout
}
}
if (slot.Hidden && (this.User != slot.Creator && !(bool)this.User?.IsModerator)) return this.NotFound();
if (slot.Hidden || slot.SubLevel && this.User == null && this.User != slot.Creator || !this.User!.IsModerator)
return this.NotFound();
this.Slot = slot;

View file

@ -5,7 +5,6 @@ using LBPUnion.ProjectLighthouse.Levels;
using LBPUnion.ProjectLighthouse.PlayerData;
using LBPUnion.ProjectLighthouse.PlayerData.Profiles;
using LBPUnion.ProjectLighthouse.Servers.Website.Pages.Layouts;
using LBPUnion.ProjectLighthouse.Types;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
@ -49,7 +48,7 @@ public class SlotsPage : BaseLayout
}
else
{
finalSearch.Append(part);
finalSearch.Append(part).Append(' ');
}
}
@ -59,6 +58,7 @@ public class SlotsPage : BaseLayout
.Where(p => p.Type == SlotType.User && !p.Hidden)
.Where(p => p.Name.Contains(finalSearch.ToString()))
.Where(p => p.Creator != null && (targetAuthor == null || string.Equals(p.Creator.Username.ToLower(), targetAuthor.ToLower())))
.Where(p => p.Creator != null && (!p.SubLevel || p.Creator == this.User))
.Where(p => targetGame == null || p.GameVersion == targetGame)
.CountAsync();
@ -71,6 +71,7 @@ public class SlotsPage : BaseLayout
.Where(p => p.Type == SlotType.User && !p.Hidden)
.Where(p => p.Name.Contains(finalSearch.ToString()))
.Where(p => p.Creator != null && (targetAuthor == null || string.Equals(p.Creator.Username.ToLower(), targetAuthor.ToLower())))
.Where(p => p.Creator != null && (!p.SubLevel || p.Creator == this.User))
.Where(p => p.Creator!.LevelVisibility == PrivacyType.All) // TODO: change check for when user is logged in
.Where(p => targetGame == null || p.GameVersion == targetGame)
.OrderByDescending(p => p.FirstUploaded)