forked from FriendsOfFlarum/best-answer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNotifyCommand.php
More file actions
141 lines (112 loc) · 4.58 KB
/
NotifyCommand.php
File metadata and controls
141 lines (112 loc) · 4.58 KB
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
<?php
/*
* This file is part of fof/best-answer.
*
* Copyright (c) FriendsOfFlarum.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace FoF\BestAnswer\Console;
use Carbon\Carbon;
use Flarum\Discussion\Discussion;
use Flarum\Notification\NotificationSyncer;
use Flarum\Settings\SettingsRepositoryInterface;
use Flarum\Tags\Tag;
use FoF\BestAnswer\Notification\SelectBestAnswerBlueprint;
use Illuminate\Console\Command;
use Throwable;
class NotifyCommand extends Command
{
/**
* @var SettingsRepositoryInterface
*/
private $settings;
/**
* @var NotificationSyncer
*/
private $notifications;
public function __construct(SettingsRepositoryInterface $settings, NotificationSyncer $notifications)
{
parent::__construct();
$this->settings = $settings;
$this->notifications = $notifications;
}
/**
* @var string
*/
protected $signature = 'fof:best-answer:notify';
/**
* @var string
*/
protected $description = 'After a configurable number of days, notifies OP of discussions with no post selected as best answer to select one.';
public function handle()
{
$days = (int) $this->settings->get('fof-best-answer.select_best_answer_reminder_days');
$canSelectOwn = (bool) (int) $this->settings->get('fof-best-answer.allow_select_own_post');
$time = Carbon::now()->subDays($days);
// set a max time period to go back, so we don't spam really old discussions too.
$timeLimit = Carbon::now()->subDays(7 + $days);
if ($days <= 0) {
$this->info('Reminders are disabled');
return;
}
$this->info('Looking at discussions before '.$time->toDateTimeString().' but not older than '.$timeLimit->toDateTimeString());
$tags = Tag::where('qna_reminders', true)->pluck('id');
$query = Discussion::query()
->leftJoin('discussion_tag', 'discussion_tag.discussion_id', '=', 'discussions.id')
->whereIn('discussion_tag.tag_id', $tags)
->whereNull('discussions.best_answer_post_id')
->whereNull('discussions.hidden_at')
->where('discussions.best_answer_notified', false)
->where('discussions.comment_count', '>', 1)
->where('discussions.is_private', 0)
->whereDate('discussions.created_at', '<', $time->toIso8601String())
->whereDate('discussions.created_at', '>', $timeLimit->toIso8601String());
$count = $query->count();
if ($count == 0) {
$this->info('Nothing to do');
return;
}
$errors = [];
$query->chunkById(20, function ($discussions) use ($canSelectOwn, &$errors) {
// Filter out discussions where the user can't select a post as best answer.
// - The user must have permission to select a best answer on their own discussion
// - The user must be able to select a post, whether they can select any post (including their own) or not.
$discussions = $discussions->filter(function ($d) use ($canSelectOwn) {
$hasPermission = $d->user->can('discussion.selectBestAnswerOwnDiscussion', $d);
$canSelectPosts = $canSelectOwn || $d->posts()->where('user_id', '!=', $d->user_id)->count() != 0;
return $hasPermission && $canSelectPosts;
});
/*
* @var $discussions Discussion[]
*/
$this->output->write('<info>Sending '.count($discussions).' notifications </info>');
foreach ($discussions as $d) {
try {
$this->notifications->sync(
new SelectBestAnswerBlueprint($d),
[$d->user]
);
$this->output->write('<info>#</info>');
$d->best_answer_notified = true;
$d->save();
} catch (Throwable $e) {
$this->output->write('<error>#</error>');
$errors[] = $e;
}
}
$this->line('');
});
if (count($errors) > 0) {
$this->line("\n");
$this->alert('Failed to send '.count($errors).' notifications');
$this->warn('');
foreach ($errors as $i => $e) {
$n = $i + 1;
$this->output->writeln("<warning>$n >>>>>></warning> <error>$e</error>");
$this->line('');
}
}
}
}