Skip to content

Commit e43464e

Browse files
authored
feat: notification roll-up for comments & answers, and unified UserMentionNotification (#468)
* feat: notification roll-up for comments and answers, and unified UserMentionNotification * fix(resources): update resource types order in frontend to prioritize notes, classes, and books * fix(resources): support chapters in ResourceFormModal chapter dropdown * refactor(notifications): delete redundant mention classes and use UserMentionNotification everywhere * fix(navigation): persist infinite scroll items using useRemember for native browser back * feat(forum): add infinite scroll, remove mobile footer, add guide to mobile nav
1 parent dcb740c commit e43464e

21 files changed

Lines changed: 537 additions & 217 deletions

‎app/Http/Controllers/BlogController.php‎

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
use App\Models\Blog;
88
use App\Models\BlogComment;
99
use App\Models\User;
10-
use App\Notifications\BlogCommentMentionNotification;
1110
use App\Notifications\BlogCommentNotification;
1211
use App\Notifications\BlogReactionNotification;
12+
use App\Notifications\UserMentionNotification;
1313
use Illuminate\Http\Request;
1414
use Illuminate\Support\Facades\Auth;
1515
use Illuminate\Support\Facades\Notification;
@@ -237,15 +237,27 @@ public function storeComment(Request $request, Blog $blog)
237237
->get();
238238

239239
if ($mentionedUsers->isNotEmpty()) {
240-
Notification::send($mentionedUsers, new BlogCommentMentionNotification($blog, $comment, $user));
240+
Notification::send(
241+
$mentionedUsers,
242+
new UserMentionNotification(
243+
sender: $user,
244+
contextTitle: "\"{$blog->title}\"",
245+
content: $comment->content,
246+
url: route('blogs.show', $blog->slug).'#comments',
247+
itemType: 'blog',
248+
itemId: $blog->id,
249+
)
250+
);
241251
$mentionedUserIds = $mentionedUsers->pluck('id')->all();
242252
}
243253
}
244254
}
245255

246256
// Notify the blog author if the commenter is someone else and hasn't already been notified via @mention
247257
if ($blog->user_id !== $user->id && ! in_array($blog->user_id, $mentionedUserIds, true)) {
248-
$blog->user?->notify(new BlogCommentNotification($blog, $comment));
258+
if ($blog->user) {
259+
BlogCommentNotification::sendTo($blog->user, $blog, $comment);
260+
}
249261
}
250262

251263
return back()->with('success', 'Comment posted successfully.');

‎app/Http/Controllers/ChatController.php‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
use App\Models\ChatMessage;
1010
use App\Models\Report;
1111
use App\Models\User;
12-
use App\Notifications\ChatMentionNotification;
1312
use App\Notifications\ChatReportNotification;
13+
use App\Notifications\UserMentionNotification;
1414
use App\Services\ChatProfanityFilter;
1515
use Illuminate\Http\Request;
1616
use Illuminate\Support\Facades\Notification;
@@ -239,7 +239,17 @@ public function store(Request $request)
239239
->get();
240240

241241
if ($mentionedUsers->isNotEmpty()) {
242-
Notification::send($mentionedUsers, new ChatMentionNotification($message, $user));
242+
Notification::send(
243+
$mentionedUsers,
244+
new UserMentionNotification(
245+
sender: $user,
246+
contextTitle: 'Global Chat',
247+
content: $message->content,
248+
url: route('chat.index'),
249+
itemType: 'chat_message',
250+
itemId: $message->id,
251+
)
252+
);
243253
}
244254
}
245255
}

‎app/Http/Controllers/ForumController.php‎

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@
1010
use App\Models\Subject;
1111
use App\Models\User;
1212
use App\Notifications\ForumAnswerNotification;
13-
use App\Notifications\ForumMentionNotification;
1413
use App\Notifications\ForumQuestionPendingNotification;
1514
use App\Notifications\ForumReportNotification;
1615
use App\Notifications\ForumStatusNotification;
1716
use App\Notifications\ForumVoteNotification;
17+
use App\Notifications\UserMentionNotification;
1818
use App\Rules\CleanText;
1919
use Illuminate\Http\JsonResponse;
2020
use Illuminate\Http\RedirectResponse;
@@ -52,7 +52,7 @@ public function index(Request $request): Response
5252
])
5353
->filter($filters);
5454

55-
$posts = $postsQuery->paginate(15)->withQueryString();
55+
$posts = $postsQuery->simplePaginate(15)->withQueryString();
5656

