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

269 lines
8 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)
obj.device = obj.sp_device.split("-")[0]
playlists = obj.sp_playlists.split(",")
var i;
playlistsar = []
for (i = 0; i < playlists.length; i++) {
playlistsar.push(playlists[i].split("-")[0])
}
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'
},
],
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,
locationcbPath: "",
2021-04-29 00:40:58 +01:00
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
2021-04-27 10:07:17 +01:00
}
},
computed: {
2021-04-29 22:38:57 +01:00
2021-04-27 10:07:17 +01:00
},
methods: {
2021-04-29 22:38:57 +01:00
getJukeboxes(){
self = this
LNbits.api
.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)
})
},
deleteJukebox(juke_id){
self = this
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-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-04-28 12:04:47 +01:00
submitSpotify() {
self = this
2021-04-29 22:38:57 +01:00
self.jukeboxDialog.data.user = self.g.user.id
2021-04-28 12:04:47 +01:00
self.requestAuthorization()
this.$q.notify({
spinner: true,
message: 'Fetching token',
timeout: 4000
2021-04-27 10:07:17 +01:00
})
2021-04-28 12:04:47 +01:00
LNbits.api.request(
'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-04-28 12:04:47 +01:00
if(response.data){
var timerId = setInterval(function(){
if(!self.jukeboxDialog.data.sp_user){
clearInterval(timerId);
}
2021-04-29 00:40:58 +01:00
self.jukeboxDialog.data.sp_id = response.data.id
2021-04-28 12:04:47 +01:00
LNbits.api
2021-04-29 00:40:58 +01:00
.request('GET', '/jukebox/api/v1/jukebox/spotify/' + self.jukeboxDialog.data.sp_id, self.g.user.wallets[0].inkey)
2021-04-28 12:04:47 +01:00
.then(response => {
2021-04-29 00:40:58 +01:00
if(response.data.sp_access_token){
self.jukeboxDialog.data.sp_access_token = response.data.sp_access_token
2021-04-28 12:04:47 +01:00
self.step = 3
2021-04-29 00:40:58 +01:00
self.fetchAccessToken()
clearInterval(timerId)
2021-04-28 12:04:47 +01:00
}
})
.catch(err => {
LNbits.utils.notifyApiError(err)
})
}, 3000)
}
2021-04-27 10:07:17 +01:00
})
.catch(err => {
2021-04-28 12:04:47 +01:00
LNbits.utils.notifyApiError(err)
2021-04-27 10:07:17 +01:00
})
},
2021-04-28 12:04:47 +01:00
requestAuthorization(){
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-04-29 08:47:58 +01:00
url += '&redirect_uri=' + encodeURI(self.locationcbPath) + self.jukeboxDialog.data.sp_user
2021-04-29 00:40:58 +01:00
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 = {}
},
openUpdateDialog(itemId) {
this.jukeboxDialog.show = true
let item = this.jukebox.items.find(item => item.id === itemId)
this.jukeboxDialog.data = item
},
2021-04-29 00:40:58 +01:00
createJukebox(){
self = this
2021-04-29 22:38:57 +01:00
this.jukeboxDialog.data.sp_playlists = this.jukeboxDialog.data.sp_playlists.join()
2021-04-29 00:40:58 +01:00
LNbits.api.request(
'PUT',
'/jukebox/api/v1/jukebox/' + this.jukeboxDialog.data.sp_id,
self.g.user.wallets[0].adminkey,
self.jukeboxDialog.data
)
2021-04-29 22:38:57 +01:00
.then(function (response) {
self.JukeboxLinks.push(mapJukebox(response.data))
self.jukeboxDialog.show = false
2021-04-29 00:40:58 +01:00
})
},
playlistApi(method, url, body){
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-04-29 00:40:58 +01:00
xhr.setRequestHeader('Authorization', 'Bearer ' + this.jukeboxDialog.data.sp_access_token)
2021-04-28 12:04:47 +01:00
xhr.send(body)
2021-04-29 00:40:58 +01:00
xhr.onload = function() {
let responseObj = JSON.parse(xhr.response)
2021-04-29 08:47:58 +01:00
self.playlists = []
2021-04-29 00:40:58 +01:00
var i;
for (i = 0; i < responseObj.items.length; i++) {
self.playlists.push(responseObj.items[i].name + "-" + responseObj.items[i].id)
}
}
2021-04-28 12:04:47 +01:00
},
refreshPlaylists(){
2021-04-29 00:40:58 +01:00
self = this
self.playlistApi( "GET", "https://api.spotify.com/v1/me/playlists", null)
2021-04-27 10:07:17 +01:00
},
2021-04-29 00:40:58 +01:00
deviceApi(method, url, body){
self = this
let xhr = new XMLHttpRequest()
xhr.open(method, url, true)
xhr.setRequestHeader('Content-Type', 'application/json')
xhr.setRequestHeader('Authorization', 'Bearer ' + this.jukeboxDialog.data.sp_access_token)
xhr.send(body)
xhr.onload = function() {
2021-04-29 08:47:58 +01:00
let responseObj = JSON.parse(xhr.response)
self.devices = []
2021-04-29 00:40:58 +01:00
var i;
2021-04-29 08:47:58 +01:00
for (i = 0; i < responseObj.devices.length; i++) {
self.devices.push(responseObj.devices[i].name + "-" + responseObj.devices[i].id)
2021-04-29 22:38:57 +01:00
2021-04-29 00:40:58 +01:00
}
2021-04-29 08:47:58 +01:00
2021-04-27 10:07:17 +01:00
}
},
2021-04-29 00:40:58 +01:00
refreshDevices(){
self = this
self.deviceApi( "GET", "https://api.spotify.com/v1/me/player/devices", null)
},
fetchAccessToken( ){
self = this
let body = "grant_type=authorization_code"
body += "&code=" + self.jukeboxDialog.data.sp_access_token
2021-04-29 08:47:58 +01:00
body += '&redirect_uri=' + encodeURI(self.locationcbPath) + self.jukeboxDialog.data.sp_user
2021-04-29 00:40:58 +01:00
this.callAuthorizationApi(body)
},
2021-04-28 12:04:47 +01:00
refreshAccessToken(){
2021-04-29 00:40:58 +01:00
self = this
2021-04-28 12:04:47 +01:00
let body = "grant_type=refresh_token"
2021-04-29 00:40:58 +01:00
body += "&refresh_token=" + self.jukeboxDialog.data.sp_refresh_token
2021-04-28 12:04:47 +01:00
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
},
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-04-29 00:40:58 +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-04-29 00:40:58 +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-04-29 00:40:58 +01:00
xhr.onload = function() {
let responseObj = JSON.parse(xhr.response)
self.jukeboxDialog.data.sp_access_token = responseObj.access_token
self.jukeboxDialog.data.sp_refresh_token = responseObj.refresh_token
self.refreshPlaylists()
self.refreshDevices()
}
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-04-28 12:04:47 +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
}
})