@@ -88,6 +88,7 @@ public function endpoints(): array
88
88
'mentionsGroups ' ,
89
89
'mentionsTags ' ,
90
90
])
91
+ ->defaultSort ('-number ' )
91
92
->eagerLoad (function () {
92
93
if ($ this ->extensions ->isEnabled ('flarum-mentions ' )) {
93
94
return ['mentionsUsers ' , 'mentionsPosts ' , 'mentionsGroups ' , 'mentionsTags ' ];
@@ -100,28 +101,24 @@ public function endpoints(): array
100
101
$ near = intval (Arr::get ($ queryParams , 'page.near ' ));
101
102
102
103
if ($ near > 1 ) {
104
+ $ sort = $ defaultExtracts ['sort ' ];
103
105
$ filter = $ defaultExtracts ['filter ' ];
104
106
$ dialogId = $ filter ['dialog ' ] ?? null ;
105
107
106
- if (count ($ filter ) > 1 || ! $ dialogId || ($ context -> queryParam ( ' sort ' ) && $ context -> queryParam ( ' sort ' ) !== ' - number ' )) {
108
+ if (count ($ filter ) > 1 || ! $ dialogId || ($ sort && $ sort !== [ ' number ' => ' desc ' ] )) {
107
109
throw new BadRequestException (
108
110
'You can only use page[near] with filter[dialog] and the default sort order '
109
111
);
110
112
}
111
113
112
114
$ limit = $ defaultExtracts ['limit ' ];
113
115
114
- // Change the offset to the one nearest to the message number.
115
116
$ 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 ();
125
122
126
123
return max (0 , $ index - $ limit / 2 );
127
124
}
0 commit comments