diff options
| author | Jules Laplace <jules@okfoc.us> | 2017-02-16 02:17:03 +0100 |
|---|---|---|
| committer | Jules Laplace <jules@okfoc.us> | 2017-02-16 02:17:03 +0100 |
| commit | f40e2286faef696c25a81c04635aaf737606a39a (patch) | |
| tree | bedf10e8c731deae7d275e0e5d9d6ded604db154 /lib/okpush/db.js | |
| parent | 71e277a76862a1787ab34194faf762b7623347fd (diff) | |
lib
Diffstat (limited to 'lib/okpush/db.js')
| -rw-r--r-- | lib/okpush/db.js | 132 |
1 files changed, 132 insertions, 0 deletions
diff --git a/lib/okpush/db.js b/lib/okpush/db.js new file mode 100644 index 00000000..844bccae --- /dev/null +++ b/lib/okpush/db.js @@ -0,0 +1,132 @@ +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({ + type: { + 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 (deviceType, token, channel) { + var pushItem = new PushToken({ + type: deviceType, + token: token, + channel: channel + }) + pushItem.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 (token, channel) { + PushToken.find({ token: token, channel: channel }).remove().exec() +} +function getDeviceCount (channel, cb) { + PushToken.count({ channel: channel }, cb) +} + +/* 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 |