5757
if ($userId = auth()->id()) {
5858
$postIds = $posts->getCollection()->pluck('id');
@@ -466,7 +466,17 @@ public function storeAnswer(Request $request, ForumPost $post): RedirectResponse
466466
->get();
467467

468468
if ($mentionedUsers->isNotEmpty()) {
469-
Notification::send($mentionedUsers, new ForumMentionNotification($post, $answer, $user));
469+
Notification::send(
470+
$mentionedUsers,
471+
new UserMentionNotification(
472+
sender: $user,
473+
contextTitle: "discussion \"{$post->snippet(40)}\"",
474+
content: $answer->body,
475+
url: route('forum.show', $post->slug),
476+
itemType: 'forum_post',
477+
itemId: $post->id,
478+
)
479+
);
470480
$mentionedUserIds = $mentionedUsers->pluck('id')->all();
471481
}
472482
}
@@ -475,11 +485,13 @@ public function storeAnswer(Request $request, ForumPost $post): RedirectResponse
475485
if (isset($parent)) {
476486
$parentAuthor = $parent->user;
477487
if ($parentAuthor && $parentAuthor->id !== $user->id && empty($mentionedUserIds)) {
478-
$parentAuthor->notify(new ForumAnswerNotification($post, $answer, true));
488+
ForumAnswerNotification::sendTo($parentAuthor, $post, $answer, true);
479489
$mentionedUserIds[] = $parentAuthor->id;
480490
}
481491
} elseif ($post->user_id && $post->user_id !== $user->id && ! in_array($post->user_id, $mentionedUserIds, true)) {
482-
$post->user?->notify(new ForumAnswerNotification($post, $answer, false));
492+
if ($post->user) {
493+
ForumAnswerNotification::sendTo($post->user, $post, $answer, false);
494+
}
483495
}
484496

485497
return back()->with('success', 'Answer posted!');

‎app/Http/Controllers/ResourceController.php‎

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@
1111
use App\Models\ResourceVote;
1212
use App\Models\Subject;
1313
use App\Models\User;
14-
use App\Notifications\ResourceCommentMentionNotification;
1514
use App\Notifications\ResourceCommentNotification;
1615
use App\Notifications\ResourceVoteNotification;
16+
use App\Notifications\UserMentionNotification;
1717
use Illuminate\Http\Request;
1818
use Illuminate\Support\Facades\Auth;
1919
use Illuminate\Support\Facades\Cache;
@@ -568,14 +568,26 @@ public function storeComment(Request $request, Resource $resource)
568568
->get();
569569

570570
if ($mentionedUsers->isNotEmpty()) {
571-
Notification::send($mentionedUsers, new ResourceCommentMentionNotification($resource, $comment, $user));
571+
Notification::send(
572+
$mentionedUsers,
573+
new UserMentionNotification(
574+
sender: $user,
575+
contextTitle: "\"{$resource->title}\"",
576+
content: $comment->content,
577+
url: route('resources.show', $resource->id).'#comments',
578+
itemType: 'resource',
579+
itemId: $resource->id,
580+
)
581+
);
572582
$mentionedUserIds = $mentionedUsers->pluck('id')->all();
573583
}
574584
}
575585
}
576586

577587
if ($resource->user_id !== $user->id && ! in_array($resource->user_id, $mentionedUserIds, true)) {
578-
$resource->user?->notify(new ResourceCommentNotification($resource, $comment));
588+
if ($resource->user) {
589+
ResourceCommentNotification::sendTo($resource->user, $resource, $comment);
590+
}
579591
}
580592

581593
return back()->with('success', 'Comment posted successfully.');

‎app/Notifications/BlogCommentMentionNotification.php‎

Lines changed: 0 additions & 42 deletions
This file was deleted.

‎app/Notifications/BlogCommentNotification.php‎

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use App\Models\Blog;
66
use App\Models\BlogComment;
7+
use App\Models\User;
78
use Illuminate\Bus\Queueable;
89
use Illuminate\Contracts\Queue\ShouldQueue;
910
use Illuminate\Notifications\Notification;
@@ -18,18 +19,63 @@ public function __construct(
1819
public BlogComment $comment,
1920
) {}
2021

