From fd45e8f43c2638478599ca75632518054461da85 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Tue, 31 Oct 2017 11:52:52 +0100 Subject: Add video privacy setting --- server/controllers/api/remote/videos.ts | 4 +++- server/controllers/api/users.ts | 21 ++++++++++++++++++++- server/controllers/api/videos/index.ts | 28 +++++++++++++++++++++++----- 3 files changed, 46 insertions(+), 7 deletions(-) (limited to 'server/controllers') diff --git a/server/controllers/api/remote/videos.ts b/server/controllers/api/remote/videos.ts index 3ecc62ada..cba47f0a1 100644 --- a/server/controllers/api/remote/videos.ts +++ b/server/controllers/api/remote/videos.ts @@ -267,7 +267,8 @@ async function addRemoteVideo (videoToCreateData: RemoteVideoCreateData, fromPod views: videoToCreateData.views, likes: videoToCreateData.likes, dislikes: videoToCreateData.dislikes, - remote: true + remote: true, + privacy: videoToCreateData.privacy } const video = db.Video.build(videoData) @@ -334,6 +335,7 @@ async function updateRemoteVideo (videoAttributesToUpdate: RemoteVideoUpdateData videoInstance.set('views', videoAttributesToUpdate.views) videoInstance.set('likes', videoAttributesToUpdate.likes) videoInstance.set('dislikes', videoAttributesToUpdate.dislikes) + videoInstance.set('privacy', videoAttributesToUpdate.privacy) await videoInstance.save(sequelizeOptions) diff --git a/server/controllers/api/users.ts b/server/controllers/api/users.ts index fdc9b0c87..dcd407fdf 100644 --- a/server/controllers/api/users.ts +++ b/server/controllers/api/users.ts @@ -30,6 +30,8 @@ import { } from '../../../shared' import { createUserAuthorAndChannel } from '../../lib' import { UserInstance } from '../../models' +import { videosSortValidator } from '../../middlewares/validators/sort' +import { setVideosSort } from '../../middlewares/sort' const usersRouter = express.Router() @@ -38,6 +40,15 @@ usersRouter.get('/me', asyncMiddleware(getUserInformation) ) +usersRouter.get('/me/videos', + authenticate, + paginationValidator, + videosSortValidator, + setVideosSort, + setPagination, + asyncMiddleware(getUserVideos) +) + usersRouter.get('/me/videos/:videoId/rating', authenticate, usersVideoRatingValidator, @@ -101,6 +112,13 @@ export { // --------------------------------------------------------------------------- +async function getUserVideos (req: express.Request, res: express.Response, next: express.NextFunction) { + const user = res.locals.oauth.token.User + const resultList = await db.Video.listUserVideosForApi(user.id ,req.query.start, req.query.count, req.query.sort) + + return res.json(getFormattedObjects(resultList.data, resultList.total)) +} + async function createUserRetryWrapper (req: express.Request, res: express.Response, next: express.NextFunction) { const options = { arguments: [ req, res ], @@ -146,13 +164,14 @@ async function registerUser (req: express.Request, res: express.Response, next: } async function getUserInformation (req: express.Request, res: express.Response, next: express.NextFunction) { + // We did not load channels in res.locals.user const user = await db.User.loadByUsernameAndPopulateChannels(res.locals.oauth.token.user.username) return res.json(user.toFormattedJSON()) } function getUser (req: express.Request, res: express.Response, next: express.NextFunction) { - return res.json(res.locals.user.toFormattedJSON()) + return res.json(res.locals.oauth.token.User.toFormattedJSON()) } async function getUserVideoRating (req: express.Request, res: express.Response, next: express.NextFunction) { diff --git a/server/controllers/api/videos/index.ts b/server/controllers/api/videos/index.ts index 49f0e4630..4dd09917b 100644 --- a/server/controllers/api/videos/index.ts +++ b/server/controllers/api/videos/index.ts @@ -9,7 +9,8 @@ import { REQUEST_VIDEO_EVENT_TYPES, VIDEO_CATEGORIES, VIDEO_LICENCES, - VIDEO_LANGUAGES + VIDEO_LANGUAGES, + VIDEO_PRIVACIES } from '../../../initializers' import { addEventToRemoteVideo, @@ -43,7 +44,7 @@ import { resetSequelizeInstance } from '../../../helpers' import { VideoInstance } from '../../../models' -import { VideoCreate, VideoUpdate } from '../../../../shared' +import { VideoCreate, VideoUpdate, VideoPrivacy } from '../../../../shared' import { abuseVideoRouter } from './abuse' import { blacklistRouter } from './blacklist' @@ -84,6 +85,7 @@ videosRouter.use('/', videoChannelRouter) videosRouter.get('/categories', listVideoCategories) videosRouter.get('/licences', listVideoLicences) videosRouter.get('/languages', listVideoLanguages) +videosRouter.get('/privacies', listVideoPrivacies) videosRouter.get('/', paginationValidator, @@ -149,6 +151,10 @@ function listVideoLanguages (req: express.Request, res: express.Response) { res.json(VIDEO_LANGUAGES) } +function listVideoPrivacies (req: express.Request, res: express.Response) { + res.json(VIDEO_PRIVACIES) +} + // Wrapper to video add that retry the function if there is a database error // We need this because we run the transaction in SERIALIZABLE isolation that can fail async function addVideoRetryWrapper (req: express.Request, res: express.Response, next: express.NextFunction) { @@ -179,6 +185,7 @@ async function addVideo (req: express.Request, res: express.Response, videoPhysi language: videoInfo.language, nsfw: videoInfo.nsfw, description: videoInfo.description, + privacy: videoInfo.privacy, duration: videoPhysicalFile['duration'], // duration was added by a previous middleware channelId: res.locals.videoChannel.id } @@ -240,6 +247,8 @@ async function addVideo (req: express.Request, res: express.Response, videoPhysi // Let transcoding job send the video to friends because the video file extension might change if (CONFIG.TRANSCODING.ENABLED === true) return undefined + // Don't send video to remote pods, it is private + if (video.privacy === VideoPrivacy.PRIVATE) return undefined const remoteVideo = await video.toAddRemoteJSON() // Now we'll add the video's meta data to our friends @@ -264,6 +273,7 @@ async function updateVideo (req: express.Request, res: express.Response) { const videoInstance = res.locals.video const videoFieldsSave = videoInstance.toJSON() const videoInfoToUpdate: VideoUpdate = req.body + const wasPrivateVideo = videoInstance.privacy === VideoPrivacy.PRIVATE try { await db.sequelize.transaction(async t => { @@ -276,6 +286,7 @@ 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', videoInfoToUpdate.privacy) if (videoInfoToUpdate.description !== undefined) videoInstance.set('description', videoInfoToUpdate.description) await videoInstance.save(sequelizeOptions) @@ -287,10 +298,17 @@ async function updateVideo (req: express.Request, res: express.Response) { videoInstance.Tags = tagInstances } - const json = videoInstance.toUpdateRemoteJSON() - // Now we'll update the video's meta data to our friends - return updateVideoToFriends(json, t) + if (wasPrivateVideo === false) { + const json = videoInstance.toUpdateRemoteJSON() + return updateVideoToFriends(json, t) + } + + // Video is not private anymore, send a create action to remote pods + if (wasPrivateVideo === true && videoInstance.privacy !== VideoPrivacy.PRIVATE) { + const remoteVideo = await videoInstance.toAddRemoteJSON() + return addVideoToFriends(remoteVideo, t) + } }) logger.info('Video with name %s and uuid %s updated.', videoInstance.name, videoInstance.uuid) -- cgit v1.2.3