1 import { isArray } from 'lodash'
2 import { checkUrlsSameHost } from '@server/helpers/activitypub'
3 import { isPlaylistElementObjectValid, isPlaylistObjectValid } from '@server/helpers/custom-validators/activitypub/playlist'
4 import { logger, loggerTagsFactory } from '@server/helpers/logger'
5 import { doJSONRequest } from '@server/helpers/requests'
6 import { PlaylistElementObject, PlaylistObject } from '@shared/models'
8 async function fetchRemoteVideoPlaylist (playlistUrl: string): Promise<{ statusCode: number, playlistObject: PlaylistObject }> {
9 const lTags = loggerTagsFactory('ap', 'video-playlist', playlistUrl)
11 logger.info('Fetching remote playlist %s.', playlistUrl, lTags())
13 const { body, statusCode } = await doJSONRequest<any>(playlistUrl, { activityPub: true })
15 if (isPlaylistObjectValid(body) === false || checkUrlsSameHost(body.id, playlistUrl) !== true) {
16 logger.debug('Remote video playlist JSON is not valid.', { body, ...lTags() })
17 return { statusCode, playlistObject: undefined }
20 if (!isArray(body.to)) {
21 logger.debug('Remote video playlist JSON does not have a valid audience.', { body, ...lTags() })
22 return { statusCode, playlistObject: undefined }
25 return { statusCode, playlistObject: body }
28 async function fetchRemotePlaylistElement (elementUrl: string): Promise<{ statusCode: number, elementObject: PlaylistElementObject }> {
29 const lTags = loggerTagsFactory('ap', 'video-playlist', 'element', elementUrl)
31 logger.debug('Fetching remote playlist element %s.', elementUrl, lTags())
33 const { body, statusCode } = await doJSONRequest<PlaylistElementObject>(elementUrl, { activityPub: true })
35 if (!isPlaylistElementObjectValid(body)) throw new Error(`Invalid body in fetch playlist element ${elementUrl}`)
37 if (checkUrlsSameHost(body.id, elementUrl) !== true) {
38 throw new Error(`Playlist element url ${elementUrl} host is different from the AP object id ${body.id}`)
41 return { statusCode, elementObject: body }
45 fetchRemoteVideoPlaylist,
46 fetchRemotePlaylistElement