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 from = req.query.from ?? undefined;
const channelId = req.query.channel_id ?? undefined; const channelId = req.query.channel_id ?? undefined;
const gatewayId = req.query.gateway_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 lastId = req.query.last_id ? parseInt(req.query.last_id) : undefined;
const count = req.query.count ? parseInt(req.query.count) : 50; const count = req.query.count ? parseInt(req.query.count) : 50;
const order = req.query.order ?? "asc"; const order = req.query.order ?? "asc";
// get text messages from db // if direct message node ids are provided, there should be exactly two node ids
const textMessages = await prisma.textMessage.findMany({ if(directMessageNodeIds !== undefined && directMessageNodeIds.length !== 2){
where: { 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, to: to,
from: from, 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 // get text messages from db
id: order === "asc" ? { const textMessages = await prisma.textMessage.findMany({
gt: lastId, where: where,
} : {
lt: lastId,
},
},
orderBy: { orderBy: {
id: order, id: order,
}, },

View File

@ -110,6 +110,7 @@
this.from = queryParams.get('from'); this.from = queryParams.get('from');
this.channelId = queryParams.get('channel_id'); this.channelId = queryParams.get('channel_id');
this.gatewayId = queryParams.get('gateway_id'); this.gatewayId = queryParams.get('gateway_id');
this.directMessageNodeIds = queryParams.get('direct_message_node_ids');
this.count = queryParams.get('count'); this.count = queryParams.get('count');
// listen for scrolling of messages list // listen for scrolling of messages list
@ -170,6 +171,7 @@
from: this.from, from: this.from,
channel_id: this.channelId, channel_id: this.channelId,
gateway_id: this.gatewayId, gateway_id: this.gatewayId,
direct_message_node_ids: this.directMessageNodeIds,
count: this.count, count: this.count,
order: "desc", order: "desc",
last_id: this.oldestMessageId, last_id: this.oldestMessageId,
@ -217,6 +219,7 @@
from: this.from, from: this.from,
channel_id: this.channelId, channel_id: this.channelId,
gateway_id: this.gatewayId, gateway_id: this.gatewayId,
direct_message_node_ids: this.directMessageNodeIds,
count: this.count, count: this.count,
order: "asc", order: "asc",
last_id: this.latestMessageId, last_id: this.latestMessageId,