/
podcast.ts
145 lines (124 loc) · 4.62 KB
/
podcast.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import { checkIfContainsStringMatch } from 'podverse-shared'
import { getGlobal, setGlobal } from 'reactn'
import { safelyUnwrapNestedVariable } from '../../lib/utility'
import { PV } from '../../resources'
import PVEventEmitter from '../../services/eventEmitter'
import {
removeAddByRSSPodcast as removeAddByRSSPodcastService
} from '../../services/parser'
import {
combineWithAddByRSSPodcasts as combineWithAddByRSSPodcastsService,
getPodcast as getPodcastService,
getSubscribedPodcasts as getSubscribedPodcastsService,
insertOrRemovePodcastFromAlphabetizedArray,
setSubscribedPodcasts,
toggleSubscribeToPodcast as toggleSubscribeToPodcastService
} from '../../services/podcast'
import { updateDownloadedPodcasts } from './downloads'
const handleCombineWithAddByRSSPodcasts = async (searchTitle?: string, sort?: string | null) => {
const { appMode } = getGlobal()
const videoOnlyMode = appMode === PV.AppMode.videos
const combinedPodcasts = await combineWithAddByRSSPodcastsService(sort)
let finalPodcasts = []
if (searchTitle) {
finalPodcasts = combinedPodcasts.filter((podcast) => checkIfContainsStringMatch(searchTitle, podcast.title))
} else {
finalPodcasts = combinedPodcasts
}
if (videoOnlyMode) {
finalPodcasts = finalPodcasts.filter((podcast) => podcast.hasVideo)
}
return finalPodcasts
}
export const findCombineWithAddByRSSPodcasts = async (
searchTitle?: string
) => {
return handleCombineWithAddByRSSPodcasts(searchTitle)
}
export const combineWithAddByRSSPodcasts = async (
searchTitle?: string,
sort?: string | null
) => {
const finalPodcasts = await handleCombineWithAddByRSSPodcasts(searchTitle, sort)
setGlobal({
subscribedPodcasts: finalPodcasts,
subscribedPodcastsTotalCount: finalPodcasts?.length
})
}
export const getSubscribedPodcasts = async (sort?: string | null) => {
const globalState = getGlobal()
const subscribedPodcastIds = globalState.session.userInfo.subscribedPodcastIds || []
const data = await getSubscribedPodcastsService(subscribedPodcastIds, sort)
const subscribedPodcasts = data[0] || []
const subscribedPodcastsTotalCount = data[1] || 0
setGlobal({
subscribedPodcasts,
subscribedPodcastsTotalCount
})
return subscribedPodcasts
}
export const toggleSubscribeToPodcast = async (id: string) => {
return new Promise<void>((resolve, reject) => {
(async () => {
try {
const subscribedPodcastIds = await toggleSubscribeToPodcastService(id)
const subscribedPodcast = await getPodcastService(id)
const globalState = getGlobal()
let { subscribedPodcasts = [] } = globalState
subscribedPodcasts = insertOrRemovePodcastFromAlphabetizedArray(subscribedPodcasts, subscribedPodcast) as any
await setSubscribedPodcasts(subscribedPodcasts)
const subscribedPodcastsTotalCount = subscribedPodcasts ? subscribedPodcasts.length : 0
setGlobal(
{
session: {
...globalState.session,
userInfo: {
...globalState.session.userInfo,
subscribedPodcastIds
}
},
subscribedPodcasts,
subscribedPodcastsTotalCount
},
async () => {
await updateDownloadedPodcasts()
PVEventEmitter.emit(PV.Events.PODCAST_SUBSCRIBE_TOGGLED)
resolve()
}
)
} catch (error) {
console.log('toggleSubscribeToPodcast action', error)
reject()
}
})()
})
}
export const removeAddByRSSPodcast = async (feedUrl: string) => {
await removeAddByRSSPodcastService(feedUrl)
const globalState = getGlobal()
const { subscribedPodcasts = [] } = globalState
const filteredPodcasts = subscribedPodcasts.filter((x: any) => {
return !x.addByRSSPodcastFeedUrl || x.addByRSSPodcastFeedUrl !== feedUrl
})
await setSubscribedPodcasts(filteredPodcasts)
setGlobal({
subscribedPodcasts: filteredPodcasts,
subscribedPodcastsTotalCount: filteredPodcasts.length
})
PVEventEmitter.emit(PV.Events.PODCAST_SUBSCRIBE_TOGGLED)
}
export const checkIfSubscribedToPodcast = (
subscribedPodcastIds: string[],
podcastId?: string,
addByRSSPodcastFeedUrl?: string
) => {
const globalState = getGlobal()
let isSubscribed = subscribedPodcastIds.some((x: string) => podcastId && podcastId === x)
if (!isSubscribed && addByRSSPodcastFeedUrl) {
const subscribedPodcasts = safelyUnwrapNestedVariable(() => globalState.subscribedPodcasts, [])
isSubscribed = subscribedPodcasts.some(
(x: any) => x.addByRSSPodcastFeedUrl && x.addByRSSPodcastFeedUrl === addByRSSPodcastFeedUrl
)
}
return isSubscribed
}