Fix Link header on account statuses

This commit is contained in:
Jesse Wierzbinski 2024-04-07 12:29:31 -10:00
parent 48ff510889
commit 564b47c21a
No known key found for this signature in database

View file

@ -78,16 +78,63 @@ export default apiRoute<{
},
});
// Constuct HTTP Link header (next and prev)
// Constuct HTTP Link header (next and prev) only if there are more statuses
const linkHeader = [];
if (objects.length > 0) {
// Check if there are statuses before the first one
const objectsBefore = await client.status.findMany({
where: {
authorId: id,
isReblog: false,
pinnedBy: {
some: {
id: user.id,
},
},
id: {
lt: objects[0].id,
},
},
take: 1,
});
if (objectsBefore.length > 0) {
const urlWithoutQuery = req.url.split("?")[0];
// Add prev link
linkHeader.push(
`<${urlWithoutQuery}?max_id=${objects.at(-1)?.id}>; rel="next"`,
`<${urlWithoutQuery}?min_id=${objects[0].id}>; rel="prev"`,
`<${urlWithoutQuery}?max_id=${objects[0].id}>; rel="prev"`,
);
}
// Check if there are statuses after the last one
const objectsAfter = await client.status.findMany({
where: {
authorId: id,
isReblog: false,
pinnedBy: {
some: {
id: user.id,
},
},
id: {
gt: objects.at(-1)?.id,
},
},
take: 1,
});
if (objectsAfter.length > 0) {
const urlWithoutQuery = req.url.split("?")[0];
// Add next link
linkHeader.push(
`<${urlWithoutQuery}?min_id=${
objects.at(-1)?.id
}>; rel="next"`,
);
}
}
return jsonResponse(
await Promise.all(
objects.map((status) => statusToAPI(status, user)),
@ -116,16 +163,50 @@ export default apiRoute<{
},
});
// Constuct HTTP Link header (next and prev)
// Constuct HTTP Link header (next and prev) only if there are more statuses
const linkHeader = [];
if (objects.length > 0) {
// Check if there are statuses before the first one
const objectsBefore = await client.status.findMany({
where: {
authorId: id,
isReblog: exclude_reblogs ? true : undefined,
id: {
lt: objects[0].id,
},
},
take: 1,
});
if (objectsBefore.length > 0) {
const urlWithoutQuery = req.url.split("?")[0];
// Add prev link
linkHeader.push(
`<${urlWithoutQuery}?max_id=${objects.at(-1)?.id}>; rel="next"`,
`<${urlWithoutQuery}?min_id=${objects[0].id}>; rel="prev"`,
`<${urlWithoutQuery}?max_id=${objects[0].id}>; rel="prev"`,
);
}
// Check if there are statuses after the last one
const objectsAfter = await client.status.findMany({
where: {
authorId: id,
isReblog: exclude_reblogs ? true : undefined,
id: {
gt: objects.at(-1)?.id,
},
},
take: 1,
});
if (objectsAfter.length > 0) {
const urlWithoutQuery = req.url.split("?")[0];
// Add next link
linkHeader.push(
`<${urlWithoutQuery}?min_id=${objects.at(-1)?.id}>; rel="next"`,
);
}
}
return jsonResponse(
await Promise.all(objects.map((status) => statusToAPI(status, user))),
200,