From bd3920cfff2f9b8afeb1cb2e0969e9030c1b00d5 Mon Sep 17 00:00:00 2001 From: MartinBraquet Date: Tue, 14 Oct 2025 22:09:20 +0200 Subject: [PATCH] Fix pagination for last active sorting --- backend/api/src/get-profiles.ts | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/backend/api/src/get-profiles.ts b/backend/api/src/get-profiles.ts index 70ceb932..3d540707 100644 --- a/backend/api/src/get-profiles.ts +++ b/backend/api/src/get-profiles.ts @@ -27,6 +27,8 @@ export type profileQueryType = { lastModificationWithin?: string | undefined, } +const userActivityColumns = ['last_online_time'] + export const loadProfiles = async (props: profileQueryType) => { const pg = createSupabaseDirectClient() @@ -97,11 +99,14 @@ export const loadProfiles = async (props: profileQueryType) => { return profiles } + const tablePrefix = userActivityColumns.includes(orderByParam) ? 'user_activity' : 'profiles' + const userActivityJoin = 'user_activity on user_activity.user_id = profiles.user_id' + const query = renderSql( select('profiles.*, name, username, users.data as user, user_activity.last_online_time'), from('profiles'), join('users on users.id = profiles.user_id'), - leftJoin('user_activity on user_activity.user_id = profiles.user_id'), + leftJoin(userActivityJoin), where('looking_for_matches = true'), // where(`pinned_url is not null and pinned_url != ''`), where( @@ -150,11 +155,16 @@ export const loadProfiles = async (props: profileQueryType) => { skipId && where(`user_id != $(skipId)`, {skipId}), - orderBy(`${orderByParam} desc`), + orderBy(`${tablePrefix}.${orderByParam} DESC`), after && where( - `profiles.${orderByParam} < (select profiles.${orderByParam} from profiles where id = $(after))`, - {after} + `${tablePrefix}.${orderByParam} < ( + SELECT ${tablePrefix}.${orderByParam} + FROM profiles + LEFT JOIN ${userActivityJoin} + WHERE profiles.id = $(after) + )`, + { after } ), !shortBio && where(`bio_length >= ${MIN_BIO_LENGTH}`, {MIN_BIO_LENGTH}),