Skip to content

Commit

Permalink
Timeline, version 6
Browse files Browse the repository at this point in the history
  • Loading branch information
danon committed Oct 6, 2023
1 parent 1a6ec32 commit a696adb
Show file tree
Hide file tree
Showing 10 changed files with 70 additions and 156 deletions.
2 changes: 1 addition & 1 deletion app/Http/Controllers/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ abstract class Controller extends BaseController
protected $userId;

/**
* @var \Coyote\User
* @var ?\Coyote\User
*/
protected $auth;

Expand Down
12 changes: 10 additions & 2 deletions app/Http/Controllers/HomeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Coyote\Repositories\Criteria\Forum\SkipHiddenCategories;
use Coyote\Repositories\Criteria\Topic;
use Coyote\Services\Flags;
use Coyote\Services\Helper\DateDifference;
use Coyote\Services\Microblogs\Builder;
use Coyote\Services\Session\Renderer;
use Coyote\Services\Widgets\Patronage;
Expand Down Expand Up @@ -41,15 +42,14 @@ public function index(): View

$this->activity->pushCriteria(new Forum\OnlyThoseWithAccess($this->auth));
$this->activity->pushCriteria(new SkipHiddenCategories($this->userId));
$timeline = new Timeline($this->activity->latest(30));

return $this->view('home', [
'flags' => $this->flags(),
'microblogs' => $this->getMicroblogs(),
'interesting' => $this->topic->interesting(),
'newest' => $this->topic->newest(),
'viewers' => $this->getViewers(),
'timeline' => $timeline,
'timeline' => new Timeline($this->activity->latest(30), $this->dateFormat()),
'reputation' => $cache->remember('homepage:reputation', 30 * 60, fn() => [
'month' => $this->reputation->monthly(),
'year' => $this->reputation->yearly(),
Expand Down Expand Up @@ -85,4 +85,12 @@ private function flags(): array
->get();
return FlagResource::collection($flags)->toArray($this->request);
}

public function dateFormat(): DateDifference
{
if ($this->auth !== null) {
return new DateDifference('%Y-%m-%d %H:%M', true);
}
return new DateDifference($this->auth->date_format, true);
}
}
4 changes: 2 additions & 2 deletions app/Http/Forms/Auth/RegisterForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ public function buildForm(): void
])
->add('terms', 'checkbox', [
'rules' => 'accepted',
'label' => new TwigLiteral((new UserSettings)->termsAndPrivacyPolicyAgreement())
'label' => TwigLiteral::fromHtml((new UserSettings)->termsAndPrivacyPolicyAgreement())
])
->add('marketing_agreement', 'checkbox', [
'label' => new TwigLiteral((new UserSettings)->marketingAgreement())
'label' => TwigLiteral::fromHtml((new UserSettings)->marketingAgreement())
])
->add('email_confirmation', 'honeypot')
->add('submit', 'submit', [
Expand Down
6 changes: 3 additions & 3 deletions app/Http/Forms/User/SettingsForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,16 +93,16 @@ public function buildForm(): void
])
->add('terms', 'checkbox', [
'rules' => 'boolean',
'label' => new TwigHtml((new UserSettings)->termsAndPrivacyPolicyAgreement()),
'label' => TwigLiteral::fromHtml((new UserSettings)->termsAndPrivacyPolicyAgreement()),
'attr' => ['disabled' => 'disabled', 'checked' => 'checked']
])
->add('marketing_agreement', 'checkbox', [
'rules' => 'boolean',
'label' => new TwigLiteral((new UserSettings)->marketingAgreement())
'label' => TwigLiteral::fromHtml((new UserSettings)->marketingAgreement())
])
->add('newsletter_agreement', 'checkbox', [
'rules' => 'boolean',
'label' => new TwigHtml((new UserSettings)->newsletterAgreement())
'label' => TwigLiteral::fromHtml((new UserSettings)->newsletterAgreement())
])
->add('firm', 'text', [
'rules' => 'nullable|string|max:100',
Expand Down
85 changes: 0 additions & 85 deletions app/Http/Resources/ActivityResource.php

This file was deleted.

42 changes: 16 additions & 26 deletions app/Models/Activity.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,62 +2,52 @@

namespace Coyote;

use Carbon\Carbon;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\MorphTo;

/**
* @property Carbon $created_at
* @property string $content_type
* @property string $excerpt
* @property string $user_name
* @property string $created_at
*
* @property int $user_id
* @property int $content_id
*
* @property User $user
* @property Topic $topic
* @property User $user
* @property Forum $forum
* @property Post|Post\Comment $content
*/
class Activity extends Model
{
/**
* @var bool
*/
/** @var bool */
public $timestamps = false;

/**
* @var array
*/
protected $fillable = ['created_at', 'forum_id', 'user_id', 'topic_id', 'content_type', 'content_id', 'excerpt', 'user_name'];
/** @var array */
protected $fillable = [
'content_type', 'excerpt', 'user_name', 'created_at',

'user_id', 'topic_id', 'forum_id', 'content_id',
];

/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function user()
public function user(): BelongsTo
{
return $this->belongsTo(User::class);
}

/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function topic()
public function topic(): BelongsTo
{
return $this->belongsTo(Topic::class);
}

/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function forum()
public function forum(): BelongsTo
{
return $this->belongsTo(Forum::class);
}

/**
* @return \Illuminate\Database\Eloquent\Relations\MorphTo
*/
public function content()
public function content(): MorphTo
{
return $this->morphTo()->withTrashed();
}
Expand Down
56 changes: 27 additions & 29 deletions app/Presenter/Homepage/Timeline.php
Original file line number Diff line number Diff line change
@@ -1,21 +1,25 @@
<?php
namespace Coyote\Presenter\Homepage;

use Carbon\Carbon;
use Coyote\Activity;
use Coyote\Post;
use Coyote\Post\Comment;
use Coyote\Services\Media\MediaInterface;
use Coyote\Services\Helper\DateDifference;
use Coyote\Services\UrlBuilder;
use Coyote\View\Twig\TwigLiteral;
use Illuminate\Database\Eloquent;
use function cdn;

class Timeline
{
private DateDifference $dateDifference;
/** @var array[] */
public array $activities;

public function __construct(Eloquent\Collection $activities)
public function __construct(Eloquent\Collection $activities, DateDifference $dateDifference)
{
$this->dateDifference = $dateDifference;
$this->activities = $activities
->map(fn($activity) => $this->activity($activity))
->toArray();
Expand All @@ -24,14 +28,12 @@ public function __construct(Eloquent\Collection $activities)
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' => [
'excerpt' => $activity->excerpt,
'date' => $this->dateDifference->format(new Carbon($activity->created_at)),
'headline' => $this->headline($activity),
'type' => $this->byType($activity, [Post::class => 'post', Comment::class => 'comment']),
'icon' => $this->byType($activity, [Comment::class => 'far fa-comment', Post::class => 'fas fa-quote-right']),
'user' => [
'id' => $activity->user_id,
'name' => $this->userName($activity),
'photo' => $this->userPhoto($activity),
Expand All @@ -40,29 +42,20 @@ private function activity(Activity $activity): array
];
}

private function objectType(Activity $activity): string
private function headline(Activity $activity): TwigLiteral
{
return [Post::class => 'post', Comment::class => 'comment'][$activity->content_type];
return $this->headlineString($this->user($activity), $this->topic($activity), $this->byType($activity, [
Post::class => 'post',
Comment::class => 'komentarz',
]));
}

private function icon(Activity $activity): string
private function headlineString(string $username, string $topic, string $activity): TwigLiteral
{
$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)
]);
return new TwigLiteral("$username dodał <b>$activity</b> w $topic");
}

public function user(Activity $activity): string
private function user(Activity $activity): string
{
if ($activity->user_id) {
return link_to_route('profile', $activity->user->name, ['user_trashed' => $activity->user_id]);
Expand All @@ -78,7 +71,7 @@ private function userName(Activity $activity): string
return 'guest';
}

public function topic(Activity $activity): string
private function topic(Activity $activity): string
{
if ($activity->content_type === Post::class) {
$activity->content->setRelations(['topic' => $activity->topic, 'forum' => $activity->forum]);
Expand All @@ -91,7 +84,7 @@ public function topic(Activity $activity): string
return link_to(UrlBuilder::postComment($activity->content), $post->topic->title);
}

public function userPhoto(Activity $activity): string
private function userPhoto(Activity $activity): string
{
if ($activity->user_id) {
if ($activity->user->photo->getFilename()) {
Expand All @@ -100,4 +93,9 @@ public function userPhoto(Activity $activity): string
}
return cdn('img/avatar.png');
}

private function byType(Activity $activity, array $values): mixed
{
return $values[$activity->content_type];
}
}
6 changes: 2 additions & 4 deletions app/Services/Helper/DateDifference.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,8 @@

class DateDifference
{
/** @var string */
private $format;
/** @var bool */
private $diffForHumans;
private string $format;
private bool $diffForHumans;

public function __construct(string $format, bool $diffForHumans)
{
Expand Down
9 changes: 7 additions & 2 deletions app/View/Twig/TwigLiteral.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,17 @@

class TwigLiteral
{
public function __construct(private Html $html)
public function __construct(private string $content)
{
}

public static function fromHtml(Html $html): self
{
return new TwigLiteral($html->content);
}

public function __toString(): string
{
return $this->html->content;
return $this->content;
}
}
Loading

0 comments on commit a696adb

Please sign in to comment.