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
|
import { isArray } from 'lodash'
import { isPlaylistElementObjectValid, isPlaylistObjectValid } from '@server/helpers/custom-validators/activitypub/playlist'
import { logger, loggerTagsFactory } from '@server/helpers/logger'
import { doJSONRequest } from '@server/helpers/requests'
import { PlaylistElementObject, PlaylistObject } from '@shared/models'
import { checkUrlsSameHost } from '../../url'
async function fetchRemoteVideoPlaylist (playlistUrl: string): Promise<{ statusCode: number, playlistObject: PlaylistObject }> {
const lTags = loggerTagsFactory('ap', 'video-playlist', playlistUrl)
logger.info('Fetching remote playlist %s.', playlistUrl, lTags())
const { body, statusCode } = await doJSONRequest<any>(playlistUrl, { activityPub: true })
if (isPlaylistObjectValid(body) === false || checkUrlsSameHost(body.id, playlistUrl) !== true) {
logger.debug('Remote video playlist JSON is not valid.', { body, ...lTags() })
return { statusCode, playlistObject: undefined }
}
if (!isArray(body.to)) {
logger.debug('Remote video playlist JSON does not have a valid audience.', { body, ...lTags() })
return { statusCode, playlistObject: undefined }
}
return { statusCode, playlistObject: body }
}
async function fetchRemotePlaylistElement (elementUrl: string): Promise<{ statusCode: number, elementObject: PlaylistElementObject }> {
const lTags = loggerTagsFactory('ap', 'video-playlist', 'element', elementUrl)
logger.debug('Fetching remote playlist element %s.', elementUrl, lTags())
const { body, statusCode } = await doJSONRequest<PlaylistElementObject>(elementUrl, { activityPub: true })
if (!isPlaylistElementObjectValid(body)) throw new Error(`Invalid body in fetch playlist element ${elementUrl}`)
if (checkUrlsSameHost(body.id, elementUrl) !== true) {
throw new Error(`Playlist element url ${elementUrl} host is different from the AP object id ${body.id}`)
}
return { statusCode, elementObject: body }
}
export {
fetchRemoteVideoPlaylist,
fetchRemotePlaylistElement
}
|