/
SendNotificationWhenDiscussionIsStarted.php
72 lines (61 loc) · 2.07 KB
/
SendNotificationWhenDiscussionIsStarted.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
<?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\Discussion\Discussion;
use Flarum\Notification\NotificationSyncer;
use Flarum\User\User;
use FoF\FollowTags\Notifications\NewDiscussionBlueprint;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Collection;
class SendNotificationWhenDiscussionIsStarted implements ShouldQueue
{
use Queueable;
use SerializesModels;
/**
* @var Discussion
*/
protected $discussion;
public function __construct(Discussion $discussion)
{
$this->discussion = $discussion;
}
public function handle(NotificationSyncer $notifications)
{
if (!$this->discussion->exists) {
return;
}
/**
* @var Collection
* @var $tagIds Collection
*/
$tags = $this->discussion->tags;
$tagIds = $tags->map->id;
$firstPost = $this->discussion->firstPost ?? $this->discussion->posts()->orderBy('number')->first();
if ($tags->isEmpty() || !$firstPost) {
return;
}
$notify = User::where('users.id', '!=', $this->discussion->user_id)
->join('tag_user', 'tag_user.user_id', '=', 'users.id')
->whereIn('tag_user.tag_id', $tagIds->all())
->whereIn('tag_user.subscription', ['follow', 'lurk'])
->get()
->reject(function ($user) use ($firstPost, $tags) {
return $tags->map->stateFor($user)->map->subscription->contains('ignore')
|| !$this->discussion->newQuery()->whereVisibleTo($user)->find($this->discussion->id)
|| !$firstPost->isVisibleTo($user);
});
$notifications->sync(
new NewDiscussionBlueprint($this->discussion, $firstPost),
$notify->all()
);
}
}