From 342a8011f1f588507d5225475c010d74ec883985 Mon Sep 17 00:00:00 2001 From: Jesse Wierzbinski Date: Mon, 8 Apr 2024 16:40:35 -1000 Subject: [PATCH] Fix for follower/following timelines being wrong --- server/api/api/v1/accounts/[id]/followers.ts | 51 +++++++++----------- server/api/api/v1/accounts/[id]/following.ts | 51 +++++++++----------- server/api/api/v1/accounts/[id]/statuses.ts | 3 ++ 3 files changed, 49 insertions(+), 56 deletions(-) diff --git a/server/api/api/v1/accounts/[id]/followers.ts b/server/api/api/v1/accounts/[id]/followers.ts index 39dcf03f..485b6160 100644 --- a/server/api/api/v1/accounts/[id]/followers.ts +++ b/server/api/api/v1/accounts/[id]/followers.ts @@ -1,7 +1,8 @@ import { apiRoute, applyConfig } from "@api"; import { errorResponse, jsonResponse } from "@response"; +import { fetchTimeline } from "@timelines"; import { client } from "~database/datasource"; -import { userToAPI } from "~database/entities/User"; +import { type UserWithRelations, userToAPI } from "~database/entities/User"; import { userRelations } from "~database/entities/relations"; export const meta = applyConfig({ @@ -40,42 +41,36 @@ export default apiRoute<{ if (!user) return errorResponse("User not found", 404); - const objects = await client.user.findMany({ - where: { - relationships: { - some: { - subjectId: user.id, - following: true, + const { objects, link } = await fetchTimeline( + client.user, + { + where: { + relationships: { + some: { + subjectId: user.id, + following: true, + }, + }, + id: { + lt: max_id, + gt: min_id, + gte: since_id, }, }, - id: { - lt: max_id, - gt: min_id, - gte: since_id, + include: userRelations, + take: Number(limit), + orderBy: { + id: "desc", }, }, - include: userRelations, - take: Number(limit), - orderBy: { - id: "desc", - }, - }); - - // Constuct HTTP Link header (next and prev) - const linkHeader = []; - if (objects.length > 0) { - const urlWithoutQuery = req.url.split("?")[0]; - linkHeader.push( - `<${urlWithoutQuery}?max_id=${objects.at(-1)?.id}>; rel="next"`, - `<${urlWithoutQuery}?min_id=${objects[0].id}>; rel="prev"`, - ); - } + req, + ); return jsonResponse( await Promise.all(objects.map((object) => userToAPI(object))), 200, { - Link: linkHeader.join(", "), + Link: link, }, ); }); diff --git a/server/api/api/v1/accounts/[id]/following.ts b/server/api/api/v1/accounts/[id]/following.ts index 9a3ca34d..f35a8624 100644 --- a/server/api/api/v1/accounts/[id]/following.ts +++ b/server/api/api/v1/accounts/[id]/following.ts @@ -1,7 +1,8 @@ import { apiRoute, applyConfig } from "@api"; import { errorResponse, jsonResponse } from "@response"; +import { fetchTimeline } from "@timelines"; import { client } from "~database/datasource"; -import { userToAPI } from "~database/entities/User"; +import { userToAPI, type UserWithRelations } from "~database/entities/User"; import { userRelations } from "~database/entities/relations"; export const meta = applyConfig({ @@ -40,42 +41,36 @@ export default apiRoute<{ if (!user) return errorResponse("User not found", 404); - const objects = await client.user.findMany({ - where: { - relationshipSubjects: { - some: { - ownerId: user.id, - following: true, + const { objects, link } = await fetchTimeline( + client.user, + { + where: { + relationshipSubjects: { + some: { + ownerId: user.id, + following: true, + }, + }, + id: { + lt: max_id, + gt: min_id, + gte: since_id, }, }, - id: { - lt: max_id, - gt: min_id, - gte: since_id, + include: userRelations, + take: Number(limit), + orderBy: { + id: "desc", }, }, - include: userRelations, - take: Number(limit), - orderBy: { - id: "desc", - }, - }); - - // Constuct HTTP Link header (next and prev) - const linkHeader = []; - if (objects.length > 0) { - const urlWithoutQuery = req.url.split("?")[0]; - linkHeader.push( - `<${urlWithoutQuery}?max_id=${objects.at(-1)?.id}>; rel="next"`, - `<${urlWithoutQuery}?min_id=${objects[0].id}>; rel="prev"`, - ); - } + req, + ); return jsonResponse( await Promise.all(objects.map((object) => userToAPI(object))), 200, { - Link: linkHeader.join(", "), + Link: link, }, ); }); diff --git a/server/api/api/v1/accounts/[id]/statuses.ts b/server/api/api/v1/accounts/[id]/statuses.ts index 269413c0..075fd6f2 100644 --- a/server/api/api/v1/accounts/[id]/statuses.ts +++ b/server/api/api/v1/accounts/[id]/statuses.ts @@ -48,6 +48,7 @@ export default apiRoute<{ since_id, limit = "20", exclude_reblogs, + only_media = false, pinned, } = extraData.parsedRequest; @@ -70,6 +71,8 @@ export default apiRoute<{ id: user.id, }, }, + // If only_media is true, only return statuses with attachments + attachments: only_media ? { some: {} } : undefined, id: { lt: max_id, gt: min_id,