2016-12-29 00:49:51 +02:00
|
|
|
import * as mongo from 'mongodb';
|
|
|
|
import Notification from '../models/notification';
|
2017-12-22 00:38:57 +02:00
|
|
|
import Mute from '../models/mute';
|
2016-12-29 00:49:51 +02:00
|
|
|
import event from '../event';
|
2018-02-02 01:21:30 +02:00
|
|
|
import { pack } from '../models/notification';
|
2016-12-29 00:49:51 +02:00
|
|
|
|
|
|
|
export default (
|
|
|
|
notifiee: mongo.ObjectID,
|
|
|
|
notifier: mongo.ObjectID,
|
|
|
|
type: string,
|
2017-03-05 05:09:34 +02:00
|
|
|
content?: any
|
2016-12-29 00:49:51 +02:00
|
|
|
) => new Promise<any>(async (resolve, reject) => {
|
|
|
|
if (notifiee.equals(notifier)) {
|
|
|
|
return resolve();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create notification
|
2017-01-17 22:28:12 +02:00
|
|
|
const notification = await Notification.insert(Object.assign({
|
2016-12-29 00:49:51 +02:00
|
|
|
created_at: new Date(),
|
|
|
|
notifiee_id: notifiee,
|
|
|
|
notifier_id: notifier,
|
|
|
|
type: type,
|
|
|
|
is_read: false
|
|
|
|
}, content));
|
|
|
|
|
|
|
|
resolve(notification);
|
|
|
|
|
|
|
|
// Publish notification event
|
|
|
|
event(notifiee, 'notification',
|
2018-02-02 01:21:30 +02:00
|
|
|
await pack(notification));
|
2017-11-20 02:09:11 +02:00
|
|
|
|
|
|
|
// 3秒経っても(今回作成した)通知が既読にならなかったら「未読の通知がありますよ」イベントを発行する
|
|
|
|
setTimeout(async () => {
|
|
|
|
const fresh = await Notification.findOne({ _id: notification._id }, { is_read: true });
|
|
|
|
if (!fresh.is_read) {
|
2017-12-22 00:38:57 +02:00
|
|
|
//#region ただしミュートしているユーザーからの通知なら無視
|
|
|
|
const mute = await Mute.find({
|
|
|
|
muter_id: notifiee,
|
|
|
|
deleted_at: { $exists: false }
|
|
|
|
});
|
|
|
|
const mutedUserIds = mute.map(m => m.mutee_id.toString());
|
2017-12-22 00:43:56 +02:00
|
|
|
if (mutedUserIds.indexOf(notifier.toString()) != -1) {
|
2017-12-22 00:38:57 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
//#endregion
|
|
|
|
|
2018-02-02 01:21:30 +02:00
|
|
|
event(notifiee, 'unread_notification', await pack(notification));
|
2017-11-20 02:09:11 +02:00
|
|
|
}
|
|
|
|
}, 3000);
|
2016-12-29 00:49:51 +02:00
|
|
|
});
|