2017-10-24 12:41:09 -05:00
|
|
|
import * as Sequelize from 'sequelize'
|
2017-12-14 10:38:41 -06:00
|
|
|
import * as uuidv4 from 'uuid/v4'
|
2017-11-17 08:52:26 -06:00
|
|
|
import { VideoChannelCreate } from '../../shared/models'
|
2017-12-12 10:53:50 -06:00
|
|
|
import { VideoChannelModel } from '../models/video/video-channel'
|
2019-05-31 09:30:11 -05:00
|
|
|
import { buildActorInstance, federateVideoIfNeeded, getVideoChannelActivityPubUrl } from './activitypub'
|
|
|
|
import { VideoModel } from '../models/video/video'
|
2019-08-20 12:05:31 -05:00
|
|
|
import { MAccountId, MChannelDefault, MChannelId } from '../typings/models'
|
2017-10-24 12:41:09 -05:00
|
|
|
|
2019-08-20 12:05:31 -05:00
|
|
|
type CustomVideoChannelModelAccount <T extends MAccountId> = MChannelDefault &
|
2019-08-15 04:53:26 -05:00
|
|
|
{ Account?: T }
|
|
|
|
|
2019-08-20 12:05:31 -05:00
|
|
|
async function createLocalVideoChannel <T extends MAccountId> (
|
2019-08-15 04:53:26 -05:00
|
|
|
videoChannelInfo: VideoChannelCreate,
|
|
|
|
account: T,
|
|
|
|
t: Sequelize.Transaction
|
|
|
|
): Promise<CustomVideoChannelModelAccount<T>> {
|
2017-12-14 10:38:41 -06:00
|
|
|
const uuid = uuidv4()
|
2018-08-17 08:45:42 -05:00
|
|
|
const url = getVideoChannelActivityPubUrl(videoChannelInfo.name)
|
|
|
|
const actorInstance = buildActorInstance('Group', url, videoChannelInfo.name, uuid)
|
2017-12-14 10:38:41 -06:00
|
|
|
|
|
|
|
const actorInstanceCreated = await actorInstance.save({ transaction: t })
|
|
|
|
|
2017-10-24 12:41:09 -05:00
|
|
|
const videoChannelData = {
|
2018-04-26 09:11:38 -05:00
|
|
|
name: videoChannelInfo.displayName,
|
2017-10-24 12:41:09 -05:00
|
|
|
description: videoChannelInfo.description,
|
2018-02-15 07:46:26 -06:00
|
|
|
support: videoChannelInfo.support,
|
2017-12-14 10:38:41 -06:00
|
|
|
accountId: account.id,
|
|
|
|
actorId: actorInstanceCreated.id
|
2017-10-24 12:41:09 -05:00
|
|
|
}
|
|
|
|
|
2019-08-15 04:53:26 -05:00
|
|
|
const videoChannel = new VideoChannelModel(videoChannelData)
|
2017-11-14 03:57:56 -06:00
|
|
|
|
2017-10-24 12:41:09 -05:00
|
|
|
const options = { transaction: t }
|
2019-08-20 12:05:31 -05:00
|
|
|
const videoChannelCreated: CustomVideoChannelModelAccount<T> = await videoChannel.save(options) as MChannelDefault
|
2017-10-25 04:55:06 -05:00
|
|
|
|
2017-12-14 10:38:41 -06:00
|
|
|
// Do not forget to add Account/Actor information to the created video channel
|
2017-11-09 10:51:58 -06:00
|
|
|
videoChannelCreated.Account = account
|
2017-12-14 10:38:41 -06:00
|
|
|
videoChannelCreated.Actor = actorInstanceCreated
|
2017-10-25 04:55:06 -05:00
|
|
|
|
2017-11-16 11:40:50 -06:00
|
|
|
// No need to seed this empty video channel to followers
|
2017-10-25 04:55:06 -05:00
|
|
|
return videoChannelCreated
|
|
|
|
}
|
|
|
|
|
2019-08-15 04:53:26 -05:00
|
|
|
async function federateAllVideosOfChannel (videoChannel: MChannelId) {
|
2019-05-31 09:30:11 -05:00
|
|
|
const videoIds = await VideoModel.getAllIdsFromChannel(videoChannel)
|
|
|
|
|
|
|
|
for (const videoId of videoIds) {
|
|
|
|
const video = await VideoModel.loadAndPopulateAccountAndServerAndTags(videoId)
|
|
|
|
|
|
|
|
await federateVideoIfNeeded(video, false)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-24 12:41:09 -05:00
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
export {
|
2019-08-20 12:05:31 -05:00
|
|
|
createLocalVideoChannel,
|
2019-05-31 09:30:11 -05:00
|
|
|
federateAllVideosOfChannel
|
2017-10-24 12:41:09 -05:00
|
|
|
}
|