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