Update Application model for the push notification

This commit is contained in:
Raymond Feng 2013-10-11 13:44:10 -07:00
parent 26d0f74d66
commit bfffd839a7
3 changed files with 97 additions and 98 deletions

View File

@ -217,3 +217,4 @@ loopback.Model = require('./models/model');
loopback.Email = require('./models/email'); loopback.Email = require('./models/email');
loopback.User = require('./models/user'); loopback.User = require('./models/user');
loopback.Session = require('./models/session'); loopback.Session = require('./models/session');
loopback.Application = require('./models/application');

View File

@ -1,10 +1,11 @@
var loopback = require('../loopback');
var assert = require('assert'); var assert = require('assert');
// Authentication schemes // Authentication schemes
var AuthenticationSchemeSchema = { var AuthenticationSchemeSchema = {
scheme: String, // local, facebook, google, twitter, linkedin, github scheme: String, // local, facebook, google, twitter, linkedin, github
credential: Object // Scheme-specific credentials credential: Object // Scheme-specific credentials
} };
var APNSSettingSchema = { var APNSSettingSchema = {
pushOptions: {type: { pushOptions: {type: {
@ -27,13 +28,13 @@ var PushNotificationSettingSchema = {
platform: {type: String, required: true}, // apns, gcm, mpns platform: {type: String, required: true}, // apns, gcm, mpns
// configuration: {type: Object} // platform-specific configurations // configuration: {type: Object} // platform-specific configurations
apns: APNSSettingSchema apns: APNSSettingSchema
} };
/** /**
* Data model for Application * Data model for Application
*/ */
var ApplicationSchema = { var ApplicationSchema = {
id: {type: String, id: true, generated: true},
// Basic information // Basic information
name: {type: String, required: true}, // The name name: {type: String, required: true}, // The name
description: String, // The description description: String, // The description
@ -59,7 +60,7 @@ var ApplicationSchema = {
masterKey: String, masterKey: String,
// Push notification // Push notification
pushSettings: [PushNotificationSettingSchema], pushSettings: PushNotificationSettingSchema,
// User Authentication // User Authentication
authenticationEnabled: {type: Boolean, default: true}, authenticationEnabled: {type: Boolean, default: true},
@ -90,21 +91,18 @@ function generateKey(hmacKey, algorithm, encoding) {
return hmac.digest('base64'); return hmac.digest('base64');
} }
module.exports = function (dataSource) { // var AuthenticationScheme = dataSource.define('AuthenticationScheme', AuthenticationSchemeSchema);
dataSource = dataSource || new require('loopback-datasource-juggler').ModelBuilder(); // ApplicationSchema.authenticationSchemes = [AuthenticationScheme];
// var AuthenticationScheme = dataSource.define('AuthenticationScheme', AuthenticationSchemeSchema); // var PushNotificationSetting = dataSource.define('PushNotificationSetting', PushNotificationSettingSchema);
// ApplicationSchema.authenticationSchemes = [AuthenticationScheme]; // ApplicationSchema.pushSettings = [PushNotificationSetting];
// var PushNotificationSetting = dataSource.define('PushNotificationSetting', PushNotificationSettingSchema); var Application = loopback.createModel('Application', ApplicationSchema);
// ApplicationSchema.pushSettings = [PushNotificationSetting];
var Application = dataSource.define('Application', ApplicationSchema); // Application.hasMany(AuthenticationScheme, {as: 'authenticationSchemes', foreignKey: 'appId'});
// Application.hasMany(PushNotificationSetting, {as: 'pushNotificationSettings', foreignKey: 'appId'});
// Application.hasMany(AuthenticationScheme, {as: 'authenticationSchemes', foreignKey: 'appId'}); Application.beforeCreate = function (next) {
// Application.hasMany(PushNotificationSetting, {as: 'pushNotificationSettings', foreignKey: 'appId'});
Application.beforeCreate = function (next) {
// console.trace(); // console.trace();
var app = this; var app = this;
// use data argument to update object // use data argument to update object
@ -116,16 +114,16 @@ module.exports = function (dataSource) {
app.windowsKey = generateKey('windows'); app.windowsKey = generateKey('windows');
app.masterKey = generateKey('master'); app.masterKey = generateKey('master');
next(); next();
}; };
/** /**
* Register a new application * Register a new application
* @param owner Owner's user id * @param owner Owner's user id
* @param name Name of the application * @param name Name of the application
* @param options Other options * @param options Other options
* @param cb Callback function * @param cb Callback function
*/ */
Application.register = function (owner, name, options, cb) { Application.register = function (owner, name, options, cb) {
assert(owner, 'owner is required'); assert(owner, 'owner is required');
assert(name, 'name is required'); assert(name, 'name is required');
@ -140,13 +138,13 @@ module.exports = function (dataSource) {
} }
} }
Application.create(props, cb); Application.create(props, cb);
} };
/** /**
* Reset keys for the application instance * Reset keys for the application instance
* @param cb * @param cb
*/ */
Application.prototype.resetKeys = function(cb) { Application.prototype.resetKeys = function(cb) {
this.clientKey = generateKey('client'); this.clientKey = generateKey('client');
this.javaScriptKey = generateKey('javaScript'); this.javaScriptKey = generateKey('javaScript');
this.restApiKey = generateKey('restApi'); this.restApiKey = generateKey('restApi');
@ -154,14 +152,14 @@ module.exports = function (dataSource) {
this.masterKey = generateKey('master'); this.masterKey = generateKey('master');
this.modified = new Date(); this.modified = new Date();
this.save(cb); this.save(cb);
} };
/** /**
* Reset keys for a given application by the appId * Reset keys for a given application by the appId
* @param appId * @param appId
* @param cb * @param cb
*/ */
Application.resetKeys = function(appId, cb) { Application.resetKeys = function(appId, cb) {
Application.findById(appId, function(err, app) { Application.findById(appId, function(err, app) {
if(err) { if(err) {
cb && cb(err, app); cb && cb(err, app);
@ -169,15 +167,15 @@ module.exports = function (dataSource) {
} }
app.resetKeys(cb); app.resetKeys(cb);
}); });
} };
/** /**
* *
* @param appId * @param appId
* @param key * @param key
* @param cb * @param cb
*/ */
Application.authenticate = function(appId, key, cb) { Application.authenticate = function(appId, key, cb) {
Application.findById(appId, function(err, app) { Application.findById(appId, function(err, app) {
if(err || !app) { if(err || !app) {
cb && cb(err, null); cb && cb(err, null);
@ -191,10 +189,10 @@ module.exports = function (dataSource) {
}); });
cb && cb(null, matched); cb && cb(null, matched);
}); });
} };
module.exports = Application;
return Application;
}

View File

@ -1,10 +1,10 @@
var models = require('../lib/models');
var loopback = require(('../')); var loopback = require(('../'));
var assert = require('assert'); var assert = require('assert');
var dataSource = loopback.createDataSource('db', {connector: loopback.Memory}); var dataSource = loopback.createDataSource('db', {connector: loopback.Memory});
var Application = models.Application(dataSource); var Application = loopback.Application;
Application.attachTo(dataSource);
describe('Application', function () { describe('Application', function () {
var registeredApp = null; var registeredApp = null;