Skip to content

Commit 9733081

Browse files
committed
fix
1 parent edc4e55 commit 9733081

File tree

2 files changed

+9
-11
lines changed

2 files changed

+9
-11
lines changed

extensions/messages/src/Api/Resource/DialogMessageResource.php

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ public function endpoints(): array
8888
'mentionsGroups',
8989
'mentionsTags',
9090
])
91+
->defaultSort('-number')
9192
->eagerLoad(function () {
9293
if ($this->extensions->isEnabled('flarum-mentions')) {
9394
return ['mentionsUsers', 'mentionsPosts', 'mentionsGroups', 'mentionsTags'];
@@ -100,28 +101,24 @@ public function endpoints(): array
100101
$near = intval(Arr::get($queryParams, 'page.near'));
101102

102103
if ($near > 1) {
104+
$sort = $defaultExtracts['sort'];
103105
$filter = $defaultExtracts['filter'];
104106
$dialogId = $filter['dialog'] ?? null;
105107

106-
if (count($filter) > 1 || ! $dialogId || ($context->queryParam('sort') && $context->queryParam('sort') !== '-number')) {
108+
if (count($filter) > 1 || ! $dialogId || ($sort && $sort !== ['number' => 'desc'])) {
107109
throw new BadRequestException(
108110
'You can only use page[near] with filter[dialog] and the default sort order'
109111
);
110112
}
111113

112114
$limit = $defaultExtracts['limit'];
113115

114-
// Change the offset to the one nearest to the message number.
115116
$index = DialogMessage::query()
116-
->select('row_index')
117-
->fromSub(function (QueryBuilder $query) use ($dialogId) {
118-
$query->select('number')
119-
->selectRaw('ROW_NUMBER() OVER (ORDER BY number DESC) AS row_index')
120-
->from('dialog_messages')
121-
->where('dialog_id', $dialogId);
122-
}, 'dialog_messages')
123-
->where('number', '<=', $near)
124-
->value('row_index');
117+
->where('dialog_id', $dialogId)
118+
->where('number', '>=', $near)
119+
->orderBy('number', 'desc')
120+
->whereVisibleTo($context->getActor())
121+
->count();
125122

126123
return max(0, $index - $limit / 2);
127124
}

framework/core/src/Api/Endpoint/Index.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ protected function setUp(): void
7474

7575
// This model has a searcher API, so we'll use that instead of the default.
7676
// The searcher API allows swapping the default search engine for a custom one.
77+
/** @var SearchManager $search */
7778
$search = $context->api->getContainer()->make(SearchManager::class);
7879
$modelClass = $collection instanceof AbstractDatabaseResource ? $collection->model() : null;
7980

0 commit comments

Comments
 (0)