From a14d78614556325d882c23f2bb14c385b2bcd6fd Mon Sep 17 00:00:00 2001 From: Mirco Wittrien Date: Fri, 23 Nov 2018 14:37:30 +0100 Subject: [PATCH] changed notificationsound behaviour in Friend-/StalkerNotifications --- .../FriendNotifications.plugin.js | 52 +++++-- .../StalkerNotifications.plugin.js | 133 ++++++++++-------- 2 files changed, 116 insertions(+), 69 deletions(-) diff --git a/Plugins/FriendNotifications/FriendNotifications.plugin.js b/Plugins/FriendNotifications/FriendNotifications.plugin.js index 4178030fa5..8e09df88c9 100644 --- a/Plugins/FriendNotifications/FriendNotifications.plugin.js +++ b/Plugins/FriendNotifications/FriendNotifications.plugin.js @@ -102,6 +102,12 @@ class FriendNotifications { muteOnDND: {value:false, description:"Do not notify me when I am DnD:"}, onlyOnOnline: {value:false, description:"Only notify me when a User logs in:"}, openOnClick: {value:false, description:"Open the DM when you click a Notification:"} + }, + notificationsounds: { + toastonline: {value:{url:null,song:null,mute:false}}, + toastoffline: {value:{url:null,song:null,mute:false}}, + desktoponline: {value:{url:null,song:null,mute:false}}, + desktopoffline: {value:{url:null,song:null,mute:false}} } }; } @@ -110,7 +116,7 @@ class FriendNotifications { getDescription () {return "Notifies you when a friend either logs in or out. Click the Online Friend-Counter to display a timelog of the current session.";} - getVersion () {return "1.1.3";} + getVersion () {return "1.1.4";} getAuthor () {return "DevilBro";} @@ -118,14 +124,16 @@ class FriendNotifications { if (!this.started || typeof BDFDB !== "object") return; var settings = BDFDB.getAllData(this, "settings"); - var notificationsound = BDFDB.loadAllData(this, "notificationsound"); + var notificationsounds = BDFDB.getAllData(this, "notificationsounds"); var desktop = BDFDB.loadAllData(this, "desktop"); var disabled = BDFDB.loadAllData(this, "disabled"); var settingshtml = `
${this.getName()}
`; for (let key in settings) { settingshtml += `

${this.defaults.settings[key].description}

`; } - if ("Notification" in window) settingshtml += `
Desktop Notification Sound:
Mute:
`; + for (let key in notificationsounds) { + if (key.indexOf("desktop") == -1 || "Notification" in window) settingshtml += `
${key} notification sound:
Mute:
`; + } settingshtml += `

Click on a Icon to toggle Notifications for that User:

`; if ("Notification" in window) settingshtml += `

Rightclick on a Icon to toggle Notifications for that User:

`; settingshtml += `
`; @@ -146,11 +154,12 @@ class FriendNotifications { $(settingspanel) .on("click", ".settings-checkbox", () => {this.updateSettings(settingspanel);}) - .on("click", ".btn-savesong", () => {this.saveAudio(settingspanel);}) + .on("click", ".btn-savesong", (e) => {this.saveAudio(settingspanel, e.currentTarget.getAttribute("option"));}) .on("click", ".mute-checkbox", (e) => { - var notificationsound = BDFDB.loadAllData(this, "notificationsound"); + var option = e.currentTarget.getAttribute("option"); + var notificationsound = BDFDB.getData(option, this, "notificationsounds"); notificationsound.mute = e.currentTarget.checked; - BDFDB.saveAllData(notificationsound, this, "notificationsound"); + BDFDB.saveData(option, notificationsound, this, "notificationsounds"); }) .on("mouseenter", ".settings-avatar", (e) => { let user = this.UserUtils.getUser(e.currentTarget.getAttribute("user-id")); @@ -216,6 +225,15 @@ class FriendNotifications { if (typeof BDFDB === "object") { BDFDB.loadMessage(this); + var notificationsound = BDFDB.loadAllData(this, "notificationsound"); + if (!BDFDB.isObjectEmpty(notificationsound)) { + var data = {}; + data.desktoponline = notificationsound; + data.desktopoffline = notificationsound; + BDFDB.saveAllData(data, this, "notificationsounds"); + BDFDB.removeAllData(this, "notificationsound"); + } + this.FriendUtils = BDFDB.WebModules.findByProperties(["getFriendIDs", "getRelationships"]); this.ChannelUtils = BDFDB.WebModules.findByProperties(["getDMFromUserId"]); this.ChannelSwitchUtils = BDFDB.WebModules.findByProperties(["selectPrivateChannel"]); @@ -229,7 +247,6 @@ class FriendNotifications { changes.forEach( (change, i) => { let settings = BDFDB.getAllData(this, "settings"); - let notificationsound = BDFDB.loadAllData(this, "notificationsound"); for (let id of this.FriendUtils.getFriendIDs()) { let online = this.UserMetaStore.getStatus(id) != "offline"; let user = this.UserUtils.getUser(id); @@ -241,7 +258,7 @@ class FriendNotifications { let avatar = data.removeIcon ? "" : (data.url ? data.url : BDFDB.getUserAvatar(user.id)); let openChannel = () => { if (settings.openOnClick) { - let DMid = this.ChannelStore.getDMFromUserId(user.id) + let DMid = this.ChannelUtils.getDMFromUserId(user.id) if (DMid) this.ChannelSwitchUtils.selectPrivateChannel(DMid); else this.PrivateChannelUtils.openPrivateChannel(BDFDB.myData.id, user.id); require("electron").remote.getCurrentWindow().maximize(); @@ -250,8 +267,15 @@ class FriendNotifications { if (!BDFDB.loadData(id, this, "desktop")) { let toast = BDFDB.showToast(`
${string}
`, {html:true, timeout:5000, type:(online ? "success" : null), icon:false}); $(toast).on("click." + this.getName(), openChannel); + let notificationsound = BDFDB.getData(online ? "toastonline" : "toastoffline", this, "notificationsounds"); + if (!notificationsound.mute && notificationsound.song) { + var audio = new Audio(); + audio.src = notificationsound.song; + audio.play(); + } } else { + let notificationsound = BDFDB.getData(online ? "desktoponline" : "desktopoffline", this, "notificationsounds"); BDFDB.showDesktopNotification(string, {icon:avatar, timeout:5000, click:openChannel, silent:notificationsound.mute, sound:notificationsound.song}); } } @@ -290,24 +314,24 @@ class FriendNotifications { updateSettings (settingspanel) { var settings = {}; - for (var input of settingspanel.querySelectorAll(BDFDB.dotCN.switchinner)) { + for (var input of settingspanel.querySelectorAll(BDFDB.dotCN.switchinner + ".settings-checkbox")) { settings[input.value] = input.checked; } BDFDB.saveAllData(settings, this, "settings"); } - saveAudio (settingspanel) { + saveAudio (settingspanel, option) { var successSavedAudio = (parsedurl, parseddata) => { if (parsedurl && parseddata) BDFDB.showToast(`Sound was saved successfully.`, {type:"success"}); - let notificationsound = BDFDB.loadAllData(this, "notificationsound"); + let notificationsound = BDFDB.getData(option, this, "notificationsounds"); notificationsound.url = parsedurl; notificationsound.song = parseddata; - BDFDB.saveAllData(notificationsound, this, "notificationsound"); + BDFDB.saveData(option, notificationsound, this, "notificationsounds"); }; - var url = settingspanel.querySelector(".songInput").value; + var url = settingspanel.querySelector(`.songInput[option="${option}"]`).value; if (url.length == 0) { - BDFDB.showToast(`Sound was set to the default sound.`, {type:"warn"}); + BDFDB.showToast(`Sound file was removed.`, {type:"warn"}); successSavedAudio(url, url); } else if (url.indexOf("http") == 0) { diff --git a/Plugins/StalkerNotifications/StalkerNotifications.plugin.js b/Plugins/StalkerNotifications/StalkerNotifications.plugin.js index 9f6803994a..34d390db70 100644 --- a/Plugins/StalkerNotifications/StalkerNotifications.plugin.js +++ b/Plugins/StalkerNotifications/StalkerNotifications.plugin.js @@ -113,6 +113,12 @@ class StalkerNotifications { onlyOnOnline: {value:false, description:"Only notify me when a User logs in:"}, openOnClick: {value:false, description:"Open the DM when you click a Notification:"} }, + notificationsounds: { + toastonline: {value:{url:null,song:null,mute:false}}, + toastoffline: {value:{url:null,song:null,mute:false}}, + desktoponline: {value:{url:null,song:null,mute:false}}, + desktopoffline: {value:{url:null,song:null,mute:false}} + }, amounts: { checkInterval: {value:10, description:"Check Users every X seconds:"} } @@ -123,7 +129,7 @@ class StalkerNotifications { getDescription () {return "Lets you observe the status of people that aren't your friends.";} - getVersion () {return "1.0.7";} + getVersion () {return "1.0.8";} getAuthor () {return "DevilBro";} @@ -133,7 +139,7 @@ class StalkerNotifications { var amounts = BDFDB.getAllData(this, "amounts"); var settings = BDFDB.getAllData(this, "settings"); var users = BDFDB.loadAllData(this, "users"); - var notificationsound = BDFDB.loadAllData(this, "notificationsound"); + var notificationsounds = BDFDB.getAllData(this, "notificationsounds"); var settingshtml = `
${this.getName()}
`; settingshtml += `

Add User:

`; for (let key in amounts) { @@ -142,7 +148,9 @@ class StalkerNotifications { for (let key in settings) { settingshtml += `

${this.defaults.settings[key].description}

`; } - if ("Notification" in window) settingshtml += `
Desktop Notification Sound:
Mute:
`; + for (let key in notificationsounds) { + if (key.indexOf("desktop") == -1 || "Notification" in window) settingshtml += `
${key} notification sound:
Mute:
`; + } settingshtml += `

Click on a Icon to toggle Notifications for that User:

`; if ("Notification" in window) settingshtml += `

Rightclick on a Icon to toggle Notifications for that User:

`; settingshtml += `
`; @@ -161,11 +169,12 @@ class StalkerNotifications { $(settingspanel) .on("click", ".settings-checkbox", () => {this.updateSettings(settingspanel);}) - .on("click", ".btn-savesong", () => {this.saveAudio(settingspanel);}) + .on("click", ".btn-savesong", (e) => {this.saveAudio(settingspanel, e.currentTarget.getAttribute("option"));}) .on("click", ".mute-checkbox", (e) => { - var notificationsound = BDFDB.loadAllData(this, "notificationsound"); + var option = e.currentTarget.getAttribute("option"); + var notificationsound = BDFDB.getData(option, this, "notificationsounds"); notificationsound.mute = e.currentTarget.checked; - BDFDB.saveAllData(notificationsound, this, "notificationsound"); + BDFDB.saveData(option, notificationsound, this, "notificationsounds"); }) .on("mouseenter", ".settings-avatar", (e) => { let user = this.UserUtils.getUser(e.currentTarget.getAttribute("user-id")); @@ -269,6 +278,15 @@ class StalkerNotifications { if (typeof BDFDB === "object") { BDFDB.loadMessage(this); + var notificationsound = BDFDB.loadAllData(this, "notificationsound"); + if (!BDFDB.isObjectEmpty(notificationsound)) { + var data = {}; + data.desktoponline = notificationsound; + data.desktopoffline = notificationsound; + BDFDB.saveAllData(data, this, "notificationsounds"); + BDFDB.removeAllData(this, "notificationsound"); + } + this.ChannelUtils = BDFDB.WebModules.findByProperties(["getDMFromUserId"]); this.ChannelSwitchUtils = BDFDB.WebModules.findByProperties(["selectPrivateChannel"]); this.PrivateChannelUtils = BDFDB.WebModules.findByProperties(["openPrivateChannel"]); @@ -298,67 +316,24 @@ class StalkerNotifications { updateSettings (settingspanel) { var settings = {}; - for (var input of settingspanel.querySelectorAll(BDFDB.dotCN.switchinner)) { + for (var input of settingspanel.querySelectorAll(BDFDB.dotCN.switchinner + ".settings-checkbox")) { settings[input.value] = input.checked; } BDFDB.saveAllData(settings, this, "settings"); } - createSettingsAvatarHtml (user, settings) { - let data = BDFDB.loadData(user.id, "EditUsers", "users") || {}; - return `
`; - } - - startInterval () { - clearInterval(this.checkInterval); - this.checkInterval = setInterval(() => { - let settings = BDFDB.getAllData(this, "settings"); - let notificationsound = BDFDB.loadAllData(this, "notificationsound"); - let users = BDFDB.loadAllData(this, "users"); - for (let id in users) { - let online = this.UserMetaStore.getStatus(id) != "offline"; - let user = this.UserUtils.getUser(id); - if (user && this.stalkerOnlineList[id] != online && !users[id].disabled) { - this.timeLog.push({user, online, time: new Date()}); - if (!(settings.onlyOnOnline && !online) && !(settings.muteOnDND && BDFDB.getUserStatus() == "dnd")) { - let data = BDFDB.loadData(user.id, "EditUsers", "users") || {}; - let string = `${BDFDB.encodeToHTML(data.name ? data.name : user.username)} is ${online ? "online" : "offline"}.`; - let avatar = data.removeIcon ? "" : (data.url ? data.url : BDFDB.getUserAvatar(user.id)); - let openChannel = () => { - if (settings.openOnClick) { - let DMid = this.ChannelStore.getDMFromUserId(user.id) - if (DMid) this.ChannelSwitchUtils.selectPrivateChannel(DMid); - else this.PrivateChannelUtils.openPrivateChannel(BDFDB.myData.id, user.id); - require("electron").remote.getCurrentWindow().maximize(); - } - }; - if (!users[id].desktop) { - let toast = BDFDB.showToast(`
${string}
`, {html:true, timeout:5000, type:(online ? "success" : null), icon:false}); - $(toast).on("click." + this.getName(), openChannel); - } - else { - let notificationsound = BDFDB.loadAllData(this, "notificationsound"); - BDFDB.showDesktopNotification(string, {icon:avatar, timeout:5000, click:openChannel, silent:notificationsound.mute, sound:notificationsound.song}); - } - } - } - this.stalkerOnlineList[id] = online; - } - },BDFDB.getData("checkInterval", this, "amounts") * 1000); - } - - saveAudio (settingspanel) { + saveAudio (settingspanel, option) { var successSavedAudio = (parsedurl, parseddata) => { if (parsedurl && parseddata) BDFDB.showToast(`Sound was saved successfully.`, {type:"success"}); - let notificationsound = BDFDB.loadAllData(this, "notificationsound"); + let notificationsound = BDFDB.getData(option, this, "notificationsounds"); notificationsound.url = parsedurl; notificationsound.song = parseddata; - BDFDB.saveAllData(notificationsound, this, "notificationsound"); + BDFDB.saveData(option, notificationsound, this, "notificationsounds"); }; - var url = settingspanel.querySelector(".songInput").value; + var url = settingspanel.querySelector(`.songInput[option="${option}"]`).value; if (url.length == 0) { - BDFDB.showToast(`Sound was set to the default sound.`, {type:"warn"}); + BDFDB.showToast(`Sound file was removed.`, {type:"warn"}); successSavedAudio(url, url); } else if (url.indexOf("http") == 0) { @@ -385,6 +360,54 @@ class StalkerNotifications { } } + createSettingsAvatarHtml (user, settings) { + let data = BDFDB.loadData(user.id, "EditUsers", "users") || {}; + return `
`; + } + + startInterval () { + clearInterval(this.checkInterval); + this.checkInterval = setInterval(() => { + let settings = BDFDB.getAllData(this, "settings"); + let users = BDFDB.loadAllData(this, "users"); + for (let id in users) { + let online = this.UserMetaStore.getStatus(id) != "offline"; + let user = this.UserUtils.getUser(id); + if (user && this.friendsOnlineList[id] != online && !BDFDB.loadData(id, this, "disabled")) { + this.timeLog.push({user, online, time: new Date()}); + if (!(settings.onlyOnOnline && !online) && !(settings.muteOnDND && BDFDB.getUserStatus() == "dnd")) { + let data = BDFDB.loadData(user.id, "EditUsers", "users") || {}; + let string = `${BDFDB.encodeToHTML(data.name ? data.name : user.username)} is ${online ? "online" : "offline"}.`; + let avatar = data.removeIcon ? "" : (data.url ? data.url : BDFDB.getUserAvatar(user.id)); + let openChannel = () => { + if (settings.openOnClick) { + let DMid = this.ChannelUtils.getDMFromUserId(user.id) + if (DMid) this.ChannelSwitchUtils.selectPrivateChannel(DMid); + else this.PrivateChannelUtils.openPrivateChannel(BDFDB.myData.id, user.id); + require("electron").remote.getCurrentWindow().maximize(); + } + }; + if (!BDFDB.loadData(id, this, "desktop")) { + let toast = BDFDB.showToast(`
${string}
`, {html:true, timeout:5000, type:(online ? "success" : null), icon:false}); + $(toast).on("click." + this.getName(), openChannel); + let notificationsound = BDFDB.getData(online ? "toastonline" : "toastoffline", this, "notificationsounds"); + if (!notificationsound.mute && notificationsound.song) { + var audio = new Audio(); + audio.src = notificationsound.song; + audio.play(); + } + } + else { + let notificationsound = BDFDB.getData(online ? "desktoponline" : "desktopoffline", this, "notificationsounds"); + BDFDB.showDesktopNotification(string, {icon:avatar, timeout:5000, click:openChannel, silent:notificationsound.mute, sound:notificationsound.song}); + } + } + } + this.stalkerOnlineList[id] = online; + } + },BDFDB.getData("checkInterval", this, "amounts") * 1000); + } + showTimeLog () { var timeLogModal = $(this.timeLogModalMarkup); let logs = this.timeLog.slice(0).reverse();