Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,7 @@ xunit.xml
checkstyle.xml
example/server-2.0/client/public/vendor
doc

!intl/
intl/*
!intl/en/
2 changes: 2 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
var loopback = require('loopback');
var PushConnector = require('./lib/push-connector');
exports = module.exports = PushConnector;
var SG = require('strong-globalize');
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not following strong-globalize coding guideline here?
https://www.npmjs.com/package/strong-globalize#autonomous-message-loading
Please study paragraphs after "For example, the following does not work as intended because the package sub calls SG.SetRootDir first:"

SG.SetRootDir(__dirname);

/**
* Export two model classes as properties
Expand Down
13 changes: 13 additions & 0 deletions intl/en/messages.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"0a11bfdd7655e825fbd1f998b2e24db9": "Empty payload",
"71a2b601d1e750aa05c7a28ed76b9973": "Invalid field (empty)",
"8bc87eb582af546d32505f2839234119": "Invalid payload",
"db1e3eb9bf0934d72eb8b0b110281f39": "Invalid field: {0}",
"e254b20a16461c7379202f6a5f8350cc": "Invalid variable type for ${{0}}",
"f5146dece5cd70db519daf8c7e6d8478": "The ${{0}} does not exist",
"f95c75ae3da1e88794fff4be7188f3dc": "Invalid value for `{0}`",
"e9023b2ab0a052c9ccbd257198c7429f": "Cannot send {{APNS}} notification: {0}",
"704e7bf9910b8532f7c4889366fdcbac": "{{GCM}} error code: {0}, deviceToken: {1}",
"033d31e7bd62a420f23e4b154945a2b8": "notification must be an object",
"1ec12f42dab8979b9fc90a95a023419e": "deviceTokens must be an array"
}
19 changes: 11 additions & 8 deletions lib/payload.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
// this file has no tests so avoid refactor and ignore jshint for now
/*jshint ignore: start */
var serial,__hasProp = {}.hasOwnProperty;
var SG = require('strong-globalize');
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not following strong-globalize coding guideline here?
https://www.npmjs.com/package/strong-globalize#autonomous-message-loading
Please study paragraphs after "For example, the following does not work as intended because the package sub calls SG.SetRootDir first:"

var g = SG();

serial = 0;

Expand All @@ -14,7 +16,7 @@ Payload.prototype.locale_format = /^[a-z]{2}_[A-Z]{2}$/;
function Payload(data) {
var key, prefix, subkey, sum, type, value, _i, _len, _ref, _ref1;
if (typeof data !== 'object') {
throw new Error('Invalid payload');
throw new Error(g.t('Invalid payload'));
}
this.id = serial++;
this.compiled = false;
Expand All @@ -26,11 +28,12 @@ function Payload(data) {
if (!__hasProp.call(data, key)) continue;
value = data[key];
if (typeof key !== 'string' || key.length === 0) {
throw new Error("Invalid field (empty)");
throw new Error(g.t("Invalid field (empty)"));
Copy link
Copy Markdown
Contributor

@0candy 0candy Jul 22, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nitpick: change it to ' single quotes
Don't have to fix this.

Copy link
Copy Markdown
Contributor Author

@superkhau superkhau Jul 22, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That would make it an unrelated change. That should be done in a separate PR (ie. Change double quotes to single quotes).

}
if (typeof value !== 'string') {
throw new Error("Invalid value for `" + key + "'");
throw new Error(g.f("Invalid value for `%s`", key));
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here

}

switch (key) {
case 'title':
this.title["default"] = value;
Expand All @@ -45,7 +48,7 @@ function Payload(data) {
if ((_ref = key.split('.', 2), prefix = _ref[0], subkey = _ref[1], _ref).length === 2) {
this[prefix][subkey] = value;
} else {
throw new Error("Invalid field: " + key);
throw new Error(g.f("Invalid field: %s", key));
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same

}
}
}
Expand All @@ -65,7 +68,7 @@ function Payload(data) {
}).call(this)).length;
}
if (sum === 0) {
throw new Error('Empty payload');
throw new Error(g.t('Empty payload'));
}
}

Expand Down Expand Up @@ -118,15 +121,15 @@ Payload.prototype.variable = function (keyPath) {
if ((_ref = this.event) != null ? _ref.name : undefined) {
return (_ref1 = this.event) != null ? _ref1.name : undefined;
} else {
throw new Error("The ${" + keyPath + "} does not exist");
throw new Error(g.f("The ${%s} does not exist", keyPath));
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same

}
}
_ref2 = keyPath.split('.', 2), prefix = _ref2[0], key = _ref2[1];
if (prefix !== 'var' && prefix !== 'data') {
throw new Error("Invalid variable type for ${" + keyPath + "}");
throw new Error(g.f("Invalid variable type for ${%s}", keyPath));
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same

}
if (this[prefix][key] == null) {
throw new Error("The ${" + keyPath + "} does not exist");
throw new Error(g.f("The ${%s} does not exist", keyPath));
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same

}
return this[prefix][key];
};
Expand Down
7 changes: 4 additions & 3 deletions lib/providers/apns.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@

