funkwhale/front/src/store/instance.js

176 wiersze
4.5 KiB
JavaScript
Czysty Zwykły widok Historia

import axios from 'axios'
import logger from '@/logging'
import _ from 'lodash'
function getDefaultUrl () {
return (
window.location.protocol + '//' + window.location.hostname +
2019-01-17 11:17:29 +00:00
(window.location.port ? ':' + window.location.port : '') + '/'
)
}
function notifyServiceWorker (registration, message) {
if (registration && registration.active) {
registration.active.postMessage(message)
}
}
export default {
namespaced: true,
state: {
2018-03-01 23:13:45 +00:00
maxEvents: 200,
frontSettings: {},
2022-02-21 22:23:13 +00:00
instanceUrl: import.meta.env.VUE_APP_INSTANCE_URL,
2018-03-01 23:13:45 +00:00
events: [],
2019-01-17 11:17:29 +00:00
knownInstances: [],
2019-09-23 09:14:54 +00:00
nodeinfo: null,
settings: {
instance: {
name: {
value: ''
},
short_description: {
value: ''
},
long_description: {
value: ''
},
funkwhale_support_message_enabled: {
value: true
},
support_message: {
value: ''
}
},
2018-02-24 13:34:28 +00:00
users: {
registration_enabled: {
value: true
},
upload_quota: {
value: 0
2018-02-24 13:34:28 +00:00
}
},
2020-03-18 10:57:33 +00:00
moderation: {
signup_approval_enabled: {
2021-12-06 10:35:20 +00:00
value: false
2020-03-18 10:57:33 +00:00
},
2021-12-06 10:35:20 +00:00
signup_form_customization: { value: null }
2020-03-18 10:57:33 +00:00
},
subsonic: {
enabled: {
value: true
}
2021-12-06 10:35:20 +00:00
}
}
},
mutations: {
settings: (state, value) => {
_.merge(state.settings, value)
2018-03-01 23:13:45 +00:00
},
event: (state, value) => {
state.events.unshift(value)
if (state.events.length > state.maxEvents) {
state.events = state.events.slice(0, state.maxEvents)
}
},
events: (state, value) => {
state.events = value
},
2019-09-23 09:14:54 +00:00
nodeinfo: (state, value) => {
state.nodeinfo = value
},
frontSettings: (state, value) => {
state.frontSettings = value
},
instanceUrl: (state, value) => {
if (value && !value.endsWith('/')) {
value = value + '/'
}
state.instanceUrl = value
2021-12-06 10:35:20 +00:00
notifyServiceWorker(state.registration, { command: 'serverChosen', serverUrl: state.instanceUrl })
2019-01-17 11:17:29 +00:00
// append the URL to the list (and remove existing one if needed)
if (value) {
2021-12-06 10:35:20 +00:00
const index = state.knownInstances.indexOf(value)
2019-01-17 11:17:29 +00:00
if (index > -1) {
2021-12-06 10:35:20 +00:00
state.knownInstances.splice(index, 1)
2019-01-17 11:17:29 +00:00
}
state.knownInstances.splice(0, 0, value)
}
if (!value) {
axios.defaults.baseURL = null
return
}
2021-12-06 10:35:20 +00:00
const suffix = 'api/v1/'
axios.defaults.baseURL = state.instanceUrl + suffix
}
},
getters: {
defaultUrl: (state) => () => {
return getDefaultUrl()
},
absoluteUrl: (state) => (relativeUrl) => {
if (relativeUrl.startsWith('http')) {
return relativeUrl
}
if (state.instanceUrl.endsWith('/') && relativeUrl.startsWith('/')) {
relativeUrl = relativeUrl.slice(1)
}
2021-12-06 10:35:20 +00:00
const instanceUrl = state.instanceUrl || getDefaultUrl()
return instanceUrl + relativeUrl
2020-02-05 14:06:07 +00:00
},
domain: (state) => {
2021-12-06 10:35:20 +00:00
const url = state.instanceUrl
const parser = document.createElement('a')
2020-02-05 14:06:07 +00:00
parser.href = url
return parser.hostname
},
appDomain: (state) => {
return location.hostname
}
},
actions: {
2021-12-06 10:35:20 +00:00
setUrl ({ commit, dispatch }, url) {
commit('instanceUrl', url)
2021-12-06 10:35:20 +00:00
const modules = [
'auth',
'favorites',
2019-02-14 09:49:06 +00:00
'moderation',
'player',
'playlists',
'queue',
'radios'
]
modules.forEach(m => {
2021-12-06 10:35:20 +00:00
commit(`${m}/reset`, null, { root: true })
})
},
// Send a request to the login URL and save the returned JWT
2021-12-06 10:35:20 +00:00
fetchSettings ({ commit }, payload) {
return axios.get('instance/settings/').then(response => {
logger.default.info('Successfully fetched instance settings')
2021-12-06 10:35:20 +00:00
const sections = {}
response.data.forEach(e => {
sections[e.section] = {}
})
response.data.forEach(e => {
sections[e.section][e.name] = e
})
commit('settings', sections)
2018-02-24 13:55:08 +00:00
if (payload && payload.callback) {
2018-02-24 14:10:47 +00:00
payload.callback()
2018-02-24 13:34:28 +00:00
}
}, response => {
logger.default.error('Error while fetching settings', response.data)
})
},
2021-12-06 10:35:20 +00:00
fetchFrontSettings ({ commit }) {
return axios.get('/settings.json').then(response => {
commit('frontSettings', response.data)
}, response => {
logger.default.error('Error when fetching front-end configuration (or no customization available)')
})
}
}
}