From b4055e1c23eeefb0c8a85a77f312b2827d98f483 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Thu, 18 Jul 2019 14:28:37 +0200 Subject: Add server hooks --- server/controllers/api/videos/comment.ts | 36 ++++++++++++++++++++++++++------ server/controllers/api/videos/index.ts | 36 +++++++++++++++++++++----------- 2 files changed, 54 insertions(+), 18 deletions(-) (limited to 'server/controllers') 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' import { auditLoggerFactory, CommentAuditView, getAuditIdFromRes } from '../../../helpers/audit-logger' import { AccountModel } from '../../../models/account/account' import { Notifier } from '../../../lib/notifier' +import { Hooks } from '../../../lib/plugins/hooks' const auditLogger = auditLoggerFactory('comments') const videoCommentRouter = express.Router() @@ -76,7 +77,18 @@ async function listVideoThreads (req: express.Request, res: express.Response) { let resultList: ResultList if (video.commentsEnabled === true) { - resultList = await VideoCommentModel.listThreadsForApi(video.id, req.query.start, req.query.count, req.query.sort, user) + const apiOptions = await Hooks.wrapObject({ + videoId: video.id, + start: req.query.start, + count: req.query.count, + sort: req.query.sort, + user: user + }, 'filter:api.video-threads.list.params') + + resultList = await Hooks.wrapPromise( + VideoCommentModel.listThreadsForApi(apiOptions), + 'filter:api.video-threads.list.result' + ) } else { resultList = { total: 0, @@ -94,7 +106,16 @@ async function listVideoThreadComments (req: express.Request, res: express.Respo let resultList: ResultList if (video.commentsEnabled === true) { - resultList = await VideoCommentModel.listThreadCommentsForApi(video.id, res.locals.videoCommentThread.id, user) + const apiOptions = await Hooks.wrapObject({ + videoId: video.id, + threadId: res.locals.videoCommentThread.id, + user: user + }, 'filter:api.video-thread-comments.list.params') + + resultList = await Hooks.wrapPromise( + VideoCommentModel.listThreadCommentsForApi(apiOptions), + 'filter:api.video-thread-comments.list.result' + ) } else { resultList = { total: 0, @@ -122,6 +143,8 @@ 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 }) + return res.json({ comment: comment.toFormattedJSON() }).end() @@ -144,6 +167,8 @@ 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 }) + return res.json({ comment: comment.toFormattedJSON() }).end() } @@ -154,11 +179,10 @@ async function removeVideoComment (req: express.Request, res: express.Response) await videoCommentInstance.destroy({ transaction: t }) }) - auditLogger.delete( - getAuditIdFromRes(res), - new CommentAuditView(videoCommentInstance.toFormattedJSON()) - ) + auditLogger.delete(getAuditIdFromRes(res), new CommentAuditView(videoCommentInstance.toFormattedJSON())) logger.info('Video comment %d deleted.', videoCommentInstance.id) + Hooks.runAction('action:api.video-comment.deleted', { comment: videoCommentInstance }) + return res.type('json').status(204).end() } 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' import { createVideoMiniatureFromExisting, generateVideoMiniature } from '../../../lib/thumbnail' import { ThumbnailType } from '../../../../shared/models/videos/thumbnail.type' import { VideoTranscodingPayload } from '../../../lib/job-queue/handlers/video-transcoding' +import { Hooks } from '../../../lib/plugins/hooks' const auditLogger = auditLoggerFactory('videos') const videosRouter = express.Router() @@ -268,10 +269,7 @@ async function addVideo (req: express.Request, res: express.Response) { } const videoWasAutoBlacklisted = await autoBlacklistVideoIfNeeded(video, res.locals.oauth.token.User, t) - - if (!videoWasAutoBlacklisted) { - await federateVideoIfNeeded(video, true, t) - } + if (!videoWasAutoBlacklisted) await federateVideoIfNeeded(video, true, t) auditLogger.create(getAuditIdFromRes(res), new VideoAuditView(videoCreated.toFormattedDetailsJSON())) 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) { return { videoCreated, videoWasAutoBlacklisted } }) - if (videoWasAutoBlacklisted) { - Notifier.Instance.notifyOnVideoAutoBlacklist(videoCreated) - } else { - Notifier.Instance.notifyOnNewVideo(videoCreated) - } + if (videoWasAutoBlacklisted) Notifier.Instance.notifyOnVideoAutoBlacklist(videoCreated) + else Notifier.Instance.notifyOnNewVideo(videoCreated) if (video.state === VideoState.TO_TRANSCODE) { // 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) { await JobQueue.Instance.createJob({ type: 'video-transcoding', payload: dataInput }) } + Hooks.runAction('action:api.video.uploaded', { video: videoCreated }) + return res.json({ video: { id: videoCreated.id, @@ -421,6 +418,8 @@ async function updateVideo (req: express.Request, res: express.Response) { if (wasUnlistedVideo || wasPrivateVideo) { Notifier.Instance.notifyOnNewVideo(videoInstanceUpdated) } + + Hooks.runAction('action:api.video.updated', { video: videoInstanceUpdated }) } catch (err) { // Force fields we want to update // 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) { async function getVideo (req: express.Request, res: express.Response) { // We need more attributes const userId: number = res.locals.oauth ? res.locals.oauth.token.User.id : null - const video = await VideoModel.loadForGetAPI(res.locals.video.id, undefined, userId) + + const video = await Hooks.wrapPromise( + VideoModel.loadForGetAPI(res.locals.video.id, undefined, userId), + 'filter:api.video.get.result' + ) if (video.isOutdated()) { 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) { const serverActor = await getServerActor() await sendView(serverActor, videoInstance, undefined) + Hooks.runAction('action:api.video.viewed', { video: videoInstance, ip }) + return res.status(204).end() } @@ -481,7 +486,7 @@ async function getVideoDescription (req: express.Request, res: express.Response) } async function listVideos (req: express.Request, res: express.Response) { - const resultList = await VideoModel.listForApi({ + const apiOptions = await Hooks.wrapObject({ start: req.query.start, count: req.query.count, sort: req.query.sort, @@ -495,7 +500,12 @@ async function listVideos (req: express.Request, res: express.Response) { filter: req.query.filter as VideoFilter, withFiles: false, user: res.locals.oauth ? res.locals.oauth.token.User : undefined - }) + }, 'filter:api.videos.list.params') + + const resultList = await Hooks.wrapPromise( + VideoModel.listForApi(apiOptions), + 'filter:api.videos.list.result' + ) return res.json(getFormattedObjects(resultList.data, resultList.total)) } @@ -510,5 +520,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 }) + return res.type('json').status(204).end() } -- cgit v1.2.3