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 all commits
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
6 changes: 6 additions & 0 deletions app/lib/client/methods/sendMessage.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
import { Meteor } from 'meteor/meteor';
import { TimeSync } from 'meteor/mizzao:timesync';
import s from 'underscore.string';
import toastr from 'toastr';

import { ChatMessage } from '../../../models';
import { settings } from '../../../settings';
import { callbacks } from '../../../callbacks';
import { promises } from '../../../promises/client';
import { t } from '../../../utils/client';

Meteor.methods({
sendMessage(message) {
if (!Meteor.userId() || s.trim(message.msg) === '') {
return false;
}
const messageAlreadyExists = message._id && ChatMessage.findOne({ _id: message._id });
if (messageAlreadyExists) {
return toastr.error(t('Message_Already_Sent'));
}
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, { fields: { _id: 1 } });
if (messageAlreadyExists) {
return;
}
message._id = Messages.insert(message);
}

Expand Down
1 change: 1 addition & 0 deletions packages/rocketchat-i18n/i18n/en.i18n.json
Original file line number Diff line number Diff line change
Expand Up @@ -2010,6 +2010,7 @@
"Message_AllowSnippeting": "Allow Message Snippeting",
"Message_AllowStarring": "Allow Message Starring",
"Message_AllowUnrecognizedSlashCommand": "Allow Unrecognized Slash Commands",
"Message_Already_Sent": "This message has already been sent and is being processed by the server",
"Message_AlwaysSearchRegExp": "Always Search Using RegExp",
"Message_AlwaysSearchRegExp_Description": "We recommend to set `True` if your language is not supported on <a target=\"_blank\" href=\"https://docs.mongodb.org/manual/reference/text-search-languages/#text-search-languages\">MongoDB text search</a>.",
"Message_Attachments": "Message Attachments",
Expand Down