Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -1075,6 +1075,38 @@ public function isModerator()
return $this->isGMT() || $this->isNAT();
}

/**
* Determine if the user has a "bad standing" based on recent infringements.
*
* @return bool
*/
public function inBadStanding(): bool
{
$silences = $this->accountHistories()
->recent()
->where('ban_status', UserAccountHistory::TYPES['silence'])
->get();

if ($silences->isEmpty()) {
return false;
}

foreach ($silences as $silence) {
if ($silence->endTime()->isFuture()) {
return true;
}
}

$recentCount = $silences->count();
$longestPeriod = $silences->max('period') ?? 0;

if ($longestPeriod >= 1440 * 60 || $recentCount >= 3) {
return true;
}

return false;
}

public function isAlumni()
{
return $this->isGroup(app('groups')->byIdentifier('alumni'));
Expand Down
1 change: 1 addition & 0 deletions app/Transformers/UserTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public function transform(User $user)
...$result,
'cover_url' => $user->cover()->url(), // TODO: deprecated.
'discord' => $user->user_discord,
'in_bad_standing' => $user->inBadStanding() || (\Auth::user()?->isModerator() === true && $user->accountHistories->isNotEmpty()),
'has_supported' => $user->hasSupported(),
'interests' => $user->user_interests,
'join_date' => json_time($user->user_regdate),
Expand Down
1 change: 1 addition & 0 deletions resources/js/interfaces/user-extended-json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ type UserExtendedDefaultIncludes =

interface UserExtendedAdditionalAttributes {
discord: string | null;
in_bad_standing: boolean;
has_supported: boolean;
interests: string | null;
join_date: string;
Expand Down
2 changes: 1 addition & 1 deletion resources/js/profile-page/account-standing.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ export default class AccountStanding extends React.Component<ExtraPageProps> {
<div className='page-extra'>
<ExtraHeader name={this.props.name} withEdit={false} />

{this.latest != null && (
{this.props.controller.state.user.in_bad_standing && (
<div className='page-extra__alert page-extra__alert--warning'>
<StringWithComponent
mappings={{ username: <strong>{this.props.controller.state.user.username}</strong> }}
Expand Down
59 changes: 59 additions & 0 deletions tests/Models/UserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use App\Models\OAuth\Token;
use App\Models\User;
use App\Models\UsernameChangeHistory;
use App\Models\UserAccountHistory;
use Carbon\CarbonImmutable;
use Database\Factories\OAuth\RefreshTokenFactory;
use Tests\TestCase;
Expand Down Expand Up @@ -269,4 +270,62 @@ public function testValidDiscordUsername(string $username, bool $valid)
$this->assertArrayHasKey('user_discord', $user->validationErrors()->all());
}
}

public function testInBadStandingActiveSilence()
{
$user = User::factory()->create();
UserAccountHistory::factory()->create([
'user_id' => $user->user_id,
'ban_status' => UserAccountHistory::TYPES['silence'],
'timestamp' => CarbonImmutable::now()->subMinutes(5),
'period' => 600, // active
]);

$this->assertTrue($user->inBadStanding());
}

public function testInBadStandingLongPastSilence()
{
$user = User::factory()->create();
UserAccountHistory::factory()->create([
'user_id' => $user->user_id,
'ban_status' => UserAccountHistory::TYPES['silence'],
'timestamp' => CarbonImmutable::now()->subDays(2),
'period' => 1440 * 60, // 24 hours
]);

$this->assertTrue($user->inBadStanding());
}

public function testInBadStandingShortPastSilence()
{
$user = User::factory()->create();
UserAccountHistory::factory()->create([
'user_id' => $user->user_id,
'ban_status' => UserAccountHistory::TYPES['silence'],
'timestamp' => CarbonImmutable::now()->subDays(2),
'period' => 600, // 10 minutes
]);

// regular user should not see warning for single short inactive silence
$this->assertFalse($user->inBadStanding());
}

public function testInBadStandingMultipleShortSilences()
{
$user = User::factory()->create();

// 3 short silences
for ($i = 0; $i < 3; $i++) {
UserAccountHistory::factory()->create([
'user_id' => $user->user_id,
'ban_status' => UserAccountHistory::TYPES['silence'],
'timestamp' => CarbonImmutable::now()->subDays(10 + $i),
'period' => 60,
]);
}

// 3 infringements should trigger warning
$this->assertTrue($user->inBadStanding());
}
}