Skip to content

Commit

Permalink
Fix sort order when using ?around
Browse files Browse the repository at this point in the history
  • Loading branch information
DEVTomatoCake authored and MaddyUnderStars committed Jul 28, 2024
1 parent d3ece93 commit 481cf38
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions src/api/routes/channels/#channel_id/messages/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,19 +130,25 @@ router.get(
query.take = Math.floor(limit / 2);
if (query.take != 0) {
const [right, left] = await Promise.all([
Message.find({ ...query, where: { id: LessThan(around) } }),
Message.find({
...query,
where: { id: MoreThanOrEqual(around) },
where: { channel_id, id: LessThan(around) },
}),
Message.find({
...query,
where: { channel_id, id: MoreThanOrEqual(around) },
order: { timestamp: "ASC" },
}),
]);
left.push(...right);
messages = left;
messages = left.sort(
(a, b) => a.timestamp.getTime() - b.timestamp.getTime(),
);
} else {
query.take = 1;
const message = await Message.findOne({
...query,
where: { id: around },
where: { channel_id, id: around },
});
messages = message ? [message] : [];
}
Expand Down

0 comments on commit 481cf38

Please sign in to comment.