diff --git a/app/Http/Controllers/Microblog/HomeController.php b/app/Http/Controllers/Microblog/HomeController.php index 737023d9c..a725fc266 100644 --- a/app/Http/Controllers/Microblog/HomeController.php +++ b/app/Http/Controllers/Microblog/HomeController.php @@ -7,126 +7,85 @@ use Coyote\Http\Resources\MicroblogCollection; use Coyote\Http\Resources\MicroblogResource; use Coyote\Http\Resources\UserResource; -use Coyote\Repositories\Contracts\MicroblogRepositoryInterface as MicroblogRepository; +use Coyote\Repositories\Eloquent\MicroblogRepository; use Coyote\Services\Microblogs\Builder; use Coyote\Tag; use Illuminate\Http\Request; +use Illuminate\View\View; class HomeController extends BaseController { use CacheFactory; - /** - * @var MicroblogRepository - */ - private MicroblogRepository $microblog; - - /** - * @var Builder - */ private Builder $builder; - /** - * @param MicroblogRepository $microblog - * @param Builder $builder - */ - public function __construct(MicroblogRepository $microblog) + public function __construct(private MicroblogRepository $microblog) { parent::__construct(); - - $this->microblog = $microblog; $this->breadcrumb->push('Mikroblog', route('microblog.home')); - $this->middleware(function (Request $request, $next) { $this->builder = resolve(Builder::class); - return $next($request); }); } - /** - * @return \Illuminate\View\View - */ - public function index(RenderParams $renderParams = null) + public function index(RenderParams $renderParams = null): View { - $paginator = $this->builder->orderById()->paginate(); - - // let's cache microblog tags. we don't need to run this query every time - $tags = $this->getCacheFactory()->remember('microblog:tags', 30 * 60, function () { - return $this->microblog->getTags(); - }); - - [$tech, $others] = $tags->partition(function (Tag $tag) { - return $tag->category_id === Tag\Category::LANGUAGE; - }); - return $this->view('microblog.home', [ - 'flags' => $this->flags(), - 'count' => $this->microblog->count(), - 'count_user' => $this->microblog->countForUser($this->userId), - 'pagination' => new MicroblogCollection($paginator), - 'route' => request()->route()->getName(), - 'popular_tags' => $this->microblog->popularTags($this->userId), - 'recommended_users' => UserResource::collection($this->microblog->recommendedUsers($this->userId)), - 'tags' => [ - 'tech' => $tech, - 'others' => $others->splice(0, 10) - ], - 'render_params' => $renderParams, + 'flags' => $this->flags(), + 'count' => $this->microblog->count(), + 'count_user' => $this->microblog->countForUser($this->userId), + 'pagination' => new MicroblogCollection($this->builder->orderById()->paginate()), + 'route' => request()->route()->getName(), + 'popular_tags' => $this->microblog->popularTags($this->userId), + 'recommended_users' => UserResource::collection($this->microblog->recommendedUsers($this->userId)), + 'tags' => $this->tags(), + 'render_params' => $renderParams, ]); } - /** - * @param string $tag - * @return \Illuminate\View\View - */ - public function tag(string $tag) + public function tag(string $tag): View { $this->breadcrumb->push('Wpisy z tagiem: ' . $tag, route('microblog.tag', [$tag])); - $this->builder->withTag($tag); - $renderParams = new RenderParams(); $renderParams->tagName = $tag; - return $this->index($renderParams); } - /** - * @return \Illuminate\View\View - */ - public function mine() + public function mine(): View { $this->breadcrumb->push('Moje wpisy', route('microblog.mine')); - $this->builder->onlyUsers($this->auth); - return $this->index(); } - /** - * @param $id - * @return \Illuminate\View\View - */ - public function show($id) + public function show(int $id): View { $microblog = $this->builder->one($id); - abort_if(!is_null($microblog->parent_id), 404); - $excerpt = excerpt($microblog->html); - $this->breadcrumb->push($excerpt, route('microblog.view', [$microblog->id])); - MicroblogResource::withoutWrapping(); - $resource = new MicroblogResource($microblog); $resource->preserverKeys(); - return $this->view('microblog.view')->with([ - 'flags' => $this->flags(), - 'microblog' => $resource, - 'excerpt' => $excerpt + 'flags' => $this->flags(), + 'microblog' => $resource, + 'excerpt' => $excerpt, + 'popular_tags' => $this->microblog->popularTags($this->userId), + 'recommended_users' => UserResource::collection($this->microblog->recommendedUsers($this->userId)), + 'tags' => $this->tags(), ]); } + + private function tags(): array + { + $tags = $this->getCacheFactory()->remember('microblog:tags', 30 * 60, fn() => $this->microblog->getTags()); + [$tech, $others] = $tags->partition(fn(Tag $tag) => $tag->category_id === Tag\Category::LANGUAGE); + return [ + 'tech' => $tech, + 'others' => $others->splice(0, 10), + ]; + } } diff --git a/resources/views/microblog/aside.twig b/resources/views/microblog/aside.twig new file mode 100644 index 000000000..e50e787a4 --- /dev/null +++ b/resources/views/microblog/aside.twig @@ -0,0 +1,62 @@ + diff --git a/resources/views/microblog/home.twig b/resources/views/microblog/home.twig index 37791ed0f..e2463c0c6 100644 --- a/resources/views/microblog/home.twig +++ b/resources/views/microblog/home.twig @@ -67,69 +67,7 @@ - - + {% include 'microblog.aside' %} diff --git a/resources/views/microblog/view.twig b/resources/views/microblog/view.twig index 610b3dbd5..66e1ec57e 100644 --- a/resources/views/microblog/view.twig +++ b/resources/views/microblog/view.twig @@ -11,16 +11,17 @@ {% block container %}
-
+
- + {% include 'microblog.aside' %}
{% endblock %} -