|
| 1 | +var ipc = window.require('ipc'); |
| 2 | +var Reflux = require('reflux'); |
| 3 | +var _ = require('underscore'); |
| 4 | + |
| 5 | +var Actions = require('../actions/actions'); |
| 6 | +var SettingsStore = require('../stores/settings'); |
| 7 | + |
| 8 | +var SoundNotificationStore = Reflux.createStore({ |
| 9 | + listenables: Actions, |
| 10 | + |
| 11 | + init: function () { |
| 12 | + this._previousNotifications = []; |
| 13 | + }, |
| 14 | + |
| 15 | + playSound: function () { |
| 16 | + var audio = new Audio('sounds/digi.wav'); |
| 17 | + audio.play(); |
| 18 | + }, |
| 19 | + |
| 20 | + showNotification: function (countNew, response, latestNotification) { |
| 21 | + var title = (countNew == 1 ? |
| 22 | + 'Gitify - ' + latestNotification.full_name : |
| 23 | + 'Gitify'); |
| 24 | + var body = (countNew == 1 ? |
| 25 | + latestNotification.subject : |
| 26 | + 'You\'ve got ' + countNew + ' notifications.'); |
| 27 | + var nativeNotification = new Notification(title, { |
| 28 | + body: body |
| 29 | + }); |
| 30 | + nativeNotification.onclick = function () { |
| 31 | + ipc.sendChannel('reopen-window'); |
| 32 | + }; |
| 33 | + }, |
| 34 | + |
| 35 | + onIsNewNotification: function (response) { |
| 36 | + var self = this; |
| 37 | + var playSound = SettingsStore.getSettings().playSound; |
| 38 | + var showNotifications = SettingsStore.getSettings().showNotifications; |
| 39 | + |
| 40 | + if (!playSound && !showNotifications) { return; } |
| 41 | + |
| 42 | + // Check if notification is already in the store. |
| 43 | + var newNotifications = _.filter(response, function (obj) { |
| 44 | + return !_.contains(self._previousNotifications, obj.id); |
| 45 | + }); |
| 46 | + |
| 47 | + // Play Sound / Show Notification. |
| 48 | + if (newNotifications && newNotifications.length) { |
| 49 | + if (playSound) { |
| 50 | + self.playSound(); |
| 51 | + } |
| 52 | + if (showNotifications) { |
| 53 | + this.showNotification(newNotifications.length, response, { |
| 54 | + full_name: newNotifications[0].repository.full_name, |
| 55 | + subject: newNotifications[0].subject.title |
| 56 | + }); |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + // Now Reset the previousNotifications array. |
| 61 | + self._previousNotifications = _.map(response, function (obj) { |
| 62 | + return obj.id; |
| 63 | + }); |
| 64 | + } |
| 65 | + |
| 66 | +}); |
| 67 | + |
| 68 | +module.exports = SoundNotificationStore; |
0 commit comments