2019-12-25 12:47:09 +01:00
|
|
|
const formatString = require('../helpers/strings').formatString
|
|
|
|
|
2019-12-24 16:30:45 +01:00
|
|
|
class ArtistSong {
|
|
|
|
constructor (models) {
|
|
|
|
this.models = models
|
|
|
|
}
|
|
|
|
|
2019-12-30 13:36:15 +01:00
|
|
|
/**
|
|
|
|
* Fonction sauvegardant en historique le morceau en cours de lecture
|
|
|
|
* @param {Object} value
|
|
|
|
*/
|
|
|
|
saveCurrent (value) {
|
|
|
|
// Find if previous song was the same (on bot reload)
|
|
|
|
this.models.Histories
|
2019-12-30 13:37:53 +01:00
|
|
|
.find({
|
|
|
|
radio: value.radio
|
|
|
|
})
|
2019-12-30 13:36:15 +01:00
|
|
|
.sort({
|
|
|
|
createdAt: 'desc'
|
|
|
|
})
|
|
|
|
.limit(1)
|
|
|
|
.exec((err, last) => {
|
|
|
|
if (err ||
|
|
|
|
last.length === 0 ||
|
|
|
|
(last[0] !== undefined &&
|
|
|
|
last[0].artist !== value.artist &&
|
|
|
|
last[0].title !== value.title
|
|
|
|
)
|
|
|
|
) {
|
|
|
|
console.log('Save song!', value)
|
|
|
|
// Previous song was different => save song!
|
|
|
|
const history = new this.models.Histories(value)
|
|
|
|
history.save()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-12-24 16:30:45 +01:00
|
|
|
/**
|
|
|
|
* Point d'entrée pour l'ajout suppression de favoris
|
|
|
|
* @param {String} type
|
|
|
|
* @param {Function} botSay
|
|
|
|
* @param {String} from
|
|
|
|
* @param {Object} currentSong
|
|
|
|
* @param {Array} line
|
|
|
|
*/
|
|
|
|
action (type, botSay, from, currentSong, line) {
|
|
|
|
let value = currentSong[type]
|
|
|
|
|
|
|
|
if (line.length > 3) {
|
|
|
|
value = ''
|
|
|
|
for (let i = 3; i < line.length; i += 1) {
|
|
|
|
value += ' ' + line[i]
|
|
|
|
}
|
|
|
|
value = value.replace(' ', '')
|
|
|
|
}
|
|
|
|
|
|
|
|
const item = {
|
|
|
|
user: from,
|
|
|
|
type: type,
|
|
|
|
value: value
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (line[2]) {
|
|
|
|
case 'add':
|
|
|
|
this.add(botSay, from, item)
|
|
|
|
break
|
|
|
|
case 'del':
|
|
|
|
this.delete(botSay, from, item)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-24 16:41:18 +01:00
|
|
|
/**
|
|
|
|
* Fonction permettant de notifier les utilisateurs lorsqu'un bon song est joué
|
|
|
|
* @param {Function} botSay
|
|
|
|
* @param {String} where
|
|
|
|
* @param {Object} currentSong
|
|
|
|
*/
|
|
|
|
notify (botSay, where, currentSong) {
|
|
|
|
this.models.Notifications
|
|
|
|
.find({
|
|
|
|
$or: [
|
|
|
|
{
|
|
|
|
type: 'artist',
|
2019-12-25 12:47:09 +01:00
|
|
|
value: formatString(currentSong.artist)
|
2019-12-24 16:41:18 +01:00
|
|
|
},
|
|
|
|
{
|
|
|
|
type: 'title',
|
2019-12-25 12:47:09 +01:00
|
|
|
value: formatString(currentSong.title)
|
2019-12-24 16:41:18 +01:00
|
|
|
}
|
|
|
|
]
|
|
|
|
})
|
|
|
|
.then(list => {
|
|
|
|
const artist = []
|
|
|
|
const song = []
|
|
|
|
|
2020-01-02 13:58:41 +01:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
}
|
2019-12-24 16:41:18 +01:00
|
|
|
|
2020-01-02 13:58:41 +01:00
|
|
|
if (!isMute) {
|
|
|
|
if (list[i].type === 'artist') {
|
|
|
|
artist.push(list[i].user)
|
|
|
|
} else {
|
|
|
|
song.push(list[i].user)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (artist.length > 0) {
|
2020-01-07 16:38:04 +01:00
|
|
|
botSay(where, `Hey ${artist.toString().replace(/,/g, ', ')} ! Y'a ${currentSong.artist} ! Monte${artist.length > 1 ? 'z' : ''} le son !`)
|
2020-01-02 13:58:41 +01:00
|
|
|
}
|
|
|
|
if (song.length > 0) {
|
2020-01-07 16:38:04 +01:00
|
|
|
botSay(where, `Hey ${song.toString().replace(/,/g, ', ')} ! Y'a ${song.title} ! Monte${song.length > 1 ? 'z' : ''} le son !`)
|
2020-01-02 13:58:41 +01:00
|
|
|
}
|
|
|
|
})
|
2019-12-24 16:41:18 +01:00
|
|
|
})
|
|
|
|
.catch(err => {
|
|
|
|
console.log('ERR:', err)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-12-24 16:30:45 +01:00
|
|
|
/**
|
|
|
|
* Fonction permettant d'ajouter un favoris
|
|
|
|
* @param {Function} botSay
|
|
|
|
* @param {String} from
|
|
|
|
* @param {Object} item
|
|
|
|
*/
|
|
|
|
add (botSay, from, item) {
|
|
|
|
this.models.Notifications
|
|
|
|
.findOne(item)
|
|
|
|
.then(notification => {
|
|
|
|
if (!notification) {
|
|
|
|
const newItem = new this.models.Notifications(item)
|
|
|
|
newItem.save((err, res) => {
|
|
|
|
if (err) {
|
|
|
|
console.log('ERR:', err)
|
|
|
|
} else {
|
|
|
|
botSay(from, `${item.value} correctement ajouté dans vos favoris`)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
botSay(from, `${item.value} est déjà dans tes favoris, c'est moche de vieillir...`)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.catch(err => {
|
|
|
|
console.log('ERR:', err)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Fonction permettant de supprimer un favoris
|
|
|
|
* @param {Function} botSay
|
|
|
|
* @param {String} from
|
|
|
|
* @param {Object} item
|
|
|
|
*/
|
|
|
|
delete (botSay, from, item) {
|
|
|
|
this.models.Notifications
|
|
|
|
.findOne(item)
|
|
|
|
.then(notification => {
|
|
|
|
if (notification) {
|
|
|
|
notification.remove((err, res) => {
|
|
|
|
if (err) {
|
|
|
|
console.log('ERR:', err)
|
|
|
|
} else {
|
|
|
|
botSay(from, `${item.value} correctement retiré dans vos favoris`)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
botSay(from, `${item.value} n'est dpas dans tes favoris, c'est moche de vieillir...`)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.catch(err => {
|
|
|
|
console.log('ERR:', err)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = ArtistSong
|