lnbits-legend/lnbits/extensions/jukebox/static/js/index.js

373 lines
10 KiB
JavaScript
Raw Normal View History

2021-04-27 10:07:17 +01:00
/* globals Quasar, Vue, _, VueQrcode, windowMixin, LNbits, LOCALE */
Vue.component(VueQrcode.name, VueQrcode)
2021-04-29 22:38:57 +01:00
var mapJukebox = obj => {
obj._data = _.clone(obj)
2021-05-03 23:22:40 +01:00
obj.sp_id = obj.id
obj.device = obj.sp_device.split('-')[0]
playlists = obj.sp_playlists.split(',')
var i
2021-04-29 22:38:57 +01:00
playlistsar = []
for (i = 0; i < playlists.length; i++) {
2021-05-03 23:22:40 +01:00
playlistsar.push(playlists[i].split('-')[0])
2021-04-29 22:38:57 +01:00
}
obj.playlist = playlistsar.join()
return obj
}
2021-04-27 10:07:17 +01:00
new Vue({
el: '#vue',
mixins: [windowMixin],
data() {
return {
2021-04-29 22:38:57 +01:00
JukeboxTable: {
columns: [
{
name: 'title',
align: 'left',
label: 'Title',
field: 'title'
},
{
name: 'device',
align: 'left',
label: 'Device',
field: 'device'
},
{
name: 'playlist',
align: 'left',
label: 'Playlist',
field: 'playlist'
},
{
name: 'price',
align: 'left',
label: 'Price',
field: 'price'
},
2021-05-03 23:22:40 +01:00
{
name: 'profit',
align: 'left',
label: 'Profit',
field: 'profit'
}
2021-04-29 22:38:57 +01:00
],
pagination: {
rowsPerPage: 10
}
},
2021-04-28 12:04:47 +01:00
isPwd: true,
tokenFetched: true,
2021-04-29 00:40:58 +01:00
devices: [],
2021-04-29 22:38:57 +01:00
filter: '',
2021-04-28 12:04:47 +01:00
jukebox: {},
playlists: [],
2021-04-29 22:38:57 +01:00
JukeboxLinks: [],
2021-04-28 12:04:47 +01:00
step: 1,
2021-05-03 23:22:40 +01:00
locationcbPath: '',
locationcb: '',
2021-04-28 12:04:47 +01:00
jukeboxDialog: {
2021-04-27 10:07:17 +01:00
show: false,
2021-04-28 12:04:47 +01:00
data: {}
},
spotifyDialog: false,
qrCodeDialog: {
show: false,
data: null
}
2021-04-27 10:07:17 +01:00
}
},
2021-05-03 23:22:40 +01:00
computed: {},
2021-04-27 10:07:17 +01:00
methods: {
openQrCodeDialog: function (linkId) {
var link = _.findWhere(this.JukeboxLinks, {id: linkId})
this.qrCodeDialog.data = _.clone(link)
console.log(this.qrCodeDialog.data)
this.qrCodeDialog.data.url =
window.location.protocol + '//' + window.location.host
this.qrCodeDialog.show = true
},
2021-05-03 23:22:40 +01:00
getJukeboxes() {
2021-04-29 22:38:57 +01:00
self = this
LNbits.api
2021-05-03 23:22:40 +01:00
.request('GET', '/jukebox/api/v1/jukebox', self.g.user.wallets[0].inkey)
.then(function (response) {
self.JukeboxLinks = response.data.map(mapJukebox)
})
.catch(err => {
LNbits.utils.notifyApiError(err)
})
2021-04-29 22:38:57 +01:00
},
2021-05-03 23:22:40 +01:00
deleteJukebox(juke_id) {
2021-04-29 22:38:57 +01:00
self = this
2021-05-03 23:22:40 +01:00
LNbits.utils
.confirmDialog('Are you sure you want to delete this Jukebox?')
.onOk(function () {
LNbits.api
.request(
'DELETE',
'/jukebox/api/v1/jukebox/' + juke_id,
self.g.user.wallets[0].adminkey
)
.then(function (response) {
self.JukeboxLinks = _.reject(self.JukeboxLinks, function (obj) {
return obj.id === juke_id
})
})
.catch(err => {
LNbits.utils.notifyApiError(err)
})
2021-04-29 22:38:57 +01:00
})
2021-05-03 23:22:40 +01:00
},
updateJukebox: function (linkId) {
self = this
var link = _.findWhere(self.JukeboxLinks, {id: linkId})
self.jukeboxDialog.data = _.clone(link._data)
console.log(this.jukeboxDialog.data.sp_access_token)
2021-04-29 22:38:57 +01:00
2021-05-03 23:22:40 +01:00
self.refreshDevices()
self.refreshPlaylists()
self.step = 4
self.jukeboxDialog.data.sp_device = []
self.jukeboxDialog.data.sp_playlists = []
self.jukeboxDialog.data.sp_id = self.jukeboxDialog.data.id
self.jukeboxDialog.data.price = String(self.jukeboxDialog.data.price)
2021-05-03 23:22:40 +01:00
self.jukeboxDialog.show = true
2021-04-29 22:38:57 +01:00
},
2021-04-28 12:04:47 +01:00
closeFormDialog() {
this.jukeboxDialog.data = {}
this.jukeboxDialog.show = false
this.step = 1
2021-04-27 10:07:17 +01:00
},
2021-05-03 23:22:40 +01:00
submitSpotifyKeys() {
2021-04-28 12:04:47 +01:00
self = this
2021-04-29 22:38:57 +01:00
self.jukeboxDialog.data.user = self.g.user.id
2021-05-03 23:22:40 +01:00
LNbits.api
.request(
2021-04-28 12:04:47 +01:00
'POST',
'/jukebox/api/v1/jukebox/',
self.g.user.wallets[0].adminkey,
self.jukeboxDialog.data
)
2021-04-27 10:07:17 +01:00
.then(response => {
2021-05-03 23:22:40 +01:00
if (response.data) {
2021-04-29 00:40:58 +01:00
self.jukeboxDialog.data.sp_id = response.data.id
2021-05-03 23:22:40 +01:00
self.step = 3
}
2021-04-27 10:07:17 +01:00
})
.catch(err => {
2021-05-03 23:22:40 +01:00
LNbits.utils.notifyApiError(err)
2021-04-27 10:07:17 +01:00
})
},
2021-05-03 23:22:40 +01:00
authAccess() {
self = this
self.requestAuthorization()
self.$q.notify({
spinner: true,
message: 'Fetching token',
timeout: 4000
})
self.getSpotifyTokens()
},
getSpotifyTokens() {
self = this
var timerId = setInterval(function () {
if (!self.jukeboxDialog.data.sp_user) {
clearInterval(timerId)
}
LNbits.api
.request(
'GET',
'/jukebox/api/v1/jukebox/spotify/' + self.jukeboxDialog.data.sp_id,
self.g.user.wallets[0].inkey
)
.then(response => {
if (response.data.sp_access_token) {
self.fetchAccessToken(response.data.sp_access_token)
if (self.jukeboxDialog.data.sp_access_token) {
self.refreshPlaylists()
self.refreshDevices()
self.step = 4
clearInterval(timerId)
}
}
})
.catch(err => {
LNbits.utils.notifyApiError(err)
})
}, 5000)
},
requestAuthorization() {
2021-04-28 12:04:47 +01:00
self = this
2021-04-29 00:40:58 +01:00
var url = 'https://accounts.spotify.com/authorize'
url += '?client_id=' + self.jukeboxDialog.data.sp_user
url += '&response_type=code'
2021-05-03 23:22:40 +01:00
url +=
'&redirect_uri=' +
encodeURI(self.locationcbPath + self.jukeboxDialog.data.sp_id)
url += '&show_dialog=true'
url +=
'&scope=user-read-private user-read-email user-modify-playback-state user-read-playback-position user-library-read streaming user-read-playback-state user-read-recently-played playlist-read-private'
2021-04-29 22:38:57 +01:00
2021-04-28 12:04:47 +01:00
window.open(url)
},
openNewDialog() {
this.jukeboxDialog.show = true
this.jukeboxDialog.data = {}
},
2021-05-03 23:22:40 +01:00
createJukebox() {
2021-04-29 00:40:58 +01:00
self = this
self.jukeboxDialog.data.sp_playlists = self.jukeboxDialog.data.sp_playlists.join()
self.updateDB()
self.jukeboxDialog.show = false
2021-05-05 19:20:53 +01:00
self.getJukeboxes()
},
updateDB(){
self = this
console.log(self.jukeboxDialog.data)
2021-05-03 23:22:40 +01:00
LNbits.api
.request(
2021-04-29 00:40:58 +01:00
'PUT',
2021-05-05 19:20:53 +01:00
'/jukebox/api/v1/jukebox/' + this.jukeboxDialog.data.sp_id,
2021-04-29 00:40:58 +01:00
self.g.user.wallets[0].adminkey,
self.jukeboxDialog.data
)
2021-04-29 22:38:57 +01:00
.then(function (response) {
console.log(response.data)
2021-05-05 19:20:53 +01:00
if(this.jukeboxDialog.data.devices){
self.getJukeboxes()
}
self.JukeboxLinks.push(mapJukebox(response.data))
2021-04-29 00:40:58 +01:00
})
},
2021-05-03 23:22:40 +01:00
playlistApi(method, url, body) {
2021-04-29 00:40:58 +01:00
self = this
2021-04-28 12:04:47 +01:00
let xhr = new XMLHttpRequest()
xhr.open(method, url, true)
xhr.setRequestHeader('Content-Type', 'application/json')
2021-05-03 23:22:40 +01:00
xhr.setRequestHeader(
'Authorization',
'Bearer ' + this.jukeboxDialog.data.sp_access_token
)
2021-04-28 12:04:47 +01:00
xhr.send(body)
2021-05-03 23:22:40 +01:00
xhr.onload = function () {
if(xhr.status == 401){
self.refreshAccessToken()
self.playlistApi('GET', 'https://api.spotify.com/v1/me/playlists', null)
}
2021-04-29 00:40:58 +01:00
let responseObj = JSON.parse(xhr.response)
2021-05-03 23:22:40 +01:00
self.jukeboxDialog.data.playlists = null
2021-04-29 08:47:58 +01:00
self.playlists = []
2021-05-03 23:22:40 +01:00
self.jukeboxDialog.data.playlists = []
var i
2021-04-29 00:40:58 +01:00
for (i = 0; i < responseObj.items.length; i++) {
2021-05-03 23:22:40 +01:00
self.playlists.push(
responseObj.items[i].name + '-' + responseObj.items[i].id
)
2021-04-29 00:40:58 +01:00
}
}
2021-04-28 12:04:47 +01:00
},
2021-05-03 23:22:40 +01:00
refreshPlaylists() {
2021-04-29 00:40:58 +01:00
self = this
2021-05-03 23:22:40 +01:00
self.playlistApi('GET', 'https://api.spotify.com/v1/me/playlists', null)
2021-04-27 10:07:17 +01:00
},
2021-05-03 23:22:40 +01:00
deviceApi(method, url, body) {
2021-04-29 00:40:58 +01:00
self = this
let xhr = new XMLHttpRequest()
xhr.open(method, url, true)
xhr.setRequestHeader('Content-Type', 'application/json')
2021-05-03 23:22:40 +01:00
xhr.setRequestHeader(
'Authorization',
'Bearer ' + this.jukeboxDialog.data.sp_access_token
)
2021-04-29 00:40:58 +01:00
xhr.send(body)
2021-05-03 23:22:40 +01:00
xhr.onload = function () {
if(xhr.status == 401){
self.refreshAccessToken()
self.deviceApi('GET', 'https://api.spotify.com/v1/me/player/devices', null)
}
2021-04-29 08:47:58 +01:00
let responseObj = JSON.parse(xhr.response)
2021-05-03 23:22:40 +01:00
self.jukeboxDialog.data.devices = []
2021-04-29 08:47:58 +01:00
self.devices = []
2021-05-03 23:22:40 +01:00
var i
2021-04-29 08:47:58 +01:00
for (i = 0; i < responseObj.devices.length; i++) {
2021-05-03 23:22:40 +01:00
self.devices.push(
responseObj.devices[i].name + '-' + responseObj.devices[i].id
)
2021-04-29 00:40:58 +01:00
}
2021-04-27 10:07:17 +01:00
}
},
2021-05-03 23:22:40 +01:00
refreshDevices() {
2021-04-29 00:40:58 +01:00
self = this
2021-05-03 23:22:40 +01:00
self.deviceApi(
'GET',
'https://api.spotify.com/v1/me/player/devices',
null
)
2021-04-29 00:40:58 +01:00
},
2021-05-03 23:22:40 +01:00
fetchAccessToken(code) {
2021-04-29 00:40:58 +01:00
self = this
2021-05-03 23:22:40 +01:00
let body = 'grant_type=authorization_code'
body += '&code=' + code
body +=
'&redirect_uri=' +
encodeURI(self.locationcbPath + self.jukeboxDialog.data.sp_id)
2021-04-29 00:40:58 +01:00
this.callAuthorizationApi(body)
},
2021-05-03 23:22:40 +01:00
refreshAccessToken() {
2021-04-29 00:40:58 +01:00
self = this
2021-05-03 23:22:40 +01:00
let body = 'grant_type=refresh_token'
body += '&refresh_token=' + self.jukeboxDialog.data.sp_refresh_token
body += '&client_id=' + self.jukeboxDialog.data.sp_user
2021-04-29 00:40:58 +01:00
this.callAuthorizationApi(body)
2021-04-28 12:04:47 +01:00
},
2021-05-03 23:22:40 +01:00
callAuthorizationApi(body) {
2021-04-29 00:40:58 +01:00
self = this
2021-04-28 12:04:47 +01:00
let xhr = new XMLHttpRequest()
2021-05-03 23:22:40 +01:00
xhr.open('POST', 'https://accounts.spotify.com/api/token', true)
2021-04-28 12:04:47 +01:00
xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded')
2021-05-03 23:22:40 +01:00
xhr.setRequestHeader(
'Authorization',
'Basic ' +
btoa(
this.jukeboxDialog.data.sp_user +
':' +
this.jukeboxDialog.data.sp_secret
)
)
2021-04-28 12:04:47 +01:00
xhr.send(body)
2021-05-03 23:22:40 +01:00
xhr.onload = function () {
2021-04-29 00:40:58 +01:00
let responseObj = JSON.parse(xhr.response)
2021-05-03 23:22:40 +01:00
if (responseObj.access_token) {
self.jukeboxDialog.data.sp_access_token = responseObj.access_token
self.jukeboxDialog.data.sp_refresh_token = responseObj.refresh_token
self.updateDB()
2021-05-03 23:22:40 +01:00
}
2021-04-29 00:40:58 +01:00
}
2021-05-03 23:22:40 +01:00
}
2021-04-27 10:07:17 +01:00
},
created() {
2021-04-29 22:38:57 +01:00
var getJukeboxes = this.getJukeboxes
getJukeboxes()
2021-04-27 10:07:17 +01:00
this.selectedWallet = this.g.user.wallets[0]
2021-05-03 23:22:40 +01:00
this.locationcbPath = String(
[
window.location.protocol,
'//',
window.location.host,
'/jukebox/api/v1/jukebox/spotify/cb/'
].join('')
)
2021-04-29 00:40:58 +01:00
this.locationcb = this.locationcbPath
2021-04-27 10:07:17 +01:00
}
})