ffplayout/stores/config.ts

262 lines
8.4 KiB
TypeScript
Raw Normal View History

2023-01-11 10:54:25 +01:00
import _ from 'lodash'
import { defineStore } from 'pinia'
export const useConfig = defineStore('config', {
state: () => ({
id: 0,
2023-01-11 10:54:25 +01:00
configCount: 0,
2023-09-26 09:29:46 +02:00
contentType: { 'content-type': 'application/json;charset=UTF-8' },
channels: [] as Channel[],
channelsRaw: [] as Channel[],
2023-01-11 10:54:25 +01:00
playlistLength: 86400.0,
2024-06-17 18:05:56 +02:00
advanced: {} as any,
playout: {} as any,
2024-06-17 15:25:52 +02:00
currentUser: 0,
2023-01-11 10:54:25 +01:00
configUser: {} as User,
utcOffset: 0,
2024-05-15 08:53:25 +02:00
onetimeInfo: true,
2024-07-04 16:41:32 +02:00
showPlayer: true,
2023-01-11 10:54:25 +01:00
}),
getters: {},
actions: {
2024-08-22 19:56:05 +02:00
async configInit() {
2023-05-15 14:08:40 +02:00
const authStore = useAuth()
2023-01-11 10:54:25 +01:00
authStore.inspectToken()
if (authStore.isLogin) {
2024-04-25 14:49:57 +02:00
await authStore.obtainUuid()
2024-08-22 19:56:05 +02:00
await this.getChannelConfig().then(async () => {
2024-08-21 12:17:15 +02:00
await this.getPlayoutConfig()
await this.getUserConfig()
2024-06-17 18:05:56 +02:00
2024-08-21 12:17:15 +02:00
if (this.configUser.id === 1) {
await this.getAdvancedConfig()
}
})
2023-01-11 10:54:25 +01:00
}
},
2024-08-21 12:17:15 +02:00
logout() {
const authStore = useAuth()
const cookie = useCookie('token')
cookie.value = null
authStore.isLogin = false
navigateTo('/')
},
2024-06-17 18:05:56 +02:00
async getChannelConfig() {
2023-05-15 14:08:40 +02:00
const authStore = useAuth()
const indexStore = useIndex()
2023-01-11 10:54:25 +01:00
let statusCode = 0
await fetch('/api/channels', {
2023-01-11 10:54:25 +01:00
method: 'GET',
headers: authStore.authHeader,
})
.then((response) => {
2023-01-11 10:54:25 +01:00
statusCode = response.status
return response
})
.then((response) => response.json())
.then((objs) => {
2024-08-21 12:17:15 +02:00
if (!objs[0]) {
this.logout()
throw new Error('User not found')
}
this.utcOffset = objs[0].utc_offset
this.channels = objs
this.channelsRaw = _.cloneDeep(objs)
this.configCount = objs.length
2023-01-11 10:54:25 +01:00
})
.catch((e) => {
if (statusCode === 401) {
2024-08-21 12:17:15 +02:00
this.logout()
2023-01-11 10:54:25 +01:00
}
this.channels = [
2023-01-11 10:54:25 +01:00
{
id: 1,
extra_extensions: '',
name: 'Channel 1',
preview_url: '',
2024-08-22 17:22:20 +02:00
hls_path: '',
playlist_path: '',
storage_path: '',
2023-01-11 10:54:25 +01:00
uts_offset: 0,
},
]
2023-01-11 10:54:25 +01:00
indexStore.msgAlert('error', e, 3)
2023-01-11 10:54:25 +01:00
})
},
async setChannelConfig(obj: Channel): Promise<any> {
2023-05-15 14:08:40 +02:00
const authStore = useAuth()
2023-01-11 10:54:25 +01:00
const stringObj = _.cloneDeep(obj)
let response
if (this.channelsRaw.some((e) => e.id === stringObj.id)) {
response = await fetch(`/api/channel/${obj.id}`, {
2023-01-11 10:54:25 +01:00
method: 'PATCH',
2023-09-26 09:29:46 +02:00
headers: { ...this.contentType, ...authStore.authHeader },
2023-01-11 10:54:25 +01:00
body: JSON.stringify(stringObj),
})
} else {
response = await fetch('/api/channel/', {
2023-01-11 10:54:25 +01:00
method: 'POST',
2023-09-26 09:29:46 +02:00
headers: { ...this.contentType, ...authStore.authHeader },
2023-01-11 10:54:25 +01:00
body: JSON.stringify(stringObj),
})
const json = await response.json()
const guiConfigs = []
for (const obj of this.channels) {
2023-01-11 10:54:25 +01:00
if (obj.name === stringObj.name) {
guiConfigs.push(json)
} else {
guiConfigs.push(obj)
}
}
this.channels = guiConfigs
this.channelsRaw = _.cloneDeep(guiConfigs)
this.configCount = guiConfigs.length
2023-01-11 10:54:25 +01:00
}
2023-07-26 21:21:19 +02:00
await this.getPlayoutConfig()
2023-01-11 10:54:25 +01:00
return response
},
async getPlayoutConfig() {
const { $i18n } = useNuxtApp()
2024-04-17 12:32:07 +02:00
const { timeToSeconds } = stringFormatter()
2023-05-15 14:08:40 +02:00
const authStore = useAuth()
const indexStore = useIndex()
const channel = this.channels[this.id].id
2023-01-11 10:54:25 +01:00
await fetch(`/api/playout/config/${channel}`, {
2023-01-11 10:54:25 +01:00
method: 'GET',
headers: authStore.authHeader,
})
.then((response) => response.json())
.then((data) => {
data.playlist.startInSec = timeToSeconds(data.playlist.day_start ?? 0)
data.playlist.lengthInSec = timeToSeconds(data.playlist.length ?? this.playlistLength)
2023-01-11 10:54:25 +01:00
if (data.storage.extensions) {
data.storage.extensions = data.storage.extensions.join(',')
}
this.playout = data
2023-01-11 10:54:25 +01:00
})
.catch(() => {
indexStore.msgAlert('error', $i18n.t('config.noPlayoutConfig'), 3)
2023-01-11 10:54:25 +01:00
})
2024-06-17 18:05:56 +02:00
},
async getAdvancedConfig() {
const { $i18n } = useNuxtApp()
const authStore = useAuth()
const indexStore = useIndex()
const channel = this.channels[this.id].id
2024-06-17 18:05:56 +02:00
await $fetch(`/api/playout/advanced/${channel}`, {
method: 'GET',
headers: authStore.authHeader,
})
.then((data) => {
this.advanced = data
})
.catch(() => {
indexStore.msgAlert('error', $i18n.t('config.noAdvancedConfig'), 3)
})
2023-01-11 10:54:25 +01:00
},
async setPlayoutConfig(obj: any) {
2024-04-17 12:32:07 +02:00
const { timeToSeconds } = stringFormatter()
2023-05-15 14:08:40 +02:00
const authStore = useAuth()
const channel = this.channels[this.id].id
2023-01-11 10:54:25 +01:00
this.playlistLength = timeToSeconds(obj.playlist.length)
this.playout.playlist.startInSec = timeToSeconds(obj.playlist.day_start)
this.playout.playlist.lengthInSec = timeToSeconds(obj.playlist.length)
if (typeof obj.storage.extensions === 'string') {
obj.storage.extensions = obj.storage.extensions.replace(' ', '').split(/,|;/)
}
2023-05-19 08:46:08 +02:00
const update = await fetch(`/api/playout/config/${channel}`, {
2023-01-11 10:54:25 +01:00
method: 'PUT',
2023-09-26 09:29:46 +02:00
headers: { ...this.contentType, ...authStore.authHeader },
2023-01-11 10:54:25 +01:00
body: JSON.stringify(obj),
})
return update
},
2024-06-17 18:35:53 +02:00
async setAdvancedConfig() {
const authStore = useAuth()
const channel = this.channels[this.id].id
2024-06-17 18:35:53 +02:00
const update = await fetch(`/api/playout/advanced/${channel}`, {
method: 'PUT',
headers: { ...this.contentType, ...authStore.authHeader },
body: JSON.stringify(this.advanced),
})
return update
},
2023-01-11 10:54:25 +01:00
async getUserConfig() {
2023-05-15 14:08:40 +02:00
const authStore = useAuth()
await fetch('/api/user', {
2023-01-11 10:54:25 +01:00
method: 'GET',
headers: authStore.authHeader,
})
.then((response) => response.json())
.then((data) => {
2024-08-21 12:17:15 +02:00
if (data.id === 0) {
this.logout()
throw new Error('User not found')
}
2024-06-17 15:25:52 +02:00
this.currentUser = data.id
this.configUser = data
2023-01-11 10:54:25 +01:00
})
},
async setUserConfig(obj: any) {
2023-05-15 14:08:40 +02:00
const authStore = useAuth()
2023-01-11 10:54:25 +01:00
const update = await fetch(`/api/user/${obj.id}`, {
2023-01-11 10:54:25 +01:00
method: 'PUT',
2023-09-26 09:29:46 +02:00
headers: { ...this.contentType, ...authStore.authHeader },
2023-01-11 10:54:25 +01:00
body: JSON.stringify(obj),
})
return update
},
2023-09-26 09:29:46 +02:00
async addNewUser(user: User) {
const authStore = useAuth()
delete user.confirm
const update = await fetch('/api/user/', {
method: 'Post',
headers: { ...this.contentType, ...authStore.authHeader },
body: JSON.stringify(user),
})
return update
},
2023-01-11 10:54:25 +01:00
},
})