2021-06-17 09:02:38 -05:00
|
|
|
import { VideoPlaylistModel } from '@server/models/video/video-playlist'
|
|
|
|
import { MVideoPlaylistFullSummary } from '@server/types/models'
|
2023-06-05 08:51:16 -05:00
|
|
|
import { APObjectId } from '@shared/models'
|
2022-03-23 08:24:50 -05:00
|
|
|
import { getAPId } from '../activity'
|
2021-06-17 09:02:38 -05:00
|
|
|
import { createOrUpdateVideoPlaylist } from './create-update'
|
|
|
|
import { scheduleRefreshIfNeeded } from './refresh'
|
|
|
|
import { fetchRemoteVideoPlaylist } from './shared'
|
|
|
|
|
2023-06-05 08:51:16 -05:00
|
|
|
async function getOrCreateAPVideoPlaylist (playlistObjectArg: APObjectId): Promise<MVideoPlaylistFullSummary> {
|
2021-06-17 09:02:38 -05:00
|
|
|
const playlistUrl = getAPId(playlistObjectArg)
|
|
|
|
|
|
|
|
const playlistFromDatabase = await VideoPlaylistModel.loadByUrlWithAccountAndChannelSummary(playlistUrl)
|
|
|
|
|
|
|
|
if (playlistFromDatabase) {
|
|
|
|
scheduleRefreshIfNeeded(playlistFromDatabase)
|
|
|
|
|
|
|
|
return playlistFromDatabase
|
|
|
|
}
|
|
|
|
|
|
|
|
const { playlistObject } = await fetchRemoteVideoPlaylist(playlistUrl)
|
|
|
|
if (!playlistObject) throw new Error('Cannot fetch remote playlist with url: ' + playlistUrl)
|
|
|
|
|
2021-10-20 08:01:17 -05:00
|
|
|
// playlistUrl is just an alias/redirection, so process object id instead
|
2021-06-17 09:02:38 -05:00
|
|
|
if (playlistObject.id !== playlistUrl) return getOrCreateAPVideoPlaylist(playlistObject)
|
|
|
|
|
|
|
|
const playlistCreated = await createOrUpdateVideoPlaylist(playlistObject)
|
|
|
|
|
|
|
|
return playlistCreated
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
export {
|
|
|
|
getOrCreateAPVideoPlaylist
|
|
|
|
}
|