]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/commitdiff
Add `req` and `res` as controllers hooks parameters
authorlutangar <johan.dufour@gmail.com>
Wed, 24 Nov 2021 13:33:14 +0000 (14:33 +0100)
committerChocobozzz <chocobozzz@cpy.re>
Thu, 25 Nov 2021 08:54:22 +0000 (09:54 +0100)
Hooks prefixed by `action:api` now give access the original express req and res.
Checkout guide.md for possible usage.

12 files changed:
server/controllers/api/bulk.ts
server/controllers/api/users/index.ts
server/controllers/api/users/token.ts
server/controllers/api/video-playlist.ts
server/controllers/api/videos/comment.ts
server/controllers/api/videos/index.ts
server/controllers/api/videos/live.ts
server/controllers/api/videos/update.ts
server/controllers/api/videos/upload.ts
server/lib/video-comment.ts
shared/models/plugins/server/server-hook.model.ts
support/doc/plugins/guide.md

index d27c3c73e64a435723d1abd96294bab6f70bbf36..51292175bfc31497fd8a993e9bedd9a3a6cb748f 100644 (file)
@@ -37,6 +37,6 @@ async function bulkRemoveCommentsOf (req: express.Request, res: express.Response
   res.status(HttpStatusCode.NO_CONTENT_204).end()
 
   for (const comment of comments) {
-    await removeComment(comment)
+    await removeComment(comment, req, res)
   }
 }
index bc47e5fec6bef5b1df55a5868c7cfc277be9a327..11d3525e4af85e05cf85e027e2ace1a6fd14ea17 100644 (file)
@@ -212,7 +212,7 @@ async function createUser (req: express.Request, res: express.Response) {
     await Emailer.Instance.addPasswordCreateEmailJob(userToCreate.username, user.email, url)
   }
 
