2017-10-24 12:41:09 -05:00
|
|
|
import * as Sequelize from 'sequelize'
|
|
|
|
|
|
|
|
import { addVideoChannelToFriends } from './friends'
|
|
|
|
import { database as db } from '../initializers'
|
2017-10-25 04:55:06 -05:00
|
|
|
import { logger } from '../helpers'
|
2017-10-24 12:41:09 -05:00
|
|
|
import { AuthorInstance } from '../models'
|
|
|
|
import { VideoChannelCreate } from '../../shared/models'
|
|
|
|
|
2017-10-25 04:55:06 -05:00
|
|
|
async function createVideoChannel (videoChannelInfo: VideoChannelCreate, author: AuthorInstance, t: Sequelize.Transaction) {
|
2017-10-24 12:41:09 -05:00
|
|
|
const videoChannelData = {
|
|
|
|
name: videoChannelInfo.name,
|
|
|
|
description: videoChannelInfo.description,
|
|
|
|
remote: false,
|
|
|
|
authorId: author.id
|
|
|
|
}
|
|
|
|
|
|
|
|
const videoChannel = db.VideoChannel.build(videoChannelData)
|
|
|
|
const options = { transaction: t }
|
|
|
|
|
2017-10-25 04:55:06 -05:00
|
|
|
const videoChannelCreated = await videoChannel.save(options)
|
|
|
|
|
|
|
|
// Do not forget to add Author information to the created video channel
|
|
|
|
videoChannelCreated.Author = author
|
|
|
|
|
|
|
|
const remoteVideoChannel = videoChannelCreated.toAddRemoteJSON()
|
|
|
|
|
|
|
|
// Now we'll add the video channel's meta data to our friends
|
|
|
|
await addVideoChannelToFriends(remoteVideoChannel, t)
|
|
|
|
|
|
|
|
return videoChannelCreated
|
|
|
|
}
|
|
|
|
|
|
|
|
async function fetchVideoChannelByHostAndUUID (podHost: string, uuid: string, t: Sequelize.Transaction) {
|
|
|
|
try {
|
|
|
|
const videoChannel = await db.VideoChannel.loadByHostAndUUID(podHost, uuid, t)
|
|
|
|
if (!videoChannel) throw new Error('Video channel not found')
|
|
|
|
|
|
|
|
return videoChannel
|
|
|
|
} catch (err) {
|
|
|
|
logger.error('Cannot load video channel from host and uuid.', { error: err.stack, podHost, uuid })
|
|
|
|
throw err
|
|
|
|
}
|
2017-10-24 12:41:09 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
export {
|
2017-10-25 04:55:06 -05:00
|
|
|
createVideoChannel,
|
|
|
|
fetchVideoChannelByHostAndUUID
|
2017-10-24 12:41:09 -05:00
|
|
|
}
|