Skip to content

Commit

Permalink
Revert "Paginate microblogs in homepage #590"
Browse files Browse the repository at this point in the history
This reverts commit b0e16df.
  • Loading branch information
danon committed Jan 24, 2024
1 parent 22b07b9 commit 8bf5de5
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 70 deletions.
17 changes: 11 additions & 6 deletions app/Http/Controllers/HomeController.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
<?php

namespace Coyote\Http\Controllers;

use Coyote\Http\Resources\ActivityResource as ActivityResource;
use Coyote\Http\Resources\Api\MicroblogResource;
use Coyote\Http\Resources\FlagResource;
use Coyote\Http\Resources\MicroblogCollection;
use Coyote\Microblog;
Expand Down Expand Up @@ -37,34 +39,37 @@ public function index(): View
$this->topic->pushCriteria(new SkipHiddenCategories($this->userId));
return $this->view('home', [
'flags' => $this->flags(),
'pagination' => $this->getMicroblogs(),
'microblogs' => $this->getMicroblogs(),
'interesting' => $this->topic->interesting(),
'newest' => $this->topic->newest(),
'viewers' => $this->getViewers(),
'activities' => $this->getActivities(),
'reputation' => $cache->remember('homepage:reputation', 30 * 60, fn() => [
'month' => $this->reputation->monthly(),
'year' => $this->reputation->yearly(),
'total' => $this->reputation->total(),
]),
'total' => $this->reputation->total()
])
])
->with('settings', $this->getSettings())
->with('whats_new', resolve(WhatsNew::class)->render())
->with('patronage', resolve(Patronage::class)->render());
}

private function getMicroblogs(): MicroblogCollection
private function getMicroblogs(): array
{
/** @var Builder $builder */
$builder = app(Builder::class);
return new MicroblogCollection($builder->orderByScore()->popular());
$microblogs = $builder->orderByScore()->popular();
MicroblogResource::withoutWrapping();
return (new MicroblogCollection($microblogs))->resolve($this->request);
}

private function getActivities(): array
{
$this->activity->pushCriteria(new OnlyThoseForumsWithAccess($this->auth));
$this->activity->pushCriteria(new SkipHiddenCategories($this->userId));
return ActivityResource::collection($this->activity->latest(20))->toArray($this->request);
$result = $this->activity->latest(20);
return ActivityResource::collection($result)->toArray($this->request);
}

private function getViewers(): View
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

interface MicroblogRepositoryInterface extends RepositoryInterface
{
public function popular(int $pageSize, int $pageNumber): array;
public function popular(int $pageSize, int $pageNumber): Eloquent\Collection;

/**
* @param int $id
Expand Down
29 changes: 12 additions & 17 deletions app/Repositories/Eloquent/MicroblogRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,24 +58,19 @@ public function recent()
->get());
}

public function popular(int $pageSize, int $pageNumber): array
public function popular(int $pageSize, int $pageNumber): Eloquent\Collection
{
return $this->applyCriteria(function () use ($pageNumber, $pageSize) {
$query = $this->model
->newQuery()
->whereNull('parent_id')
->with(['user', 'assets', 'tags'])
->withCount('comments')
->where(fn(Builder $query) => $query
->where('votes', '>=', 2)
->orWhere('is_sponsored', true));
$count = $query->count();
$result = $query
->limit($pageSize)
->offset(\max(0, $pageNumber - 1) * $pageSize)
->get();
return [$result, $count];
});
return $this->applyCriteria(fn() => $this->model
->newQuery()
->whereNull('parent_id')
->with(['user', 'assets', 'tags'])
->withCount('comments')
->where(fn(Builder $query) => $query
->where('votes', '>=', 2)
->orWhere('is_sponsored', true))
->limit($pageSize)
->offset(\max(0, $pageNumber - 1) * $pageSize)
->get());
}

/**
Expand Down
18 changes: 4 additions & 14 deletions app/Services/Microblogs/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,24 +70,14 @@ public function paginate(): LengthAwarePaginator
return $paginator;
}

public function popular(): LengthAwarePaginator
public function popular(): Eloquent\Collection
{
$page = LengthAwarePaginator::resolveCurrentPage();
$this->loadUserScope();
[$items, $count] = $this->microblog->popular(5, $page);
$paginator = new LengthAwarePaginator(
$items,
$count,
5,
$page,
['path' => LengthAwarePaginator::resolveCurrentPath()]
);
$result = $this->microblog->popular(5, 1);
$this->microblog->resetCriteria();
/** @var Eloquent\Collection $microblogs */
$microblogs = $paginator->keyBy('id');
$microblogs = $result->keyBy('id');
$this->setCommentsRelations($microblogs);
$paginator->setCollection($microblogs);
return $paginator;
return $microblogs;
}

/**
Expand Down
28 changes: 4 additions & 24 deletions resources/js/pages/homepage.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import PerfectScrollbar from 'perfect-scrollbar';
import Vue from "vue";
import VueNotifications from "vue-notification";
import {mapGetters} from "vuex";

import VueMicroblog from "../components/microblog/microblog";
import VuePagination from "../components/pagination.vue";
import VueModals from "../plugins/modals.ts";
import VuePaste from '../plugins/paste.js';
import store from "../store/index.ts";
Expand All @@ -18,37 +18,17 @@ new Vue({
el: '#js-microblog',
delimiters: ['${', '}'],
mixins: [LiveMixin],
components: {
'vue-microblog': VueMicroblog,
'vue-pagination': VuePagination,
},
components: {'vue-microblog': VueMicroblog},
store,
created() {
if ('pagination' in window) {
store.commit('microblogs/INIT', window.pagination);
}
Object.keys(window.microblogs).forEach(id => store.commit('microblogs/ADD', window.microblogs[id]));

store.commit('flags/init', window.flags);
},
mounted() {
this.liveNotifications();
},
methods: {
changePage(page) {
window.location.href = `${window.location.href.split('?')[0]}?page=${page}`;
},

scrollToMicroblog(microblog) {
window.location.hash = `#entry-${microblog.id}`;
}
},
computed: {
...mapGetters('microblogs', ['microblogs', 'currentPage', 'totalPages']),

microblog() {
return this.microblogs[Object.keys(this.microblogs)[0]];
}
}
computed: mapGetters('microblogs', ['microblogs'])
});

function switchForumTab(index) {
Expand Down
9 changes: 1 addition & 8 deletions resources/views/home.twig
Original file line number Diff line number Diff line change
Expand Up @@ -146,13 +146,6 @@
<vue-microblog
v-for="microblog in microblogs" :key="microblog.id" :microblog="microblog"
:wrap="true"></vue-microblog>
<div class="mt-3 mb-3 d-flex justify-content-center">
<vue-pagination
:current-page="currentPage"
:total-pages="totalPages"
@change="changePage">
</vue-pagination>
</div>
<vue-notifications position="bottom right"/>
</section>
</main>
Expand Down Expand Up @@ -273,7 +266,7 @@
</div>

<script type="text/javascript">
var pagination = {{ pagination|json_encode|raw }};
var microblogs = {{ microblogs|json_encode|raw }};
var flags = {{ flags|json_encode|raw }};
</script>
{% endblock %}

0 comments on commit 8bf5de5

Please sign in to comment.