PeerTube/server/lib/activitypub/playlists/get.ts

36 lines
1.3 KiB
TypeScript
Raw Normal View History

2021-06-17 16:02:38 +02:00
import { getAPId } from '@server/helpers/activitypub'
import { VideoPlaylistModel } from '@server/models/video/video-playlist'
import { MVideoPlaylistFullSummary } from '@server/types/models'
import { APObject } from '@shared/models'
import { createOrUpdateVideoPlaylist } from './create-update'
import { scheduleRefreshIfNeeded } from './refresh'
import { fetchRemoteVideoPlaylist } from './shared'
async function getOrCreateAPVideoPlaylist (playlistObjectArg: APObject): Promise<MVideoPlaylistFullSummary> {
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)
// playlistUrl is just an alias/redirection, so process object id instead
2021-06-17 16:02:38 +02:00
if (playlistObject.id !== playlistUrl) return getOrCreateAPVideoPlaylist(playlistObject)
const playlistCreated = await createOrUpdateVideoPlaylist(playlistObject)
return playlistCreated
}
// ---------------------------------------------------------------------------
export {
getOrCreateAPVideoPlaylist
}