Skip to content

Commit

Permalink
[FIX] Prevent error on trying insert message with duplicated id (#14945)
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcosSpessatto authored and sampaiodiego committed Jul 11, 2019
1 parent 7762aaa commit d0763c6
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 0 deletions.
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

0 comments on commit d0763c6

Please sign in to comment.