|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace App\Services; |
| 4 | + |
| 5 | +use App\Models\ActivityEvent; |
| 6 | +use Carbon\Carbon; |
| 7 | +use Illuminate\Support\Collection; |
| 8 | +use Illuminate\Support\Facades\Cache; |
| 9 | +use Illuminate\Support\Str; |
| 10 | +use Statamic\Entries\Entry; |
| 11 | +use Statamic\Facades\Entry as Entries; |
| 12 | + |
| 13 | +class ActivityService |
| 14 | +{ |
| 15 | + /** |
| 16 | + * @return Collection<string, ActivityEvent> |
| 17 | + */ |
| 18 | + public function events(): Collection |
| 19 | + { |
| 20 | + $lastModificationDate = $this->lastModificationDate(); |
| 21 | + |
| 22 | + return Cache::remember( |
| 23 | + 'events_' . $lastModificationDate->timestamp, |
| 24 | + 86400, // 24 hours |
| 25 | + fn () => $this->getEvents(), |
| 26 | + ); |
| 27 | + } |
| 28 | + |
| 29 | + public function lastModificationDate(): Carbon |
| 30 | + { |
| 31 | + return collect([ |
| 32 | + Entries::whereCollection('tasks')->sortByDesc('completion_date')->first()->completion_date, |
| 33 | + Entries::whereCollection('tasks')->sortByDesc('publication_date')->first()->publication_date, |
| 34 | + Entries::whereCollection('comments')->sortByDesc('publication_date')->first()->publication_date, |
| 35 | + Entries::whereCollection('posts')->sortByDesc('publication_date')->first()->publication_date, |
| 36 | + ]) |
| 37 | + ->sort() |
| 38 | + ->last(); |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * @return Collection<string,ActivityEvent> |
| 43 | + */ |
| 44 | + private function getEvents(): Collection |
| 45 | + { |
| 46 | + $entries = collect(Entries::whereInCollection(['tasks', 'comments', 'posts'])->all()); |
| 47 | + |
| 48 | + $this->fillRelations($entries); |
| 49 | + |
| 50 | + return $entries |
| 51 | + ->flatMap(fn ($entry) => call_user_func([$this, 'createEventsFrom' . Str::studly($entry->value('blueprint'))], $entry)) |
| 52 | + ->sortByDesc->date; |
| 53 | + } |
| 54 | + |
| 55 | + private function fillRelations(Collection $entries): void |
| 56 | + { |
| 57 | + $comments = $entries->where('blueprint', 'comment'); |
| 58 | + |
| 59 | + foreach ($entries->where('blueprint', 'task') as $task) { |
| 60 | + $taskLink = 'entry::' . $task->id; |
| 61 | + $taskComments = $comments |
| 62 | + ->filter(fn ($comment) => $comment->value('task') === $taskLink) |
| 63 | + ->sortBy->publication_date |
| 64 | + ->values(); |
| 65 | + |
| 66 | + foreach ($taskComments as $index => $comment) { |
| 67 | + $comment->position = $index + 2; |
| 68 | + } |
| 69 | + |
| 70 | + $task->totalComments = $taskComments->count(); |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * @return array<ActivityEvent> |
| 76 | + */ |
| 77 | + private function createEventsFromTask(Entry $task): array |
| 78 | + { |
| 79 | + $url = url($task->url); |
| 80 | + |
| 81 | + $events = [new ActivityEvent( |
| 82 | + date: $task->publication_date, |
| 83 | + title: "Started \"{$task->title}\"", |
| 84 | + description: "Started <a href=\"{$url}\">{$task->title}</a>", |
| 85 | + longDescription: "<p>I just started a new task: {$task->title}</p>{$task->content}", |
| 86 | + url: $url, |
| 87 | + )]; |
| 88 | + |
| 89 | + if ($task->completion_date) { |
| 90 | + $url .= '#comment-' . $task->totalComments + 2; |
| 91 | + |
| 92 | + $events[] = new ActivityEvent( |
| 93 | + date: $task->completion_date, |
| 94 | + title: "Completed \"{$task->title}\"", |
| 95 | + description: "Completed <a href=\"{$url}\">{$task->title}</a>", |
| 96 | + longDescription: "<p>I just completed the task {$task->title}.</p>", |
| 97 | + url: $url, |
| 98 | + ); |
| 99 | + } |
| 100 | + |
| 101 | + return $events; |
| 102 | + } |
| 103 | + |
| 104 | + /** |
| 105 | + * @return array<ActivityEvent> |
| 106 | + */ |
| 107 | + private function createEventsFromComment(Entry $comment): array |
| 108 | + { |
| 109 | + $task = $comment->task->value(); |
| 110 | + $url = url($task->url) . '#comment-' . $comment->position; |
| 111 | + |
| 112 | + return [new ActivityEvent( |
| 113 | + date: $comment->publication_date, |
| 114 | + title: "Commented on \"{$task->title}\"", |
| 115 | + description: "Commented on <a href=\"{$url}\">{$task->title}</a>", |
| 116 | + longDescription: "<p>I just added the following comment on {$task->title}:</p>{$comment->content}", |
| 117 | + url: $url |
| 118 | + )]; |
| 119 | + } |
| 120 | + |
| 121 | + /** |
| 122 | + * @return array<ActivityEvent> |
| 123 | + */ |
| 124 | + private function createEventsFromPost(Entry $post): array |
| 125 | + { |
| 126 | + $url = url($post->url); |
| 127 | + |
| 128 | + return [new ActivityEvent( |
| 129 | + date: $post->publication_date, |
| 130 | + title: "Published \"{$post->title}\"", |
| 131 | + description: "Published <a href=\"{$url}\">{$post->title}</a>", |
| 132 | + longDescription: "<p>I just published a new blog post: <a href=\"{$url}\">{$post->title}</a></p>", |
| 133 | + url: $url |
| 134 | + )]; |
| 135 | + } |
| 136 | +} |
0 commit comments