Implement email config
This commit is contained in:
@ -228,7 +228,56 @@ export const meta = {
|
||||
desc: {
|
||||
'ja-JP': '外部ユーザーレコメンデーションのタイムアウト (ミリ秒)'
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
enableEmail: {
|
||||
validator: $.bool.optional,
|
||||
desc: {
|
||||
'ja-JP': 'メール配信を有効にするか否か'
|
||||
}
|
||||
},
|
||||
|
||||
email: {
|
||||
validator: $.str.optional.nullable,
|
||||
desc: {
|
||||
'ja-JP': 'メール配信する際に利用するメールアドレス'
|
||||
}
|
||||
},
|
||||
|
||||
smtpSecure: {
|
||||
validator: $.bool.optional,
|
||||
desc: {
|
||||
'ja-JP': 'SMTPサーバがSSLを使用しているか否か'
|
||||
}
|
||||
},
|
||||
|
||||
smtpHost: {
|
||||
validator: $.str.optional,
|
||||
desc: {
|
||||
'ja-JP': 'SMTPサーバのホスト'
|
||||
}
|
||||
},
|
||||
|
||||
smtpPort: {
|
||||
validator: $.num.optional,
|
||||
desc: {
|
||||
'ja-JP': 'SMTPサーバのポート'
|
||||
}
|
||||
},
|
||||
|
||||
smtpUser: {
|
||||
validator: $.str.optional,
|
||||
desc: {
|
||||
'ja-JP': 'SMTPサーバのユーザー名'
|
||||
}
|
||||
},
|
||||
|
||||
smtpPass: {
|
||||
validator: $.str.optional,
|
||||
desc: {
|
||||
'ja-JP': 'SMTPサーバのパスワード'
|
||||
}
|
||||
},
|
||||
}
|
||||
};
|
||||
|
||||
@ -359,6 +408,34 @@ export default define(meta, (ps) => new Promise(async (res, rej) => {
|
||||
set.externalUserRecommendationTimeout = ps.externalUserRecommendationTimeout;
|
||||
}
|
||||
|
||||
if (ps.enableEmail !== undefined) {
|
||||
set.enableEmail = ps.enableEmail;
|
||||
}
|
||||
|
||||
if (ps.email !== undefined) {
|
||||
set.email = ps.email;
|
||||
}
|
||||
|
||||
if (ps.smtpSecure !== undefined) {
|
||||
set.smtpSecure = ps.smtpSecure;
|
||||
}
|
||||
|
||||
if (ps.smtpHost !== undefined) {
|
||||
set.smtpHost = ps.smtpHost;
|
||||
}
|
||||
|
||||
if (ps.smtpPort !== undefined) {
|
||||
set.smtpPort = ps.smtpPort;
|
||||
}
|
||||
|
||||
if (ps.smtpUser !== undefined) {
|
||||
set.smtpUser = ps.smtpUser;
|
||||
}
|
||||
|
||||
if (ps.smtpPass !== undefined) {
|
||||
set.smtpPass = ps.smtpPass;
|
||||
}
|
||||
|
||||
await Meta.update({}, {
|
||||
$set: set
|
||||
}, { upsert: true });
|
||||
|
85
src/server/api/endpoints/i/update_email.ts
Normal file
85
src/server/api/endpoints/i/update_email.ts
Normal file
@ -0,0 +1,85 @@
|
||||
import $ from 'cafy';
|
||||
import User, { pack } from '../../../../models/user';
|
||||
import { publishMainStream } from '../../../../stream';
|
||||
import define from '../../define';
|
||||
import * as nodemailer from 'nodemailer';
|
||||
import fetchMeta from '../../../../misc/fetch-meta';
|
||||
import rndstr from 'rndstr';
|
||||
import config from '../../../../config';
|
||||
const ms = require('ms');
|
||||
|
||||
export const meta = {
|
||||
requireCredential: true,
|
||||
|
||||
secure: true,
|
||||
|
||||
limit: {
|
||||
duration: ms('1hour'),
|
||||
max: 3
|
||||
},
|
||||
|
||||
params: {
|
||||
email: {
|
||||
validator: $.str.optional.nullable
|
||||
},
|
||||
}
|
||||
};
|
||||
|
||||
export default define(meta, (ps, user) => new Promise(async (res, rej) => {
|
||||
await User.update(user._id, {
|
||||
$set: {
|
||||
email: ps.email,
|
||||
emailVerified: false,
|
||||
emailVerifyCode: null
|
||||
}
|
||||
});
|
||||
|
||||
// Serialize
|
||||
const iObj = await pack(user._id, user, {
|
||||
detail: true,
|
||||
includeSecrets: true
|
||||
});
|
||||
|
||||
// Send response
|
||||
res(iObj);
|
||||
|
||||
// Publish meUpdated event
|
||||
publishMainStream(user._id, 'meUpdated', iObj);
|
||||
|
||||
if (ps.email != null) {
|
||||
const code = rndstr('a-z0-9', 16);
|
||||
|
||||
await User.update(user._id, {
|
||||
$set: {
|
||||
emailVerifyCode: code
|
||||
}
|
||||
});
|
||||
|
||||
const meta = await fetchMeta();
|
||||
|
||||
const transporter = nodemailer.createTransport({
|
||||
host: meta.smtpHost,
|
||||
port: meta.smtpPort,
|
||||
secure: meta.smtpSecure,
|
||||
auth: {
|
||||
user: meta.smtpUser,
|
||||
pass: meta.smtpPass
|
||||
}
|
||||
});
|
||||
|
||||
const link = `${config.url}/vefify-email/${code}`;
|
||||
|
||||
transporter.sendMail({
|
||||
from: meta.email,
|
||||
to: ps.email,
|
||||
subject: meta.name,
|
||||
text: `To verify email, please click this link: ${link}`
|
||||
}, (error, info) => {
|
||||
if (error) {
|
||||
return console.error(error);
|
||||
}
|
||||
|
||||
console.log('Message sent: %s', info.messageId);
|
||||
});
|
||||
}
|
||||
}));
|
@ -108,6 +108,13 @@ export default define(meta, (ps, me) => new Promise(async (res, rej) => {
|
||||
response.discordClientId = instance.discordClientId;
|
||||
response.discordClientSecret = instance.discordClientSecret;
|
||||
response.summalyProxy = instance.summalyProxy;
|
||||
response.enableEmail = instance.enableEmail;
|
||||
response.email = instance.email;
|
||||
response.smtpSecure = instance.smtpSecure;
|
||||
response.smtpHost = instance.smtpHost;
|
||||
response.smtpPort = instance.smtpPort;
|
||||
response.smtpUser = instance.smtpUser;
|
||||
response.smtpPass = instance.smtpPass;
|
||||
}
|
||||
|
||||
res(response);
|
||||
|
Reference in New Issue
Block a user