support fetching direct messages between two nodes

This commit is contained in:
liamcottle
2024-07-07 16:43:30 +12:00
parent 86787334ef
commit f249856887
2 changed files with 52 additions and 13 deletions

View File

@ -472,25 +472,61 @@ app.get('/api/v1/text-messages', async (req, res) => {
const from = req.query.from ?? undefined;
const channelId = req.query.channel_id ?? undefined;
const gatewayId = req.query.gateway_id ?? undefined;
const directMessageNodeIds = req.query.direct_message_node_ids?.split(",") ?? undefined;
const lastId = req.query.last_id ? parseInt(req.query.last_id) : undefined;
const count = req.query.count ? parseInt(req.query.count) : 50;
const order = req.query.order ?? "asc";
// get text messages from db
const textMessages = await prisma.textMessage.findMany({
where: {
// if direct message node ids are provided, there should be exactly two node ids
if(directMessageNodeIds !== undefined && directMessageNodeIds.length !== 2){
res.status(400).json({
message: "direct_message_node_ids requires 2 node ids separated by a comma.",
});
return;
}
// default where clauses that should always be used for filtering
var where = {
channel_id: channelId,
gateway_id: gatewayId,
// when ordered oldest to newest (asc), only get records after last id
// when ordered newest to oldest (desc), only get records before last id
id: order === "asc" ? {
gt: lastId,
} : {
lt: lastId,
},
};
// if direct message node ids are provided, we expect exactly 2 node ids
if(directMessageNodeIds !== undefined && directMessageNodeIds.length === 2){
// filter message by "to -> from" or "from -> to"
const [firstNodeId, secondNodeId] = directMessageNodeIds;
where = {
AND: where,
OR: [
{
to: firstNodeId,
from: secondNodeId,
},
{
to: secondNodeId,
from: firstNodeId,
},
],
};
} else {
// filter by to and from
where = {
...where,
to: to,
from: from,
channel_id: channelId,
gateway_id: gatewayId,
// when ordered oldest to newest (asc), only get records after last id
// when ordered newest to oldest (desc), only get records before last id
id: order === "asc" ? {
gt: lastId,
} : {
lt: lastId,
},
},
};
}
// get text messages from db
const textMessages = await prisma.textMessage.findMany({
where: where,
orderBy: {
id: order,
},