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/models/account/user.ts | 14 ++++++++------ server/models/video/video.ts | 24 +++++++++++++++++------- 2 files changed, 25 insertions(+), 13 deletions(-) (limited to 'server/models') diff --git a/server/models/account/user.ts b/server/models/account/user.ts index 8afd246b2..56af2f30a 100644 --- a/server/models/account/user.ts +++ b/server/models/account/user.ts @@ -21,7 +21,7 @@ import { hasUserRight, USER_ROLE_LABELS, UserRight } from '../../../shared' import { User, UserRole } from '../../../shared/models/users' import { isUserAutoPlayVideoValid, - isUserDisplayNSFWValid, + isUserNSFWPolicyValid, isUserPasswordValid, isUserRoleValid, isUserUsernameValid, @@ -32,6 +32,9 @@ import { OAuthTokenModel } from '../oauth/oauth-token' import { getSort, throwIfNotValid } from '../utils' import { VideoChannelModel } from '../video/video-channel' import { AccountModel } from './account' +import { NSFWPolicyType } from '../../../shared/models/videos/nsfw-policy.type' +import { values } from 'lodash' +import { NSFW_POLICY_TYPES } from '../../initializers' @DefaultScope({ include: [ @@ -83,10 +86,9 @@ export class UserModel extends Model { email: string @AllowNull(false) - @Default(false) - @Is('UserDisplayNSFW', value => throwIfNotValid(value, isUserDisplayNSFWValid, 'display NSFW boolean')) - @Column - displayNSFW: boolean + @Is('UserNSFWPolicy', value => throwIfNotValid(value, isUserNSFWPolicyValid, 'NSFW policy')) + @Column(DataType.ENUM(values(NSFW_POLICY_TYPES))) + nsfwPolicy: NSFWPolicyType @AllowNull(false) @Default(true) @@ -265,7 +267,7 @@ export class UserModel extends Model { id: this.id, username: this.username, email: this.email, - displayNSFW: this.displayNSFW, + nsfwPolicy: this.nsfwPolicy, autoPlayVideo: this.autoPlayVideo, role: this.role, roleLabel: USER_ROLE_LABELS[ this.role ], diff --git a/server/models/video/video.ts b/server/models/video/video.ts index a7923b477..2e66f9aa7 100644 --- a/server/models/video/video.ts +++ b/server/models/video/video.ts @@ -95,7 +95,7 @@ enum ScopeNames { } @Scopes({ - [ScopeNames.AVAILABLE_FOR_LIST]: (actorId: number, filter?: VideoFilter, withFiles?: boolean) => { + [ScopeNames.AVAILABLE_FOR_LIST]: (actorId: number, hideNSFW: boolean, filter?: VideoFilter, withFiles?: boolean) => { const query: IFindOptions = { where: { id: { @@ -161,6 +161,11 @@ enum ScopeNames { }) } + // Hide nsfw videos? + if (hideNSFW === true) { + query.where['nsfw'] = false + } + return query }, [ScopeNames.WITH_ACCOUNT_DETAILS]: { @@ -640,7 +645,7 @@ export class VideoModel extends Model { }) } - static listAccountVideosForApi (accountId: number, start: number, count: number, sort: string, withFiles = false) { + static listAccountVideosForApi (accountId: number, start: number, count: number, sort: string, hideNSFW: boolean, withFiles = false) { const query: IFindOptions = { offset: start, limit: count, @@ -669,6 +674,12 @@ export class VideoModel extends Model { }) } + if (hideNSFW === true) { + query.where = { + nsfw: false + } + } + return VideoModel.findAndCountAll(query).then(({ rows, count }) => { return { data: rows, @@ -677,7 +688,7 @@ export class VideoModel extends Model { }) } - static async listForApi (start: number, count: number, sort: string, filter?: VideoFilter, withFiles = false) { + static async listForApi (start: number, count: number, sort: string, hideNSFW: boolean, filter?: VideoFilter, withFiles = false) { const query = { offset: start, limit: count, @@ -685,8 +696,7 @@ export class VideoModel extends Model { } const serverActor = await getServerActor() - - return VideoModel.scope({ method: [ ScopeNames.AVAILABLE_FOR_LIST, serverActor.id, filter, withFiles ] }) + return VideoModel.scope({ method: [ ScopeNames.AVAILABLE_FOR_LIST, serverActor.id, hideNSFW, filter, withFiles ] }) .findAndCountAll(query) .then(({ rows, count }) => { return { @@ -696,7 +706,7 @@ export class VideoModel extends Model { }) } - static async searchAndPopulateAccountAndServer (value: string, start: number, count: number, sort: string) { + static async searchAndPopulateAccountAndServer (value: string, start: number, count: number, sort: string, hideNSFW: boolean) { const query: IFindOptions = { offset: start, limit: count, @@ -724,7 +734,7 @@ export class VideoModel extends Model { const serverActor = await getServerActor() - return VideoModel.scope({ method: [ ScopeNames.AVAILABLE_FOR_LIST, serverActor.id ] }) + return VideoModel.scope({ method: [ ScopeNames.AVAILABLE_FOR_LIST, serverActor.id, hideNSFW ] }) .findAndCountAll(query) .then(({ rows, count }) => { return { -- cgit v1.2.3