2021-09-01 20:07:11 -05:00
|
|
|
import { io } from 'socket.io-client'
|
2021-09-12 18:37:08 -05:00
|
|
|
import { Storage } from '@capacitor/storage'
|
2021-09-01 20:07:11 -05:00
|
|
|
import axios from 'axios'
|
|
|
|
import EventEmitter from 'events'
|
|
|
|
|
|
|
|
class Server extends EventEmitter {
|
|
|
|
constructor(store) {
|
|
|
|
super()
|
|
|
|
|
|
|
|
this.store = store
|
|
|
|
|
|
|
|
this.url = null
|
|
|
|
this.socket = null
|
|
|
|
|
|
|
|
this.user = null
|
|
|
|
this.connected = false
|
|
|
|
|
|
|
|
this.stream = null
|
|
|
|
}
|
|
|
|
|
|
|
|
get token() {
|
|
|
|
return this.user ? this.user.token : null
|
|
|
|
}
|
|
|
|
|
|
|
|
getAxiosConfig() {
|
|
|
|
return { headers: { Authorization: `Bearer ${this.token}` } }
|
|
|
|
}
|
|
|
|
|
|
|
|
getServerUrl(url) {
|
2021-09-12 18:37:08 -05:00
|
|
|
if (!url) return null
|
2021-10-18 19:40:05 -05:00
|
|
|
try {
|
|
|
|
var urlObject = new URL(url)
|
|
|
|
return `${urlObject.protocol}//${urlObject.hostname}:${urlObject.port}`
|
|
|
|
} catch (error) {
|
|
|
|
console.error('Invalid URL', error)
|
|
|
|
return null
|
|
|
|
}
|
2021-09-01 20:07:11 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
setUser(user) {
|
|
|
|
this.user = user
|
|
|
|
this.store.commit('user/setUser', user)
|
|
|
|
if (user) {
|
2021-09-19 18:44:10 -05:00
|
|
|
// this.store.commit('user/setSettings', user.settings)
|
2021-09-12 18:37:08 -05:00
|
|
|
Storage.set({ key: 'token', value: user.token })
|
2021-09-01 20:07:11 -05:00
|
|
|
} else {
|
2021-09-12 18:37:08 -05:00
|
|
|
Storage.remove({ key: 'token' })
|
2021-09-01 20:07:11 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
setServerUrl(url) {
|
|
|
|
this.url = url
|
|
|
|
this.store.commit('setServerUrl', url)
|
2021-09-12 18:37:08 -05:00
|
|
|
|
|
|
|
if (url) {
|
|
|
|
Storage.set({ key: 'serverUrl', value: url })
|
|
|
|
} else {
|
|
|
|
Storage.remove({ key: 'serverUrl' })
|
|
|
|
}
|
2021-09-01 20:07:11 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
async connect(url, token) {
|
2021-09-12 18:37:08 -05:00
|
|
|
if (!url) {
|
|
|
|
console.error('Invalid url to connect')
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2021-09-01 20:07:11 -05:00
|
|
|
var serverUrl = this.getServerUrl(url)
|
|
|
|
var res = await this.ping(serverUrl)
|
2021-09-12 18:37:08 -05:00
|
|
|
|
2021-09-01 20:07:11 -05:00
|
|
|
if (!res || !res.success) {
|
2021-10-03 13:51:28 +08:00
|
|
|
//this.setServerUrl(null)
|
2021-09-01 20:07:11 -05:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
var authRes = await this.authorize(serverUrl, token)
|
|
|
|
if (!authRes || !authRes.user) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
this.setServerUrl(serverUrl)
|
2021-09-12 18:37:08 -05:00
|
|
|
|
2021-09-01 20:07:11 -05:00
|
|
|
this.setUser(authRes.user)
|
|
|
|
this.connectSocket()
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
async check(url) {
|
|
|
|
var serverUrl = this.getServerUrl(url)
|
2021-10-18 19:40:05 -05:00
|
|
|
if (!serverUrl) {
|
|
|
|
return false
|
|
|
|
}
|
2021-09-01 20:07:11 -05:00
|
|
|
var res = await this.ping(serverUrl)
|
|
|
|
if (!res || !res.success) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return serverUrl
|
|
|
|
}
|
|
|
|
|
|
|
|
async login(url, username, password) {
|
|
|
|
var serverUrl = this.getServerUrl(url)
|
|
|
|
var authUrl = serverUrl + '/login'
|
|
|
|
return axios.post(authUrl, { username, password }).then((res) => {
|
|
|
|
if (!res.data || !res.data.user) {
|
|
|
|
console.error(res.data.error)
|
|
|
|
return {
|
|
|
|
error: res.data.error || 'Unknown Error'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
this.setServerUrl(serverUrl)
|
|
|
|
this.setUser(res.data.user)
|
|
|
|
this.connectSocket()
|
|
|
|
return {
|
|
|
|
user: res.data.user
|
|
|
|
}
|
|
|
|
}).catch(error => {
|
|
|
|
console.error('[Server] Server auth failed', error)
|
|
|
|
return {
|
|
|
|
error: 'Request Failed'
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
logout() {
|
|
|
|
this.setUser(null)
|
2021-09-12 18:37:08 -05:00
|
|
|
if (this.socket) {
|
|
|
|
this.socket.disconnect()
|
|
|
|
}
|
2021-09-01 20:07:11 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
authorize(serverUrl, token) {
|
|
|
|
var authUrl = serverUrl + '/api/authorize'
|
|
|
|
return axios.post(authUrl, null, { headers: { Authorization: `Bearer ${token}` } }).then((res) => {
|
|
|
|
return res.data
|
|
|
|
}).catch(error => {
|
|
|
|
console.error('[Server] Server auth failed', error)
|
|
|
|
return false
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
ping(url) {
|
|
|
|
var pingUrl = url + '/ping'
|
|
|
|
console.log('[Server] Check server', pingUrl)
|
2021-10-18 19:40:05 -05:00
|
|
|
return axios.get(pingUrl, { timeout: 1000 }).then((res) => {
|
2021-09-01 20:07:11 -05:00
|
|
|
return res.data
|
|
|
|
}).catch(error => {
|
|
|
|
console.error('Server check failed', error)
|
|
|
|
return false
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
connectSocket() {
|
|
|
|
console.log('[SERVER] Connect Socket', this.url)
|
|
|
|
|
|
|
|
this.socket = io(this.url)
|
|
|
|
this.socket.on('connect', () => {
|
|
|
|
console.log('[Server] Socket Connected')
|
|
|
|
|
|
|
|
// Authenticate socket with token
|
|
|
|
this.socket.emit('auth', this.token)
|
|
|
|
|
|
|
|
this.connected = true
|
|
|
|
this.emit('connected', true)
|
2021-09-12 18:37:08 -05:00
|
|
|
this.store.commit('setSocketConnected', true)
|
2021-09-01 20:07:11 -05:00
|
|
|
})
|
|
|
|
this.socket.on('disconnect', () => {
|
|
|
|
console.log('[Server] Socket Disconnected')
|
2021-09-12 18:37:08 -05:00
|
|
|
this.connected = false
|
|
|
|
this.emit('connected', false)
|
|
|
|
this.store.commit('setSocketConnected', false)
|
2021-09-01 20:07:11 -05:00
|
|
|
})
|
|
|
|
this.socket.on('init', (data) => {
|
|
|
|
console.log('[Server] Initial socket data received', data)
|
|
|
|
if (data.stream) {
|
|
|
|
this.stream = data.stream
|
|
|
|
this.store.commit('setStreamAudiobook', data.stream.audiobook)
|
|
|
|
this.emit('initialStream', data.stream)
|
|
|
|
}
|
|
|
|
})
|
2021-09-04 12:31:00 -05:00
|
|
|
this.socket.on('user_updated', (user) => {
|
|
|
|
if (this.user && user.id === this.user.id) {
|
|
|
|
this.setUser(user)
|
|
|
|
}
|
|
|
|
})
|
2021-09-01 20:07:11 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default Server
|