2024-09-13 10:35:08 +00:00
|
|
|
import type {
|
2024-09-13 14:33:48 +00:00
|
|
|
PersonalSettingsMetricsInterface,
|
2024-09-13 10:35:08 +00:00
|
|
|
PodcastDataInterface,
|
|
|
|
SubscriptionInterface,
|
|
|
|
} from '../utils/types'
|
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-09-13 10:35:08 +00:00
|
|
|
subs: [] as SubscriptionInterface[],
|
2024-08-09 09:38:00 +00:00
|
|
|
}),
|
2024-09-02 08:51:48 +00:00
|
|
|
getters: {
|
2024-09-13 10:35:08 +00:00
|
|
|
getSubByUrl: (state) => (url: string) =>
|
|
|
|
state.subs.find((sub) => sub.metrics.url === url),
|
2024-09-02 08:51:48 +00:00
|
|
|
},
|
2023-07-07 16:38:53 +00:00
|
|
|
actions: {
|
2024-08-09 20:51:32 +00:00
|
|
|
async fetch() {
|
2024-09-13 10:35:08 +00:00
|
|
|
let favorites: string[] = []
|
2024-08-17 15:56:12 +00:00
|
|
|
try {
|
2024-09-13 10:35:08 +00:00
|
|
|
favorites = JSON.parse(getCookie('repod.favorites') || '[]') || []
|
2024-08-17 15:56:12 +00:00
|
|
|
} catch {}
|
2024-09-13 10:35:08 +00:00
|
|
|
|
2024-09-13 14:33:48 +00:00
|
|
|
const metrics = await axios.get<PersonalSettingsMetricsInterface>(
|
2024-09-13 10:35:08 +00:00
|
|
|
generateUrl('/apps/gpoddersync/personal_settings/metrics'),
|
2024-08-22 15:35:05 +00:00
|
|
|
)
|
2024-09-13 10:35:08 +00:00
|
|
|
this.subs = [...metrics.data.subscriptions]
|
|
|
|
.sort((a, b) => b.listenedSeconds - a.listenedSeconds)
|
|
|
|
.map((sub) => ({
|
|
|
|
metrics: sub,
|
|
|
|
isFavorite: favorites.includes(sub.url),
|
|
|
|
data: this.subs.find((s) => s.metrics.url === sub.url)?.data,
|
|
|
|
}))
|
2024-08-22 15:35:05 +00:00
|
|
|
},
|
2024-09-13 10:35:08 +00:00
|
|
|
addFavoriteData(data: PodcastDataInterface) {
|
|
|
|
this.subs = this.subs.map((sub) =>
|
|
|
|
sub.metrics.url === data.link ? { ...sub, data } : sub,
|
2024-08-22 15:35:05 +00:00
|
|
|
)
|
2024-08-17 15:56:12 +00:00
|
|
|
},
|
2024-09-13 10:35:08 +00:00
|
|
|
setFavorite(link: string, isFavorite: boolean) {
|
|
|
|
this.subs.map((sub) =>
|
|
|
|
sub.metrics.url === link ? { ...sub, isFavorite } : sub,
|
|
|
|
)
|
|
|
|
|
2024-08-22 15:35:05 +00:00
|
|
|
setCookie(
|
|
|
|
'repod.favorites',
|
2024-09-13 10:35:08 +00:00
|
|
|
JSON.stringify(
|
|
|
|
this.subs
|
|
|
|
.filter((sub) => sub.isFavorite)
|
|
|
|
.map((sub) => sub.metrics.url),
|
|
|
|
),
|
2024-08-22 15:35:05 +00:00
|
|
|
365,
|
|
|
|
)
|
2023-07-07 16:38:53 +00:00
|
|
|
},
|
|
|
|
},
|
2024-08-09 09:38:00 +00:00
|
|
|
})
|