{"record":{"id":"3d6d2c0defe62171","repo":"flarum/framework","slug":"you-can-only-use-page-near-with-filter-dialog-and-the","errorCode":null,"errorMessage":"You can only use page[near] with filter[dialog] and the default sort order","messagePattern":"You can only use page\\[near\\] with filter\\[dialog\\] and the default sort order","errorType":"http","errorClass":"BadRequestException","httpStatus":400,"severity":"error","filePath":"extensions/messages/src/Api/Resource/DialogMessageResource.php","lineNumber":123,"sourceCode":"                ->defaultSort('-number')\n                ->eagerLoad(function () {\n                    if ($this->extensions->isEnabled('flarum-mentions')) {\n                        return ['mentionsUsers', 'mentionsPosts', 'mentionsGroups', 'mentionsTags'];\n                    }\n\n                    return [];\n                })\n                ->extractOffset(function (Context $context, array $defaultExtracts): int {\n                    $queryParams = $context->request->getQueryParams();\n                    $near = intval(Arr::get($queryParams, 'page.near'));\n\n                    if ($near > 1) {\n                        $sort = $defaultExtracts['sort'];\n                        $filter = $defaultExtracts['filter'];\n                        $dialogId = $filter['dialog'] ?? null;\n\n                        if (count($filter) > 1 || ! $dialogId || ($sort && $sort !== ['number' => 'desc'])) {\n                            throw new BadRequestException(\n                                'You can only use page[near] with filter[dialog] and the default sort order'\n                            );\n                        }\n\n                        $limit = $defaultExtracts['limit'];\n\n                        $index = DialogMessage::query()\n                            ->where('dialog_id', $dialogId)\n                            ->where('number', '>=', $near)\n                            ->orderBy('number', 'desc')\n                            ->whereVisibleTo($context->getActor())\n                            ->count();\n\n                        return max(0, $index - $limit / 2);\n                    }\n\n                    return $defaultExtracts['offset'];\n                })","sourceCodeStart":105,"sourceCodeEnd":141,"githubUrl":"https://github.com/flarum/framework/blob/4b939f685389bfe8a380e9e28ddf305a1c66950c/extensions/messages/src/Api/Resource/DialogMessageResource.php#L105-L141","documentation":"DialogMessageResource restricts the page[near] pagination parameter: it may only be combined with a single filter[dialog] and the default sort (number desc). Anything else throws a BadRequestException in the endpoints() hook.","triggerScenarios":"Requesting /api/dialog-messages with page[near] while either filtering by more than just dialog, omitting filter[dialog], or passing a non-default sort (e.g. sort by id or number asc).","commonSituations":"Frontend passing extra filters (e.g. user filter) together with near-pagination; sorting params leaking from a generic list view; calling the API without scoping to one dialog.","solutions":["Add filter[dialog]=<dialogId> to the request.","Remove all other filters so only filter[dialog] remains.","Drop any sort parameter or use the default sort (number desc).","If you need other filter/sort combinations, fetch without page[near] and paginate normally."],"exampleFix":"// before\nGET /api/dialog-messages?page[near]=5&sort=-id\n// after\nGET /api/dialog-messages?filter[dialog]=12&page[near]=5","handlingStrategy":"validation","validationCode":"const ok = 'near' in page && Object.keys(filter).length === 1 && 'dialog' in filter && (!sort || JSON.stringify(sort) === JSON.stringify({number:'desc'})); if (!ok) throw new Error('page[near] requires only filter[dialog] and default sort');","typeGuard":null,"tryCatchPattern":"try { const res = await api.get('/api/dialog-messages', params); } catch (e) { if (e.status === 400 && /page\\[near\\]/.test(e.message)) { /* fix query params */ } }","preventionTips":["Only combine page[near] with filter[dialog]","Never attach extra filters or custom sorts with near pagination","Centralize dialog-message fetching params in one helper"],"tags":["api","pagination","bad-request"],"backgroundTag":"invalid-query-parameter","analyzedSha":"4b939f685389bfe8a380e9e28ddf305a1c66950c","analyzedAt":"2026-09-15T18:09:20.879Z","contentChangedAt":"2026-09-15T18:09:20.879Z","schemaVersion":2},"datasetVersion":"2026-09-16T09:17:16.951Z"}