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