Skip to content

Commit

Permalink
Merge branch 'main' into recent-activity
Browse files Browse the repository at this point in the history
  • Loading branch information
Slendy authored May 13, 2024
2 parents 8f91875 + 9e84e46 commit 27cbb14
Show file tree
Hide file tree
Showing 7 changed files with 145 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,13 @@ public async Task<IActionResult> ReviewsFor(int slotId)

List<GameReview> reviews = (await this.database.Reviews
.Where(r => r.SlotId == slotId)
.Select(r => new
{
Review = r,
SlotVersion = r.Slot!.GameVersion,
})
.Where(a => a.SlotVersion <= token.GameVersion)
.Select(a => a.Review)
.OrderByDescending(r => r.ThumbsUp - r.ThumbsDown)
.ThenByDescending(r => r.Timestamp)
.ApplyPagination(pageData)
Expand All @@ -194,6 +201,13 @@ public async Task<IActionResult> ReviewsBy(string username)

List<GameReview> reviews = (await this.database.Reviews
.Where(r => r.ReviewerId == targetUserId)
.Select(r => new
{
Review = r,
SlotVersion = r.Slot!.GameVersion,
})
.Where(a => a.SlotVersion <= token.GameVersion)
.Select(a => a.Review)
.OrderByDescending(r => r.Timestamp)
.ApplyPagination(pageData)
.ToListAsync()).ToSerializableList(r => GameReview.CreateFromEntity(r, token));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ public async Task<IActionResult> UpdateMyPins()
// Sometimes the update gets called periodically as pin progress updates via playing,
// may not affect equipped profile pins however, so check before setting it.
string currentPins = user.Pins;
string newPins = string.Join(",", pinJson.ProfilePins);
string newPins = string.Join(",", pinJson.ProfilePins.Distinct());

if (string.Equals(currentPins, newPins)) return this.Ok("[{\"StatusCode\":200}]");

Expand Down
3 changes: 2 additions & 1 deletion ProjectLighthouse.Servers.Website/Pages/UserPage.cshtml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ public async Task<IActionResult> OnGet([FromRoute] int userId, string? slug)
if (this.ProfileUser == null) return this.NotFound();

string userSlug = this.ProfileUser.GenerateSlug();
if (slug == null || userSlug != slug)
// Only redirect if there is a valid slug for this user and the current slug doesn't match
if (!string.IsNullOrWhiteSpace(userSlug) && (slug == null || userSlug != slug))
{
return this.Redirect($"~/user/{userId}/{userSlug}");
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using LBPUnion.ProjectLighthouse.Database;
using LBPUnion.ProjectLighthouse.Servers.GameServer.Controllers.Slots;
using LBPUnion.ProjectLighthouse.Tests.Helpers;
using LBPUnion.ProjectLighthouse.Types.Entities.Level;
using LBPUnion.ProjectLighthouse.Types.Entities.Token;
using LBPUnion.ProjectLighthouse.Types.Serialization;
using LBPUnion.ProjectLighthouse.Types.Users;
using Microsoft.AspNetCore.Mvc;
using Xunit;

namespace ProjectLighthouse.Tests.GameApiTests.Unit.Controllers;

public class ReviewControllerTests
{
private static async Task InsertTestData(DatabaseContext database)
{
database.Slots.Add(new SlotEntity
{
SlotId = 1,
CreatorId = 1,
GameVersion = GameVersion.LittleBigPlanet3,
});

database.Slots.Add(new SlotEntity
{
SlotId = 2,
CreatorId = 1,
GameVersion = GameVersion.LittleBigPlanet2,
});

database.Reviews.Add(new ReviewEntity
{
ReviewId = 1,
ReviewerId = 1,
SlotId = 1,
});

database.Reviews.Add(new ReviewEntity
{
ReviewId = 2,
ReviewerId = 1,
SlotId = 2,
});
await database.SaveChangesAsync();
}

[Theory]
[InlineData(GameVersion.LittleBigPlanet2, 1)]
[InlineData(GameVersion.LittleBigPlanet3, 2)]
public async Task ReviewsBy_ShouldNotList_HigherGameVersions(GameVersion version, int expected)
{
GameTokenEntity token = MockHelper.GetUnitTestToken();
token.GameVersion = version;
DatabaseContext database = await MockHelper.GetTestDatabase(new List<GameTokenEntity>
{
token,
});

await InsertTestData(database);

ReviewController controller = new(database);
controller.SetupTestController(token);

IActionResult response = await controller.ReviewsBy("unittest");
ReviewResponse review = response.CastTo<OkObjectResult, ReviewResponse>();

Assert.Equal(expected, review.Reviews.Count);
Assert.True(review.Reviews.All(r => database.Slots.FirstOrDefault(s => s.SlotId == r.Slot.SlotId)?.GameVersion <= version));
}

[Theory]
[InlineData(GameVersion.LittleBigPlanet2, 2, 1)]
[InlineData(GameVersion.LittleBigPlanet2, 1, 0)]
[InlineData(GameVersion.LittleBigPlanet3, 2, 1)]
[InlineData(GameVersion.LittleBigPlanet3, 1, 1)]
public async Task ReviewsFor_ShouldNotList_HigherGameVersions(GameVersion version, int slotId, int expected)
{
GameTokenEntity token = MockHelper.GetUnitTestToken();
token.GameVersion = version;
DatabaseContext database = await MockHelper.GetTestDatabase(new List<GameTokenEntity>
{
token,
});

await InsertTestData(database);

ReviewController controller = new(database);
controller.SetupTestController(token);

IActionResult response = await controller.ReviewsFor(slotId);
ReviewResponse review = response.CastTo<OkObjectResult, ReviewResponse>();

Assert.Equal(expected, review.Reviews.Count);
Assert.True(review.Reviews.All(r => database.Slots.FirstOrDefault(s => s.SlotId == r.Slot.SlotId)?.GameVersion <= version));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -177,4 +177,29 @@ public async Task UpdateMyPins_ShouldNotSave_WhenPinsAreEqual()
Assert.Equal(expectedPins, dbMock.Users.First().Pins);
Assert.Equal(expectedResponse, pinsResponse);
}

[Fact]
public async Task UpdateMyPins_ShouldRemove_DuplicatePins()
{
UserEntity entity = MockHelper.GetUnitTestUser();
entity.Pins = "1234";
List<UserEntity> users = new()
{
entity,
};
await using DatabaseContext dbMock = await MockHelper.GetTestDatabase(users);

UserController userController = new(dbMock);
userController.SetupTestController("{\"profile_pins\": [1234, 1234]}");

const string expectedPins = "1234";
const string expectedResponse = "[{\"StatusCode\":200}]";

IActionResult result = await userController.UpdateMyPins();

string pinsResponse = result.CastTo<OkObjectResult, string>();

Assert.Equal(expectedPins, dbMock.Users.First().Pins);
Assert.Equal(expectedResponse, pinsResponse);
}
}
2 changes: 1 addition & 1 deletion ProjectLighthouse/ProjectLighthouse.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Pfim" Version="0.11.2" />
<PackageReference Include="SixLabors.ImageSharp" Version="3.1.3" />
<PackageReference Include="SixLabors.ImageSharp" Version="3.1.4" />
<PackageReference Include="Discord.Net.Webhook" Version="3.14.1" />
<PackageReference Include="JetBrains.Annotations" Version="2023.3.0" />
<PackageReference Include="Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore" Version="8.0.3" />
Expand Down
3 changes: 3 additions & 0 deletions ProjectLighthouse/Tickets/TicketReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ public struct SectionHeader
public SectionType Type;
public ushort Length;
public int Position;

public override string ToString() =>
$"SectionHeader(Type='{this.Type}', Length='{this.Length}', Position='{this.Position}')";
}

public class TicketReader : BinaryReader
Expand Down

0 comments on commit 27cbb14

Please sign in to comment.