-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathSendEmptyWikiNotificationsJob.php
More file actions
74 lines (64 loc) · 2.47 KB
/
SendEmptyWikiNotificationsJob.php
File metadata and controls
74 lines (64 loc) · 2.47 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
<?php
namespace App\Jobs;
use App\Notifications\EmptyWikiNotification;
use App\Wiki;
use App\WikiNotificationSentRecord;
use Carbon\CarbonImmutable;
use Illuminate\Contracts\Queue\ShouldBeUnique;
use Illuminate\Support\Facades\Log;
class SendEmptyWikiNotificationsJob extends Job implements ShouldBeUnique
{
public function handle (): void
{
$wikis = Wiki::with(['wikiLifecycleEvents'])
->has('wikiLifecycleEvents')
->get();
foreach ($wikis as $wiki) {
try {
if ($this->checkIfWikiIsOldAndEmpty($wiki)) {
$this->sendEmptyWikiNotification($wiki);
}
} catch (\Exception $exception) {
Log::error(
'Failure processing wiki '.$wiki->getAttribute('domain').' for EmptyWikiNotification check: '.$exception->getMessage()
);
$this->fail();
}
}
}
public function checkIfWikiIsOldAndEmpty(Wiki $wiki)
{
// Calculate how many days has passed since the wiki instance was first created
$emptyDaysThreshold = config('wbstack.wiki_empty_notification_threshold');
$createdAt = $wiki->created_at;
$now = CarbonImmutable::now();
$emptyWikiDays = $createdAt->diffInDays($now);
$firstEdited = $wiki->wikiLifecycleEvents->first_edited;
$emptyWikiNotificationCount = WikiNotificationSentRecord::where([
'wiki_id' => $wiki->id,
'notification_type' => EmptyWikiNotification::TYPE
])->count();
if (
$firstEdited == null &&
$emptyWikiDays >= $emptyDaysThreshold &&
$emptyWikiNotificationCount == 0
) {
return true;
} else {
return false;
}
}
public function sendEmptyWikiNotification (Wiki $wiki): void
{
$wikiManagers = $wiki->wikiManagersWithEmail()->get();
foreach($wikiManagers as $wikiManager) {
// we think the order here matters, so that people do not get spammed in case creating a record fails
// discussed here https://github.com/wbstack/api/pull/656#discussion_r1392443739
$wiki->wikiNotificationSentRecords()->create([
'notification_type' => EmptyWikiNotification::TYPE,
'user_id' => $wikiManager->pivot->user_id,
]);
$wikiManager->notify(new EmptyWikiNotification($wiki->sitename));
}
}
}