Update dependencies
[oweals/peertube.git] / server / lib / video-channel.ts
1 import * as Sequelize from 'sequelize'
2 import { v4 as uuidv4 } from 'uuid'
3 import { VideoChannelCreate } from '../../shared/models'
4 import { VideoChannelModel } from '../models/video/video-channel'
5 import { buildActorInstance, federateVideoIfNeeded, getVideoChannelActivityPubUrl } from './activitypub'
6 import { VideoModel } from '../models/video/video'
7 import { MAccountId, MChannelDefault, MChannelId } from '../typings/models'
8
9 type CustomVideoChannelModelAccount <T extends MAccountId> = MChannelDefault & { Account?: T }
10
11 async function createLocalVideoChannel <T extends MAccountId> (
12   videoChannelInfo: VideoChannelCreate,
13   account: T,
14   t: Sequelize.Transaction
15 ): Promise<CustomVideoChannelModelAccount<T>> {
16   const uuid = uuidv4()
17   const url = getVideoChannelActivityPubUrl(videoChannelInfo.name)
18   const actorInstance = buildActorInstance('Group', url, videoChannelInfo.name, uuid)
19
20   const actorInstanceCreated = await actorInstance.save({ transaction: t })
21
22   const videoChannelData = {
23     name: videoChannelInfo.displayName,
24     description: videoChannelInfo.description,
25     support: videoChannelInfo.support,
26     accountId: account.id,
27     actorId: actorInstanceCreated.id
28   }
29
30   const videoChannel = new VideoChannelModel(videoChannelData)
31
32   const options = { transaction: t }
33   const videoChannelCreated: CustomVideoChannelModelAccount<T> = await videoChannel.save(options) as MChannelDefault
34
35   // Do not forget to add Account/Actor information to the created video channel
36   videoChannelCreated.Account = account
37   videoChannelCreated.Actor = actorInstanceCreated
38
39   // No need to seed this empty video channel to followers
40   return videoChannelCreated
41 }
42
43 async function federateAllVideosOfChannel (videoChannel: MChannelId) {
44   const videoIds = await VideoModel.getAllIdsFromChannel(videoChannel)
45
46   for (const videoId of videoIds) {
47     const video = await VideoModel.loadAndPopulateAccountAndServerAndTags(videoId)
48
49     await federateVideoIfNeeded(video, false)
50   }
51 }
52
53 // ---------------------------------------------------------------------------
54
55 export {
56   createLocalVideoChannel,
57   federateAllVideosOfChannel
58 }