gpt4 book ai didi

node.js - GET 不返回发送的消息。仅收件箱项目

转载 作者:行者123 更新时间:2023-12-02 13:51:52 27 4
gpt4 key购买 nike

当用户发送消息时,会生成一个messageTrackingId。现在它 $unwinds CreatorName 作为收件箱中唯一的返回值。我只想要一个用户条目。同一用户不存在重复项。目前,如果其他用户没有响应,他们可以发送多条消息,从而生成新的 messageTrackingIds。如何使初始发送的消息也出现在收件箱中,以便我可以使用该 messageTrackingId 而不是生成新消息?我已经被困在这个问题上有一段时间了,所以我感谢任何帮助。

应用程序获取

app.get('/api/messages', (req, res, next) => {
query = {};
inbox = false;
messageId = false;
if (req.query.recipientId) {
query = { recipientId: req.query.recipientId }
inbox = true;

Messages.aggregate([
{
$match: {
$or: [ { recipientId: req.query.recipientId }, { creator: req.query.recipientId } ]
}
},
{
$addFields: {
conversant: {
$cond: [ { $ne: [ "$recipientId", req.query.recipientId ] }, "$recipientId", "$creator" ]
}
}
},
{
$sort: { creationDate: 1 }
},
{
$group: {
_id: "$conversant",
message: { $first: "$message" },
recipientId: { $first: "$recipientId" },
creator: { $first: "$creator" },
messageTrackingId: { $first: "$messageTrackingId" },
creationDate: { $first: "$creationDate" }
}
},
{
$lookup: {
from: "users",
localField: "creator",
foreignField: "_id",
as: "creatorName",
pipeline: [
{
$project: {
_id: 1,
message: { $arrayElemAt: ["$message", 0] },
recipientId: { $arrayElemAt: ["$recipientId", 0] },
creator: { $arrayElemAt: ["$creator", 0] },
messageTrackingId: { $arrayElemAt: ["$messageTrackingId", 0] },
creatorName: { $arrayElemAt: ["$creatorName", 0] },

}
}
],
as: 'messageTest'
},
}
])



//.populate('creator', 'username')

.then(documents => {
if (res.subject === "Test") {
}
if (inbox === false && messageId === false) {
res.status(200).json({
message: "User's Sent Messages Retrieved!",
posts: documents
});
}
if (inbox === true) {
res.status(200).json({
message: "User's Inbox Retrieved!",
posts: documents
});
}
if (messageId === true) {
res.status(200).json({
message: "Message Chain Retrieved!",
posts: documents
});
}
});
} else if (req.query.creator) {
query = { creator: req.query.creator };
inbox = false;
Messages.find(query)
.populate("creator", "username")
.then(documents => {
if (inbox === false && messageId === false) {
res.status(200).json({
message: "User's Sent Messages Retrieved!",
posts: documents
});
}
if (inbox === true) {
res.status(200).json({
message: "User's Inbox Retrieved!",
posts: documents
});
}
if (messageId === true) {
res.status(200).json({
message: "Message Chain Retrieved!",
posts: documents
});
}
});
} else if (req.query.messageId) {
query = { messageTrackingId: req.query.messageId };
messageId = true;
Messages.find(query)
.populate("creator", "instagramName")
.then(documents => {
if (inbox === false && messageId === false) {
res.status(200).json({
message: "User's Sent Messages Retrieved!",
posts: documents
});
}
if (inbox === true) {
res.status(200).json({
message: "User's Inbox Retrieved!",
posts: documents
});
}
if (messageId === true) {
res.status(200).json({
message: "Message Chain Retrieved!",
posts: documents
});
}
});
}
});

应用程序.post

app.post("/api/messages", checkAuth, (req, res, next) => {
console.log("Made It")
messagingTrackingIDValue = "";

const messaging = new Messages({
creator: req.userData.userId,
recipient: req.body.recipient,
recipientId: req.body.recipientId,
message: req.body.message,
//message: req.body.message,
messageTrackingId: req.body.messageTrackingId,
creatorName: req.userData.username,
creationDate: req.body.creationDate
});

//saves to database with mongoose
messaging.save().then(result => {
if (result.creator !== messaging.creator) {
} else if (result.creator === req.userData.userId) {
}
console.log(result);
res.status(201).json({
message: "Message Sent Successfully!",
postId: result._id
});
});
});

Angular 服务

  sendMessage(
recipient: string,
message: string,
creationDate: Date,
recipientId: string,
creatorName: string,
messageTrackingId: string
) {
const messaging: Messages = {
id: null,
recipient: recipient,
message: message,
creationDate: creationDate,
creator: null,
recipientId: recipientId,
creatorName: creatorName,
messageTrackingId: messageTrackingId
};

this.http
.post<{ message: string; messagingId: string; creator: string }>(
"http://localhost:3000/api/messages",
messaging
)
.subscribe(responseData => {
console.log(responseData);
const id = responseData.messagingId;
messaging.id = id;

console.log("Message sent successfully!");

// window.location.reload();
// this.posts.push();
// this.postsUpdated.next([...this.posts]);
});
}




