summaryrefslogtreecommitdiff
path: root/src/api/common/notify.ts
diff options
context:
space:
mode:
authorsyuilo <syuilotan@yahoo.co.jp>2016-12-29 07:49:51 +0900
committersyuilo <syuilotan@yahoo.co.jp>2016-12-29 07:49:51 +0900
commitb3f42e62af698a67c2250533c437569559f1fdf9 (patch)
treecdf6937576e99cccf85e6fa3aa8860a1173c7cfb /src/api/common/notify.ts
downloadsharkey-b3f42e62af698a67c2250533c437569559f1fdf9.tar.gz
sharkey-b3f42e62af698a67c2250533c437569559f1fdf9.tar.bz2
sharkey-b3f42e62af698a67c2250533c437569559f1fdf9.zip
Initial commit :four_leaf_clover:
Diffstat (limited to 'src/api/common/notify.ts')
-rw-r--r--src/api/common/notify.ts32
1 files changed, 32 insertions, 0 deletions
diff --git a/src/api/common/notify.ts b/src/api/common/notify.ts
new file mode 100644
index 0000000000..c4c94ee704
--- /dev/null
+++ b/src/api/common/notify.ts
@@ -0,0 +1,32 @@
+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,
+ content: any
+) => new Promise<any>(async (resolve, reject) => {
+ if (notifiee.equals(notifier)) {
+ return resolve();
+ }
+
+ // Create notification
+ const res = await Notification.insert(Object.assign({
+ created_at: new Date(),
+ notifiee_id: notifiee,
+ notifier_id: notifier,
+ type: type,
+ is_read: false
+ }, content));
+
+ const notification = res.ops[0];
+
+ resolve(notification);
+
+ // Publish notification event
+ event(notifiee, 'notification',
+ await serialize(notification));
+});