var inherits = require('util').inherits;
var EventEmitter = require('events').EventEmitter;

var debug = require('debug')('loopback:component:push:provider:apns');
var apn = require('apn');
var SG = require('strong-globalize');
var g = SG();

function ApnsProvider(pushSettings) {
pushSettings = pushSettings || {};
Expand Down Expand Up @@ -83,7 +84,7 @@ ApnsProvider.prototype._setupPushConnection = function(options) {
connection.on('socketError', errorHandler);

connection.on('transmissionError', function(code, notification, recipient) {
var err = new Error('Cannot send APNS notification: ' + code);
var err = new Error(g.f('Cannot send {{APNS}} notification: %s', code));
Copy link
Copy Markdown
Contributor

@0candy 0candy Jul 25, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You have to run slt-globalize -e again for intl/en/messages.json to get updated with the brackets change.

self.emit(err, notification, recipient);
});

Expand Down Expand Up @@ -140,4 +141,4 @@ ApnsProvider.prototype.pushNotification = function(notification, deviceToken) {

debug('Pushing notification to %j:', deviceToken, note);
this._connection.pushNotification(note, deviceToken);
};
};
5 changes: 4 additions & 1 deletion lib/providers/gcm.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ var extend = require('util')._extend;
var EventEmitter = require('events').EventEmitter;
var gcm = require('node-gcm');
var debug = require('debug')('loopback:component:push:provider:gcm');
var SG = require('strong-globalize');
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not following strong-globalize coding guideline here?
https://www.npmjs.com/package/strong-globalize#autonomous-message-loading
Please study paragraphs after "For example, the following does not work as intended because the package sub calls SG.SetRootDir first:"

var g = SG();

function GcmProvider(pushSettings) {
var settings = pushSettings.gcm || {};
Expand Down Expand Up @@ -39,7 +41,8 @@ GcmProvider.prototype.pushNotification = function(notification, deviceToken) {
debug('Device %j is no longer registered.', registrationIds[index]);
devicesGoneRegistrationIds.push(registrationIds[index]);
} else if (code) {
errors.push('GCM error code: ' + (code || 'Unknown') + ', deviceToken: ' + registrationIds[index]);
errors.push(g.f('{{GCM}} error code: %s, deviceToken: %s',
(code || 'Unknown'), registrationIds[index]));
}
});

Expand Down
8 changes: 5 additions & 3 deletions lib/push-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ var providers = require('./providers');
var loopback = require('loopback');
var NodeCache = require('node-cache');
var debug = require('debug')('loopback:component:push:push-manager');
var SG = require('strong-globalize');
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not following strong-globalize coding guideline here?
https://www.npmjs.com/package/strong-globalize#autonomous-message-loading
Please study paragraphs after "For example, the following does not work as intended because the package sub calls SG.SetRootDir first:"

var g = SG();

var Installation = require('../models').Installation;
var Notification = require('../models').Notification;
Expand Down Expand Up @@ -277,7 +279,7 @@ PushManager.prototype.notify = function(installation, notification, cb) {
assert(cb, 'callback should be defined');

if(!(typeof notification === 'object' && notification)) {
return cb(new Error('notification must be an object'));
return cb(new Error(g.t('notification must be an object')));
}

var appId = installation.appId;
Expand Down Expand Up @@ -327,11 +329,11 @@ PushManager.prototype.notifyMany = function(appId, deviceType, deviceTokens, not
assert(cb, 'callback should be defined');

if(!(typeof notification === 'object' && notification)) {
return cb(new Error('notification must be an object'));
return cb(new Error(g.t('notification must be an object')));
}

if(!(Array.isArray(deviceTokens) && deviceTokens.length > 0)) {
return cb(new Error('deviceTokens must be an array'));
return cb(new Error(g.t('deviceTokens must be an array')));
}

// Normalize the notification from a plain object
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
"lodash": "^3.10.1",
"mpns": "^2.1.0",
"node-cache": "^3.2.1",
"node-gcm": "^0.14.0"
"node-gcm": "^0.14.0",
"strong-globalize": "^2.5.6"
},
"devDependencies": {
"chai": "^2.3.0",
Expand Down