aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/lib/activitypub/videos/shared/url-to-object.ts
blob: 5b7007530f274bd3202fc1fb3bdc42793995ff1c (plain) (blame)
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
import { sanitizeAndCheckVideoTorrentObject } from '@server/helpers/custom-validators/activitypub/videos'
import { logger, loggerTagsFactory } from '@server/helpers/logger'
import { doJSONRequest } from '@server/helpers/requests'
import { VideoObject } from '@shared/models'
import { checkUrlsSameHost } from '../../url'

const lTags = loggerTagsFactory('ap', 'video')

async function fetchRemoteVideo (videoUrl: string): Promise<{ statusCode: number, videoObject: VideoObject }> {
  logger.info('Fetching remote video %s.', videoUrl, lTags(videoUrl))

  const { statusCode, body } = await doJSONRequest<any>(videoUrl, { activityPub: true })

  if (sanitizeAndCheckVideoTorrentObject(body) === false || checkUrlsSameHost(body.id, videoUrl) !== true) {
    logger.debug('Remote video JSON is not valid.', { body, ...lTags(videoUrl) })

    return { statusCode, videoObject: undefined }
  }

  return { statusCode, videoObject: body }
}

export {
  fetchRemoteVideo
}