22+
/**
23+
* Send or roll up a blog comment notification to the blog author.
24+
*/
25+
public static function sendTo(User $targetUser, Blog $blog, BlogComment $comment): void
26+
{
27+
if ($targetUser->id === $comment->user_id) {
28+
return;
29+
}
30+
31+
$blogId = $blog->id;
32+
33+
$unread = $targetUser->unreadNotifications()
34+
->where('type', self::class)
35+
->get()
36+
->first(fn ($n) => ($n->data['blog_id'] ?? null) === $blogId);
37+
38+
$commenterName = $comment->user?->name ?? 'Someone';
39+
$snippet = Str::limit(strip_tags($comment->content), 80);
40+
41+
if ($unread) {
42+
$data = $unread->data;
43+
$count = ($data['count'] ?? 1) + 1;
44+
$others = $count - 1;
45+
$othersText = $others === 1 ? '1 other person' : "{$others} other people";
46+
47+
$data['count'] = $count;
48+
$data['title'] = "{$commenterName} and {$othersText} commented on your blog";
49+
$data['message'] = "{$commenterName}: \"{$snippet}\"";
50+
$data['comment_id'] = $comment->id;
51+
52+
$unread->update([
53+
'data' => $data,
54+
'created_at' => now(),
55+
]);
56+
} else {
57+
$targetUser->notify(new self($blog, $comment));
58+
}
59+
}
60+
2161
public function via(object $notifiable): array
2262
{
2363
return ['database'];
2464
}
2565

2666
public function toArray(object $notifiable): array
2767
{
68+
$commenterName = $this->comment->user?->name ?? 'Someone';
69+
$snippet = Str::limit(strip_tags($this->comment->content), 80);
70+
2871
return [
2972
'type' => 'blog_comment',
3073
'title' => "New comment on \"{$this->blog->title}\"",
31-
'message' => "{$this->comment->user->name}: ".Str::limit($this->comment->content, 80),
74+
'message' => "{$commenterName}: \"{$snippet}\"",
3275
'url' => route('blogs.show', $this->blog->slug).'#comments',
76+
'blog_id' => $this->blog->id,
77+
'comment_id' => $this->comment->id,
78+
'count' => 1,
3379
];
3480
}
3581
}

‎app/Notifications/ForumAnswerNotification.php‎

Lines changed: 43 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use App\Models\ForumAnswer;
66
use App\Models\ForumPost;
7+
use App\Models\User;
78
use Illuminate\Bus\Queueable;
89
use Illuminate\Contracts\Queue\ShouldQueue;
910
use Illuminate\Notifications\Notification;
@@ -20,20 +21,52 @@ public function __construct(
2021
) {}
2122

2223
/**
23-
* Get the notification's delivery channels.
24-
*
25-
* @return array<int, string>
24+
* Send or roll up a forum answer/reply notification to the recipient.
2625
*/
26+
public static function sendTo(User $targetUser, ForumPost $post, ForumAnswer $answer, bool $isReply = false): void
27+
{
28+
if ($targetUser->id === $answer->user_id) {
29+
return;
30+
}
31+
32+
$postId = $post->id;
33+
34+
$unread = $targetUser->unreadNotifications()
35+
->where('type', self::class)
36+
->get()
37+
->first(fn ($n) => ($n->data['post_id'] ?? null) === $postId && ($n->data['is_reply'] ?? false) === $isReply);
38+
39+
$authorName = $answer->user?->name ?? 'Someone';
40+
$snippet = Str::limit(strip_tags($answer->body), 80);
41+
$postSnippet = $post->snippet(40);
42+
43+
if ($unread) {
44+
$data = $unread->data;
45+
$count = ($data['count'] ?? 1) + 1;
46+
$others = $count - 1;
47+
$othersText = $others === 1 ? '1 other person' : "{$others} other people";
48+
49+
$action = $isReply ? 'replied to your answer on' : 'answered your question';
50+
51+
$data['count'] = $count;
52+
$data['title'] = "{$authorName} and {$othersText} {$action} \"{$postSnippet}\"";
53+
$data['message'] = "{$authorName}: \"{$snippet}\"";
54+
$data['answer_id'] = $answer->id;
55+
56+
$unread->update([
57+
'data' => $data,
58+
'created_at' => now(),
59+
]);
60+
} else {
61+
$targetUser->notify(new self($post, $answer, $isReply));
62+
}
63+
}
64+
2765
public function via(object $notifiable): array
2866
{
2967
return ['database'];
3068
}
3169

32-
/**
33-
* Get the array representation of the notification (Database in-app bell).
34-
*
35-
* @return array<string, mixed>
36-
*/
3770
public function toArray(object $notifiable): array
3871
{
3972
$authorName = $this->answer->user?->name ?? 'Someone';
@@ -50,6 +83,8 @@ public function toArray(object $notifiable): array
5083
'url' => route('forum.show', $this->post->slug),
5184
'post_id' => $this->post->id,
5285
'answer_id' => $this->answer->id,
86+
'is_reply' => $this->isReply,
87+
'count' => 1,
5388
];
5489
}
5590
}

0 commit comments

Comments
 (0)