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,15 +472,21 @@ 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: {
to: to,
from: from,
// 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
@ -490,7 +496,37 @@ app.get('/api/v1/text-messages', async (req, res) => {
} : {
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,
};
}
// get text messages from db
const textMessages = await prisma.textMessage.findMany({
where: where,
orderBy: {
id: order,
},

View File

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