Skip to content

Commit

Permalink
Merge branch 'master' into http-str
Browse files Browse the repository at this point in the history
  • Loading branch information
notbakaneko authored Nov 22, 2024
2 parents ab069e7 + 366f130 commit 2b75104
Show file tree
Hide file tree
Showing 11 changed files with 67 additions and 53 deletions.
7 changes: 6 additions & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -137,11 +137,16 @@ jobs:
key: ${{ runner.os }}-composer-${{ hashFiles('composer.lock') }}
restore-keys: ${{ runner.os }}-composer-

- name: Get current date
id: current-date
run: echo "today=$(date '+%Y%m%d')" >> "$GITHUB_OUTPUT"

- name: Cache ip2asn
uses: actions/cache@v4
with:
path: database/ip2asn/
key: ip2asn
key: ip2asn-${{ steps.current-date.outputs.today }}
restore-keys: ip2asn-

- run: ./build.sh

Expand Down
2 changes: 1 addition & 1 deletion app/Http/Controllers/UserCoverPresetsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,6 @@ public function update(string $id): Response
$item->update(['active' => $params['active']]);
}

return ujs_redirect(route('user-cover-presets.index').'#cover-'.$item->getKey());
return response(null, 204);
}
}
10 changes: 6 additions & 4 deletions app/Http/Controllers/UsersController.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
use App\Libraries\UserRegistration;
use App\Models\Beatmap;
use App\Models\BeatmapDiscussion;
use App\Models\Country;
use App\Models\IpBan;
use App\Models\Log;
use App\Models\User;
Expand Down Expand Up @@ -983,9 +982,8 @@ private function storeUser(array $rawParams)
'username',
], ['null_missing' => true]);
$countryCode = request_country();
$country = Country::find($countryCode);
$params['user_ip'] = $ip;
$params['country_acronym'] = $country === null ? '' : $country->getKey();
$params['country_acronym'] = $countryCode;
$params['user_lang'] = \App::getLocale();

$registration = new UserRegistration($params);
Expand All @@ -1009,7 +1007,11 @@ private function storeUser(array $rawParams)
$user = $registration->user();

