123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193 |
- var GNTP = require('./gntp.js');
- function Growly() {
- this.appname = 'Growly';
- this.notifications = undefined;
- this.labels = undefined;
- this.count = 0;
- this.registered = false;
- this.host = undefined;
- this.port = undefined;
- }
- Growly.prototype.getLabels = function() {
- return this.notifications.map(function(notif) {
- return notif.label;
- });
- };
- Growly.prototype.setHost = function(host, port) {
- this.host = host;
- this.port = port;
- };
- Growly.prototype.register = function(appname, appicon, notifications, callback) {
- var gntp;
- if (typeof appicon === 'object') {
- notifications = appicon;
- appicon = undefined;
- }
- if (notifications === undefined || !notifications.length) {
- notifications = [{ label: 'default', dispname: 'Default Notification', enabled: true }];
- }
- if (typeof arguments[arguments.length - 1] === 'function') {
- callback = arguments[arguments.length - 1];
- } else {
- callback = function() {};
- }
- this.appname = appname;
- this.notifications = notifications;
- this.labels = this.getLabels();
- this.registered = true;
- gntp = new GNTP('REGISTER', { host: this.host, port: this.port });
- gntp.add('Application-Name', appname);
- gntp.add('Application-Icon', appicon);
- gntp.add('Notifications-Count', notifications.length);
- gntp.newline();
- notifications.forEach(function(notif) {
- if (notif.enabled === undefined) notif.enabled = true;
- gntp.add('Notification-Name', notif.label);
- gntp.add('Notification-Display-Name', notif.dispname);
- gntp.add('Notification-Enabled', notif.enabled ? 'True' : 'False');
- gntp.add('Notification-Icon', notif.icon);
- gntp.newline();
- });
- gntp.send(callback);
- };
- Growly.prototype.notify = function(text, opts, callback) {
- var self = this,
- gntp;
-
- if (!this.registered) {
- this.register(this.appname, function(err) {
- if (err) console.log(err);
- self.notify.call(self, text, opts, callback);
- });
- return;
- }
- opts = opts || {};
- if (typeof opts === 'function') {
- callback = opts;
- opts = {};
- }
- gntp = new GNTP('NOTIFY', { host: this.host, port: this.port });
- gntp.add('Application-Name', this.appname);
- gntp.add('Notification-Name', opts.label || this.labels[0]);
- gntp.add('Notification-ID', ++this.count);
- gntp.add('Notification-Title', opts.title);
- gntp.add('Notification-Text', text);
- gntp.add('Notification-Sticky', opts.sticky ? 'True' : 'False');
- gntp.add('Notification-Priority', opts.priority);
- gntp.add('Notification-Icon', opts.icon);
- gntp.add('Notification-Coalescing-ID', opts.coalescingId || undefined);
- gntp.add('Notification-Callback-Context', callback ? 'context' : undefined);
- gntp.add('Notification-Callback-Context-Type', callback ? 'string' : undefined);
- gntp.add('Notification-Callback-Target', undefined);
- gntp.newline();
- gntp.send(function(err, resp) {
- if (callback && err) {
- callback(err);
- } else if (callback && resp.state === 'CALLBACK') {
- callback(undefined, resp['Notification-Callback-Result'].toLowerCase());
- }
- });
- };
- module.exports = new Growly();
|