summaryrefslogtreecommitdiff
path: root/lib/okpush/db.js
diff options
context:
space:
mode:
Diffstat (limited to 'lib/okpush/db.js')
-rw-r--r--lib/okpush/db.js140
1 files changed, 140 insertions, 0 deletions
diff --git a/lib/okpush/db.js b/lib/okpush/db.js
new file mode 100644
index 00000000..f64acf69
--- /dev/null
+++ b/lib/okpush/db.js
@@ -0,0 +1,140 @@
+var mongoose = require('mongoose')
+var findOrCreate = require('mongoose-findorcreate')
+var _ = require('lodash')
+var db, PushToken, Notification, Channel
+
+mongoose.Promise = require('bluebird')
+
+function init (config) {
+ db = mongoose.connect(config.mongodbUrl)
+ mongoose.connection.on('error', errorHandler)
+
+ var pushTokenSchema = new db.Schema({
+ platform: {
+ type: 'String',
+ required: true,
+ lowercase: true,
+ enum: ['ios', 'android'],
+ },
+ token: {
+ type: 'String',
+ required: true,
+ },
+ channel: {
+ type: 'String',
+ required: true,
+ },
+ })
+
+ var channelSchema = new db.Schema({
+ channel: {
+ type: 'String',
+ required: true,
+ lowercase: true,
+ },
+ last_push: {
+ type: 'Date',
+ required: true,
+ }
+ })
+ channelSchema.plugin(findOrCreate);
+
+ var notificationSchema = new db.Schema({
+ channel: {
+ type: 'String',
+ required: true,
+ lowercase: true,
+ },
+ date: {
+ type: 'Date',
+ required: true,
+ }
+ })
+
+ PushToken = db.model('PushToken', pushTokenSchema)
+ Notification = db.model('Notification', notificationSchema)
+ Channel = db.model('Channel', channelSchema)
+}
+
+function errorHandler (error) {
+ console.error('ERROR: ' + error)
+}
+
+/* devices / tokens */
+
+function addToken (data) {
+ return new PushToken(data).save()
+}
+function getAllTokens (channel, cb) {
+ PushToken.find({ channel: channel }, function (err, items) {
+ if (err) return cb(err, null)
+ var items = _.map(items, function (item) {
+ return _.pick(item, ['type', 'token'])
+ })
+ return cb(null, items)
+ })
+}
+function removeToken (data) {
+ PushToken.find(data).remove().exec()
+}
+function getDeviceCount (channels, cb) {
+ var countz = {}
+ get_next()
+ function get_next() {
+ var channel = channels.pop()
+ PushToken.count({ channel: channel }, function(err, count){
+ countz[channel] = count
+ if (channels.length) {
+ return get_next()
+ }
+ else {
+ cb(countz)
+ }
+ })
+ }
+}
+
+/* notifications */
+
+function addNotification (channel, cb) {
+ var now = new Date
+ Channel.findOrCreate({channel: channel}, {last_push: now}, function(err, note, created) {
+ if (err) {
+ console.error("Error finding/creating notification", err)
+ cb(err, false)
+ return
+ }
+ else if (! created) {
+ note.last_push = now
+ note.save()
+ }
+ cb(null, note)
+ })
+ new Notification ({
+ channel: channel,
+ date: now,
+ }).save()
+}
+function getNotifications (cb) {
+ Notification.find(function (err, items) {
+ if (err) return cb(err, null)
+
+ var items = _.map(items, function (item) {
+ return _.pick(item, ['channel', 'last_push'])
+ })
+
+ return cb(null, items)
+ })
+}
+
+/* wrap functions for some reason */
+
+module.exports = {
+ init: init,
+ addToken: addToken,
+ removeToken: removeToken,
+ getAllTokens: getAllTokens,
+ getDeviceCount: getDeviceCount,
+ addNotification: addNotification,
+ getNotifications: getNotifications,
+} \ No newline at end of file