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