459d9d4a8456cf7bb7b3dc8a56137cb475949b9d
[oweals/peertube.git] / server / lib / video-channel.ts
1 import * as Sequelize from 'sequelize'
2
3 import { database as db } from '../initializers'
4 import { logger } from '../helpers'
5 import { AccountInstance } from '../models'
6 import { VideoChannelCreate } from '../../shared/models'
7 import { sendCreateVideoChannel } from './activitypub/send-request'
8
9 async function createVideoChannel (videoChannelInfo: VideoChannelCreate, account: AccountInstance, t: Sequelize.Transaction) {
10   const videoChannelData = {
11     name: videoChannelInfo.name,
12     description: videoChannelInfo.description,
13     remote: false,
14     accountId: account.id
15   }
16
17   const videoChannel = db.VideoChannel.build(videoChannelData)
18   const options = { transaction: t }
19
20   const videoChannelCreated = await videoChannel.save(options)
21
22   // Do not forget to add Account information to the created video channel
23   videoChannelCreated.Account = account
24
25   sendCreateVideoChannel(videoChannelCreated, t)
26
27   return videoChannelCreated
28 }
29
30 async function fetchVideoChannelByHostAndUUID (podHost: string, uuid: string, t: Sequelize.Transaction) {
31   try {
32     const videoChannel = await db.VideoChannel.loadByHostAndUUID(podHost, uuid, t)
33     if (!videoChannel) throw new Error('Video channel not found')
34
35     return videoChannel
36   } catch (err) {
37     logger.error('Cannot load video channel from host and uuid.', { error: err.stack, podHost, uuid })
38     throw err
39   }
40 }
41
42 // ---------------------------------------------------------------------------
43
44 export {
45   createVideoChannel,
46   fetchVideoChannelByHostAndUUID
47 }