Refractor activity pub lib/helpers
[oweals/peertube.git] / server / lib / video-channel.ts
1 import * as Sequelize from 'sequelize'
2 import { VideoChannelCreate } from '../../shared/models'
3 import { logger } from '../helpers'
4 import { database as db } from '../initializers'
5 import { AccountInstance } from '../models'
6 import { getVideoChannelActivityPubUrl } from '../helpers/activitypub'
7
8 async function createVideoChannel (videoChannelInfo: VideoChannelCreate, account: AccountInstance, t: Sequelize.Transaction) {
9   const videoChannelData = {
10     name: videoChannelInfo.name,
11     description: videoChannelInfo.description,
12     remote: false,
13     accountId: account.id
14   }
15
16   const videoChannel = db.VideoChannel.build(videoChannelData)
17   videoChannel.set('url', getVideoChannelActivityPubUrl(videoChannel))
18
19   const options = { transaction: t }
20
21   const videoChannelCreated = await videoChannel.save(options)
22
23   // Do not forget to add Account information to the created video channel
24   videoChannelCreated.Account = account
25
26   // No need to seed this empty video channel to followers
27   return videoChannelCreated
28 }
29
30 async function fetchVideoChannelByHostAndUUID (serverHost: string, uuid: string, t: Sequelize.Transaction) {
31   try {
32     const videoChannel = await db.VideoChannel.loadByHostAndUUID(serverHost, 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, serverHost, uuid })
38     throw err
39   }
40 }
41
42 // ---------------------------------------------------------------------------
43
44 export {
45   createVideoChannel,
46   fetchVideoChannelByHostAndUUID
47 }