summaryrefslogtreecommitdiff
path: root/src/services/create-notification.ts
diff options
context:
space:
mode:
authorsyuilo <syuilotan@yahoo.co.jp>2019-02-05 14:14:23 +0900
committersyuilo <syuilotan@yahoo.co.jp>2019-02-05 14:14:23 +0900
commit8129d4dc2366aea07da60e21abe3440230387bfe (patch)
treea3df66c5b7b80db69f79b62f0af0d9c5ae2eab12 /src/services/create-notification.ts
parentFix log (diff)
downloadmisskey-8129d4dc2366aea07da60e21abe3440230387bfe.tar.gz
misskey-8129d4dc2366aea07da60e21abe3440230387bfe.tar.bz2
misskey-8129d4dc2366aea07da60e21abe3440230387bfe.zip
Refactoring
Diffstat (limited to 'src/services/create-notification.ts')
-rw-r--r--src/services/create-notification.ts62
1 files changed, 62 insertions, 0 deletions
diff --git a/src/services/create-notification.ts b/src/services/create-notification.ts
new file mode 100644
index 0000000000..3e000ef2ed
--- /dev/null
+++ b/src/services/create-notification.ts
@@ -0,0 +1,62 @@
+import * as mongo from 'mongodb';
+import Notification from '../models/notification';
+import Mute from '../models/mute';
+import { pack } from '../models/notification';
+import { publishMainStream } from './stream';
+import User from '../models/user';
+import pushSw from './push-notification';
+
+export default (
+ notifiee: mongo.ObjectID,
+ notifier: mongo.ObjectID,
+ type: string,
+ content?: any
+) => new Promise<any>(async (resolve, reject) => {
+ if (notifiee.equals(notifier)) {
+ return resolve();
+ }
+
+ // Create notification
+ const notification = await Notification.insert(Object.assign({
+ createdAt: new Date(),
+ notifieeId: notifiee,
+ notifierId: notifier,
+ type: type,
+ isRead: false
+ }, content));
+
+ resolve(notification);
+
+ const packed = await pack(notification);
+
+ // Publish notification event
+ publishMainStream(notifiee, 'notification', packed);
+
+ // Update flag
+ User.update({ _id: notifiee }, {
+ $set: {
+ hasUnreadNotification: true
+ }
+ });
+
+ // 2秒経っても(今回作成した)通知が既読にならなかったら「未読の通知がありますよ」イベントを発行する
+ setTimeout(async () => {
+ const fresh = await Notification.findOne({ _id: notification._id }, { isRead: true });
+ if (!fresh.isRead) {
+ //#region ただしミュートしているユーザーからの通知なら無視
+ const mute = await Mute.find({
+ muterId: notifiee,
+ deletedAt: { $exists: false }
+ });
+ const mutedUserIds = mute.map(m => m.muteeId.toString());
+ if (mutedUserIds.indexOf(notifier.toString()) != -1) {
+ return;
+ }
+ //#endregion
+
+ publishMainStream(notifiee, 'unreadNotification', packed);
+
+ pushSw(notifiee, 'notification', packed);
+ }
+ }, 2000);
+});