X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Fcontrollers%2Fapi%2Fvideos%2Findex.ts;h=6e8601fa1c65cdab1c8e3b81961f91c31c6a718d;hb=0883b3245bf0deb9106c4041e9afbd3521b79280;hp=552e5edac74c08223a07feb3a3394f5ff4af6713;hpb=07197db4c567f22bbc9c12339062896dc76bac2f;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/controllers/api/videos/index.ts b/server/controllers/api/videos/index.ts index 552e5edac..6e8601fa1 100644 --- a/server/controllers/api/videos/index.ts +++ b/server/controllers/api/videos/index.ts @@ -19,13 +19,18 @@ import { VIDEO_MIMETYPE_EXT, VIDEO_PRIVACIES } from '../../../initializers' -import { fetchRemoteVideoDescription, getVideoActivityPubUrl, shareVideoByServerAndChannel } from '../../../lib/activitypub' +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', @@ -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) @@ -384,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, req.query.filter) + 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)) } @@ -411,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' +}