Skip to content
This repository has been archived by the owner on Feb 4, 2021. It is now read-only.

Commit

Permalink
Properly handle nonlive states in notifs
Browse files Browse the repository at this point in the history
  • Loading branch information
freaktechnik committed Apr 30, 2016
1 parent 23f5d67 commit 73f4a0c
Showing 1 changed file with 36 additions and 3 deletions.
39 changes: 36 additions & 3 deletions lib/notifier.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ const NOTIFICATION_ICON_SIZE = 100;
*/
function Notifier(options) {
this.channelTitles = new Map();
this.channelStates = new Map();
this._onClick = options.onClick;
}

Expand All @@ -37,6 +38,12 @@ Notifier.prototype = {
* @type {Map.<number, string>}
*/
channelTitles: null,
/**
* Proprietary thing I'm too tired to explain, but it stores the relevant
* info about a channel's current state.
* @type {Map.<number, object>}
*/
channelStates: null,
/**
* If online notifications should be shown.
* @type {boolean}
Expand Down Expand Up @@ -87,6 +94,26 @@ Notifier.prototype = {
isSimple(channel) {
return prefs.panel_nonlive === 0 || prefs.panel_nonlive == 3 || !channel.state.enabled;
},
/**
* Store a channel's state.
* @argument {module:channel/core.Channel} channel - The channel to store
*/
_setChannelState(channel) {
this.channelStates.set(channel.id, {
state: channel.state.state,
user: channel.state.alternateUsername
});
},
/**
* Determine if the state of a channel has changed.
* @argument {moduel:channel/core.Channel} channel - The channel that might
* have changed.
* @return {boolean}
*/
_channelStateChanged(channel) {
const oldState = this.channelStates.get(channel.id);
return oldState.state != channel.state.state || oldState.user != channel.state.alternateUsername;
},
/**
* Show a notification to the user, if the channel isn't in the currently
* active tab, the channel changed accordingly and the respective
Expand All @@ -96,20 +123,19 @@ Notifier.prototype = {
* notification for.
*/
sendNotification(channel) {
//TODO don't announce state changes as title changes.
// Mute notifications for the current tab
if(this.showNotifications && !channel.url.some((url) => url === tabs.activeTab.url)) {
let title = null;
if(this.isSimple(channel) && channel.live && this.onlineNotifications && !this.channelTitles.has(channel.id)) {
title = _("onlineNotification", channel.toString());
}
else if((channel.live || !channel.state.enabled) && (this.isSimple(channel) || this.nonliveNotifications) && this.titleNotifications && this.channelTitles.get(channel.id) != channel.title) {
else if((channel.live || !channel.state.enabled) && (this.isSimple(channel) || (this.nonliveNotifications && !this._channelStateChanged(channel))) && this.titleNotifications && this.channelTitles.get(channel.id) != channel.title) {
title = _("updateNotification", channel.toString());
}
else if(this.isSimple(channel) && !channel.live && this.offlineNotifications && this.channelTitles.has(channel.id)) {
title = _("offlineNotification", channel.toString());
}
else if(channel.state.enabled && this.nonliveNotifications && this.channelTitles.get(channel.id) == channel.title) {
else if(channel.state.enabled && this.nonliveNotifications && this.channelTitles.get(channel.id) == channel.title && this._channelStateChanged(channel)) {
const stateName = LiveState.REDIRECT === channel.state.state ? "redirect" : "rebroadcast";
title = _("nonliveNotification", channel.toString(), _("nonliveNotification_state_"+stateName, channel.state.alternateUsername));
}
Expand All @@ -128,6 +154,11 @@ Notifier.prototype = {
this.channelTitles.set(channel.id, channel.title);
else
this.channelTitles.delete(channel.id);

if(this.isSimple(channel))
this._setChannelState(channel);
else if(this.channelStates.has(channel.id))
this.channelStates.delete(channel.id);
},
/**
* Callback to call, whenever an event gets removed. This removes the
Expand All @@ -137,6 +168,8 @@ Notifier.prototype = {
onChannelRemoved(channelId) {
if(this.channelTitles.has(channelId))
this.channelTitles.delete(channelId);
if(this.channelStates.has(channelId))
this.channelStates.delete(channelId);
}
};

Expand Down

0 comments on commit 73f4a0c

Please sign in to comment.