From 7226e90fdc61a3c6cad5ccab18b6707d55cf0992 Mon Sep 17 00:00:00 2001 From: lutangar Date: Wed, 24 Nov 2021 14:33:14 +0100 Subject: Add `req` and `res` as controllers hooks parameters Hooks prefixed by `action:api` now give access the original express req and res. Checkout guide.md for possible usage. --- server/controllers/api/videos/comment.ts | 6 +++--- server/controllers/api/videos/index.ts | 6 +++--- server/controllers/api/videos/live.ts | 2 +- server/controllers/api/videos/update.ts | 2 +- server/controllers/api/videos/upload.ts | 9 +++++---- 5 files changed, 13 insertions(+), 12 deletions(-) (limited to 'server/controllers/api/videos') diff --git a/server/controllers/api/videos/comment.ts b/server/controllers/api/videos/comment.ts index 23bba9089..47fa2f2e2 100644 --- a/server/controllers/api/videos/comment.ts +++ b/server/controllers/api/videos/comment.ts @@ -192,7 +192,7 @@ async function addVideoCommentThread (req: express.Request, res: express.Respons Notifier.Instance.notifyOnNewComment(comment) auditLogger.create(getAuditIdFromRes(res), new CommentAuditView(comment.toFormattedJSON())) - Hooks.runAction('action:api.video-thread.created', { comment }) + Hooks.runAction('action:api.video-thread.created', { comment, req, res }) return res.json({ comment: comment.toFormattedJSON() }) } @@ -214,7 +214,7 @@ async function addVideoCommentReply (req: express.Request, res: express.Response Notifier.Instance.notifyOnNewComment(comment) auditLogger.create(getAuditIdFromRes(res), new CommentAuditView(comment.toFormattedJSON())) - Hooks.runAction('action:api.video-comment-reply.created', { comment }) + Hooks.runAction('action:api.video-comment-reply.created', { comment, req, res }) return res.json({ comment: comment.toFormattedJSON() }) } @@ -222,7 +222,7 @@ async function addVideoCommentReply (req: express.Request, res: express.Response async function removeVideoComment (req: express.Request, res: express.Response) { const videoCommentInstance = res.locals.videoCommentFull - await removeComment(videoCommentInstance) + await removeComment(videoCommentInstance, req, res) auditLogger.delete(getAuditIdFromRes(res), new CommentAuditView(videoCommentInstance.toFormattedJSON())) diff --git a/server/controllers/api/videos/index.ts b/server/controllers/api/videos/index.ts index fc1bcc73d..61a030ba1 100644 --- a/server/controllers/api/videos/index.ts +++ b/server/controllers/api/videos/index.ts @@ -158,7 +158,7 @@ async function viewVideo (req: express.Request, res: express.Response) { const serverActor = await getServerActor() await sendView(serverActor, video, undefined) - Hooks.runAction('action:api.video.viewed', { video: video, ip }) + Hooks.runAction('action:api.video.viewed', { video: video, ip, req, res }) } return res.status(HttpStatusCode.NO_CONTENT_204).end() @@ -201,7 +201,7 @@ async function listVideos (req: express.Request, res: express.Response) { return res.json(getFormattedObjects(resultList.data, resultList.total, guessAdditionalAttributesFromQuery(query))) } -async function removeVideo (_req: express.Request, res: express.Response) { +async function removeVideo (req: express.Request, res: express.Response) { const videoInstance = res.locals.videoAll await sequelizeTypescript.transaction(async t => { @@ -211,7 +211,7 @@ async function removeVideo (_req: express.Request, res: express.Response) { auditLogger.delete(getAuditIdFromRes(res), new VideoAuditView(videoInstance.toFormattedDetailsJSON())) logger.info('Video with name %s and uuid %s deleted.', videoInstance.name, videoInstance.uuid) - Hooks.runAction('action:api.video.deleted', { video: videoInstance }) + Hooks.runAction('action:api.video.deleted', { video: videoInstance, req, res }) return res.type('json') .status(HttpStatusCode.NO_CONTENT_204) diff --git a/server/controllers/api/videos/live.ts b/server/controllers/api/videos/live.ts index efafe64e9..e29615ff5 100644 --- a/server/controllers/api/videos/live.ts +++ b/server/controllers/api/videos/live.ts @@ -133,7 +133,7 @@ async function addLiveVideo (req: express.Request, res: express.Response) { return { videoCreated } }) - Hooks.runAction('action:api.live-video.created', { video: videoCreated }) + Hooks.runAction('action:api.live-video.created', { video: videoCreated, req, res }) return res.json({ video: { diff --git a/server/controllers/api/videos/update.ts b/server/controllers/api/videos/update.ts index de5d94d55..3fcff3e86 100644 --- a/server/controllers/api/videos/update.ts +++ b/server/controllers/api/videos/update.ts @@ -153,7 +153,7 @@ async function updateVideo (req: express.Request, res: express.Response) { Notifier.Instance.notifyOnNewVideoIfNeeded(videoInstanceUpdated) } - Hooks.runAction('action:api.video.updated', { video: videoInstanceUpdated, body: req.body }) + Hooks.runAction('action:api.video.updated', { video: videoInstanceUpdated, body: req.body, req, res }) } catch (err) { // Force fields we want to update // If the transaction is retried, sequelize will think the object has not changed diff --git a/server/controllers/api/videos/upload.ts b/server/controllers/api/videos/upload.ts index 3e9979330..6773b500f 100644 --- a/server/controllers/api/videos/upload.ts +++ b/server/controllers/api/videos/upload.ts @@ -129,7 +129,7 @@ async function addVideoLegacy (req: express.Request, res: express.Response) { const videoInfo: VideoCreate = req.body const files = req.files - const response = await addVideo({ res, videoPhysicalFile, videoInfo, files }) + const response = await addVideo({ req, res, videoPhysicalFile, videoInfo, files }) return res.json(response) } @@ -139,19 +139,20 @@ async function addVideoResumable (req: express.Request, res: express.Response) { const videoInfo = videoPhysicalFile.metadata const files = { previewfile: videoInfo.previewfile } - const response = await addVideo({ res, videoPhysicalFile, videoInfo, files }) + const response = await addVideo({ req, res, videoPhysicalFile, videoInfo, files }) await Redis.Instance.setUploadSession(req.query.upload_id, response) return res.json(response) } async function addVideo (options: { + req: express.Request res: express.Response videoPhysicalFile: express.VideoUploadFile videoInfo: VideoCreate files: express.UploadFiles }) { - const { res, videoPhysicalFile, videoInfo, files } = options + const { req, res, videoPhysicalFile, videoInfo, files } = options const videoChannel = res.locals.videoChannel const user = res.locals.oauth.token.User @@ -235,7 +236,7 @@ async function addVideo (options: { }) .catch(err => logger.error('Cannot add optimize/merge audio job for %s.', videoCreated.uuid, { err, ...lTags(videoCreated.uuid) })) - Hooks.runAction('action:api.video.uploaded', { video: videoCreated }) + Hooks.runAction('action:api.video.uploaded', { video: videoCreated, req, res }) return { video: { -- cgit v1.2.3