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

Regression: Fix duplicate email messages in multiple instances #20495

Merged
merged 19 commits into from
Jan 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
c4cf843
Update index.js
ggazzo Jan 8, 2021
a0c9821
Merge branch 'develop' of https://github.com/RocketChat/Rocket.Chat i…
renatobecker Jan 8, 2021
383415a
Merge branch 'develop' of https://github.com/RocketChat/Rocket.Chat i…
renatobecker Jan 15, 2021
1a87b16
Merge branch 'develop' of https://github.com/RocketChat/Rocket.Chat i…
renatobecker Jan 15, 2021
6eb122f
Merge branch 'develop' of https://github.com/RocketChat/Rocket.Chat i…
renatobecker Jan 20, 2021
2997291
Merge branch 'develop' of https://github.com/RocketChat/Rocket.Chat i…
renatobecker Jan 22, 2021
1789ee9
Merge branch 'develop' of https://github.com/RocketChat/Rocket.Chat i…
renatobecker Jan 22, 2021
63c9d8d
Merge branch 'develop' of https://github.com/RocketChat/Rocket.Chat i…
renatobecker Jan 25, 2021
f16aecb
Merge branch 'develop' of https://github.com/RocketChat/Rocket.Chat i…
renatobecker Jan 27, 2021
af38815
Merge branch 'develop' of https://github.com/RocketChat/Rocket.Chat i…
renatobecker Jan 28, 2021
a6c8298
Merge branch 'develop' of https://github.com/RocketChat/Rocket.Chat i…
renatobecker Jan 28, 2021
49d5c5f
Create a new collection to queue new emails.
renatobecker Jan 29, 2021
2885310
Merge branch 'develop' into omnichannel/fix-email-channel-multi-insta…
renatobecker Jan 29, 2021
d76392b
Fix error when receiving emails in multiple intances.
renatobecker Jan 29, 2021
9f154be
Merge branch 'omnichannel/fix-email-channel-multi-instances' of https…
renatobecker Jan 29, 2021
4783a68
Add a new collection to store messages already received.
renatobecker Jan 29, 2021
0f37845
Prevent unhandled promise errors.
renatobecker Jan 29, 2021
781547d
Fix type casting.
renatobecker Jan 29, 2021
b74a7a5
Improve email message history collection.
renatobecker Jan 29, 2021
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
10 changes: 10 additions & 0 deletions app/models/server/models/EmailMessageHistory.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { Base } from './_Base';

export class EmailMessageHistory extends Base {
constructor() {
super('email_message_history');
this.tryEnsureIndex({ createdAt: 1 }, { expireAfterSeconds: 60 * 60 * 24 });
}
}

export default new EmailMessageHistory();
13 changes: 13 additions & 0 deletions app/models/server/raw/EmailMessageHistory.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/* eslint-disable @typescript-eslint/explicit-function-return-type */
import { BaseRaw } from './BaseRaw';
import { IEmailMessageHistory } from '../../../../definition/IEmailMessageHistory';

export class EmailMessageHistoryRaw extends BaseRaw<IEmailMessageHistory> {
insertOne({ _id, email }: IEmailMessageHistory) {
return this.col.insertOne({
_id,
email,
createdAt: new Date(),
});
}
}
3 changes: 3 additions & 0 deletions app/models/server/raw/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ import OmnichannelQueueModel from '../models/OmnichannelQueue';
import { OmnichannelQueueRaw } from './OmnichannelQueue';
import EmailInboxModel from '../models/EmailInbox';
import { EmailInboxRaw } from './EmailInbox';
import EmailMessageHistoryModel from '../models/EmailMessageHistory';
import { EmailMessageHistoryRaw } from './EmailMessageHistory';
import { api } from '../../../../server/sdk/api';
import { initWatchers } from '../../../../server/modules/watchers/watchers.module';

Expand Down Expand Up @@ -103,6 +105,7 @@ export const IntegrationHistory = new IntegrationHistoryRaw(IntegrationHistoryMo
export const Sessions = new SessionsRaw(SessionsModel.model.rawCollection(), trashCollection);
export const OmnichannelQueue = new OmnichannelQueueRaw(OmnichannelQueueModel.model.rawCollection(), trashCollection);
export const EmailInbox = new EmailInboxRaw(EmailInboxModel.model.rawCollection(), trashCollection);
export const EmailMessageHistory = new EmailMessageHistoryRaw(EmailMessageHistoryModel.model.rawCollection(), trashCollection);

const map = {
[Messages.col.collectionName]: MessagesModel,
Expand Down
5 changes: 5 additions & 0 deletions definition/IEmailMessageHistory.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export interface IEmailMessageHistory {
_id: string;
email: string;
createdAt?: Date;
}
15 changes: 13 additions & 2 deletions server/features/EmailInbox/EmailInbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Meteor } from 'meteor/meteor';
import nodemailer from 'nodemailer';
import Mail from 'nodemailer/lib/mailer';

import { EmailInbox } from '../../../app/models/server/raw';
import { EmailInbox, EmailMessageHistory } from '../../../app/models/server/raw';
import { IMAPInterceptor } from '../../email/IMAPInterceptor';
import { IEmailInbox } from '../../../definition/IEmailInbox';
import { onEmailReceived } from './EmailInbox_Incoming';
Expand Down Expand Up @@ -46,7 +46,18 @@ export async function configureEmailInboxes(): Promise<void> {
markSeen: true,
});

imap.on('email', Meteor.bindEnvironment((email) => onEmailReceived(email, emailInboxRecord.email, emailInboxRecord.department)));
imap.on('email', Meteor.bindEnvironment(async (email) => {
if (!email.messageId) {
return;
}

try {
await EmailMessageHistory.insertOne({ _id: email.messageId, email: emailInboxRecord.email });
onEmailReceived(email, emailInboxRecord.email, emailInboxRecord.department);
} catch (e) {
// In case the email message history has been received by other instance..
}
}));

imap.start();

Expand Down