wip: email notification
This commit is contained in:
parent
2d3248504b
commit
ebadd7fd3f
20 changed files with 355 additions and 159 deletions
|
|
@ -4,6 +4,7 @@ import { Notifications, Mutings, UserProfiles } from '../models';
|
|||
import { genId } from '../misc/gen-id';
|
||||
import { User } from '../models/entities/user';
|
||||
import { Notification } from '../models/entities/notification';
|
||||
import { sendEmailNotification } from './send-email-notification';
|
||||
|
||||
export async function createNotification(
|
||||
notifieeId: User['id'],
|
||||
|
|
@ -38,20 +39,22 @@ export async function createNotification(
|
|||
setTimeout(async () => {
|
||||
const fresh = await Notifications.findOne(notification.id);
|
||||
if (fresh == null) return; // 既に削除されているかもしれない
|
||||
if (!fresh.isRead) {
|
||||
//#region ただしミュートしているユーザーからの通知なら無視
|
||||
const mutings = await Mutings.find({
|
||||
muterId: notifieeId
|
||||
});
|
||||
if (data.notifierId && mutings.map(m => m.muteeId).includes(data.notifierId)) {
|
||||
return;
|
||||
}
|
||||
//#endregion
|
||||
if (fresh.isRead) return;
|
||||
|
||||
publishMainStream(notifieeId, 'unreadNotification', packed);
|
||||
|
||||
pushSw(notifieeId, 'notification', packed);
|
||||
//#region ただしミュートしているユーザーからの通知なら無視
|
||||
const mutings = await Mutings.find({
|
||||
muterId: notifieeId
|
||||
});
|
||||
if (data.notifierId && mutings.map(m => m.muteeId).includes(data.notifierId)) {
|
||||
return;
|
||||
}
|
||||
//#endregion
|
||||
|
||||
publishMainStream(notifieeId, 'unreadNotification', packed);
|
||||
|
||||
pushSw(notifieeId, 'notification', packed);
|
||||
if (type === 'follow') sendEmailNotification.follow(notifieeId, data);
|
||||
if (type === 'receiveFollowRequest') sendEmailNotification.receiveFollowRequest(notifieeId, data);
|
||||
}, 2000);
|
||||
|
||||
return notification;
|
||||
|
|
|
|||
28
src/services/send-email-notification.ts
Normal file
28
src/services/send-email-notification.ts
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
import { UserProfiles } from '../models';
|
||||
import { User } from '../models/entities/user';
|
||||
import { sendEmail } from './send-email';
|
||||
import * as locales from '../../locales/';
|
||||
import { I18n } from '../misc/i18n';
|
||||
|
||||
// TODO: locale ファイルをクライアント用とサーバー用で分けたい
|
||||
|
||||
async function follow(userId: User['id'], args: {}) {
|
||||
const userProfile = await UserProfiles.findOneOrFail({ userId: userId });
|
||||
if (!userProfile.email || !userProfile.emailNotificationTypes.includes('follow')) return;
|
||||
const locale = locales[userProfile.lang || 'ja-JP'];
|
||||
const i18n = new I18n(locale);
|
||||
sendEmail(userProfile.email, i18n.t('_email._follow.title'), 'test', 'test');
|
||||
}
|
||||
|
||||
async function receiveFollowRequest(userId: User['id'], args: {}) {
|
||||
const userProfile = await UserProfiles.findOneOrFail({ userId: userId });
|
||||
if (!userProfile.email || !userProfile.emailNotificationTypes.includes('receiveFollowRequest')) return;
|
||||
const locale = locales[userProfile.lang || 'ja-JP'];
|
||||
const i18n = new I18n(locale);
|
||||
sendEmail(userProfile.email, i18n.t('_email._receiveFollowRequest.title'), 'test', 'test');
|
||||
}
|
||||
|
||||
export const sendEmailNotification = {
|
||||
follow,
|
||||
receiveFollowRequest,
|
||||
};
|
||||
|
|
@ -5,7 +5,7 @@ import config from '../config';
|
|||
|
||||
export const logger = new Logger('email');
|
||||
|
||||
export async function sendEmail(to: string, subject: string, text: string) {
|
||||
export async function sendEmail(to: string, subject: string, html: string, text: string) {
|
||||
const meta = await fetchMeta(true);
|
||||
|
||||
const iconUrl = `${config.url}/assets/mi-white.png`;
|
||||
|
|
@ -44,6 +44,9 @@ export async function sendEmail(to: string, subject: string, text: string) {
|
|||
|
||||
body {
|
||||
padding: 16px;
|
||||
margin: 0;
|
||||
font-family: sans-serif;
|
||||
font-size: 14px;
|
||||
}
|
||||
|
||||
a {
|
||||
|
|
@ -67,6 +70,7 @@ export async function sendEmail(to: string, subject: string, text: string) {
|
|||
main > header > img {
|
||||
max-width: 128px;
|
||||
max-height: 28px;
|
||||
vertical-align: bottom;
|
||||
}
|
||||
main > article {
|
||||
padding: 32px;
|
||||
|
|
@ -97,7 +101,7 @@ export async function sendEmail(to: string, subject: string, text: string) {
|
|||
</header>
|
||||
<article>
|
||||
<h1>${ subject }</h1>
|
||||
<div>${ text }</div>
|
||||
<div>${ html }</div>
|
||||
</article>
|
||||
<footer>
|
||||
<a href="${ emailSettingUrl }">${ 'Email setting' }</a>
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue