-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add 'Mark Read' Functionality for Comments (#2122)
* Adds reference buttons to all inline and overall comments to indicate unread status * Updates tooltips on references based on comment type and read status * Enables users to mark unread inline and overall comments as read by clicking/tapping on reference buttons * Makes comment focus styling for "active comments" consistent with solid stroke lines Closes #2071
- Loading branch information
Showing
28 changed files
with
796 additions
and
135 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace App\GraphQL\Mutations; | ||
|
||
use App\Models\Submission; | ||
|
||
final readonly class CommentStatusMutator | ||
{ | ||
/** | ||
* Validate supplied arguments and return the comments to be marked as read. | ||
* | ||
* @param string $type | ||
* @param string $submission_id | ||
* @param array{int} $comment_ids | ||
* @return \App\GraphQL\Mutations\Collection<\App\GraphQL\Mutations\InlineComment|\App\GraphQL\Mutations\OverallComment> | ||
*/ | ||
private function validateArgs($type, $submission_id, $comment_ids) | ||
{ | ||
if (!$submission_id) { | ||
throw new \Exception('Submission ID required'); | ||
} | ||
if (empty($comment_ids)) { | ||
throw new \Exception('Comment ID(s) required'); | ||
} | ||
if ($type === 'inline') { | ||
$comments = Submission::find($submission_id)->inlineCommentsWithReplies; | ||
} else { | ||
$comments = Submission::find($submission_id)->overallCommentsWithReplies; | ||
} | ||
$matchingComments = $comments->whereIn('id', $comment_ids); | ||
if ($matchingComments->isEmpty()) { | ||
throw new \Exception('Invalid comment ID'); | ||
} | ||
|
||
return $matchingComments; | ||
} | ||
|
||
/** | ||
* @param null $_ | ||
* @param array{} $args | ||
* @return \App\GraphQL\Mutations\Collection<\App\GraphQL\Mutations\InlineComment> | ||
*/ | ||
public function inlineRead(null $_, array $args) | ||
{ | ||
$comments = $this->validateArgs('inline', $args['input']['submission_id'], $args['input']['comment_ids']); | ||
$comments->map(function ($comment) { | ||
$comment->markRead(); | ||
}); | ||
|
||
return $comments; | ||
} | ||
|
||
/** | ||
* @param null $_ | ||
* @param array{} $args | ||
* @return \App\GraphQL\Mutations\Collection<\App\GraphQL\Mutations\OverallComment> | ||
*/ | ||
public function overallRead(null $_, array $args) | ||
{ | ||
$comments = $this->validateArgs('overall', $args['input']['submission_id'], $args['input']['comment_ids']); | ||
$comments->map(function ($comment) { | ||
$comment->markRead(); | ||
}); | ||
|
||
return $comments; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace App\Models; | ||
|
||
use Illuminate\Database\Eloquent\Model; | ||
|
||
class CommentStatus extends Model | ||
{ | ||
protected $fillable = [ | ||
'comment_id', | ||
'user_id', | ||
'type', | ||
]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace App\Models\Traits; | ||
|
||
use App\Models\CommentStatus; | ||
use Illuminate\Database\Eloquent\Casts\Attribute; | ||
use Illuminate\Database\Eloquent\Relations\HasOne; | ||
|
||
trait ReadStatus | ||
{ | ||
/** | ||
* Returns the associated CommentStatus record | ||
* | ||
* @return \Illuminate\Database\Eloquent\Relations\HasOne | ||
*/ | ||
public function readStatus(): HasOne | ||
{ | ||
return $this->hasOne(CommentStatus::class, 'comment_id') | ||
->where('type', static::class) | ||
->where('user_id', auth()->id()); | ||
} | ||
|
||
/** | ||
* Set a value for the read status of a comment. | ||
* | ||
* @return \Illuminate\Database\Eloquent\Casts\Attribute | ||
*/ | ||
public function readAt(): Attribute | ||
{ | ||
return Attribute::make( | ||
get: function () { | ||
return $this->readStatus ? $this->readStatus->created_at : null; | ||
}, | ||
set: function () { | ||
$this->markRead(); | ||
} | ||
); | ||
} | ||
|
||
/** | ||
* Create a CommentStatus for this | ||
* | ||
* @param \App\Models\Traits\User $user | ||
* @return void | ||
*/ | ||
public function markRead($user = null) | ||
{ | ||
if (!$user) { | ||
$user = auth()->user(); | ||
} | ||
if (!$user) { | ||
throw new \Exception('Unable to save read status. No user logged in.'); | ||
} | ||
|
||
CommentStatus::firstOrCreate([ | ||
'comment_id' => $this->attributes['id'], | ||
'user_id' => $user->id, | ||
'type' => static::class, | ||
]); | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
backend/database/migrations/2024_04_19_193301_create_comment_read_status.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?php | ||
|
||
use Illuminate\Database\Migrations\Migration; | ||
use Illuminate\Database\Schema\Blueprint; | ||
use Illuminate\Support\Facades\Schema; | ||
|
||
return new class extends Migration | ||
{ | ||
/** | ||
* Run the migrations. | ||
*/ | ||
public function up(): void | ||
{ | ||
Schema::create('comment_statuses', function (Blueprint $table) { | ||
$table->id(); | ||
$table->foreignId('comment_id'); | ||
$table->foreignId('user_id')->constrained('users')->onDelete('cascade'); | ||
$table->string('type'); | ||
$table->timestamps(); | ||
}); | ||
} | ||
|
||
/** | ||
* Reverse the migrations. | ||
*/ | ||
public function down(): void | ||
{ | ||
Schema::dropIfExists('comment_statuses'); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.