]>
Commit | Line | Data |
---|---|---|
48dce1c9 | 1 | import * as express from 'express' |
f37dc0dd | 2 | import { getFormattedObjects, getServerActor } from '../../helpers/utils' |
48dce1c9 C |
3 | import { |
4 | asyncMiddleware, | |
90d4bb81 | 5 | asyncRetryTransactionMiddleware, |
06215f15 C |
6 | authenticate, |
7 | commonVideosFiltersValidator, | |
cc918ac3 | 8 | optionalAuthenticate, |
48dce1c9 C |
9 | paginationValidator, |
10 | setDefaultPagination, | |
11 | setDefaultSort, | |
cc918ac3 | 12 | videoChannelsAddValidator, |
cc918ac3 C |
13 | videoChannelsRemoveValidator, |
14 | videoChannelsSortValidator, | |
418d092a C |
15 | videoChannelsUpdateValidator, |
16 | videoPlaylistsSortValidator | |
48dce1c9 C |
17 | } from '../../middlewares' |
18 | import { VideoChannelModel } from '../../models/video/video-channel' | |
8a19bee1 | 19 | import { videoChannelsNameWithHostValidator, videosSortValidator } from '../../middlewares/validators' |
cc918ac3 C |
20 | import { sendUpdateActor } from '../../lib/activitypub/send' |
21 | import { VideoChannelCreate, VideoChannelUpdate } from '../../../shared' | |
22 | import { createVideoChannel } from '../../lib/video-channel' | |
687d638c | 23 | import { buildNSFWFilter, createReqFiles, isUserAbleToSearchRemoteURI } from '../../helpers/express-utils' |
cc918ac3 | 24 | import { setAsyncActorKeys } from '../../lib/activitypub' |
cc918ac3 | 25 | import { AccountModel } from '../../models/account/account' |
14e2014a | 26 | import { CONFIG, MIMETYPES, sequelizeTypescript } from '../../initializers' |
cc918ac3 C |
27 | import { logger } from '../../helpers/logger' |
28 | import { VideoModel } from '../../models/video/video' | |
4bbfc6c6 C |
29 | import { updateAvatarValidator } from '../../middlewares/validators/avatar' |
30 | import { updateActorAvatarFile } from '../../lib/avatar' | |
993cef4b | 31 | import { auditLoggerFactory, getAuditIdFromRes, VideoChannelAuditView } from '../../helpers/audit-logger' |
06215f15 | 32 | import { resetSequelizeInstance } from '../../helpers/database-utils' |
744d0eca | 33 | import { JobQueue } from '../../lib/job-queue' |
418d092a | 34 | import { VideoPlaylistModel } from '../../models/video/video-playlist' |
df0b219d | 35 | import { commonVideoPlaylistFiltersValidator } from '../../middlewares/validators/videos/video-playlists' |
4bbfc6c6 | 36 | |
80e36cd9 | 37 | const auditLogger = auditLoggerFactory('channels') |
14e2014a | 38 | const reqAvatarFile = createReqFiles([ 'avatarfile' ], MIMETYPES.IMAGE.MIMETYPE_EXT, { avatarfile: CONFIG.STORAGE.TMP_DIR }) |
48dce1c9 C |
39 | |
40 | const videoChannelRouter = express.Router() | |
41 | ||
42 | videoChannelRouter.get('/', | |
43 | paginationValidator, | |
44 | videoChannelsSortValidator, | |
45 | setDefaultSort, | |
46 | setDefaultPagination, | |
47 | asyncMiddleware(listVideoChannels) | |
48 | ) | |
49 | ||
cc918ac3 C |
50 | videoChannelRouter.post('/', |
51 | authenticate, | |
601527d7 | 52 | asyncMiddleware(videoChannelsAddValidator), |
90d4bb81 | 53 | asyncRetryTransactionMiddleware(addVideoChannel) |
cc918ac3 C |
54 | ) |
55 | ||
8a19bee1 | 56 | videoChannelRouter.post('/:nameWithHost/avatar/pick', |
4bbfc6c6 C |
57 | authenticate, |
58 | reqAvatarFile, | |
59 | // Check the rights | |
60 | asyncMiddleware(videoChannelsUpdateValidator), | |
61 | updateAvatarValidator, | |
4a534352 | 62 | asyncMiddleware(updateVideoChannelAvatar) |
4bbfc6c6 C |
63 | ) |
64 | ||
8a19bee1 | 65 | videoChannelRouter.put('/:nameWithHost', |
cc918ac3 C |
66 | authenticate, |
67 | asyncMiddleware(videoChannelsUpdateValidator), | |
90d4bb81 | 68 | asyncRetryTransactionMiddleware(updateVideoChannel) |
cc918ac3 C |
69 | ) |
70 | ||
8a19bee1 | 71 | videoChannelRouter.delete('/:nameWithHost', |
cc918ac3 C |
72 | authenticate, |
73 | asyncMiddleware(videoChannelsRemoveValidator), | |
90d4bb81 | 74 | asyncRetryTransactionMiddleware(removeVideoChannel) |
cc918ac3 C |
75 | ) |
76 | ||
8a19bee1 C |
77 | videoChannelRouter.get('/:nameWithHost', |
78 | asyncMiddleware(videoChannelsNameWithHostValidator), | |
cc918ac3 C |
79 | asyncMiddleware(getVideoChannel) |
80 | ) | |
81 | ||
418d092a C |
82 | videoChannelRouter.get('/:nameWithHost/video-playlists', |
83 | asyncMiddleware(videoChannelsNameWithHostValidator), | |
84 | paginationValidator, | |
85 | videoPlaylistsSortValidator, | |
86 | setDefaultSort, | |
87 | setDefaultPagination, | |
df0b219d | 88 | commonVideoPlaylistFiltersValidator, |
418d092a C |
89 | asyncMiddleware(listVideoChannelPlaylists) |
90 | ) | |
91 | ||
8a19bee1 C |
92 | videoChannelRouter.get('/:nameWithHost/videos', |
93 | asyncMiddleware(videoChannelsNameWithHostValidator), | |
cc918ac3 C |
94 | paginationValidator, |
95 | videosSortValidator, | |
96 | setDefaultSort, | |
97 | setDefaultPagination, | |
98 | optionalAuthenticate, | |
d525fc39 | 99 | commonVideosFiltersValidator, |
cc918ac3 C |
100 | asyncMiddleware(listVideoChannelVideos) |
101 | ) | |
102 | ||
48dce1c9 C |
103 | // --------------------------------------------------------------------------- |
104 | ||
105 | export { | |
106 | videoChannelRouter | |
107 | } | |
108 | ||
109 | // --------------------------------------------------------------------------- | |
110 | ||
dae86118 | 111 | async function listVideoChannels (req: express.Request, res: express.Response) { |
f37dc0dd C |
112 | const serverActor = await getServerActor() |
113 | const resultList = await VideoChannelModel.listForApi(serverActor.id, req.query.start, req.query.count, req.query.sort) | |
48dce1c9 C |
114 | |
115 | return res.json(getFormattedObjects(resultList.data, resultList.total)) | |
116 | } | |
cc918ac3 | 117 | |
dae86118 | 118 | async function updateVideoChannelAvatar (req: express.Request, res: express.Response) { |
4bbfc6c6 | 119 | const avatarPhysicalFile = req.files[ 'avatarfile' ][ 0 ] |
dae86118 | 120 | const videoChannel = res.locals.videoChannel |
80e36cd9 | 121 | const oldVideoChannelAuditKeys = new VideoChannelAuditView(videoChannel.toFormattedJSON()) |
4bbfc6c6 | 122 | |
f201a749 | 123 | const avatar = await updateActorAvatarFile(avatarPhysicalFile, videoChannel) |
4bbfc6c6 | 124 | |
f201a749 | 125 | auditLogger.update(getAuditIdFromRes(res), new VideoChannelAuditView(videoChannel.toFormattedJSON()), oldVideoChannelAuditKeys) |
80e36cd9 | 126 | |
4bbfc6c6 C |
127 | return res |
128 | .json({ | |
129 | avatar: avatar.toFormattedJSON() | |
130 | }) | |
131 | .end() | |
132 | } | |
133 | ||
cc918ac3 C |
134 | async function addVideoChannel (req: express.Request, res: express.Response) { |
135 | const videoChannelInfo: VideoChannelCreate = req.body | |
cc918ac3 C |
136 | |
137 | const videoChannelCreated: VideoChannelModel = await sequelizeTypescript.transaction(async t => { | |
dae86118 | 138 | const account = await AccountModel.load(res.locals.oauth.token.User.Account.id, t) |
91411dba | 139 | |
cc918ac3 C |
140 | return createVideoChannel(videoChannelInfo, account, t) |
141 | }) | |
142 | ||
143 | setAsyncActorKeys(videoChannelCreated.Actor) | |
144 | .catch(err => logger.error('Cannot set async actor keys for account %s.', videoChannelCreated.Actor.uuid, { err })) | |
145 | ||
91411dba | 146 | auditLogger.create(getAuditIdFromRes(res), new VideoChannelAuditView(videoChannelCreated.toFormattedJSON())) |
cc918ac3 C |
147 | logger.info('Video channel with uuid %s created.', videoChannelCreated.Actor.uuid) |
148 | ||
90d4bb81 C |
149 | return res.json({ |
150 | videoChannel: { | |
151 | id: videoChannelCreated.id, | |
152 | uuid: videoChannelCreated.Actor.uuid | |
153 | } | |
154 | }).end() | |
cc918ac3 C |
155 | } |
156 | ||
157 | async function updateVideoChannel (req: express.Request, res: express.Response) { | |
dae86118 | 158 | const videoChannelInstance = res.locals.videoChannel |
cc918ac3 | 159 | const videoChannelFieldsSave = videoChannelInstance.toJSON() |
80e36cd9 | 160 | const oldVideoChannelAuditKeys = new VideoChannelAuditView(videoChannelInstance.toFormattedJSON()) |
cc918ac3 C |
161 | const videoChannelInfoToUpdate = req.body as VideoChannelUpdate |
162 | ||
163 | try { | |
164 | await sequelizeTypescript.transaction(async t => { | |
165 | const sequelizeOptions = { | |
166 | transaction: t | |
167 | } | |
168 | ||
08c1efbe | 169 | if (videoChannelInfoToUpdate.displayName !== undefined) videoChannelInstance.set('name', videoChannelInfoToUpdate.displayName) |
cc918ac3 C |
170 | if (videoChannelInfoToUpdate.description !== undefined) videoChannelInstance.set('description', videoChannelInfoToUpdate.description) |
171 | if (videoChannelInfoToUpdate.support !== undefined) videoChannelInstance.set('support', videoChannelInfoToUpdate.support) | |
172 | ||
173 | const videoChannelInstanceUpdated = await videoChannelInstance.save(sequelizeOptions) | |
174 | await sendUpdateActor(videoChannelInstanceUpdated, t) | |
cc918ac3 | 175 | |
80e36cd9 | 176 | auditLogger.update( |
993cef4b | 177 | getAuditIdFromRes(res), |
80e36cd9 AB |
178 | new VideoChannelAuditView(videoChannelInstanceUpdated.toFormattedJSON()), |
179 | oldVideoChannelAuditKeys | |
180 | ) | |
181 | logger.info('Video channel with name %s and uuid %s updated.', videoChannelInstance.name, videoChannelInstance.Actor.uuid) | |
182 | }) | |
cc918ac3 C |
183 | } catch (err) { |
184 | logger.debug('Cannot update the video channel.', { err }) | |
185 | ||
186 | // Force fields we want to update | |
187 | // If the transaction is retried, sequelize will think the object has not changed | |
188 | // So it will skip the SQL request, even if the last one was ROLLBACKed! | |
189 | resetSequelizeInstance(videoChannelInstance, videoChannelFieldsSave) | |
190 | ||
191 | throw err | |
192 | } | |
cc918ac3 C |
193 | |
194 | return res.type('json').status(204).end() | |
195 | } | |
196 | ||
197 | async function removeVideoChannel (req: express.Request, res: express.Response) { | |
dae86118 | 198 | const videoChannelInstance = res.locals.videoChannel |
cc918ac3 | 199 | |
90d4bb81 | 200 | await sequelizeTypescript.transaction(async t => { |
df0b219d C |
201 | await VideoPlaylistModel.resetPlaylistsOfChannel(videoChannelInstance.id, t) |
202 | ||
cc918ac3 C |
203 | await videoChannelInstance.destroy({ transaction: t }) |
204 | ||
993cef4b | 205 | auditLogger.delete(getAuditIdFromRes(res), new VideoChannelAuditView(videoChannelInstance.toFormattedJSON())) |
cc918ac3 C |
206 | logger.info('Video channel with name %s and uuid %s deleted.', videoChannelInstance.name, videoChannelInstance.Actor.uuid) |
207 | }) | |
208 | ||
90d4bb81 | 209 | return res.type('json').status(204).end() |
cc918ac3 C |
210 | } |
211 | ||
dae86118 | 212 | async function getVideoChannel (req: express.Request, res: express.Response) { |
cc918ac3 C |
213 | const videoChannelWithVideos = await VideoChannelModel.loadAndPopulateAccountAndVideos(res.locals.videoChannel.id) |
214 | ||
744d0eca C |
215 | if (videoChannelWithVideos.isOutdated()) { |
216 | JobQueue.Instance.createJob({ type: 'activitypub-refresher', payload: { type: 'actor', url: videoChannelWithVideos.Actor.url } }) | |
217 | .catch(err => logger.error('Cannot create AP refresher job for actor %s.', videoChannelWithVideos.Actor.url, { err })) | |
218 | } | |
219 | ||
cc918ac3 C |
220 | return res.json(videoChannelWithVideos.toFormattedJSON()) |
221 | } | |
222 | ||
418d092a C |
223 | async function listVideoChannelPlaylists (req: express.Request, res: express.Response) { |
224 | const serverActor = await getServerActor() | |
225 | ||
226 | const resultList = await VideoPlaylistModel.listForApi({ | |
227 | followerActorId: serverActor.id, | |
228 | start: req.query.start, | |
229 | count: req.query.count, | |
230 | sort: req.query.sort, | |
df0b219d C |
231 | videoChannelId: res.locals.videoChannel.id, |
232 | type: req.query.playlistType | |
418d092a C |
233 | }) |
234 | ||
235 | return res.json(getFormattedObjects(resultList.data, resultList.total)) | |
236 | } | |
237 | ||
dae86118 C |
238 | async function listVideoChannelVideos (req: express.Request, res: express.Response) { |
239 | const videoChannelInstance = res.locals.videoChannel | |
4e74e803 | 240 | const followerActorId = isUserAbleToSearchRemoteURI(res) ? null : undefined |
cc918ac3 C |
241 | |
242 | const resultList = await VideoModel.listForApi({ | |
4e74e803 | 243 | followerActorId, |
cc918ac3 C |
244 | start: req.query.start, |
245 | count: req.query.count, | |
246 | sort: req.query.sort, | |
8a19bee1 | 247 | includeLocalVideos: true, |
d525fc39 C |
248 | categoryOneOf: req.query.categoryOneOf, |
249 | licenceOneOf: req.query.licenceOneOf, | |
250 | languageOneOf: req.query.languageOneOf, | |
251 | tagsOneOf: req.query.tagsOneOf, | |
252 | tagsAllOf: req.query.tagsAllOf, | |
1cd3facc | 253 | filter: req.query.filter, |
d525fc39 | 254 | nsfw: buildNSFWFilter(res, req.query.nsfw), |
cc918ac3 | 255 | withFiles: false, |
1cd3facc | 256 | videoChannelId: videoChannelInstance.id, |
7ad9b984 | 257 | user: res.locals.oauth ? res.locals.oauth.token.User : undefined |
cc918ac3 C |
258 | }) |
259 | ||
260 | return res.json(getFormattedObjects(resultList.data, resultList.total)) | |
261 | } |