Skip to content

Commit

Permalink
Merge pull request #10468 from cl8n/beatmap-grave-when-set-approved
Browse files Browse the repository at this point in the history
Move WIP/pending beatmaps to graveyard when approving their beatmapset
  • Loading branch information
nanaya authored Sep 14, 2023
2 parents e8665b6 + 2951ff0 commit 9fd9a3a
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 5 deletions.
22 changes: 17 additions & 5 deletions app/Models/Beatmapset.php
Original file line number Diff line number Diff line change
Expand Up @@ -558,11 +558,23 @@ public function setApproved($state, $user, ?array $beatmapIds = null)
$approvedState = static::STATES[$state];
$beatmaps = $this->beatmaps();

if (isset($beatmapIds)) {
if ($beatmaps->whereKey($beatmapIds)->count() === count($beatmapIds)) {
$beatmaps = $beatmaps->whereIn('beatmap_id', $beatmapIds);
} else {
throw new InvariantException('beatmap_ids contains invalid id');
if ($beatmapIds !== null) {
$beatmaps->whereKey($beatmapIds);

if ($beatmaps->count() !== count($beatmapIds)) {
throw new InvariantException('Invalid beatmap IDs');
}

// If the beatmapset will be scoreable, set all of the unspecified
// beatmaps currently "WIP" or "pending" to "graveyard". It doesn't
// make sense for any beatmaps to be in those states when they
// cannot be updated.
if ($approvedState > 0) {
$this
->beatmaps()
->whereKeyNot($beatmapIds)
->whereIn('approved', [static::STATES['wip'], static::STATES['pending']])
->update(['approved' => static::STATES['graveyard']]);
}
}

Expand Down
24 changes: 24 additions & 0 deletions tests/Models/BeatmapsetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,30 @@ public function testLove()
$this->assertSame($notifications + 1, Notification::count());
$this->assertSame($userNotifications + 1, UserNotification::count());
$this->assertTrue($beatmapset->fresh()->isLoved());
$this->assertSame('loved', $beatmapset->beatmaps()->first()->status());
}

public function testLoveBeatmapApprovedStates(): void
{
$user = User::factory()->create();
$beatmapset = $this->createBeatmapset();

$specifiedBeatmap = $beatmapset->beatmaps()->first();
$beatmapset->beatmaps()->saveMany([
$graveyardBeatmap = Beatmap::factory()->make(['approved' => Beatmapset::STATES['graveyard']]),
$pendingBeatmap = Beatmap::factory()->make(['approved' => Beatmapset::STATES['pending']]),
$wipBeatmap = Beatmap::factory()->make(['approved' => Beatmapset::STATES['wip']]),
$rankedBeatmap = Beatmap::factory()->make(['approved' => Beatmapset::STATES['ranked']]),
]);

$beatmapset->love($user, [$specifiedBeatmap->getKey()]);

$this->assertTrue($beatmapset->fresh()->isLoved());
$this->assertSame('loved', $specifiedBeatmap->fresh()->status());
$this->assertSame('graveyard', $graveyardBeatmap->fresh()->status());
$this->assertSame('graveyard', $pendingBeatmap->fresh()->status());
$this->assertSame('graveyard', $wipBeatmap->fresh()->status());
$this->assertSame('ranked', $rankedBeatmap->fresh()->status());
}

// region single-playmode beatmap sets
Expand Down

0 comments on commit 9fd9a3a

Please sign in to comment.