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