]>
Commit | Line | Data |
---|---|---|
48dce1c9 | 1 | import * as express from 'express' |
cc918ac3 | 2 | import { getFormattedObjects, resetSequelizeInstance } from '../../helpers/utils' |
48dce1c9 C |
3 | import { |
4 | asyncMiddleware, | |
90d4bb81 | 5 | asyncRetryTransactionMiddleware, |
cc918ac3 C |
6 | authenticate, |
7 | optionalAuthenticate, | |
48dce1c9 C |
8 | paginationValidator, |
9 | setDefaultPagination, | |
10 | setDefaultSort, | |
cc918ac3 C |
11 | videoChannelsAddValidator, |
12 | videoChannelsGetValidator, | |
13 | videoChannelsRemoveValidator, | |
14 | videoChannelsSortValidator, | |
15 | videoChannelsUpdateValidator | |
48dce1c9 C |
16 | } from '../../middlewares' |
17 | import { VideoChannelModel } from '../../models/video/video-channel' | |
cc918ac3 C |
18 | import { videosSortValidator } from '../../middlewares/validators' |
19 | import { sendUpdateActor } from '../../lib/activitypub/send' | |
20 | import { VideoChannelCreate, VideoChannelUpdate } from '../../../shared' | |
21 | import { createVideoChannel } from '../../lib/video-channel' | |
22 | import { isNSFWHidden } from '../../helpers/express-utils' | |
23 | import { setAsyncActorKeys } from '../../lib/activitypub' | |
cc918ac3 C |
24 | import { AccountModel } from '../../models/account/account' |
25 | import { sequelizeTypescript } from '../../initializers' | |
26 | import { logger } from '../../helpers/logger' | |
27 | import { VideoModel } from '../../models/video/video' | |
48dce1c9 C |
28 | |
29 | const videoChannelRouter = express.Router() | |
30 | ||
31 | videoChannelRouter.get('/', | |
32 | paginationValidator, | |
33 | videoChannelsSortValidator, | |
34 | setDefaultSort, | |
35 | setDefaultPagination, | |
36 | asyncMiddleware(listVideoChannels) | |
37 | ) | |
38 | ||
cc918ac3 C |
39 | videoChannelRouter.post('/', |
40 | authenticate, | |
41 | videoChannelsAddValidator, | |
90d4bb81 | 42 | asyncRetryTransactionMiddleware(addVideoChannel) |
cc918ac3 C |
43 | ) |
44 | ||
45 | videoChannelRouter.put('/:id', | |
46 | authenticate, | |
47 | asyncMiddleware(videoChannelsUpdateValidator), | |
90d4bb81 | 48 | asyncRetryTransactionMiddleware(updateVideoChannel) |
cc918ac3 C |
49 | ) |
50 | ||
51 | videoChannelRouter.delete('/:id', | |
52 | authenticate, | |
53 | asyncMiddleware(videoChannelsRemoveValidator), | |
90d4bb81 | 54 | asyncRetryTransactionMiddleware(removeVideoChannel) |
cc918ac3 C |
55 | ) |
56 | ||
57 | videoChannelRouter.get('/:id', | |
58 | asyncMiddleware(videoChannelsGetValidator), | |
59 | asyncMiddleware(getVideoChannel) | |
60 | ) | |
61 | ||
62 | videoChannelRouter.get('/:id/videos', | |
63 | asyncMiddleware(videoChannelsGetValidator), | |
64 | paginationValidator, | |
65 | videosSortValidator, | |
66 | setDefaultSort, | |
67 | setDefaultPagination, | |
68 | optionalAuthenticate, | |
69 | asyncMiddleware(listVideoChannelVideos) | |
70 | ) | |
71 | ||
48dce1c9 C |
72 | // --------------------------------------------------------------------------- |
73 | ||
74 | export { | |
75 | videoChannelRouter | |
76 | } | |
77 | ||
78 | // --------------------------------------------------------------------------- | |
79 | ||
80 | async function listVideoChannels (req: express.Request, res: express.Response, next: express.NextFunction) { | |
81 | const resultList = await VideoChannelModel.listForApi(req.query.start, req.query.count, req.query.sort) | |
82 | ||
83 | return res.json(getFormattedObjects(resultList.data, resultList.total)) | |
84 | } | |
cc918ac3 | 85 | |
cc918ac3 C |
86 | async function addVideoChannel (req: express.Request, res: express.Response) { |
87 | const videoChannelInfo: VideoChannelCreate = req.body | |
88 | const account: AccountModel = res.locals.oauth.token.User.Account | |
89 | ||
90 | const videoChannelCreated: VideoChannelModel = await sequelizeTypescript.transaction(async t => { | |
91 | return createVideoChannel(videoChannelInfo, account, t) | |
92 | }) | |
93 | ||
94 | setAsyncActorKeys(videoChannelCreated.Actor) | |
95 | .catch(err => logger.error('Cannot set async actor keys for account %s.', videoChannelCreated.Actor.uuid, { err })) | |
96 | ||
97 | logger.info('Video channel with uuid %s created.', videoChannelCreated.Actor.uuid) | |
98 | ||
90d4bb81 C |
99 | return res.json({ |
100 | videoChannel: { | |
101 | id: videoChannelCreated.id, | |
102 | uuid: videoChannelCreated.Actor.uuid | |
103 | } | |
104 | }).end() | |
cc918ac3 C |
105 | } |
106 | ||
107 | async function updateVideoChannel (req: express.Request, res: express.Response) { | |
108 | const videoChannelInstance = res.locals.videoChannel as VideoChannelModel | |
109 | const videoChannelFieldsSave = videoChannelInstance.toJSON() | |
110 | const videoChannelInfoToUpdate = req.body as VideoChannelUpdate | |
111 | ||
112 | try { | |
113 | await sequelizeTypescript.transaction(async t => { | |
114 | const sequelizeOptions = { | |
115 | transaction: t | |
116 | } | |
117 | ||
08c1efbe | 118 | if (videoChannelInfoToUpdate.displayName !== undefined) videoChannelInstance.set('name', videoChannelInfoToUpdate.displayName) |
cc918ac3 C |
119 | if (videoChannelInfoToUpdate.description !== undefined) videoChannelInstance.set('description', videoChannelInfoToUpdate.description) |
120 | if (videoChannelInfoToUpdate.support !== undefined) videoChannelInstance.set('support', videoChannelInfoToUpdate.support) | |
121 | ||
122 | const videoChannelInstanceUpdated = await videoChannelInstance.save(sequelizeOptions) | |
123 | await sendUpdateActor(videoChannelInstanceUpdated, t) | |
124 | }) | |
125 | ||
126 | logger.info('Video channel with name %s and uuid %s updated.', videoChannelInstance.name, videoChannelInstance.Actor.uuid) | |
127 | } catch (err) { | |
128 | logger.debug('Cannot update the video channel.', { err }) | |
129 | ||
130 | // Force fields we want to update | |
131 | // If the transaction is retried, sequelize will think the object has not changed | |
132 | // So it will skip the SQL request, even if the last one was ROLLBACKed! | |
133 | resetSequelizeInstance(videoChannelInstance, videoChannelFieldsSave) | |
134 | ||
135 | throw err | |
136 | } | |
cc918ac3 C |
137 | |
138 | return res.type('json').status(204).end() | |
139 | } | |
140 | ||
141 | async function removeVideoChannel (req: express.Request, res: express.Response) { | |
142 | const videoChannelInstance: VideoChannelModel = res.locals.videoChannel | |
143 | ||
90d4bb81 | 144 | await sequelizeTypescript.transaction(async t => { |
cc918ac3 C |
145 | await videoChannelInstance.destroy({ transaction: t }) |
146 | ||
147 | logger.info('Video channel with name %s and uuid %s deleted.', videoChannelInstance.name, videoChannelInstance.Actor.uuid) | |
148 | }) | |
149 | ||
90d4bb81 | 150 | return res.type('json').status(204).end() |
cc918ac3 C |
151 | } |
152 | ||
153 | async function getVideoChannel (req: express.Request, res: express.Response, next: express.NextFunction) { | |
154 | const videoChannelWithVideos = await VideoChannelModel.loadAndPopulateAccountAndVideos(res.locals.videoChannel.id) | |
155 | ||
156 | return res.json(videoChannelWithVideos.toFormattedJSON()) | |
157 | } | |
158 | ||
159 | async function listVideoChannelVideos (req: express.Request, res: express.Response, next: express.NextFunction) { | |
160 | const videoChannelInstance: VideoChannelModel = res.locals.videoChannel | |
161 | ||
162 | const resultList = await VideoModel.listForApi({ | |
163 | start: req.query.start, | |
164 | count: req.query.count, | |
165 | sort: req.query.sort, | |
166 | hideNSFW: isNSFWHidden(res), | |
167 | withFiles: false, | |
168 | videoChannelId: videoChannelInstance.id | |
169 | }) | |
170 | ||
171 | return res.json(getFormattedObjects(resultList.data, resultList.total)) | |
172 | } |