Refactor
This commit is contained in:
parent
9c8d26affa
commit
dda7b0c80e
2 changed files with 38 additions and 31 deletions
|
@ -1,15 +1,13 @@
|
||||||
import $ from 'cafy';
|
import $ from 'cafy';
|
||||||
import { publishMainStream } from '../../../../services/stream';
|
import { publishMainStream } from '../../../../services/stream';
|
||||||
import define from '../../define';
|
import define from '../../define';
|
||||||
import * as nodemailer from 'nodemailer';
|
|
||||||
import { fetchMeta } from '../../../../misc/fetch-meta';
|
|
||||||
import rndstr from 'rndstr';
|
import rndstr from 'rndstr';
|
||||||
import config from '../../../../config';
|
import config from '../../../../config';
|
||||||
import * as ms from 'ms';
|
import * as ms from 'ms';
|
||||||
import * as bcrypt from 'bcryptjs';
|
import * as bcrypt from 'bcryptjs';
|
||||||
import { apiLogger } from '../../logger';
|
|
||||||
import { Users, UserProfiles } from '../../../../models';
|
import { Users, UserProfiles } from '../../../../models';
|
||||||
import { ensure } from '../../../../prelude/ensure';
|
import { ensure } from '../../../../prelude/ensure';
|
||||||
|
import { sendEmail } from '../../../../services/send-email';
|
||||||
|
|
||||||
export const meta = {
|
export const meta = {
|
||||||
requireCredential: true,
|
requireCredential: true,
|
||||||
|
@ -63,36 +61,9 @@ export default define(meta, async (ps, user) => {
|
||||||
emailVerifyCode: code
|
emailVerifyCode: code
|
||||||
});
|
});
|
||||||
|
|
||||||
const meta = await fetchMeta(true);
|
|
||||||
|
|
||||||
const enableAuth = meta.smtpUser != null && meta.smtpUser !== '';
|
|
||||||
|
|
||||||
const transporter = nodemailer.createTransport({
|
|
||||||
host: meta.smtpHost,
|
|
||||||
port: meta.smtpPort,
|
|
||||||
secure: meta.smtpSecure,
|
|
||||||
ignoreTLS: !enableAuth,
|
|
||||||
auth: enableAuth ? {
|
|
||||||
user: meta.smtpUser,
|
|
||||||
pass: meta.smtpPass
|
|
||||||
} : undefined
|
|
||||||
} as any);
|
|
||||||
|
|
||||||
const link = `${config.url}/verify-email/${code}`;
|
const link = `${config.url}/verify-email/${code}`;
|
||||||
|
|
||||||
transporter.sendMail({
|
sendEmail(ps.email, 'Email verification', `To verify email, please click this link: ${link}`);
|
||||||
from: meta.email!,
|
|
||||||
to: ps.email,
|
|
||||||
subject: meta.name || 'Misskey',
|
|
||||||
text: `To verify email, please click this link: ${link}`
|
|
||||||
}, (error, info) => {
|
|
||||||
if (error) {
|
|
||||||
apiLogger.error(error);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
apiLogger.info('Message sent: %s', info.messageId);
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return iObj;
|
return iObj;
|
||||||
|
|
36
src/services/send-email.ts
Normal file
36
src/services/send-email.ts
Normal file
|
@ -0,0 +1,36 @@
|
||||||
|
import * as nodemailer from 'nodemailer';
|
||||||
|
import { fetchMeta } from '../misc/fetch-meta';
|
||||||
|
import Logger from './logger';
|
||||||
|
|
||||||
|
export const logger = new Logger('email');
|
||||||
|
|
||||||
|
export async function sendEmail(to: string, subject: string, text: string) {
|
||||||
|
const meta = await fetchMeta(true);
|
||||||
|
|
||||||
|
const enableAuth = meta.smtpUser != null && meta.smtpUser !== '';
|
||||||
|
|
||||||
|
const transporter = nodemailer.createTransport({
|
||||||
|
host: meta.smtpHost,
|
||||||
|
port: meta.smtpPort,
|
||||||
|
secure: meta.smtpSecure,
|
||||||
|
ignoreTLS: !enableAuth,
|
||||||
|
auth: enableAuth ? {
|
||||||
|
user: meta.smtpUser,
|
||||||
|
pass: meta.smtpPass
|
||||||
|
} : undefined
|
||||||
|
} as any);
|
||||||
|
|
||||||
|
try {
|
||||||
|
const info = await transporter.sendMail({
|
||||||
|
from: meta.email!,
|
||||||
|
to: to,
|
||||||
|
subject: subject || 'Misskey',
|
||||||
|
text: text
|
||||||
|
});
|
||||||
|
|
||||||
|
logger.info('Message sent: %s', info.messageId);
|
||||||
|
} catch (e) {
|
||||||
|
logger.error(e);
|
||||||
|
throw e;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue