2024-09-13 06:56:04 +00:00
|
|
|
import { getCookie, setCookie } from '../utils/cookies'
|
2023-07-07 16:38:53 +00:00
|
|
|
import axios from '@nextcloud/axios'
|
2024-08-09 09:38:00 +00:00
|
|
|
import { defineStore } from 'pinia'
|
2023-07-07 16:38:53 +00:00
|
|
|
import { generateUrl } from '@nextcloud/router'
|
|
|
|
|
2024-08-09 09:38:00 +00:00
|
|
|
export const useSubscriptions = defineStore('subscriptions', {
|
|
|
|
state: () => ({
|
2024-08-17 15:56:12 +00:00
|
|
|
subs: [],
|
2024-08-22 15:35:05 +00:00
|
|
|
favs: [],
|
2024-08-09 09:38:00 +00:00
|
|
|
}),
|
2024-09-02 08:51:48 +00:00
|
|
|
getters: {
|
|
|
|
getSubscriptions: (state) => {
|
|
|
|
return state.subs
|
|
|
|
},
|
|
|
|
getFavorites: (state) => {
|
|
|
|
return state.favs
|
|
|
|
.filter((fav) => state.subs.includes(fav.url))
|
|
|
|
.sort((fav) => fav.lastPub)
|
|
|
|
},
|
|
|
|
},
|
2023-07-07 16:38:53 +00:00
|
|
|
actions: {
|
2024-08-09 20:51:32 +00:00
|
|
|
async fetch() {
|
2024-04-29 22:48:47 +00:00
|
|
|
const metrics = await axios.get(
|
|
|
|
generateUrl('/apps/gpoddersync/personal_settings/metrics'),
|
|
|
|
)
|
|
|
|
const subs = [...metrics.data.subscriptions].sort(
|
|
|
|
(a, b) => b.listenedSeconds - a.listenedSeconds,
|
|
|
|
)
|
2024-08-17 15:56:12 +00:00
|
|
|
this.subs = subs.map((sub) => sub.url)
|
|
|
|
|
|
|
|
try {
|
2024-08-22 15:35:05 +00:00
|
|
|
const favs = JSON.parse(getCookie('repod.favorites')) || []
|
|
|
|
this.favs = favs.map((url) => ({ url }))
|
2024-08-17 15:56:12 +00:00
|
|
|
} catch {}
|
|
|
|
},
|
|
|
|
addFavorite(url) {
|
2024-08-22 15:35:05 +00:00
|
|
|
this.favs.push({ url })
|
|
|
|
setCookie(
|
|
|
|
'repod.favorites',
|
|
|
|
JSON.stringify(this.favs.map((fav) => fav.url)),
|
|
|
|
365,
|
|
|
|
)
|
|
|
|
},
|
|
|
|
editFavoriteData(url, data) {
|
|
|
|
this.favs = this.favs.map((fav) =>
|
|
|
|
fav.url === url ? { ...fav, ...data } : fav,
|
|
|
|
)
|
2024-08-17 15:56:12 +00:00
|
|
|
},
|
|
|
|
removeFavorite(url) {
|
2024-08-22 15:35:05 +00:00
|
|
|
this.favs = this.favs.filter((fav) => fav.url !== url)
|
|
|
|
setCookie(
|
|
|
|
'repod.favorites',
|
|
|
|
JSON.stringify(this.favs.map((fav) => fav.url)),
|
|
|
|
365,
|
|
|
|
)
|
2023-07-07 16:38:53 +00:00
|
|
|
},
|
|
|
|
},
|
2024-08-09 09:38:00 +00:00
|
|
|
})
|