1 import { VideoChannelObject } from '../../../shared/models/activitypub/objects'
2 import { doRequest, logger } from '../../helpers'
3 import { isVideoChannelObjectValid } from '../../helpers/custom-validators/activitypub'
4 import { ACTIVITY_PUB } from '../../initializers'
5 import { AccountModel } from '../../models/account/account'
6 import { VideoChannelModel } from '../../models/video/video-channel'
7 import { videoChannelActivityObjectToDBAttributes } from './process/misc'
9 async function getOrCreateVideoChannel (ownerAccount: AccountModel, videoChannelUrl: string) {
10 let videoChannel = await VideoChannelModel.loadByUrl(videoChannelUrl)
12 // We don't have this account in our database, fetch it on remote
14 videoChannel = await fetchRemoteVideoChannel(ownerAccount, videoChannelUrl)
15 if (videoChannel === undefined) throw new Error('Cannot fetch remote video channel.')
17 // Save our new video channel in database
18 await videoChannel.save()
24 async function fetchRemoteVideoChannel (ownerAccount: AccountModel, videoChannelUrl: string) {
29 'Accept': ACTIVITY_PUB.ACCEPT_HEADER
33 logger.info('Fetching remote video channel %s.', videoChannelUrl)
37 requestResult = await doRequest(options)
39 logger.warn('Cannot fetch remote video channel %s.', videoChannelUrl, err)
43 const videoChannelJSON: VideoChannelObject = JSON.parse(requestResult.body)
44 if (isVideoChannelObjectValid(videoChannelJSON) === false) {
45 logger.debug('Remote video channel JSON is not valid.', { videoChannelJSON })
49 const videoChannelAttributes = videoChannelActivityObjectToDBAttributes(videoChannelJSON, ownerAccount)
50 const videoChannel = new VideoChannelModel(videoChannelAttributes)
51 videoChannel.Account = ownerAccount
57 getOrCreateVideoChannel,
58 fetchRemoteVideoChannel