// report unknown country code but ignore non-country from cloudflare
if ($countryCode !== null && $country === null && $countryCode !== 'T1') {
if (
$countryCode !== null
&& $countryCode !== 'T1'
&& app('countries')->byCode($countryCode) === null
) {
app('sentry')->getClient()->captureMessage(
'User registered from unknown country',
null,
Expand Down
3 changes: 1 addition & 2 deletions app/Libraries/User/CountryChange.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

use App\Exceptions\InvariantException;
use App\Models\Beatmap;
use App\Models\Country;
use App\Models\User;
use App\Models\UserAccountHistory;

Expand All @@ -18,7 +17,7 @@ class CountryChange
public static function handle(User $user, string $newCountry, string $reason): void
{
// Assert valid country acronym
$country = Country::find($newCountry);
$country = app('countries')->byCode($newCountry);
if ($country === null) {
throw new InvariantException('invalid country specified');
}
Expand Down
7 changes: 4 additions & 3 deletions app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -2320,11 +2320,12 @@ public function isValid()

if ($this->isDirty('country_acronym')) {
if (present($this->country_acronym)) {
if (($country = Country::find($this->country_acronym)) !== null) {
$country = app('countries')->byCode($this->country_acronym);
if ($country === null) {
$this->validationErrors()->add('country', '.invalid_country');
} else {
// ensure matching case
$this->country_acronym = $country->getKey();
} else {
$this->validationErrors()->add('country', '.invalid_country');
}
} else {
$this->country_acronym = Country::UNKNOWN;
Expand Down
5 changes: 5 additions & 0 deletions database/factories/CountryFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ class CountryFactory extends Factory
{
protected $model = Country::class;

public function configure(): static
{
return $this->afterCreating(fn () => app('countries')->resetMemoized());
}

public function definition(): array
{
return [
Expand Down
6 changes: 6 additions & 0 deletions resources/js/setup-turbo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import '@hotwired/turbo';
import { hideLoadingOverlay, showLoadingOverlay } from 'utils/loading-overlay';
import { reloadPage } from 'utils/turbolinks';

Turbo.config.drive.progressBarDelay = 0;

Expand All @@ -17,6 +18,11 @@ document.addEventListener('turbo:submit-start', (e) => {
}
});
document.addEventListener('turbo:submit-end', hideLoadingOverlay);
document.addEventListener('turbo:submit-end', (e) => {
if (e.detail.success && e.detail.formSubmission.formElement.dataset.reloadOnSuccess === '1') {
reloadPage();
}
});

// disable turbo navigation for old webs
document.addEventListener('turbo:click', (event) => {
Expand Down
7 changes: 3 additions & 4 deletions resources/views/layout/ujs-redirect.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
Copyright (c) ppy Pty Ltd <[email protected]>. Licensed under the GNU Affero General Public License v3.0.
See the LICENCE file in the repository root for full licence text.
--}}
;(function() {
$(document).off(".ujsHideLoadingOverlay");
window.setTimeout(() => Turbo.visit({!! json_encode($url) !!}), 0);
}).call(this);
$(document).off('.ujsHideLoadingOverlay');
Turbo.cache.clear();
Turbo.visit({!! json_encode($url) !!});
1 change: 0 additions & 1 deletion resources/views/password_reset/index.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
<form
action="{{ route('password-reset') }}"
class="password-reset js-form-error"
data-reload-on-success="1"
data-remote
data-skip-ajax-error-popup="1"
method="POST"
Expand Down
37 changes: 21 additions & 16 deletions resources/views/user_cover_presets/index.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,34 +89,39 @@ class="u-contents js-user-cover-preset-batch-enable"

<div>
<p>
<button
class="btn-osu-big btn-osu-big--rounded-small"
data-url="{{ route('user-cover-presets.update', [
<form
action="{{ route('user-cover-presets.update', [
'user_cover_preset' => $item->getKey(),
'active' => $item->active ? '0' : '1',
]) }}"
data-method="PUT"
class="u-contents"
data-reload-on-success="1"
data-remote="1"
title="{{ osu_trans('user_cover_presets.index.item.'.(
$isActive ? 'click_to_disable' : 'click_to_enable'
)) }}"
method="POST"
>
@if ($isActive)
<span class="fas fa-circle"></span>
{{ osu_trans('user_cover_presets.index.item.enabled') }}
@else
<span class="far fa-circle"></span>
{{ osu_trans('user_cover_presets.index.item.disabled') }}
@endif
</button>
<input type="hidden" name="_method" value="PUT" />
<button
class="btn-osu-big btn-osu-big--rounded-small"
title="{{ osu_trans('user_cover_presets.index.item.'.(
$isActive ? 'click_to_disable' : 'click_to_enable'
)) }}"
>
@if ($isActive)
<span class="fas fa-circle"></span>
{{ osu_trans('user_cover_presets.index.item.enabled') }}
@else
<span class="far fa-circle"></span>
{{ osu_trans('user_cover_presets.index.item.disabled') }}
@endif
</button>
</form>
</p>
<p>
<form
action="{{ route('user-cover-presets.update', $item) }}"
enctype="multipart/form-data"
method="POST"
class="user-cover-preset-replace"
data-reload-on-success="1"
>
@csrf
<input type="hidden" name="_method" value="PUT" />
Expand Down
35 changes: 14 additions & 21 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,24 +152,22 @@ protected function tearDown(): void
/**
* Act as a User with OAuth scope permissions.
*/
protected function actAsScopedUser(?User $user, ?array $scopes = ['*'], ?Client $client = null): void
protected function actAsScopedUser(?User $user, ?array $scopes = ['*'], ?Client $client = null): static
{
$this->actingWithToken($this->createToken(
return $this->actingWithToken($this->createToken(
$user,
$scopes,
$client ?? Client::factory()->create(),
));
}

protected function actAsUser(?User $user, bool $verified = false, $driver = null)
protected function actAsUser(?User $user, bool $verified = false, $driver = null): static
{
if ($user === null) {
return;
if ($user !== null) {
$this->be($user, $driver)->withSession(['verified' => $verified]);
}

$this->be($user, $driver);

$this->withSession(['verified' => $verified]);
return $this;
}

/**
Expand All @@ -179,7 +177,7 @@ protected function actAsUser(?User $user, bool $verified = false, $driver = null
* @param string $driver Auth driver to use.
* @return void
*/
protected function actAsUserWithToken(Token $token, $driver = null)
protected function actAsUserWithToken(Token $token, $driver = null): static
{
$guard = app('auth')->guard($driver);
$user = $token->getResourceOwner();
Expand All @@ -196,24 +194,19 @@ protected function actAsUserWithToken(Token $token, $driver = null)
request()->attributes->set(AuthApi::REQUEST_OAUTH_TOKEN_KEY, $token);

app('auth')->shouldUse($driver);
}

protected function actingAsVerified($user)
{
$this->actAsUser($user, true);

return $this;
}

protected function actingWithToken($token)
protected function actingAsVerified($user): static
{
$this->actAsUserWithToken($token);

$encodedToken = EncodeToken::encodeAccessToken($token);
return $this->actAsUser($user, true);
}

return $this->withHeaders([
'Authorization' => "Bearer {$encodedToken}",
]);
protected function actingWithToken($token): static
{
return $this->actAsUserWithToken($token)
->withToken(EncodeToken::encodeAccessToken($token));
}

protected function assertEqualsUpToOneSecond(CarbonInterface $expected, CarbonInterface $actual): void
Expand Down

0 comments on commit 2b75104

Please sign in to comment.