-  Hooks.runAction('action:api.user.created', { body, user, account, videoChannel })
+  Hooks.runAction('action:api.user.created', { body, user, account, videoChannel, req, res })
 
   return res.json({
     user: {
@@ -254,7 +254,7 @@ async function registerUser (req: express.Request, res: express.Response) {
 
   Notifier.Instance.notifyOnNewUserRegistration(user)
 
-  Hooks.runAction('action:api.user.registered', { body, user, account, videoChannel })
+  Hooks.runAction('action:api.user.registered', { body, user, account, videoChannel, req, res })
 
   return res.type('json').status(HttpStatusCode.NO_CONTENT_204).end()
 }
@@ -264,7 +264,7 @@ async function unblockUser (req: express.Request, res: express.Response) {
 
   await changeUserBlock(res, user, false)
 
-  Hooks.runAction('action:api.user.unblocked', { user })
+  Hooks.runAction('action:api.user.unblocked', { user, req, res })
 
   return res.status(HttpStatusCode.NO_CONTENT_204).end()
 }
@@ -275,7 +275,7 @@ async function blockUser (req: express.Request, res: express.Response) {
 
   await changeUserBlock(res, user, true, reason)
 
-  Hooks.runAction('action:api.user.blocked', { user })
+  Hooks.runAction('action:api.user.blocked', { user, req, res })
 
   return res.status(HttpStatusCode.NO_CONTENT_204).end()
 }
@@ -312,7 +312,7 @@ async function removeUser (req: express.Request, res: express.Response) {
     await user.destroy({ transaction: t })
   })
 
-  Hooks.runAction('action:api.user.deleted', { user })
+  Hooks.runAction('action:api.user.deleted', { user, req, res })
 
   return res.status(HttpStatusCode.NO_CONTENT_204).end()
 }
@@ -345,7 +345,7 @@ async function updateUser (req: express.Request, res: express.Response) {
 
   auditLogger.update(getAuditIdFromRes(res), new UserAuditView(user.toFormattedJSON()), oldUserAuditView)
 
-  Hooks.runAction('action:api.user.updated', { user })
+  Hooks.runAction('action:api.user.updated', { user, req, res })
 
   // Don't need to send this update to followers, these attributes are not federated
 
index d5dbe921c727fe5b116889cc04902c9a8b7c80fd..1d4004ce02db4554fa8b8c73215017ccb50095b9 100644 (file)
@@ -66,7 +66,7 @@ async function handleToken (req: express.Request, res: express.Response, next: e
     res.set('Cache-Control', 'no-store')
     res.set('Pragma', 'no-cache')
 
-    Hooks.runAction('action:api.user.oauth2-got-token', { username: token.user.username, ip: req.ip })
+    Hooks.runAction('action:api.user.oauth2-got-token', { username: token.user.username, ip: req.ip, req, res })
 
     return res.json({
       token_type: 'Bearer',
index 2347e18d21aafcc2bf8ded5d5c4ee880c82b3f28..8b7a76718dcd7b2e4a1109770680cd8d875b1409 100644 (file)
@@ -334,7 +334,7 @@ async function addVideoInPlaylist (req: express.Request, res: express.Response)
 
   logger.info('Video added in playlist %s at position %d.', videoPlaylist.uuid, playlistElement.position)
 
-  Hooks.runAction('action:api.video-playlist-element.created', { playlistElement })
+  Hooks.runAction('action:api.video-playlist-element.created', { playlistElement, req, res })
 
   return res.json({
     videoPlaylistElement: {
index 23bba90891265ef4b41bf87f3330de0d4e98b52c..47fa2f2e20b2d5ec43221b655d624bccae6a6048 100644 (file)
@@ -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()))
 
index fc1bcc73d4e1ae1e6017d18fa990f2625fe27ea0..61a030ba144e18835557279dc05678f50dafbedc 100644 (file)
@@ -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)
index efafe64e9af7d6c15611d8ad3b8c56fca2497a4b..e29615ff5c0292f44ffe22541dcad03d2fffeaf6 100644 (file)
@@ -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: {
index de5d94d5524132d12790071caec97c550bb7541a..3fcff3e868041a8fc4f589bcd16e873e550ef723 100644 (file)
@@ -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
index 3e9979330070894fec88892f967e6ae4b060ac01..6773b500f4e0981ddc56a74cf9ea1d497a79ed39 100644 (file)
@@ -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: {
index c76570a5df3c0b9697f53f40b59fe145f5f12a46..02f160fe833f0970932462b9b7466551f55900d1 100644 (file)
@@ -1,5 +1,6 @@
 import { cloneDeep } from 'lodash'
 import * as Sequelize from 'sequelize'
+import express from 'express'
 import { logger } from '@server/helpers/logger'
 import { sequelizeTypescript } from '@server/initializers/database'
 import { ResultList } from '../../shared/models'
@@ -10,7 +11,7 @@ import { sendCreateVideoComment, sendDeleteVideoComment } from './activitypub/se
 import { getLocalVideoCommentActivityPubUrl } from './activitypub/url'
 import { Hooks } from './plugins/hooks'
 
-async function removeComment (videoCommentInstance: MCommentOwnerVideo) {
+async function removeComment (videoCommentInstance: MCommentOwnerVideo, req: express.Request, res: express.Response) {
   const videoCommentInstanceBefore = cloneDeep(videoCommentInstance)
 
   await sequelizeTypescript.transaction(async t => {
@@ -25,7 +26,7 @@ async function removeComment (videoCommentInstance: MCommentOwnerVideo) {
 
   logger.info('Video comment %d deleted.', videoCommentInstance.id)
 
-  Hooks.runAction('action:api.video-comment.deleted', { comment: videoCommentInstanceBefore })
+  Hooks.runAction('action:api.video-comment.deleted', { comment: videoCommentInstanceBefore, req, res })
 }
 
 async function createVideoComment (obj: {
index 562c6eb126deefa00c07d4c9ea9accfa39ea1ccc..3ab910197f054ea22993c104c4155c511125cd76 100644 (file)
@@ -85,6 +85,8 @@ export const serverActionHookObject = {
   // Fired when the application has been loaded and is listening HTTP requests
   'action:application.listening': true,
 
+  // API actions hooks give access to the original express `req` and `res` parameters
+
   // Fired when a local video is updated
   'action:api.video.updated': true,
   // Fired when a local video is deleted
index 072c7c6caebe4c9b7587c5b5e3aa550f033f9288..acf4718e4ef54211d8ed273fe4af43425bccb0fb 100644 (file)
@@ -108,6 +108,20 @@ async function register ({
 }
 ```
 
+Hooks prefixed by `action:api` also give access the original **express** [Request](http://expressjs.com/en/api.html#req) and [Response](http://expressjs.com/en/api.html#res):
+
+```js
+async function register ({
+  registerHook,
+  peertubeHelpers: { logger }
+}) {
+  registerHook({
+    target: 'action:api.video.updated',
+    handler: ({ req, res }) => logger.debug('original request parameters', { params: req.params })
+  })
+}
+```
+
 
 On client side, these hooks are registered by the `clientScripts` files defined in `package.json`.
 All client scripts have scopes so PeerTube client only loads scripts it needs: