PeerTube/server/lib/video-channel.ts

61 lines
2.2 KiB
TypeScript
Raw Normal View History

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