growly.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. var GNTP = require('./gntp.js');
  2. /**
  3. * Interface for registering Growl applications and sending Growl notifications.
  4. *
  5. * @api private
  6. */
  7. function Growly() {
  8. this.appname = 'Growly';
  9. this.notifications = undefined;
  10. this.labels = undefined;
  11. this.count = 0;
  12. this.registered = false;
  13. this.host = undefined;
  14. this.port = undefined;
  15. }
  16. /**
  17. * Returns an array of label strings extracted from each notification object in
  18. * `Growly.notifications`.
  19. *
  20. * @param {Array} notifications
  21. * @return {Array} notification labels
  22. * @api private
  23. */
  24. Growly.prototype.getLabels = function() {
  25. return this.notifications.map(function(notif) {
  26. return notif.label;
  27. });
  28. };
  29. /**
  30. * Set the host to be used by GNTP requests.
  31. *
  32. * @param {String} host
  33. * @param {Number} port
  34. * @api public
  35. */
  36. Growly.prototype.setHost = function(host, port) {
  37. this.host = host;
  38. this.port = port;
  39. };
  40. /**
  41. * Register an application with the name `appname` (required), icon `appicon`, and
  42. * a list of notification types `notifications`. If provided, `callback` will be
  43. * called when the request completes with the first argument being an `err` error
  44. * object if the request failed.
  45. *
  46. * Each object in the `notifications` array defines a type of notification the
  47. * application will have with the following properties:
  48. *
  49. * - `.label` name used to identify the type of notification being used (required)
  50. * - `.dispname` name users will see in Growl's preference panel (defaults to `.label`)
  51. * - `.enabled` whether or not notifications of this type are enabled (defaults to true)
  52. * - `.icon` default icon notifications of this type should use (url, file path, or Buffer object)
  53. *
  54. * Example registration:
  55. *
  56. * growl.register('My Application', 'path/to/icon.png', [
  57. * { label: 'success', dispname: 'Success', icon: 'path/to/success.png' },
  58. * { label: 'warning', dispname: 'Warning', icon: 'path/to/warning.png', enabled: false }
  59. * ], function(err) { console.log(err || 'Registration successful!'); });
  60. *
  61. * @param {String} appname
  62. * @param {String|Buffer} appicon
  63. * @param {Array} notifications
  64. * @param {Function} callback
  65. * @api public
  66. */
  67. Growly.prototype.register = function(appname, appicon, notifications, callback) {
  68. var gntp;
  69. if (typeof appicon === 'object') {
  70. notifications = appicon;
  71. appicon = undefined;
  72. }
  73. if (notifications === undefined || !notifications.length) {
  74. notifications = [{ label: 'default', dispname: 'Default Notification', enabled: true }];
  75. }
  76. if (typeof arguments[arguments.length - 1] === 'function') {
  77. callback = arguments[arguments.length - 1];
  78. } else {
  79. callback = function() {};
  80. }
  81. this.appname = appname;
  82. this.notifications = notifications;
  83. this.labels = this.getLabels();
  84. this.registered = true;
  85. gntp = new GNTP('REGISTER', { host: this.host, port: this.port });
  86. gntp.add('Application-Name', appname);
  87. gntp.add('Application-Icon', appicon);
  88. gntp.add('Notifications-Count', notifications.length);
  89. gntp.newline();
  90. notifications.forEach(function(notif) {
  91. if (notif.enabled === undefined) notif.enabled = true;
  92. gntp.add('Notification-Name', notif.label);
  93. gntp.add('Notification-Display-Name', notif.dispname);
  94. gntp.add('Notification-Enabled', notif.enabled ? 'True' : 'False');
  95. gntp.add('Notification-Icon', notif.icon);
  96. gntp.newline();
  97. });
  98. gntp.send(callback);
  99. };
  100. /**
  101. * Send a notification with `text` content. Growly will lazily register itself
  102. * if the user hasn't already before sending the notification.
  103. *
  104. * A notification can have the following `opts` options:
  105. *
  106. * - `.label` type of notification to use (defaults to the first registered type)
  107. * - `.title` title of the notification
  108. * - `.icon` url, file path, or Buffer instance for the notification's icon.
  109. * - `.sticky` whether or not to sticky the notification (defaults to false)
  110. * - `.priority` the priority of the notification from lowest (-2) to highest (2)
  111. * - `.coalescingId` replace/update the matching previous notification. May be ignored.
  112. *
  113. * If provided, `callback` will be called when the user interacts with the notification.
  114. * The first argument will be an `err` error object, and the second argument an `action`
  115. * string equal to either 'clicked' or 'closed' (whichever action the user took.)
  116. *
  117. * Example notification:
  118. *
  119. * growl.notify('Stuffs broken!', { label: 'warning' }, function(err, action) {
  120. * console.log('Action:', action);
  121. * });
  122. *
  123. * @param {String} text
  124. * @param {Object} opts
  125. * @param {Function} callback
  126. * @api public
  127. */
  128. Growly.prototype.notify = function(text, opts, callback) {
  129. var self = this,
  130. gntp;
  131. /* Lazy registration. */
  132. if (!this.registered) {
  133. this.register(this.appname, function(err) {
  134. if (err) console.log(err);
  135. self.notify.call(self, text, opts, callback);
  136. });
  137. return;
  138. }
  139. opts = opts || {};
  140. if (typeof opts === 'function') {
  141. callback = opts;
  142. opts = {};
  143. }
  144. gntp = new GNTP('NOTIFY', { host: this.host, port: this.port });
  145. gntp.add('Application-Name', this.appname);
  146. gntp.add('Notification-Name', opts.label || this.labels[0]);
  147. gntp.add('Notification-ID', ++this.count);
  148. gntp.add('Notification-Title', opts.title);
  149. gntp.add('Notification-Text', text);
  150. gntp.add('Notification-Sticky', opts.sticky ? 'True' : 'False');
  151. gntp.add('Notification-Priority', opts.priority);
  152. gntp.add('Notification-Icon', opts.icon);
  153. gntp.add('Notification-Coalescing-ID', opts.coalescingId || undefined);
  154. gntp.add('Notification-Callback-Context', callback ? 'context' : undefined);
  155. gntp.add('Notification-Callback-Context-Type', callback ? 'string' : undefined);
  156. gntp.add('Notification-Callback-Target', undefined);
  157. gntp.newline();
  158. gntp.send(function(err, resp) {
  159. if (callback && err) {
  160. callback(err);
  161. } else if (callback && resp.state === 'CALLBACK') {
  162. callback(undefined, resp['Notification-Callback-Result'].toLowerCase());
  163. }
  164. });
  165. };
  166. /**
  167. * Expose an instance of the Growly object.
  168. */
  169. module.exports = new Growly();