1
0
mirror of https://github.com/hotomoe/hotomoe synced 2024-12-22 18:48:08 +09:00
hotomoe/src/api/common/notify.ts

39 lines
1.1 KiB
TypeScript
Raw Normal View History

2016-12-29 07:49:51 +09:00
import * as mongo from 'mongodb';
import Notification from '../models/notification';
import event from '../event';
import serialize from '../serializers/notification';
export default (
notifiee: mongo.ObjectID,
notifier: mongo.ObjectID,
type: string,
2017-03-05 12:09:34 +09:00
content?: any
2016-12-29 07:49:51 +09:00
) => new Promise<any>(async (resolve, reject) => {
if (notifiee.equals(notifier)) {
return resolve();
}
// Create notification
2017-01-18 05:28:12 +09:00
const notification = await Notification.insert(Object.assign({
2016-12-29 07:49:51 +09: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',
await serialize(notification));
2017-11-20 09:09:11 +09:00
// 3秒経っても(今回作成した)通知が既読にならなかったら「未読の通知がありますよ」イベントを発行する
setTimeout(async () => {
const fresh = await Notification.findOne({ _id: notification._id }, { is_read: true });
if (!fresh.is_read) {
event(notifiee, 'unread_notification', await serialize(notification));
}
}, 3000);
2016-12-29 07:49:51 +09:00
});