From c093e4bf64e5317ffa256cca6f419072a02830f9 Mon Sep 17 00:00:00 2001 From: dbroqua Date: Thu, 2 Jan 2020 13:58:41 +0100 Subject: [PATCH] Added !notifications --- index.js | 5 +++ libs/ArtistSong.js | 44 ++++++++++++++++------- libs/Preferences.js | 81 +++++++++++++++++++++++++++++++++++++++++++ models/Preferences.js | 11 ++++++ 4 files changed, 128 insertions(+), 13 deletions(-) create mode 100644 libs/Preferences.js create mode 100644 models/Preferences.js diff --git a/index.js b/index.js index f7d71d8..d118ad1 100644 --- a/index.js +++ b/index.js @@ -7,6 +7,7 @@ const ArtistSong = require('./libs/ArtistSong') const When = require('./libs/When') const Statistics = require('./libs/Statistics') const List = require('./libs/List') +const Preferences = require('./libs/Preferences') const allowedCommands = ['!artist', '!song', '!stats', '!when', '!help', '!notifications', '!list'] // Init des librairies @@ -14,6 +15,7 @@ const artistSong = new ArtistSong(models) const when = new When(models) const stats = new Statistics(models) const list = new List(models) +const preferences = new Preferences(models) // Stockage du morceau en cours de lecture let currentSong = { @@ -167,6 +169,9 @@ const actions = (where, message, from) => { case '!list': list.action(botSay, where, exploded) break + case '!notifications': + preferences.notifications(botSay, where, from, exploded) + break default: break } diff --git a/libs/ArtistSong.js b/libs/ArtistSong.js index eb6f2a6..53faf30 100644 --- a/libs/ArtistSong.js +++ b/libs/ArtistSong.js @@ -93,20 +93,38 @@ class ArtistSong { const artist = [] const song = [] - for (let i = 0; i < list.length; i += 1) { - if (list[i].type === 'artist') { - artist.push(list[i].user) - } else { - song.push(list[i].user) - } - } + // On get la liste des utilisateurs ne souhaitant pas être notifié pour cette radio + this.models.Preferences + .find({ + radio: process.env.RADIO_ALIAS, + notification: false + }) + .then(prefs => { + for (let i = 0; i < list.length; i += 1) { + let isMute = false + for (let j = 0; j < prefs.length; j += 1) { + if (prefs[j].user === list[i].user) { + isMute = true + break + } + } - if (artist.length > 0) { - botSay(where, `${process.env.RADIO_ALIAS} : Hey ${artist.toString().replace(/,/g, ', ')} ! Y'a ${currentSong.artist} ! Monte${artist.length > 1 ? 'z' : ''} le son !`) - } - if (song.length > 0) { - botSay(where, `${process.env.RADIO_ALIAS} : Hey ${song.toString().replace(/,/g, ', ')} ! Y'a ${song.title} ! Monte${song.length > 1 ? 'z' : ''} le son !`) - } + if (!isMute) { + if (list[i].type === 'artist') { + artist.push(list[i].user) + } else { + song.push(list[i].user) + } + } + } + + if (artist.length > 0) { + botSay(where, `${process.env.RADIO_ALIAS} : Hey ${artist.toString().replace(/,/g, ', ')} ! Y'a ${currentSong.artist} ! Monte${artist.length > 1 ? 'z' : ''} le son !`) + } + if (song.length > 0) { + botSay(where, `${process.env.RADIO_ALIAS} : Hey ${song.toString().replace(/,/g, ', ')} ! Y'a ${song.title} ! Monte${song.length > 1 ? 'z' : ''} le son !`) + } + }) }) .catch(err => { console.log('ERR:', err) diff --git a/libs/Preferences.js b/libs/Preferences.js new file mode 100644 index 0000000..300b71b --- /dev/null +++ b/libs/Preferences.js @@ -0,0 +1,81 @@ +class Preferences { + constructor (models) { + this.models = models + } + + notifications (botSay, where, from, exploded) { + if (exploded.length < 3) { + botSay(where, 'Attention ! tu as oublié de dire ce que tu voulais faire !') + return false + } + + const notification = (exploded[2] === 'on') + const radio = process.env.RADIO_ALIAS + + const errorCatcher = (err) => { + botSay(from, 'Oups... j\'ai pas réussi à faire ce que tu voulais :(') + console.log('ERR:', err) + } + const successCatcher = () => { + if (notification) { + botSay(from, 'C\'est parti pour de la notification à gogo ! Yeah !') + } else { + botSay(from, 'Tu ne m\'aime plus ? c\'est ça ? :\'(') + } + } + + switch (exploded[2]) { + case 'on': + case 'off': + this.models.Preferences + .findOne({ + user: from, + radio: radio + }) + .then((item) => { + if (!item) { + const newItem = new this.models.Preferences({ + user: from, + radio: radio, + notification: notification + }) + newItem.save() + .then(successCatcher) + .catch(errorCatcher) + } else { + item.update({ + notification: notification + }) + .then(successCatcher) + .catch(errorCatcher) + } + }) + .catch(errorCatcher) + + break + case 'state': + this.models.Preferences + .findOne({ + user: from, + radio: radio + }) + .then((item) => { + if (!item) { + botSay(from, 'Bein... vu que t\'as fait ni de on, ni de off je suis.... On ! 8)') + } else { + if (item.notification) { + botSay(from, 'Toi et moi c\'est pour la vie ! tu n\'en louperas pas une miette !') + } else { + botSay(from, 'Je boude ! Je ne te dirai rien !') + } + } + }) + .catch(errorCatcher) + break + default: + botSay(where, `T'es fort toi! t'as le choix entre on/off/state et tu m'envoies ${exploded[2]}...`) + } + } +} + +module.exports = Preferences diff --git a/models/Preferences.js b/models/Preferences.js new file mode 100644 index 0000000..8d2eddd --- /dev/null +++ b/models/Preferences.js @@ -0,0 +1,11 @@ +module.exports = mongoose => { + const schema = new mongoose.Schema({ + user: String, + notification: Boolean, + radio: String + }) + + const Preferences = mongoose.model('Preferences', schema) + + return Preferences +}