replyToMessage(
recipient: string,
message: string,
creationDate: Date,
recipientId: string,
creatorName: string,
messageTrackingId: string
) {
const messaging: Messages = {
id: null,
recipient: recipient,
message: message,
creationDate: creationDate,
creator: null,
recipientId: recipientId,
creatorName: creatorName,
messageTrackingId: messageTrackingId
};

this.http
.post<{ message: string; messagingId: string; creator: string }>(
"http://localhost:3000/api/messages",
messaging
)
.subscribe(responseData => {
console.log(responseData);
const id = responseData.messagingId;
messaging.id = id;

console.log("Message sent successfully!");
});
}







getMessages(recipientId: string) {
return this.http
.get<{
message: string;
posts: any;
maxPosts: number;
messageList: string;
}>("http://localhost:3000/api/messages?recipientId=" + recipientId)
.pipe(
map(retrievedData => {
return {
posts: retrievedData.posts.map(post => {
return {
creator: post.creator,
recipientId: post.recipientId,
creationDate: post.creationDate,
messageTrackingId: post.messageTrackingId,
creatorName: post.creatorName,
id: post._id
};
}),
maxPosts: retrievedData.maxPosts
};
})
);
}

以下是收件人回复消息的示例,以便发件人获取要使用的 messageTrackingId

先发消息,再回复消息。由于收件人回复了,发件人就有了 messageTrackingId 可用于发送给同一用户的下一条消息。

Made It
{ _id: 5e0674ddd55aae5294370870,
creator: 5df0014e25ee451beccf588a,
recipient: 'joe',
recipientId: '5df00d08c713f722909c99c1',
message: 'This is the initial message',
messageTrackingId: '3cb3f5bb-5e17-49a7-8aca-4a61ddd1d847',
creatorName: 'andy',
creationDate: 2019-12-27T21:17:17.155Z,
__v: 0 }
Made It
{ _id: 5e067529d55aae5294370872,
creator: 5df00d08c713f722909c99c1,
recipient: 'andy',
recipientId: '5df0014e25ee451beccf588a',
message: 'This is the reply message',
messageTrackingId: '3cb3f5bb-5e17-49a7-8aca-4a61ddd1d847',
creatorName: 'joe',
creationDate: 2019-12-27T21:18:33.947Z,
__v: 0 }

如果收件人从未回复并且发件人发送另一条消息,则会发生这种情况:

Made It
{ _id: 5e06756bd55aae5294370873,
creator: 5df00d08c713f722909c99c1,
recipient: 'andy',
recipientId: '5df0014e25ee451beccf588a',
message: 'This is the first message',
messageTrackingId: '2077a8e6-844c-4639-a4fa-7aee0b8beaf4',
creatorName: 'joe',
creationDate: 2019-12-27T21:19:39.217Z,
__v: 0 }
Made It
{ _id: 5e06757cd55aae5294370874,
creator: 5df00d08c713f722909c99c1,
recipient: 'andy',
recipientId: '5df0014e25ee451beccf588a',
message: 'This is another message to same user.',
messageTrackingId: 'feeb0e20-432e-4c9a-9f59-45913c194edc',
creatorName: 'joe',
creationDate: 2019-12-27T21:19:56.257Z,
__v: 0 }

enter image description here

enter image description here

最佳答案

您可以使用以下聚合来确保每个(收件人发件人)对仅返回一个文档:

db.Messages.aggregate([
{
$addFields: { conversants: [ "$recipientId", "$creator" ] }
},
{
$match: { conversants: req.query.recipientId }
},
{
$addFields: { conversant: { $arrayElemAt: [ { $filter: { input: "$conversants", cond: { $ne: [ "$$this", "5df0014e25ee451beccf588a" ] } } } , 0 ] } }
},
{
$sort: { creationDate: 1 }
},
{
$group: {
_id: "$conversant",
message: { $first: "$message" },
recipientId: { $first: "$recipientId" },
creator: { $first: "$creator" },
messageTrackingId: { $first: "$messageTrackingId" },
creationDate: { $first: "$creationDate" }
}
},
{
$lookup: {
from: "users",
let: { creator: "$creator" },
pipeline: [
{ $match: { $expr: { $eq: [ "$_id", "$$creator" ] } } },
{ $project: { creatorName: 1 } }
],
as: "creatorName"
}
},
{
$addFields: { creatorName: { $arrayElemAt: [ "$creatorName", 0 ] } }
}
])

这里的想法是创建代表两个 ID 的附加字段:creatorrecipient。这将允许您执行两件事:使用 recipientId(也可能是发件人)进行过滤 - 第二步,然后选择始终是第二人的 conversant 值 - 否如果您的请求中指定的人在该对话中发送或接收了消息,则很重要。然后您可以在该字段上 $group 以确保您在每个“对话”中只收到一条消息。您可以只运行 $first,而不是将 $addToSet$arrayElemAt 一起使用。相对繁重的 $lookup 也可以作为最后一步运行,因为您需要为每个“熟悉的人”获取一次该数据。

编辑:前三个阶段可以替换为以下阶段 - 这应该会显着提高性能,因为过滤将尽快应用:

{
$match: {
$or: [ { recipientId: req.query.recipientId }, { creator: req.query.recipientId } ]
}
},
{
$addFields: {
conversant: {
$cond: [ { $ne: [ "$recipientId", req.query.recipientId ] }, "$recipientId", "$creator" ]
}
}
},

关于node.js - GET 不返回发送的消息。仅收件箱项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59503094/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com