2017-10-24 12:41:09 -05:00
|
|
|
import * as Sequelize from 'sequelize'
|
|
|
|
|
|
|
|
import { database as db } from '../initializers'
|
2017-10-25 04:55:06 -05:00
|
|
|
import { logger } from '../helpers'
|
2017-11-09 10:51:58 -06:00
|
|
|
import { AccountInstance } from '../models'
|
2017-10-24 12:41:09 -05:00
|
|
|
import { VideoChannelCreate } from '../../shared/models'
|
2017-11-10 10:27:49 -06:00
|
|
|
import { sendCreateVideoChannel } from './activitypub/send-request'
|
2017-11-14 03:57:56 -06:00
|
|
|
import { getActivityPubUrl } from '../helpers/activitypub'
|
2017-10-24 12:41:09 -05:00
|
|
|
|
2017-11-09 10:51:58 -06:00
|
|
|
async function createVideoChannel (videoChannelInfo: VideoChannelCreate, account: AccountInstance, t: Sequelize.Transaction) {
|
2017-10-24 12:41:09 -05:00
|
|
|
const videoChannelData = {
|
|
|
|
name: videoChannelInfo.name,
|
|
|
|
description: videoChannelInfo.description,
|
|
|
|
remote: false,
|
2017-11-10 07:48:08 -06:00
|
|
|
accountId: account.id
|
2017-10-24 12:41:09 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
const videoChannel = db.VideoChannel.build(videoChannelData)
|
2017-11-14 03:57:56 -06:00
|
|
|
videoChannel.set('url', getActivityPubUrl('videoChannel', videoChannel.uuid))
|
|
|
|
|
2017-10-24 12:41:09 -05:00
|
|
|
const options = { transaction: t }
|
|
|
|
|
2017-10-25 04:55:06 -05:00
|
|
|
const videoChannelCreated = await videoChannel.save(options)
|
|
|
|
|
2017-11-09 10:51:58 -06:00
|
|
|
// Do not forget to add Account information to the created video channel
|
|
|
|
videoChannelCreated.Account = account
|
2017-10-25 04:55:06 -05:00
|
|
|
|
2017-11-10 10:27:49 -06:00
|
|
|
sendCreateVideoChannel(videoChannelCreated, t)
|
2017-10-25 04:55:06 -05:00
|
|
|
|
|
|
|
return videoChannelCreated
|
|
|
|
}
|
|
|
|
|
2017-11-15 04:00:25 -06:00
|
|
|
async function fetchVideoChannelByHostAndUUID (serverHost: string, uuid: string, t: Sequelize.Transaction) {
|
2017-10-25 04:55:06 -05:00
|
|
|
try {
|
2017-11-15 04:00:25 -06:00
|
|
|
const videoChannel = await db.VideoChannel.loadByHostAndUUID(serverHost, uuid, t)
|
2017-10-25 04:55:06 -05:00
|
|
|
if (!videoChannel) throw new Error('Video channel not found')
|
|
|
|
|
|
|
|
return videoChannel
|
|
|
|
} catch (err) {
|
2017-11-15 04:00:25 -06:00
|
|
|
logger.error('Cannot load video channel from host and uuid.', { error: err.stack, serverHost, uuid })
|
2017-10-25 04:55:06 -05:00
|
|
|
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
|
|
|
}
|