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

refactor: Remove more promise awaits #28796

Merged
merged 1 commit into from
Apr 4, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 4 additions & 4 deletions apps/meteor/app/2fa/server/code/EmailCheck.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@ export class EmailCheck implements ICodeCheck {
return this.getUserVerifiedEmails(user).length > 0;
}

private send2FAEmail(address: string, random: string, user: IUser): void {
private async send2FAEmail(address: string, random: string, user: IUser): Promise<void> {
const language = user.language || settings.get('Language') || 'en';

const t = (s: string): string => TAPi18n.__(s, { lng: language });

Mailer.send({
await Mailer.send({
to: address,
from: settings.get('From_Email'),
subject: 'Authentication code',
Expand Down Expand Up @@ -103,8 +103,8 @@ ${t('If_you_didnt_try_to_login_in_your_account_please_ignore_this_email')}

await Users.addEmailCodeByUserId(user._id, encryptedRandom, expire);

for (const address of emails) {
this.send2FAEmail(address, random, user);
for await (const address of emails) {
await this.send2FAEmail(address, random, user);
}
}

Expand Down
34 changes: 16 additions & 18 deletions apps/meteor/app/2fa/server/functions/resetTOTP.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,24 +35,22 @@ const sendResetNotification = async function (uid: string): Promise<void> {
const from = settings.get('From_Email');
const subject = t('TOTP_reset_email');

for (const address of addresses) {
Meteor.defer(() => {
try {
Mailer.send({
to: address,
from,
subject,
text,
html,
} as any);
} catch (error) {
const message = error instanceof Error ? error.message : String(error);
throw new Meteor.Error('error-email-send-failed', `Error trying to send email: ${message}`, {
function: 'resetUserTOTP',
message,
});
}
});
for await (const address of addresses) {
try {
await Mailer.send({
to: address,
from,
subject,
text,
html,
} as any);
} catch (error) {
const message = error instanceof Error ? error.message : String(error);
throw new Meteor.Error('error-email-send-failed', `Error trying to send email: ${message}`, {
function: 'resetUserTOTP',
message,
});
}
}
};

Expand Down
2 changes: 1 addition & 1 deletion apps/meteor/app/authentication/server/startup/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ Accounts.onCreateUser(function (options, user = {}) {
}),
};

Mailer.send(email);
Promise.await(Mailer.send(email));
}

callbacks.run('onCreateUser', options, user);
Expand Down
58 changes: 29 additions & 29 deletions apps/meteor/app/integrations/server/lib/triggerHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ class RocketChatIntegrationHandler {
return false;
}

updateHistory({
async updateHistory({
historyId,
step,
integration,
Expand Down Expand Up @@ -161,15 +161,15 @@ class RocketChatIntegrationHandler {
}

if (historyId) {
Promise.await(IntegrationHistory.updateOne({ _id: historyId }, { $set: history }));
await IntegrationHistory.updateOne({ _id: historyId }, { $set: history });
return historyId;
}

history._createdAt = new Date();

const _id = Random.id();

Promise.await(IntegrationHistory.insertOne({ _id, ...history }));
await IntegrationHistory.insertOne({ _id, ...history });

return _id;
}
Expand Down Expand Up @@ -323,12 +323,12 @@ class RocketChatIntegrationHandler {
return typeof script[method] !== 'undefined';
}

executeScript(integration, method, params, historyId) {
async executeScript(integration, method, params, historyId) {
let script;
try {
script = this.getIntegrationScript(integration);
} catch (e) {
this.updateHistory({
await this.updateHistory({
historyId,
step: 'execute-script-getting-script',
error: true,
Expand All @@ -339,7 +339,7 @@ class RocketChatIntegrationHandler {

if (!script[method]) {
outgoingLogger.error(`Method "${method}" no found in the Integration "${integration.name}"`);
this.updateHistory({ historyId, step: `execute-script-no-method-${method}` });
await this.updateHistory({ historyId, step: `execute-script-no-method-${method}` });
return;
}

Expand All @@ -349,7 +349,7 @@ class RocketChatIntegrationHandler {
sandbox.method = method;
sandbox.params = params;

this.updateHistory({ historyId, step: `execute-script-before-running-${method}` });
await this.updateHistory({ historyId, step: `execute-script-before-running-${method}` });

const vm = new VM({
timeout: 3000,
Expand Down Expand Up @@ -378,7 +378,7 @@ class RocketChatIntegrationHandler {

return result;
} catch (err) {
this.updateHistory({
await this.updateHistory({
historyId,
step: `execute-script-error-running-${method}`,
error: true,
Expand Down Expand Up @@ -690,7 +690,7 @@ class RocketChatIntegrationHandler {
return;
}

const historyId = this.updateHistory({
const historyId = await this.updateHistory({
step: 'start-execute-trigger-url',
integration: trigger,
event,
Expand All @@ -706,7 +706,7 @@ class RocketChatIntegrationHandler {
}

this.mapEventArgsToData(data, { trigger, event, message, room, owner, user });
this.updateHistory({ historyId, step: 'mapped-args-to-data', data, triggerWord: word });
await this.updateHistory({ historyId, step: 'mapped-args-to-data', data, triggerWord: word });

outgoingLogger.info(`Will be executing the Integration "${trigger.name}" to the url: ${url}`);
outgoingLogger.debug({ data });
Expand All @@ -723,27 +723,27 @@ class RocketChatIntegrationHandler {
};

if (this.hasScriptAndMethod(trigger, 'prepare_outgoing_request')) {
opts = this.executeScript(trigger, 'prepare_outgoing_request', { request: opts }, historyId);
opts = await this.executeScript(trigger, 'prepare_outgoing_request', { request: opts }, historyId);
}

this.updateHistory({ historyId, step: 'after-maybe-ran-prepare', ranPrepareScript: true });
await this.updateHistory({ historyId, step: 'after-maybe-ran-prepare', ranPrepareScript: true });

if (!opts) {
this.updateHistory({ historyId, step: 'after-prepare-no-opts', finished: true });
await this.updateHistory({ historyId, step: 'after-prepare-no-opts', finished: true });
return;
}

if (opts.message) {
const prepareMessage = await this.sendMessage({ trigger, room, message: opts.message, data });
this.updateHistory({
await this.updateHistory({
historyId,
step: 'after-prepare-send-message',
prepareSentMessage: prepareMessage,
});
}

if (!opts.url || !opts.method) {
this.updateHistory({ historyId, step: 'after-prepare-no-url_or_method', finished: true });
await this.updateHistory({ historyId, step: 'after-prepare-no-url_or_method', finished: true });
return;
}

Expand All @@ -757,7 +757,7 @@ class RocketChatIntegrationHandler {
opts.headers.Authorization = `Basic ${base64}`;
}

this.updateHistory({
await this.updateHistory({
historyId,
step: 'pre-http-call',
url: opts.url,
Expand Down Expand Up @@ -798,7 +798,7 @@ class RocketChatIntegrationHandler {
}
})();

this.updateHistory({
await this.updateHistory({
historyId,
step: 'after-http-call',
httpError: null,
Expand All @@ -817,7 +817,7 @@ class RocketChatIntegrationHandler {
},
};

const scriptResult = this.executeScript(trigger, 'process_outgoing_response', sandbox, historyId);
const scriptResult = await this.executeScript(trigger, 'process_outgoing_response', sandbox, historyId);

if (scriptResult && scriptResult.content) {
const resultMessage = await this.sendMessage({
Expand All @@ -826,7 +826,7 @@ class RocketChatIntegrationHandler {
message: scriptResult.content,
data,
});
this.updateHistory({
await this.updateHistory({
historyId,
step: 'after-process-send-message',
processSentMessage: resultMessage,
Expand All @@ -836,7 +836,7 @@ class RocketChatIntegrationHandler {
}

if (scriptResult === false) {
this.updateHistory({ historyId, step: 'after-process-false-result', finished: true });
await this.updateHistory({ historyId, step: 'after-process-false-result', finished: true });
return;
}
}
Expand All @@ -850,14 +850,14 @@ class RocketChatIntegrationHandler {
});

if (res.status === 410) {
this.updateHistory({ historyId, step: 'after-process-http-status-410', error: true });
await this.updateHistory({ historyId, step: 'after-process-http-status-410', error: true });
outgoingLogger.error(`Disabling the Integration "${trigger.name}" because the status code was 401 (Gone).`);
await Integrations.updateOne({ _id: trigger._id }, { $set: { enabled: false } });
return;
}

if (res.status === 500) {
this.updateHistory({ historyId, step: 'after-process-http-status-500', error: true });
await this.updateHistory({ historyId, step: 'after-process-http-status-500', error: true });
outgoingLogger.error({
msg: `Error "500" for the Integration "${trigger.name}" to ${url}.`,
content,
Expand All @@ -868,7 +868,7 @@ class RocketChatIntegrationHandler {

if (trigger.retryFailedCalls) {
if (tries < trigger.retryCount && trigger.retryDelay) {
this.updateHistory({ historyId, error: true, step: `going-to-retry-${tries + 1}` });
await this.updateHistory({ historyId, error: true, step: `going-to-retry-${tries + 1}` });

let waitTime;

Expand All @@ -887,7 +887,7 @@ class RocketChatIntegrationHandler {
break;
default:
const er = new Error("The integration's retryDelay setting is invalid.");
this.updateHistory({
await this.updateHistory({
historyId,
step: 'failed-and-retry-delay-is-invalid',
error: true,
Expand All @@ -901,10 +901,10 @@ class RocketChatIntegrationHandler {
void this.executeTriggerUrl(url, trigger, { event, message, room, owner, user }, historyId, tries + 1);
}, waitTime);
} else {
this.updateHistory({ historyId, step: 'too-many-retries', error: true });
await this.updateHistory({ historyId, step: 'too-many-retries', error: true });
}
} else {
this.updateHistory({
await this.updateHistory({
historyId,
step: 'failed-and-not-configured-to-retry',
error: true,
Expand All @@ -918,7 +918,7 @@ class RocketChatIntegrationHandler {
if (content && this.successResults.includes(res.status)) {
if (data?.text || data?.attachments) {
const resultMsg = await this.sendMessage({ trigger, room, message: data, data });
this.updateHistory({
await this.updateHistory({
historyId,
step: 'url-response-sent-message',
resultMessage: resultMsg,
Expand All @@ -927,9 +927,9 @@ class RocketChatIntegrationHandler {
}
}
})
.catch((error) => {
.catch(async (error) => {
outgoingLogger.error(error);
this.updateHistory({
await this.updateHistory({
historyId,
step: 'after-http-call',
httpError: error,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export const sendInvitationEmail = async (userId: string, emails: string[]) => {

for await (const email of validEmails) {
try {
Mailer.send({
await Mailer.send({
to: email,
from: settings.get('From_Email'),
subject,
Expand Down
10 changes: 5 additions & 5 deletions apps/meteor/app/lib/server/functions/saveUser.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ Meteor.startup(() => {
});
});

function _sendUserEmail(subject, html, userData) {
async function _sendUserEmail(subject, html, userData) {
const email = {
to: userData.email,
from: settings.get('From_Email'),
Expand All @@ -53,7 +53,7 @@ function _sendUserEmail(subject, html, userData) {
}

try {
Mailer.send(email);
await Mailer.send(email);
} catch (error) {
throw new Meteor.Error('error-email-send-failed', `Error trying to send email: ${error.message}`, {
function: 'RocketChat.saveUser',
Expand Down Expand Up @@ -306,11 +306,11 @@ const saveNewUser = async function (userData, sendPassword) {
Meteor.users.update({ _id }, updateUser);

if (userData.sendWelcomeEmail) {
_sendUserEmail(settings.get('Accounts_UserAddedEmail_Subject'), html, userData);
await _sendUserEmail(settings.get('Accounts_UserAddedEmail_Subject'), html, userData);
}

if (sendPassword) {
_sendUserEmail(settings.get('Password_Changed_Email_Subject'), passwordChangedHtml, userData);
await _sendUserEmail(settings.get('Password_Changed_Email_Subject'), passwordChangedHtml, userData);
}

userData._id = _id;
Expand Down Expand Up @@ -431,7 +431,7 @@ export const saveUser = async function (userId, userData) {
});

if (sendPassword) {
_sendUserEmail(settings.get('Password_Changed_Email_Subject'), passwordChangedHtml, userData);
await _sendUserEmail(settings.get('Password_Changed_Email_Subject'), passwordChangedHtml, userData);
}

return true;
Expand Down
6 changes: 3 additions & 3 deletions apps/meteor/app/lib/server/functions/setEmail.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ Meteor.startup(() => {
});
});

const _sendEmailChangeNotification = function (to: string, newEmail: string) {
const _sendEmailChangeNotification = async function (to: string, newEmail: string) {
const subject = String(settings.get('Email_Changed_Email_Subject'));
const email = {
to,
Expand All @@ -28,7 +28,7 @@ const _sendEmailChangeNotification = function (to: string, newEmail: string) {
};

try {
Mailer.send(email);
await Mailer.send(email);
} catch (error: any) {
throw new Meteor.Error('error-email-send-failed', `Error trying to send email: ${error.message}`, {
function: 'setEmail',
Expand Down Expand Up @@ -70,7 +70,7 @@ const _setEmail = async function (userId: string, email: string, shouldSendVerif
const oldEmail = user?.emails?.[0];

if (oldEmail) {
_sendEmailChangeNotification(oldEmail.address, email);
await _sendEmailChangeNotification(oldEmail.address, email);
}

// Set new email
Expand Down
2 changes: 1 addition & 1 deletion apps/meteor/app/lib/server/methods/sendSMTPTestEmail.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ Meteor.methods<ServerMethods>({
});
}
try {
Mailer.send({
await Mailer.send({
to: user.emails[0].address,
from: settings.get('From_Email'),
subject: 'SMTP Test Email',
Expand Down