]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/lib/video-channel.ts
Use async/await in controllers
[github/Chocobozzz/PeerTube.git] / server / lib / video-channel.ts
1 import * as Sequelize from 'sequelize'
2
3 import { addVideoChannelToFriends } from './friends'
4 import { database as db } from '../initializers'
5 import { logger } from '../helpers'
6 import { AuthorInstance } from '../models'
7 import { VideoChannelCreate } from '../../shared/models'
8
9 async function createVideoChannel (videoChannelInfo: VideoChannelCreate, author: AuthorInstance, t: Sequelize.Transaction) {
10 const videoChannelData = {
11 name: videoChannelInfo.name,
12 description: videoChannelInfo.description,
13 remote: false,
14 authorId: author.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 Author information to the created video channel
23 videoChannelCreated.Author = author
24
25 const remoteVideoChannel = videoChannelCreated.toAddRemoteJSON()
26
27 // Now we'll add the video channel's meta data to our friends
28 await addVideoChannelToFriends(remoteVideoChannel, t)
29
30 return videoChannelCreated
31 }
32
33 async function fetchVideoChannelByHostAndUUID (podHost: string, uuid: string, t: Sequelize.Transaction) {
34 try {
35 const videoChannel = await db.VideoChannel.loadByHostAndUUID(podHost, uuid, t)
36 if (!videoChannel) throw new Error('Video channel not found')
37
38 return videoChannel
39 } catch (err) {
40 logger.error('Cannot load video channel from host and uuid.', { error: err.stack, podHost, uuid })
41 throw err
42 }
43 }
44
45 // ---------------------------------------------------------------------------
46
47 export {
48 createVideoChannel,
49 fetchVideoChannelByHostAndUUID
50 }