]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/controllers/api/videos/index.ts
Add ability to choose what policy we have for NSFW videos
[github/Chocobozzz/PeerTube.git] / server / controllers / api / videos / index.ts
index c3d3acd26fb9cca844f2c2266465fed724d2b37a..6e8601fa1c65cdab1c8e3b81961f91c31c6a718d 100644 (file)
@@ -3,7 +3,7 @@ import { extname, join } from 'path'
 import { VideoCreate, VideoPrivacy, VideoUpdate } from '../../../../shared'
 import { renamePromise } from '../../../helpers/core-utils'
 import { retryTransactionWrapper } from '../../../helpers/database-utils'
-import { getVideoFileHeight } from '../../../helpers/ffmpeg-utils'
+import { getVideoFileResolution } from '../../../helpers/ffmpeg-utils'
 import { processImage } from '../../../helpers/image-utils'
 import { logger } from '../../../helpers/logger'
 import { createReqFiles, getFormattedObjects, getServerActor, resetSequelizeInstance } from '../../../helpers/utils'
@@ -19,13 +19,18 @@ import {
   VIDEO_MIMETYPE_EXT,
   VIDEO_PRIVACIES
 } from '../../../initializers'
-import { fetchRemoteVideoDescription, getVideoActivityPubUrl, shareVideoByServerAndChannel } from '../../../lib/activitypub'
-import { sendCreateVideo, sendCreateViewToOrigin, sendCreateViewToVideoFollowers, sendUpdateVideo } from '../../../lib/activitypub/send'
+import {
+  fetchRemoteVideoDescription,
+  getVideoActivityPubUrl,
+  shareVideoByServerAndChannel
+} from '../../../lib/activitypub'
+import { sendCreateVideo, sendCreateView, sendUpdateVideo } from '../../../lib/activitypub/send'
 import { JobQueue } from '../../../lib/job-queue'
 import { Redis } from '../../../lib/redis'
 import {
   asyncMiddleware,
   authenticate,
+  optionalAuthenticate,
   paginationValidator,
   setDefaultPagination,
   setDefaultSort,
@@ -44,6 +49,9 @@ import { blacklistRouter } from './blacklist'
 import { videoChannelRouter } from './channel'
 import { videoCommentRouter } from './comment'
 import { rateVideoRouter } from './rate'
+import { User } from '../../../../shared/models/users'
+import { VideoFilter } from '../../../../shared/models/videos/video-query.type'
+import { VideoSortField } from '../../../../client/src/app/shared/video/sort-field.type'
 
 const videosRouter = express.Router()
 
@@ -81,6 +89,7 @@ videosRouter.get('/',
   videosSortValidator,
   setDefaultSort,
   setDefaultPagination,
+  optionalAuthenticate,
   asyncMiddleware(listVideos)
 )
 videosRouter.get('/search',
@@ -89,6 +98,7 @@ videosRouter.get('/search',
   videosSortValidator,
   setDefaultSort,
   setDefaultPagination,
+  optionalAuthenticate,
   asyncMiddleware(searchVideos)
 )
 videosRouter.put('/:id',
@@ -187,11 +197,11 @@ async function addVideo (req: express.Request, res: express.Response, videoPhysi
   const video = new VideoModel(videoData)
   video.url = getVideoActivityPubUrl(video)
 
-  const videoFileHeight = await getVideoFileHeight(videoPhysicalFile.path)
+  const { videoFileResolution } = await getVideoFileResolution(videoPhysicalFile.path)
 
   const videoFileData = {
     extname: extname(videoPhysicalFile.filename),
-    resolution: videoFileHeight,
+    resolution: videoFileResolution,
     size: videoPhysicalFile.size
   }
   const videoFile = new VideoFileModel(videoFileData)
@@ -307,10 +317,17 @@ async function updateVideo (req: express.Request, res: express.Response) {
       if (videoInfoToUpdate.licence !== undefined) videoInstance.set('licence', videoInfoToUpdate.licence)
       if (videoInfoToUpdate.language !== undefined) videoInstance.set('language', videoInfoToUpdate.language)
       if (videoInfoToUpdate.nsfw !== undefined) videoInstance.set('nsfw', videoInfoToUpdate.nsfw)
-      if (videoInfoToUpdate.privacy !== undefined) videoInstance.set('privacy', parseInt(videoInfoToUpdate.privacy.toString(), 10))
       if (videoInfoToUpdate.support !== undefined) videoInstance.set('support', videoInfoToUpdate.support)
       if (videoInfoToUpdate.description !== undefined) videoInstance.set('description', videoInfoToUpdate.description)
       if (videoInfoToUpdate.commentsEnabled !== undefined) videoInstance.set('commentsEnabled', videoInfoToUpdate.commentsEnabled)
+      if (videoInfoToUpdate.privacy !== undefined) {
+        const newPrivacy = parseInt(videoInfoToUpdate.privacy.toString(), 10)
+        videoInstance.set('privacy', newPrivacy)
+
+        if (wasPrivateVideo === true && newPrivacy !== VideoPrivacy.PRIVATE) {
+          videoInstance.set('publishedAt', new Date())
+        }
+      }
 
       const videoInstanceUpdated = await videoInstance.save(sequelizeOptions)
 
@@ -365,11 +382,7 @@ async function viewVideo (req: express.Request, res: express.Response) {
 
   const serverAccount = await getServerActor()
 
-  if (videoInstance.isOwned()) {
-    await sendCreateViewToVideoFollowers(serverAccount, videoInstance, undefined)
-  } else {
-    await sendCreateViewToOrigin(serverAccount, videoInstance, undefined)
-  }
+  await sendCreateView(serverAccount, videoInstance, undefined)
 
   return res.status(204).end()
 }
@@ -388,7 +401,13 @@ async function getVideoDescription (req: express.Request, res: express.Response)
 }
 
 async function listVideos (req: express.Request, res: express.Response, next: express.NextFunction) {
-  const resultList = await VideoModel.listForApi(req.query.start, req.query.count, req.query.sort)
+  const resultList = await VideoModel.listForApi(
+    req.query.start as number,
+    req.query.count as number,
+    req.query.sort as VideoSortField,
+    isNSFWHidden(res),
+    req.query.filter as VideoFilter
+  )
 
   return res.json(getFormattedObjects(resultList.data, resultList.total))
 }
@@ -415,12 +434,22 @@ async function removeVideo (req: express.Request, res: express.Response) {
 }
 
 async function searchVideos (req: express.Request, res: express.Response, next: express.NextFunction) {
-  const resultList = await VideoModel.searchAndPopulateAccountAndServerAndTags(
-    req.query.search,
-    req.query.start,
-    req.query.count,
-    req.query.sort
+  const resultList = await VideoModel.searchAndPopulateAccountAndServer(
+    req.query.search as string,
+    req.query.start as number,
+    req.query.count as number,
+    req.query.sort as VideoSortField,
+    isNSFWHidden(res)
   )
 
   return res.json(getFormattedObjects(resultList.data, resultList.total))
 }
+
+function isNSFWHidden (res: express.Response) {
+  if (res.locals.oauth) {
+    const user: User = res.locals.oauth.token.User
+    if (user) return user.nsfwPolicy === 'do_not_list'
+  }
+
+  return CONFIG.INSTANCE.DEFAULT_NSFW_POLICY === 'do_not_list'
+}