]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/lib/video-channel.ts
Optimize account creation
[github/Chocobozzz/PeerTube.git] / server / lib / video-channel.ts
CommitLineData
72c7248b
C
1import * as Sequelize from 'sequelize'
2
72c7248b 3import { database as db } from '../initializers'
eb080476 4import { logger } from '../helpers'
e4f97bab 5import { AccountInstance } from '../models'
72c7248b 6import { VideoChannelCreate } from '../../shared/models'
571389d4 7import { sendCreateVideoChannel } from './activitypub/send-request'
efc32059 8import { getActivityPubUrl, shareVideoChannelByServer } from '../helpers/activitypub'
72c7248b 9
e4f97bab 10async function createVideoChannel (videoChannelInfo: VideoChannelCreate, account: AccountInstance, t: Sequelize.Transaction) {
72c7248b
C
11 const videoChannelData = {
12 name: videoChannelInfo.name,
13 description: videoChannelInfo.description,
14 remote: false,
38fa2065 15 accountId: account.id
72c7248b
C
16 }
17
18 const videoChannel = db.VideoChannel.build(videoChannelData)
e34c85e5
C
19 videoChannel.set('url', getActivityPubUrl('videoChannel', videoChannel.uuid))
20
72c7248b
C
21 const options = { transaction: t }
22
eb080476
C
23 const videoChannelCreated = await videoChannel.save(options)
24
e4f97bab
C
25 // Do not forget to add Account information to the created video channel
26 videoChannelCreated.Account = account
eb080476 27
47e0652b 28 // No need to seed this empty video channel to followers
eb080476
C
29 return videoChannelCreated
30}
31
60862425 32async function fetchVideoChannelByHostAndUUID (serverHost: string, uuid: string, t: Sequelize.Transaction) {
eb080476 33 try {
60862425 34 const videoChannel = await db.VideoChannel.loadByHostAndUUID(serverHost, uuid, t)
eb080476
C
35 if (!videoChannel) throw new Error('Video channel not found')
36
37 return videoChannel
38 } catch (err) {
60862425 39 logger.error('Cannot load video channel from host and uuid.', { error: err.stack, serverHost, uuid })
eb080476
C
40 throw err
41 }
72c7248b
C
42}
43
44// ---------------------------------------------------------------------------
45
46export {
eb080476
C
47 createVideoChannel,
48 fetchVideoChannelByHostAndUUID
72c7248b 49}