Skip to content

Commit

Permalink
feat: allow moderators to mute users (#2300)
Browse files Browse the repository at this point in the history
  • Loading branch information
wescopeland authored Mar 28, 2024
1 parent d769db2 commit 47f4962
Show file tree
Hide file tree
Showing 11 changed files with 67 additions and 21 deletions.
1 change: 1 addition & 0 deletions app/Helpers/database/user-auth.php
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ function authenticateFromCookie(
}

$userDetailsOut = $user->toArray();
$userDetailsOut['isMuted'] = $user->isMuted;
$userOut = $user->getAttribute('User');
$permissionsOut = $user->getAttribute('Permissions');

Expand Down
23 changes: 17 additions & 6 deletions app/Helpers/render/comment.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
use App\Community\Enums\ArticleType;
use App\Community\Enums\SubscriptionSubjectType;
use App\Enums\Permissions;
use App\Models\User;
use Illuminate\Support\Facades\Blade;

function RenderCommentsComponent(
?string $user,
?string $username,
int $numComments,
array $commentData,
int $articleID,
Expand All @@ -16,7 +17,7 @@ function RenderCommentsComponent(
int $offset = 0,
bool $embedded = true
): void {
$userID = getUserIDFromUser($user);
$user = User::firstWhere('User', $username);

echo "<div class='commentscomponent'>";

Expand Down Expand Up @@ -44,7 +45,7 @@ function RenderCommentsComponent(
if (isset($user)) {
$subjectType = SubscriptionSubjectType::fromArticleType($articleTypeID);
if ($subjectType !== null) {
$isSubscribed = isUserSubscribedToArticleComments($articleTypeID, $articleID, $userID);
$isSubscribed = isUserSubscribedToArticleComments($articleTypeID, $articleID, $user->id);
echo "<div>";
RenderUpdateSubscriptionForm(
'updatesubscription',
Expand Down Expand Up @@ -79,29 +80,39 @@ function RenderCommentsComponent(
$lastID = $comment['ID'];
}

$canDeleteComment = $articleTypeID == ArticleType::User && $userID === $articleID || $permissions >= Permissions::Moderator;
$canDeleteComment = $articleTypeID == ArticleType::User && $user->id === $articleID || $permissions >= Permissions::Moderator;

RenderArticleComment(
$articleID,
$comment['User'],
$comment['CommentPayload'],
// TODO no unix timestamp here
(int) $comment['Submitted'],
$user,
$username,
$articleTypeID,
(int) $comment['ID'],
$canDeleteComment
);
}

if (isset($user)) {
if (isset($user) && !$user->isMuted) {
// User comment input:
RenderCommentInputRow($user, $articleTypeID, $articleID);
}

echo "</tbody></table>";

echo "</div>";

if (isset($user) && $user->isMuted) {
$mutedDate = getNiceDate($user->muted_until->timestamp);

echo <<<HTML
<div class="bg-embed p-2 rounded-b-lg">
<p class="text-center text-muted">You are muted until $mutedDate.</p>
</div>
HTML;
}
}

function RenderArticleComment(
Expand Down
20 changes: 13 additions & 7 deletions app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -160,28 +160,29 @@ class User extends Authenticatable implements CommunityMember, Developer, HasCom
];

protected $visible = [
"ID",
"avatarUrl",
'Created',
"achievements_unlocked",
"achievements_unlocked_hardcore",
"completion_percentage_average",
"achievements_unlocked",
"avatarUrl",
"completion_percentage_average_hardcore",
"completion_percentage_average",
"ContribCount",
"ContribYield",
"Created",
"Deleted",
"ID",
"isMuted",
"LastLogin",
"ManuallyVerified",
"Motto",
"Permissions",
"RAPoints",
"RASoftcorePoints",
"TrueRAPoints",
"websitePrefs",
'UnreadMessageCount',
"UnreadMessageCount",
"Untracked",
"User",
"UserWallActive",
"websitePrefs",
];

protected $appends = [
Expand Down Expand Up @@ -373,6 +374,11 @@ public function getAvatarUrlAttribute(): string
return media_asset('UserPic/' . $this->getAttribute('User') . '.png');
}

public function getIsMutedAttribute(): bool
{
return $this->isMuted();
}

// TODO remove after rename

public function getIdAttribute(): ?int
Expand Down
1 change: 1 addition & 0 deletions database/factories/UserFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public function definition(): array
'fbUser' => 0,
'Untracked' => 0,
'UserWallActive' => 1,
'muted_until' => null,

// nullable
'APIKey' => 'apiKey',
Expand Down
4 changes: 4 additions & 0 deletions public/request/comment/create.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@
return back()->withErrors(__('legacy.error.permissions'));
}

if ($userDetails['isMuted']) {
return back()->withErrors(__('legacy.error.error'));
}

$input = Validator::validate(Arr::wrap(request()->post()), [
'body' => 'required|string|max:2000',
'commentable_id' => 'required|integer',
Expand Down
4 changes: 4 additions & 0 deletions public/request/forum-topic-comment/create.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
return back()->withErrors(__('legacy.error.permissions'));
}

if ($userDetails['isMuted']) {
return back()->withErrors(__('legacy.error.error'));
}

$input = Validator::validate(Arr::wrap(request()->post()), [
'topic' => 'required|integer|exists:ForumTopic,ID',
'body' => 'required|string|max:60000',
Expand Down
4 changes: 4 additions & 0 deletions public/request/forum-topic-comment/update.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
return back()->withErrors(__('legacy.error.permissions'));
}

if ($userDetails['isMuted']) {
return back()->withErrors(__('legacy.error.error'));
}

$input = Validator::validate(Arr::wrap(request()->post()), [
'comment' => 'required|integer|exists:ForumTopicComment,ID',
'body' => 'required|string|max:60000',
Expand Down
4 changes: 4 additions & 0 deletions public/request/forum-topic/create.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
return back()->withErrors(__('legacy.error.permissions'));
}

if ($userDetails['isMuted']) {
return back()->withErrors(__('legacy.error.error'));
}

$input = Validator::validate(Arr::wrap(request()->post()), [
'forum' => 'required|integer|exists:Forum,ID',
'title' => 'required|string|max:255',
Expand Down
5 changes: 4 additions & 1 deletion resources/views/components/forum/post.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,10 @@
<x-forum.post-moderation-tools :commentAuthor="$commentAuthor"/>
@endif

@if ($showEditButton)
@php
$user = auth()->user();
@endphp
@if ($showEditButton && !$user?->isMuted)
<a href='/editpost.php?comment={{ $commentId }}' class='btn p-1 lg:text-xs'>Edit</a>
@endif

Expand Down
4 changes: 2 additions & 2 deletions resources/views/pages-legacy/viewforum.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@
RenderPaginator($numTotalTopics, $count, $offset, "/viewforum.php?f=$requestedForumID&o=");
echo "</div>";
}
if ($permissions >= Permissions::Registered) {
if ($permissions >= Permissions::Registered && !$userDetails['isMuted']) {
echo "<a class='btn btn-link' href='createtopic.php?forum=$thisForumID'>Create New Topic</a>";
}
echo "</div>";
Expand Down Expand Up @@ -163,7 +163,7 @@
RenderPaginator($numTotalTopics, $count, $offset, "/viewforum.php?f=$requestedForumID&o=");
echo "</div>";
}
if ($permissions >= Permissions::Registered) {
if ($permissions >= Permissions::Registered && !$userDetails['isMuted']) {
echo "<a class='btn btn-link' href='createtopic.php?forum=$thisForumID'>Create New Topic</a>";
}
echo "</div>";
Expand Down
18 changes: 13 additions & 5 deletions resources/views/pages-legacy/viewtopic.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -197,13 +197,21 @@
<?php
$user = auth()->user();
?>
@if ($thisTopicID != 0 && $user?->hasVerifiedEmail())
@guest
<p class="text-center">
You must log in before you can join this conversation.
</p>
@endguest

@if ($user?->isMuted)
<div class="flex justify-center bg-embed p-2 rounded-lg -mx-2 w-[calc(100%+16px)] sm:mx-0 sm:w-full">
<p class="text-center text-muted">You are muted until {{ getNiceDate($user->muted_until->timestamp) }}.</p>
</div>
@endif

@if ($thisTopicID != 0 && $user?->hasVerifiedEmail() && !$user?->isMuted)
<x-section>
<div class="flex bg-embed p-2 rounded-lg -mx-2 w-[calc(100%+16px)] sm:mx-0 sm:w-full">
@guest
You must log in before you can join this conversation.
@endguest

@auth
<div class="hidden sm:flex flex-col gap-1 justify-start items-center lg:border-r border-neutral-700 px-0.5 pb-2 lg:py-2 lg:w-44">
<x-user.avatar :user="request()->user()" display="icon" iconSize="md" class="rounded-sm" />
Expand Down

0 comments on commit 47f4962

Please sign in to comment.