diff options
author | Chocobozzz <florian.bigard@gmail.com> | 2017-10-25 11:55:06 +0200 |
---|---|---|
committer | Chocobozzz <florian.bigard@gmail.com> | 2017-10-26 09:11:38 +0200 |
commit | eb08047657e739bcd9e592d76307befa3998482b (patch) | |
tree | fc309f51ece792fd4307c4af510710a853e1d6b2 /server/lib/video-channel.ts | |
parent | 5f04dd2f743961e0a06c29531cc3ccc9e4928d56 (diff) | |
download | PeerTube-eb08047657e739bcd9e592d76307befa3998482b.tar.gz PeerTube-eb08047657e739bcd9e592d76307befa3998482b.tar.zst PeerTube-eb08047657e739bcd9e592d76307befa3998482b.zip |
Use async/await in controllers
Diffstat (limited to 'server/lib/video-channel.ts')
-rw-r--r-- | server/lib/video-channel.ts | 46 |
1 files changed, 27 insertions, 19 deletions
diff --git a/server/lib/video-channel.ts b/server/lib/video-channel.ts index 224179973..678ffe643 100644 --- a/server/lib/video-channel.ts +++ b/server/lib/video-channel.ts | |||
@@ -2,12 +2,11 @@ import * as Sequelize from 'sequelize' | |||
2 | 2 | ||
3 | import { addVideoChannelToFriends } from './friends' | 3 | import { addVideoChannelToFriends } from './friends' |
4 | import { database as db } from '../initializers' | 4 | import { database as db } from '../initializers' |
5 | import { logger } from '../helpers' | ||
5 | import { AuthorInstance } from '../models' | 6 | import { AuthorInstance } from '../models' |
6 | import { VideoChannelCreate } from '../../shared/models' | 7 | import { VideoChannelCreate } from '../../shared/models' |
7 | 8 | ||
8 | function createVideoChannel (videoChannelInfo: VideoChannelCreate, author: AuthorInstance, t: Sequelize.Transaction) { | 9 | async function createVideoChannel (videoChannelInfo: VideoChannelCreate, author: AuthorInstance, t: Sequelize.Transaction) { |
9 | let videoChannelUUID = '' | ||
10 | |||
11 | const videoChannelData = { | 10 | const videoChannelData = { |
12 | name: videoChannelInfo.name, | 11 | name: videoChannelInfo.name, |
13 | description: videoChannelInfo.description, | 12 | description: videoChannelInfo.description, |
@@ -18,25 +17,34 @@ function createVideoChannel (videoChannelInfo: VideoChannelCreate, author: Autho | |||
18 | const videoChannel = db.VideoChannel.build(videoChannelData) | 17 | const videoChannel = db.VideoChannel.build(videoChannelData) |
19 | const options = { transaction: t } | 18 | const options = { transaction: t } |
20 | 19 | ||
21 | return videoChannel.save(options) | 20 | const videoChannelCreated = await videoChannel.save(options) |
22 | .then(videoChannelCreated => { | 21 | |
23 | // Do not forget to add Author information to the created video channel | 22 | // Do not forget to add Author information to the created video channel |
24 | videoChannelCreated.Author = author | 23 | videoChannelCreated.Author = author |
25 | videoChannelUUID = videoChannelCreated.uuid | 24 | |
26 | 25 | const remoteVideoChannel = videoChannelCreated.toAddRemoteJSON() | |
27 | return videoChannelCreated | 26 | |
28 | }) | 27 | // Now we'll add the video channel's meta data to our friends |
29 | .then(videoChannel => { | 28 | await addVideoChannelToFriends(remoteVideoChannel, t) |
30 | const remoteVideoChannel = videoChannel.toAddRemoteJSON() | 29 | |
31 | 30 | return videoChannelCreated | |
32 | // Now we'll add the video channel's meta data to our friends | 31 | } |
33 | return addVideoChannelToFriends(remoteVideoChannel, t) | 32 | |
34 | }) | 33 | async function fetchVideoChannelByHostAndUUID (podHost: string, uuid: string, t: Sequelize.Transaction) { |
35 | .then(() => videoChannelUUID) // Return video channel UUID | 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 | } | ||
36 | } | 43 | } |
37 | 44 | ||
38 | // --------------------------------------------------------------------------- | 45 | // --------------------------------------------------------------------------- |
39 | 46 | ||
40 | export { | 47 | export { |
41 | createVideoChannel | 48 | createVideoChannel, |
49 | fetchVideoChannelByHostAndUUID | ||
42 | } | 50 | } |