aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/lib/job-queue/handlers/video-channel-import.ts
blob: 600292844ba7f79f17c289ca0ae0a05d0b995c45 (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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import { Job } from 'bullmq'
import { logger } from '@server/helpers/logger'
import { CONFIG } from '@server/initializers/config'
import { synchronizeChannel } from '@server/lib/sync-channel'
import { VideoChannelModel } from '@server/models/video/video-channel'
import { VideoChannelSyncModel } from '@server/models/video/video-channel-sync'
import { MChannelSync } from '@server/types/models'
import { VideoChannelImportPayload, VideoChannelSyncState } from '@shared/models'

export async function processVideoChannelImport (job: Job) {
  const payload = job.data as VideoChannelImportPayload

  logger.info('Processing video channel import in job %s.', job.id)

  // Channel import requires only http upload to be allowed
  if (!CONFIG.IMPORT.VIDEOS.HTTP.ENABLED) {
    throw new Error('Cannot import channel as the HTTP upload is disabled')
  }

  if (!CONFIG.IMPORT.VIDEO_CHANNEL_SYNCHRONIZATION.ENABLED) {
    throw new Error('Cannot import channel as the synchronization is disabled')
  }

  let channelSync: MChannelSync
  if (payload.partOfChannelSyncId) {
    channelSync = await VideoChannelSyncModel.loadWithChannel(payload.partOfChannelSyncId)

    if (!channelSync) {
      throw new Error('Unlnown channel sync specified in videos channel import')
    }
  }

  const videoChannel = await VideoChannelModel.loadAndPopulateAccount(payload.videoChannelId)

  try {
    logger.info(`Starting importing videos from external channel "${payload.externalChannelUrl}" to "${videoChannel.name}" `)

    await synchronizeChannel({
      channel: videoChannel,
      externalChannelUrl: payload.externalChannelUrl,
      channelSync
    })
  } catch (err) {
    logger.error(`Failed to import channel ${videoChannel.name}`, { err })
    channelSync.state = VideoChannelSyncState.FAILED
    await channelSync.save()
  }
}