]>
Commit | Line | Data |
---|---|---|
1 | import { VideoPlaylistModel } from '@server/models/video/video-playlist' | |
2 | import { MVideoPlaylistFullSummary } from '@server/types/models' | |
3 | import { APObject } from '@shared/models' | |
4 | import { getAPId } from '../activity' | |
5 | import { createOrUpdateVideoPlaylist } from './create-update' | |
6 | import { scheduleRefreshIfNeeded } from './refresh' | |
7 | import { fetchRemoteVideoPlaylist } from './shared' | |
8 | ||
9 | async function getOrCreateAPVideoPlaylist (playlistObjectArg: APObject): Promise<MVideoPlaylistFullSummary> { | |
10 | const playlistUrl = getAPId(playlistObjectArg) | |
11 | ||
12 | const playlistFromDatabase = await VideoPlaylistModel.loadByUrlWithAccountAndChannelSummary(playlistUrl) | |
13 | ||
14 | if (playlistFromDatabase) { | |
15 | scheduleRefreshIfNeeded(playlistFromDatabase) | |
16 | ||
17 | return playlistFromDatabase | |
18 | } | |
19 | ||
20 | const { playlistObject } = await fetchRemoteVideoPlaylist(playlistUrl) | |
21 | if (!playlistObject) throw new Error('Cannot fetch remote playlist with url: ' + playlistUrl) | |
22 | ||
23 | // playlistUrl is just an alias/redirection, so process object id instead | |
24 | if (playlistObject.id !== playlistUrl) return getOrCreateAPVideoPlaylist(playlistObject) | |
25 | ||
26 | const playlistCreated = await createOrUpdateVideoPlaylist(playlistObject) | |
27 | ||
28 | return playlistCreated | |
29 | } | |
30 | ||
31 | // --------------------------------------------------------------------------- | |
32 | ||
33 | export { | |
34 | getOrCreateAPVideoPlaylist | |
35 | } |