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,14 +78,61 @@ 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 = []; const linkHeader = [];
if (objects.length > 0) { if (objects.length > 0) {
const urlWithoutQuery = req.url.split("?")[0]; // Check if there are statuses before the first one
linkHeader.push( const objectsBefore = await client.status.findMany({
`<${urlWithoutQuery}?max_id=${objects.at(-1)?.id}>; rel="next"`, where: {
`<${urlWithoutQuery}?min_id=${objects[0].id}>; rel="prev"`, 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[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( return jsonResponse(
@ -116,14 +163,48 @@ 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 = []; const linkHeader = [];
if (objects.length > 0) { if (objects.length > 0) {
const urlWithoutQuery = req.url.split("?")[0]; // Check if there are statuses before the first one
linkHeader.push( const objectsBefore = await client.status.findMany({
`<${urlWithoutQuery}?max_id=${objects.at(-1)?.id}>; rel="next"`, where: {
`<${urlWithoutQuery}?min_id=${objects[0].id}>; rel="prev"`, 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[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( return jsonResponse(