-
-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
130 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
<?php | ||
namespace Coyote\Presenter\Homepage; | ||
|
||
use Coyote\Activity; | ||
use Coyote\Post; | ||
use Coyote\Post\Comment; | ||
use Coyote\Services\Media\MediaInterface; | ||
use Coyote\Services\UrlBuilder; | ||
use Illuminate\Database\Eloquent; | ||
use function cdn; | ||
|
||
class Timeline | ||
{ | ||
/** @var array[] */ | ||
public array $activities; | ||
|
||
public function __construct(Eloquent\Collection $activities) | ||
{ | ||
$this->activities = $activities | ||
->map(fn($activity) => $this->activity($activity)) | ||
->toArray(); | ||
} | ||
|
||
private function activity(Activity $activity): array | ||
{ | ||
return [ | ||
'excerpt' => $activity->excerpt, | ||
'created_at' => $activity->created_at, | ||
'user_id' => $activity->user_id, | ||
'headline' => $this->headline($activity), | ||
'object' => $this->objectType($activity), | ||
'type' => $this->objectType($activity), | ||
'icon' => $this->icon($activity), | ||
'user' => [ | ||
'id' => $activity->user_id, | ||
'name' => $this->userName($activity), | ||
'photo' => $this->userPhoto($activity), | ||
'profile' => route('profile', [$activity->user_id]) | ||
] | ||
]; | ||
} | ||
|
||
private function objectType(Activity $activity): string | ||
{ | ||
return [Post::class => 'post', Comment::class => 'comment'][$activity->content_type]; | ||
} | ||
|
||
private function icon(Activity $activity): string | ||
{ | ||
$icons = [ | ||
Comment::class => 'far fa-comment', | ||
Post::class => 'fas fa-quote-right', | ||
]; | ||
return $icons[$activity->content_type]; | ||
} | ||
|
||
public function headline(Activity $activity): string | ||
{ | ||
return trans("activity.headline.{$this->objectType($activity)}", [ | ||
'user' => $this->user($activity), | ||
'topic' => $this->topic($activity) | ||
]); | ||
} | ||
|
||
public function user(Activity $activity): string | ||
{ | ||
if ($activity->user_id) { | ||
return link_to_route('profile', $activity->user->name, ['user_trashed' => $activity->user_id]); | ||
} | ||
return $activity->user_name; | ||
} | ||
|
||
private function userName(Activity $activity): string | ||
{ | ||
if ($activity->user_id) { | ||
return $activity->user->name; | ||
} | ||
return 'guest'; | ||
} | ||
|
||
public function topic(Activity $activity): string | ||
{ | ||
if ($activity->content_type === Post::class) { | ||
$activity->content->setRelations(['topic' => $activity->topic, 'forum' => $activity->forum]); | ||
return link_to(UrlBuilder::post($activity->content), $activity->topic->title); | ||
} | ||
$post = (new Post) | ||
->forceFill(['id' => $activity->content->post_id]) | ||
->setRelations(['topic' => $activity->topic, 'forum' => $activity->forum]); | ||
$activity->content->setRelations(['post' => $post]); | ||
return link_to(UrlBuilder::postComment($activity->content), $post->topic->title); | ||
} | ||
|
||
public function userPhoto(Activity $activity): string | ||
{ | ||
if ($activity->user_id) { | ||
if ($activity->user->photo->getFilename()) { | ||
return $activity->user->photo->url(); | ||
} | ||
} | ||
return cdn('img/avatar.png'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters