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