]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/controllers/api/video-channel.ts
Add user video list hooks
[github/Chocobozzz/PeerTube.git] / server / controllers / api / video-channel.ts
index 45c936978779fec268e67eb83a444d6e8919c6b9..7ac01b0efe6bb139686c833971018aa3d1ce35c4 100644 (file)
@@ -1,4 +1,5 @@
 import * as express from 'express'
+import { Hooks } from '@server/lib/plugins/hooks'
 import { getServerActor } from '@server/models/application/application'
 import { MChannelAccountDefault } from '@server/types/models'
 import { VideoChannelCreate, VideoChannelUpdate } from '../../../shared'
@@ -12,7 +13,7 @@ import { MIMETYPES } from '../../initializers/constants'
 import { sequelizeTypescript } from '../../initializers/database'
 import { setAsyncActorKeys } from '../../lib/activitypub/actor'
 import { sendUpdateActor } from '../../lib/activitypub/send'
-import { updateActorAvatarFile } from '../../lib/avatar'
+import { deleteActorAvatarFile, updateActorAvatarFile } from '../../lib/avatar'
 import { JobQueue } from '../../lib/job-queue'
 import { createLocalVideoChannel, federateAllVideosOfChannel } from '../../lib/video-channel'
 import {
@@ -38,6 +39,7 @@ import { AccountModel } from '../../models/account/account'
 import { VideoModel } from '../../models/video/video'
 import { VideoChannelModel } from '../../models/video/video-channel'
 import { VideoPlaylistModel } from '../../models/video/video-playlist'
+import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes'
 
 const auditLogger = auditLoggerFactory('channels')
 const reqAvatarFile = createReqFiles([ 'avatarfile' ], MIMETYPES.IMAGE.MIMETYPE_EXT, { avatarfile: CONFIG.STORAGE.TMP_DIR })
@@ -68,6 +70,13 @@ videoChannelRouter.post('/:nameWithHost/avatar/pick',
   asyncMiddleware(updateVideoChannelAvatar)
 )
 
+videoChannelRouter.delete('/:nameWithHost/avatar',
+  authenticate,
+  // Check the rights
+  asyncMiddleware(videoChannelsUpdateValidator),
+  asyncMiddleware(deleteVideoChannelAvatar)
+)
+
 videoChannelRouter.put('/:nameWithHost',
   authenticate,
   asyncMiddleware(videoChannelsUpdateValidator),
@@ -131,7 +140,7 @@ async function updateVideoChannelAvatar (req: express.Request, res: express.Resp
   const videoChannel = res.locals.videoChannel
   const oldVideoChannelAuditKeys = new VideoChannelAuditView(videoChannel.toFormattedJSON())
 
-  const avatar = await updateActorAvatarFile(avatarPhysicalFile, videoChannel)
+  const avatar = await updateActorAvatarFile(videoChannel, avatarPhysicalFile)
 
   auditLogger.update(getAuditIdFromRes(res), new VideoChannelAuditView(videoChannel.toFormattedJSON()), oldVideoChannelAuditKeys)
 
@@ -142,6 +151,14 @@ async function updateVideoChannelAvatar (req: express.Request, res: express.Resp
     .end()
 }
 
+async function deleteVideoChannelAvatar (req: express.Request, res: express.Response) {
+  const videoChannel = res.locals.videoChannel
+
+  await deleteActorAvatarFile(videoChannel)
+
+  return res.sendStatus(HttpStatusCode.NO_CONTENT_204)
+}
+
 async function addVideoChannel (req: express.Request, res: express.Response) {
   const videoChannelInfo: VideoChannelCreate = req.body
 
@@ -212,7 +229,7 @@ async function updateVideoChannel (req: express.Request, res: express.Response)
     throw err
   }
 
-  res.type('json').status(204).end()
+  res.type('json').status(HttpStatusCode.NO_CONTENT_204).end()
 
   // Don't process in a transaction, and after the response because it could be long
   if (doBulkVideoUpdate) {
@@ -232,7 +249,7 @@ async function removeVideoChannel (req: express.Request, res: express.Response)
     logger.info('Video channel %s deleted.', videoChannelInstance.Actor.url)
   })
 
-  return res.type('json').status(204).end()
+  return res.type('json').status(HttpStatusCode.NO_CONTENT_204).end()
 }
 
 async function getVideoChannel (req: express.Request, res: express.Response) {
@@ -265,7 +282,7 @@ async function listVideoChannelVideos (req: express.Request, res: express.Respon
   const followerActorId = isUserAbleToSearchRemoteURI(res) ? null : undefined
   const countVideos = getCountVideos(req)
 
-  const resultList = await VideoModel.listForApi({
+  const apiOptions = await Hooks.wrapObject({
     followerActorId,
     start: req.query.start,
     count: req.query.count,
@@ -282,7 +299,13 @@ async function listVideoChannelVideos (req: express.Request, res: express.Respon
     videoChannelId: videoChannelInstance.id,
     user: res.locals.oauth ? res.locals.oauth.token.User : undefined,
     countVideos
-  })
+  }, 'filter:api.video-channels.videos.list.params')
+
+  const resultList = await Hooks.wrapPromiseFun(
+    VideoModel.listForApi,
+    apiOptions,
+    'filter:api.video-channels.videos.list.result'
+  )
 
   return res.json(getFormattedObjects(resultList.data, resultList.total))
 }