]>
Commit | Line | Data |
---|---|---|
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' | |
7 | ||
8 | async function fetchRemoteVideoPlaylist (playlistUrl: string): Promise<{ statusCode: number, playlistObject: PlaylistObject }> { | |
9 | const lTags = loggerTagsFactory('ap', 'video-playlist', playlistUrl) | |
10 | ||
11 | logger.info('Fetching remote playlist %s.', playlistUrl, lTags()) | |
12 | ||
13 | const { body, statusCode } = await doJSONRequest<any>(playlistUrl, { activityPub: true }) | |
14 | ||
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 } | |
18 | } | |
19 | ||
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 } | |
23 | } | |
24 | ||
25 | return { statusCode, playlistObject: body } | |
26 | } | |
27 | ||
28 | async function fetchRemotePlaylistElement (elementUrl: string): Promise<{ statusCode: number, elementObject: PlaylistElementObject }> { | |
29 | const lTags = loggerTagsFactory('ap', 'video-playlist', 'element', elementUrl) | |
30 | ||
31 | logger.debug('Fetching remote playlist element %s.', elementUrl, lTags()) | |
32 | ||
33 | const { body, statusCode } = await doJSONRequest<PlaylistElementObject>(elementUrl, { activityPub: true }) | |
34 | ||
35 | if (!isPlaylistElementObjectValid(body)) throw new Error(`Invalid body in fetch playlist element ${elementUrl}`) | |
36 | ||
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}`) | |
39 | } | |
40 | ||
41 | return { statusCode, elementObject: body } | |
42 | } | |
43 | ||
44 | export { | |
45 | fetchRemoteVideoPlaylist, | |
46 | fetchRemotePlaylistElement | |
47 | } |