2022-02-27 03:07:39 +01:00
|
|
|
import { publishMainStream } from '@/services/stream.js';
|
2022-04-30 14:52:07 +02:00
|
|
|
import { pushNotification } from '@/services/push-notification.js';
|
2022-12-26 04:13:20 +01:00
|
|
|
import { Notifications, Mutings, NoteThreadMutings, UserProfiles, Users } from '@/models/index.js';
|
2022-02-27 03:07:39 +01:00
|
|
|
import { genId } from '@/misc/gen-id.js';
|
|
|
|
import { User } from '@/models/entities/user.js';
|
|
|
|
import { Notification } from '@/models/entities/notification.js';
|
|
|
|
import { sendEmailNotification } from './send-email-notification.js';
|
2016-12-28 23:49:51 +01:00
|
|
|
|
2019-04-07 14:50:36 +02:00
|
|
|
export async function createNotification(
|
|
|
|
notifieeId: User['id'],
|
2020-02-12 18:17:54 +01:00
|
|
|
type: Notification['type'],
|
2020-03-28 10:07:41 +01:00
|
|
|
data: Partial<Notification>
|
2019-04-07 14:50:36 +02:00
|
|
|
) {
|
2020-03-28 10:07:41 +01:00
|
|
|
if (data.notifierId && (notifieeId === data.notifierId)) {
|
2019-04-07 14:50:36 +02:00
|
|
|
return null;
|
2016-12-28 23:49:51 +01:00
|
|
|
}
|
|
|
|
|
2022-03-26 07:34:00 +01:00
|
|
|
const profile = await UserProfiles.findOneBy({ userId: notifieeId });
|
2020-08-22 03:06:17 +02:00
|
|
|
|
2020-09-18 15:18:21 +02:00
|
|
|
const isMuted = profile?.mutingNotificationTypes.includes(type);
|
2020-08-22 03:06:17 +02:00
|
|
|
|
2022-12-26 04:13:20 +01:00
|
|
|
if (data.note != null) {
|
|
|
|
const threadMute = await NoteThreadMutings.findOneBy({
|
|
|
|
userId: notifieeId,
|
|
|
|
threadId: data.note.threadId || data.note.id,
|
|
|
|
});
|
|
|
|
|
|
|
|
if (threadMute) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-28 10:07:41 +01:00
|
|
|
// Create notification
|
2022-01-02 18:20:30 +01:00
|
|
|
const notification = await Notifications.insert({
|
2019-04-07 14:50:36 +02:00
|
|
|
id: genId(),
|
2018-03-29 07:48:47 +02:00
|
|
|
createdAt: new Date(),
|
2019-04-07 14:50:36 +02:00
|
|
|
notifieeId: notifieeId,
|
2016-12-28 23:49:51 +01:00
|
|
|
type: type,
|
2020-08-22 03:06:17 +02:00
|
|
|
// 相手がこの通知をミュートしているようなら、既読を予めつけておく
|
|
|
|
isRead: isMuted,
|
2021-12-09 15:58:30 +01:00
|
|
|
...data,
|
2022-01-02 18:20:30 +01:00
|
|
|
} as Partial<Notification>)
|
2022-03-26 07:34:00 +01:00
|
|
|
.then(x => Notifications.findOneByOrFail(x.identifiers[0]));
|
2016-12-28 23:49:51 +01:00
|
|
|
|
2021-03-21 14:26:45 +01:00
|
|
|
const packed = await Notifications.pack(notification, {});
|
2018-06-21 04:35:28 +02:00
|
|
|
|
2016-12-28 23:49:51 +01:00
|
|
|
// Publish notification event
|
2019-04-07 14:50:36 +02:00
|
|
|
publishMainStream(notifieeId, 'notification', packed);
|
2018-05-28 18:22:39 +02:00
|
|
|
|
2018-10-07 19:10:46 +02:00
|
|
|
// 2秒経っても(今回作成した)通知が既読にならなかったら「未読の通知がありますよ」イベントを発行する
|
2017-11-20 01:09:11 +01:00
|
|
|
setTimeout(async () => {
|
2022-03-26 07:34:00 +01:00
|
|
|
const fresh = await Notifications.findOneBy({ id: notification.id });
|
2019-04-12 18:43:22 +02:00
|
|
|
if (fresh == null) return; // 既に削除されているかもしれない
|
2021-02-13 04:28:26 +01:00
|
|
|
if (fresh.isRead) return;
|
|
|
|
|
|
|
|
//#region ただしミュートしているユーザーからの通知なら無視
|
2022-03-26 07:34:00 +01:00
|
|
|
const mutings = await Mutings.findBy({
|
2021-12-09 15:58:30 +01:00
|
|
|
muterId: notifieeId,
|
2021-02-13 04:28:26 +01:00
|
|
|
});
|
|
|
|
if (data.notifierId && mutings.map(m => m.muteeId).includes(data.notifierId)) {
|
|
|
|
return;
|
2017-11-20 01:09:11 +01:00
|
|
|
}
|
2021-02-13 04:28:26 +01:00
|
|
|
//#endregion
|
|
|
|
|
|
|
|
publishMainStream(notifieeId, 'unreadNotification', packed);
|
2022-04-30 14:52:07 +02:00
|
|
|
pushNotification(notifieeId, 'notification', packed);
|
2021-02-13 04:28:26 +01:00
|
|
|
|
2022-03-26 07:34:00 +01:00
|
|
|
if (type === 'follow') sendEmailNotification.follow(notifieeId, await Users.findOneByOrFail({ id: data.notifierId! }));
|
|
|
|
if (type === 'receiveFollowRequest') sendEmailNotification.receiveFollowRequest(notifieeId, await Users.findOneByOrFail({ id: data.notifierId! }));
|
2018-10-07 19:10:46 +02:00
|
|
|
}, 2000);
|
2019-04-07 14:50:36 +02:00
|
|
|
|
|
|
|
return notification;
|
|
|
|
}
|