Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FIX] Prevent error on trying insert message with duplicated id #14945

Merged
merged 3 commits into from
Jul 11, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions app/lib/client/methods/sendMessage.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ Meteor.methods({
if (!Meteor.userId() || s.trim(message.msg) === '') {
return false;
}
const messageAlreadyExists = message._id && ChatMessage.findOne({ _id: message._id });
if (messageAlreadyExists) {
return;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe on the client it should show a toast message on this case? might be better than doing nothing

}
const user = Meteor.user();
message.ts = isNaN(TimeSync.serverOffset()) ? new Date() : new Date(Date.now() + TimeSync.serverOffset());
message.u = {
Expand Down
4 changes: 4 additions & 0 deletions app/lib/server/functions/sendMessage.js
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,10 @@ export const sendMessage = function(user, message, room, upsert = false) {
}, message);
message._id = _id;
} else {
const messageAlreadyExists = message._id && Messages.findOneById(message._id);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const messageAlreadyExists = message._id && Messages.findOneById(message._id);
const messageAlreadyExists = message._id && Messages.findOneById(message._id, { fields: { _id: 1 } });

this would avoid returning not used fields

if (messageAlreadyExists) {
return;
}
message._id = Messages.insert(message);
}

Expand Down