2022-08-10 02:53:39 -05:00
|
|
|
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'
|
2022-08-10 04:51:13 -05:00
|
|
|
import { VideoChannelSyncModel } from '@server/models/video/video-channel-sync'
|
|
|
|
import { MChannelSync } from '@server/types/models'
|
2022-09-16 02:37:07 -05:00
|
|
|
import { VideoChannelImportPayload, VideoChannelSyncState } from '@shared/models'
|
2022-08-10 02:53:39 -05:00
|
|
|
|
|
|
|
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) {
|
2022-08-10 04:51:13 -05:00
|
|
|
throw new Error('Cannot import channel as the HTTP upload is disabled')
|
2022-08-10 02:53:39 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!CONFIG.IMPORT.VIDEO_CHANNEL_SYNCHRONIZATION.ENABLED) {
|
2022-08-10 04:51:13 -05:00
|
|
|
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')
|
|
|
|
}
|
2022-08-10 02:53:39 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
2022-08-10 04:51:13 -05:00
|
|
|
externalChannelUrl: payload.externalChannelUrl,
|
|
|
|
channelSync
|
2022-08-10 02:53:39 -05:00
|
|
|
})
|
|
|
|
} catch (err) {
|
|
|
|
logger.error(`Failed to import channel ${videoChannel.name}`, { err })
|
2022-09-16 02:37:07 -05:00
|
|
|
channelSync.state = VideoChannelSyncState.FAILED
|
|
|
|
await channelSync.save()
|
2022-08-10 02:53:39 -05:00
|
|
|
}
|
|
|
|
}
|