-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
SendNotificationWhenReplyIsPosted.php
74 lines (62 loc) · 2.01 KB
/
SendNotificationWhenReplyIsPosted.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
<?php
/*
* This file is part of fof/follow-tags.
*
* Copyright (c) FriendsOfFlarum.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace FoF\FollowTags\Jobs;
use Flarum\Notification\NotificationSyncer;
use Flarum\Post\Post;
use Flarum\User\User;
use FoF\FollowTags\Notifications\NewPostBlueprint;
use Illuminate\Support\Collection;
class SendNotificationWhenReplyIsPosted extends NotificationJob
{
/**
* @var Post
*/
protected $post;
/**
* @var int
*/
protected $lastPostNumber;
public function __construct(Post $post, int $lastPostNumber)
{
$this->post = $post;
$this->lastPostNumber = $lastPostNumber;
}
public function handle(NotificationSyncer $notifications)
{
if (!$this->post->exists) {
return;
}
$discussion = $this->post->discussion;
/**
* @var Collection<Tag>|null $tags
*
* @phpstan-ignore-next-line
*/
$tags = $discussion->tags;
$tagIds = $tags->map->id;
if (!$tags || $tags->isEmpty()) {
return;
}
$notify = $this->post->discussion->readers()
// The `select(...)` part is not mandatory here, but makes the query safer. See #55.
->select('users.*')
->where('users.id', '!=', $this->post->user_id)
->join('tag_user', 'tag_user.user_id', '=', 'users.id')
->whereIn('tag_user.tag_id', $tagIds->all())
->where('tag_user.subscription', 'lurk')
->where('discussion_user.last_read_post_number', '>=', $this->lastPostNumber - 1)
->get()
->reject(function (User $user) use ($tags) {
return $tags->map->stateFor($user)->map->subscription->contains('ignore')
|| !$this->post->isVisibleTo($user);
});
$this->sync($notifications, new NewPostBlueprint($this->post), $notify);
}
}