From 0883b3245bf0deb9106c4041e9afbd3521b79280 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Thu, 19 Apr 2018 11:01:34 +0200 Subject: Add ability to choose what policy we have for NSFW videos There is a global instance setting and a per user setting --- server/controllers/api/config.ts | 3 +++ server/controllers/api/users.ts | 15 ++++++++++---- server/controllers/api/videos/index.ts | 38 ++++++++++++++++++++++++++++------ 3 files changed, 46 insertions(+), 10 deletions(-) (limited to 'server/controllers/api') diff --git a/server/controllers/api/config.ts b/server/controllers/api/config.ts index 88f047adc..e47b71f44 100644 --- a/server/controllers/api/config.ts +++ b/server/controllers/api/config.ts @@ -46,6 +46,7 @@ async function getConfig (req: express.Request, res: express.Response, next: exp name: CONFIG.INSTANCE.NAME, shortDescription: CONFIG.INSTANCE.SHORT_DESCRIPTION, defaultClientRoute: CONFIG.INSTANCE.DEFAULT_CLIENT_ROUTE, + defaultNSFWPolicy: CONFIG.INSTANCE.DEFAULT_NSFW_POLICY, customizations: { javascript: CONFIG.INSTANCE.CUSTOMIZATIONS.JAVASCRIPT, css: CONFIG.INSTANCE.CUSTOMIZATIONS.CSS @@ -128,6 +129,7 @@ async function updateCustomConfig (req: express.Request, res: express.Response, toUpdateJSON.user['video_quota'] = toUpdate.user.videoQuota toUpdateJSON.instance['default_client_route'] = toUpdate.instance.defaultClientRoute toUpdateJSON.instance['short_description'] = toUpdate.instance.shortDescription + toUpdateJSON.instance['default_nsfw_policy'] = toUpdate.instance.defaultNSFWPolicy await writeFilePromise(CONFIG.CUSTOM_FILE, JSON.stringify(toUpdateJSON, undefined, 2)) @@ -153,6 +155,7 @@ function customConfig (): CustomConfig { description: CONFIG.INSTANCE.DESCRIPTION, terms: CONFIG.INSTANCE.TERMS, defaultClientRoute: CONFIG.INSTANCE.DEFAULT_CLIENT_ROUTE, + defaultNSFWPolicy: CONFIG.INSTANCE.DEFAULT_NSFW_POLICY, customizations: { css: CONFIG.INSTANCE.CUSTOMIZATIONS.CSS, javascript: CONFIG.INSTANCE.CUSTOMIZATIONS.JAVASCRIPT diff --git a/server/controllers/api/users.ts b/server/controllers/api/users.ts index 56cbf9448..6540adb1c 100644 --- a/server/controllers/api/users.ts +++ b/server/controllers/api/users.ts @@ -42,6 +42,7 @@ import { AccountVideoRateModel } from '../../models/account/account-video-rate' import { UserModel } from '../../models/account/user' import { OAuthTokenModel } from '../../models/oauth/oauth-token' import { VideoModel } from '../../models/video/video' +import { VideoSortField } from '../../../client/src/app/shared/video/sort-field.type' const reqAvatarFile = createReqFiles([ 'avatarfile' ], IMAGE_MIMETYPE_EXT, { avatarfile: CONFIG.STORAGE.AVATARS_DIR }) const loginRateLimiter = new RateLimit({ @@ -161,7 +162,13 @@ export { async function getUserVideos (req: express.Request, res: express.Response, next: express.NextFunction) { const user = res.locals.oauth.token.User as UserModel - const resultList = await VideoModel.listAccountVideosForApi(user.Account.id ,req.query.start, req.query.count, req.query.sort) + const resultList = await VideoModel.listAccountVideosForApi( + user.Account.id, + req.query.start as number, + req.query.count as number, + req.query.sort as VideoSortField, + false // Display my NSFW videos + ) return res.json(getFormattedObjects(resultList.data, resultList.total)) } @@ -188,7 +195,7 @@ async function createUser (req: express.Request) { username: body.username, password: body.password, email: body.email, - displayNSFW: false, + nsfwPolicy: CONFIG.INSTANCE.DEFAULT_NSFW_POLICY, autoPlayVideo: true, role: body.role, videoQuota: body.videoQuota @@ -219,7 +226,7 @@ async function registerUser (req: express.Request) { username: body.username, password: body.password, email: body.email, - displayNSFW: false, + nsfwPolicy: CONFIG.INSTANCE.DEFAULT_NSFW_POLICY, autoPlayVideo: true, role: UserRole.USER, videoQuota: CONFIG.USER.VIDEO_QUOTA @@ -286,7 +293,7 @@ async function updateMe (req: express.Request, res: express.Response, next: expr if (body.password !== undefined) user.password = body.password if (body.email !== undefined) user.email = body.email - if (body.displayNSFW !== undefined) user.displayNSFW = body.displayNSFW + if (body.nsfwPolicy !== undefined) user.nsfwPolicy = body.nsfwPolicy if (body.autoPlayVideo !== undefined) user.autoPlayVideo = body.autoPlayVideo await sequelizeTypescript.transaction(async t => { diff --git a/server/controllers/api/videos/index.ts b/server/controllers/api/videos/index.ts index b4cd67158..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', @@ -391,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)) } @@ -419,11 +435,21 @@ 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.searchAndPopulateAccountAndServer( - req.query.search, - req.query.start, - req.query.count, - req.query.sort + 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' +} -- cgit v1.2.3