support fetching direct messages between two nodes
This commit is contained in:
62
src/index.js
62
src/index.js
@ -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,
|
||||
},
|
||||
|
@ -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,
|
||||
|
Reference in New Issue
Block a user