aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/controllers/api
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2019-07-18 14:28:37 +0200
committerChocobozzz <chocobozzz@cpy.re>2019-07-24 10:58:16 +0200
commitb4055e1c23eeefb0c8a85a77f312b2827d98f483 (patch)
tree51b6b04c1ad10897047817d2eaaa037d1331fa6a /server/controllers/api
parent66e001c848c009412c65cbce41be344d8985fd83 (diff)
downloadPeerTube-b4055e1c23eeefb0c8a85a77f312b2827d98f483.tar.gz
PeerTube-b4055e1c23eeefb0c8a85a77f312b2827d98f483.tar.zst
PeerTube-b4055e1c23eeefb0c8a85a77f312b2827d98f483.zip
Add server hooks
Diffstat (limited to 'server/controllers/api')
-rw-r--r--server/controllers/api/videos/comment.ts36
-rw-r--r--server/controllers/api/videos/index.ts36
2 files changed, 54 insertions, 18 deletions
diff --git a/server/controllers/api/videos/comment.ts b/server/controllers/api/videos/comment.ts
index 176ee8bd4..a95392543 100644
--- a/server/controllers/api/videos/comment.ts
+++ b/server/controllers/api/videos/comment.ts
@@ -26,6 +26,7 @@ import { VideoCommentModel } from '../../../models/video/video-comment'
26import { auditLoggerFactory, CommentAuditView, getAuditIdFromRes } from '../../../helpers/audit-logger' 26import { auditLoggerFactory, CommentAuditView, getAuditIdFromRes } from '../../../helpers/audit-logger'
27import { AccountModel } from '../../../models/account/account' 27import { AccountModel } from '../../../models/account/account'
28import { Notifier } from '../../../lib/notifier' 28import { Notifier } from '../../../lib/notifier'
29import { Hooks } from '../../../lib/plugins/hooks'
29 30
30const auditLogger = auditLoggerFactory('comments') 31const auditLogger = auditLoggerFactory('comments')
31const videoCommentRouter = express.Router() 32const videoCommentRouter = express.Router()
@@ -76,7 +77,18 @@ async function listVideoThreads (req: express.Request, res: express.Response) {
76 let resultList: ResultList<VideoCommentModel> 77 let resultList: ResultList<VideoCommentModel>
77 78
78 if (video.commentsEnabled === true) { 79 if (video.commentsEnabled === true) {
79 resultList = await VideoCommentModel.listThreadsForApi(video.id, req.query.start, req.query.count, req.query.sort, user) 80 const apiOptions = await Hooks.wrapObject({
81 videoId: video.id,
82 start: req.query.start,
83 count: req.query.count,
84 sort: req.query.sort,
85 user: user
86 }, 'filter:api.video-threads.list.params')
87
88 resultList = await Hooks.wrapPromise(
89 VideoCommentModel.listThreadsForApi(apiOptions),
90 'filter:api.video-threads.list.result'
91 )
80 } else { 92 } else {
81 resultList = { 93 resultList = {
82 total: 0, 94 total: 0,
@@ -94,7 +106,16 @@ async function listVideoThreadComments (req: express.Request, res: express.Respo
94 let resultList: ResultList<VideoCommentModel> 106 let resultList: ResultList<VideoCommentModel>
95 107
96 if (video.commentsEnabled === true) { 108 if (video.commentsEnabled === true) {
97 resultList = await VideoCommentModel.listThreadCommentsForApi(video.id, res.locals.videoCommentThread.id, user) 109 const apiOptions = await Hooks.wrapObject({
110 videoId: video.id,
111 threadId: res.locals.videoCommentThread.id,
112 user: user
113 }, 'filter:api.video-thread-comments.list.params')
114
115 resultList = await Hooks.wrapPromise(
116 VideoCommentModel.listThreadCommentsForApi(apiOptions),
117 'filter:api.video-thread-comments.list.result'
118 )
98 } else { 119 } else {
99 resultList = { 120 resultList = {
100 total: 0, 121 total: 0,
@@ -122,6 +143,8 @@ async function addVideoCommentThread (req: express.Request, res: express.Respons
122 Notifier.Instance.notifyOnNewComment(comment) 143 Notifier.Instance.notifyOnNewComment(comment)
123 auditLogger.create(getAuditIdFromRes(res), new CommentAuditView(comment.toFormattedJSON())) 144 auditLogger.create(getAuditIdFromRes(res), new CommentAuditView(comment.toFormattedJSON()))
124 145
146 Hooks.runAction('action:api.video-thread.created', { comment })
147
125 return res.json({ 148 return res.json({
126 comment: comment.toFormattedJSON() 149 comment: comment.toFormattedJSON()
127 }).end() 150 }).end()
@@ -144,6 +167,8 @@ async function addVideoCommentReply (req: express.Request, res: express.Response
144 Notifier.Instance.notifyOnNewComment(comment) 167 Notifier.Instance.notifyOnNewComment(comment)
145 auditLogger.create(getAuditIdFromRes(res), new CommentAuditView(comment.toFormattedJSON())) 168 auditLogger.create(getAuditIdFromRes(res), new CommentAuditView(comment.toFormattedJSON()))
146 169
170 Hooks.runAction('action:api.video-comment-reply.created', { comment })
171
147 return res.json({ comment: comment.toFormattedJSON() }).end() 172 return res.json({ comment: comment.toFormattedJSON() }).end()
148} 173}
149 174
@@ -154,11 +179,10 @@ async function removeVideoComment (req: express.Request, res: express.Response)
154 await videoCommentInstance.destroy({ transaction: t }) 179 await videoCommentInstance.destroy({ transaction: t })
155 }) 180 })
156 181
157 auditLogger.delete( 182 auditLogger.delete(getAuditIdFromRes(res), new CommentAuditView(videoCommentInstance.toFormattedJSON()))
158 getAuditIdFromRes(res),
159 new CommentAuditView(videoCommentInstance.toFormattedJSON())
160 )
161 logger.info('Video comment %d deleted.', videoCommentInstance.id) 183 logger.info('Video comment %d deleted.', videoCommentInstance.id)
162 184
185 Hooks.runAction('action:api.video-comment.deleted', { comment: videoCommentInstance })
186
163 return res.type('json').status(204).end() 187 return res.type('json').status(204).end()
164} 188}
diff --git a/server/controllers/api/videos/index.ts b/server/controllers/api/videos/index.ts
index 5ebd8fbc4..a3b1dde29 100644
--- a/server/controllers/api/videos/index.ts
+++ b/server/controllers/api/videos/index.ts
@@ -62,6 +62,7 @@ import { sequelizeTypescript } from '../../../initializers/database'
62import { createVideoMiniatureFromExisting, generateVideoMiniature } from '../../../lib/thumbnail' 62import { createVideoMiniatureFromExisting, generateVideoMiniature } from '../../../lib/thumbnail'
63import { ThumbnailType } from '../../../../shared/models/videos/thumbnail.type' 63import { ThumbnailType } from '../../../../shared/models/videos/thumbnail.type'
64import { VideoTranscodingPayload } from '../../../lib/job-queue/handlers/video-transcoding' 64import { VideoTranscodingPayload } from '../../../lib/job-queue/handlers/video-transcoding'
65import { Hooks } from '../../../lib/plugins/hooks'
65 66
66const auditLogger = auditLoggerFactory('videos') 67const auditLogger = auditLoggerFactory('videos')
67const videosRouter = express.Router() 68const videosRouter = express.Router()
@@ -268,10 +269,7 @@ async function addVideo (req: express.Request, res: express.Response) {
268 } 269 }
269 270
270 const videoWasAutoBlacklisted = await autoBlacklistVideoIfNeeded(video, res.locals.oauth.token.User, t) 271 const videoWasAutoBlacklisted = await autoBlacklistVideoIfNeeded(video, res.locals.oauth.token.User, t)
271 272 if (!videoWasAutoBlacklisted) await federateVideoIfNeeded(video, true, t)
272 if (!videoWasAutoBlacklisted) {
273 await federateVideoIfNeeded(video, true, t)
274 }
275 273
276 auditLogger.create(getAuditIdFromRes(res), new VideoAuditView(videoCreated.toFormattedDetailsJSON())) 274 auditLogger.create(getAuditIdFromRes(res), new VideoAuditView(videoCreated.toFormattedDetailsJSON()))
277 logger.info('Video with name %s and uuid %s created.', videoInfo.name, videoCreated.uuid) 275 logger.info('Video with name %s and uuid %s created.', videoInfo.name, videoCreated.uuid)
@@ -279,11 +277,8 @@ async function addVideo (req: express.Request, res: express.Response) {
279 return { videoCreated, videoWasAutoBlacklisted } 277 return { videoCreated, videoWasAutoBlacklisted }
280 }) 278 })
281 279
282 if (videoWasAutoBlacklisted) { 280 if (videoWasAutoBlacklisted) Notifier.Instance.notifyOnVideoAutoBlacklist(videoCreated)
283 Notifier.Instance.notifyOnVideoAutoBlacklist(videoCreated) 281 else Notifier.Instance.notifyOnNewVideo(videoCreated)
284 } else {
285 Notifier.Instance.notifyOnNewVideo(videoCreated)
286 }
287 282
288 if (video.state === VideoState.TO_TRANSCODE) { 283 if (video.state === VideoState.TO_TRANSCODE) {
289 // Put uuid because we don't have id auto incremented for now 284 // Put uuid because we don't have id auto incremented for now
@@ -307,6 +302,8 @@ async function addVideo (req: express.Request, res: express.Response) {
307 await JobQueue.Instance.createJob({ type: 'video-transcoding', payload: dataInput }) 302 await JobQueue.Instance.createJob({ type: 'video-transcoding', payload: dataInput })
308 } 303 }
309 304
305 Hooks.runAction('action:api.video.uploaded', { video: videoCreated })
306
310 return res.json({ 307 return res.json({
311 video: { 308 video: {
312 id: videoCreated.id, 309 id: videoCreated.id,
@@ -421,6 +418,8 @@ async function updateVideo (req: express.Request, res: express.Response) {
421 if (wasUnlistedVideo || wasPrivateVideo) { 418 if (wasUnlistedVideo || wasPrivateVideo) {
422 Notifier.Instance.notifyOnNewVideo(videoInstanceUpdated) 419 Notifier.Instance.notifyOnNewVideo(videoInstanceUpdated)
423 } 420 }
421
422 Hooks.runAction('action:api.video.updated', { video: videoInstanceUpdated })
424 } catch (err) { 423 } catch (err) {
425 // Force fields we want to update 424 // Force fields we want to update
426 // If the transaction is retried, sequelize will think the object has not changed 425 // If the transaction is retried, sequelize will think the object has not changed
@@ -436,7 +435,11 @@ async function updateVideo (req: express.Request, res: express.Response) {
436async function getVideo (req: express.Request, res: express.Response) { 435async function getVideo (req: express.Request, res: express.Response) {
437 // We need more attributes 436 // We need more attributes
438 const userId: number = res.locals.oauth ? res.locals.oauth.token.User.id : null 437 const userId: number = res.locals.oauth ? res.locals.oauth.token.User.id : null
439 const video = await VideoModel.loadForGetAPI(res.locals.video.id, undefined, userId) 438
439 const video = await Hooks.wrapPromise(
440 VideoModel.loadForGetAPI(res.locals.video.id, undefined, userId),
441 'filter:api.video.get.result'
442 )
440 443
441 if (video.isOutdated()) { 444 if (video.isOutdated()) {
442 JobQueue.Instance.createJob({ type: 'activitypub-refresher', payload: { type: 'video', url: video.url } }) 445 JobQueue.Instance.createJob({ type: 'activitypub-refresher', payload: { type: 'video', url: video.url } })
@@ -464,6 +467,8 @@ async function viewVideo (req: express.Request, res: express.Response) {
464 const serverActor = await getServerActor() 467 const serverActor = await getServerActor()
465 await sendView(serverActor, videoInstance, undefined) 468 await sendView(serverActor, videoInstance, undefined)
466 469
470 Hooks.runAction('action:api.video.viewed', { video: videoInstance, ip })
471
467 return res.status(204).end() 472 return res.status(204).end()
468} 473}
469 474
@@ -481,7 +486,7 @@ async function getVideoDescription (req: express.Request, res: express.Response)
481} 486}
482 487
483async function listVideos (req: express.Request, res: express.Response) { 488async function listVideos (req: express.Request, res: express.Response) {
484 const resultList = await VideoModel.listForApi({ 489 const apiOptions = await Hooks.wrapObject({
485 start: req.query.start, 490 start: req.query.start,
486 count: req.query.count, 491 count: req.query.count,
487 sort: req.query.sort, 492 sort: req.query.sort,
@@ -495,7 +500,12 @@ async function listVideos (req: express.Request, res: express.Response) {
495 filter: req.query.filter as VideoFilter, 500 filter: req.query.filter as VideoFilter,
496 withFiles: false, 501 withFiles: false,
497 user: res.locals.oauth ? res.locals.oauth.token.User : undefined 502 user: res.locals.oauth ? res.locals.oauth.token.User : undefined
498 }) 503 }, 'filter:api.videos.list.params')
504
505 const resultList = await Hooks.wrapPromise(
506 VideoModel.listForApi(apiOptions),
507 'filter:api.videos.list.result'
508 )
499 509
500 return res.json(getFormattedObjects(resultList.data, resultList.total)) 510 return res.json(getFormattedObjects(resultList.data, resultList.total))
501} 511}
@@ -510,5 +520,7 @@ async function removeVideo (req: express.Request, res: express.Response) {
510 auditLogger.delete(getAuditIdFromRes(res), new VideoAuditView(videoInstance.toFormattedDetailsJSON())) 520 auditLogger.delete(getAuditIdFromRes(res), new VideoAuditView(videoInstance.toFormattedDetailsJSON()))
511 logger.info('Video with name %s and uuid %s deleted.', videoInstance.name, videoInstance.uuid) 521 logger.info('Video with name %s and uuid %s deleted.', videoInstance.name, videoInstance.uuid)
512 522
523 Hooks.runAction('action:api.video.deleted', { video: videoInstance })
524
513 return res.type('json').status(204).end() 525 return res.type('json').status(204).end()
514} 526}