aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/models
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2018-04-19 11:01:34 +0200
committerChocobozzz <me@florianbigard.com>2018-04-19 11:01:34 +0200
commit0883b3245bf0deb9106c4041e9afbd3521b79280 (patch)
treefcb73005e0b31a3b763ee5d22d5fc39c2da89907 /server/models
parent04ed10b21e8e1339514faae0bb690e4d97c23b0a (diff)
downloadPeerTube-0883b3245bf0deb9106c4041e9afbd3521b79280.tar.gz
PeerTube-0883b3245bf0deb9106c4041e9afbd3521b79280.tar.zst
PeerTube-0883b3245bf0deb9106c4041e9afbd3521b79280.zip
Add ability to choose what policy we have for NSFW videos
There is a global instance setting and a per user setting
Diffstat (limited to 'server/models')
-rw-r--r--server/models/account/user.ts14
-rw-r--r--server/models/video/video.ts24
2 files changed, 25 insertions, 13 deletions
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'
21import { User, UserRole } from '../../../shared/models/users' 21import { User, UserRole } from '../../../shared/models/users'
22import { 22import {
23 isUserAutoPlayVideoValid, 23 isUserAutoPlayVideoValid,
24 isUserDisplayNSFWValid, 24 isUserNSFWPolicyValid,
25 isUserPasswordValid, 25 isUserPasswordValid,
26 isUserRoleValid, 26 isUserRoleValid,
27 isUserUsernameValid, 27 isUserUsernameValid,
@@ -32,6 +32,9 @@ import { OAuthTokenModel } from '../oauth/oauth-token'
32import { getSort, throwIfNotValid } from '../utils' 32import { getSort, throwIfNotValid } from '../utils'
33import { VideoChannelModel } from '../video/video-channel' 33import { VideoChannelModel } from '../video/video-channel'
34import { AccountModel } from './account' 34import { AccountModel } from './account'
35import { NSFWPolicyType } from '../../../shared/models/videos/nsfw-policy.type'
36import { values } from 'lodash'
37import { NSFW_POLICY_TYPES } from '../../initializers'
35 38
36@DefaultScope({ 39@DefaultScope({
37 include: [ 40 include: [
@@ -83,10 +86,9 @@ export class UserModel extends Model<UserModel> {
83 email: string 86 email: string
84 87
85 @AllowNull(false) 88 @AllowNull(false)
86 @Default(false) 89 @Is('UserNSFWPolicy', value => throwIfNotValid(value, isUserNSFWPolicyValid, 'NSFW policy'))
87 @Is('UserDisplayNSFW', value => throwIfNotValid(value, isUserDisplayNSFWValid, 'display NSFW boolean')) 90 @Column(DataType.ENUM(values(NSFW_POLICY_TYPES)))
88 @Column 91 nsfwPolicy: NSFWPolicyType
89 displayNSFW: boolean
90 92
91 @AllowNull(false) 93 @AllowNull(false)
92 @Default(true) 94 @Default(true)
@@ -265,7 +267,7 @@ export class UserModel extends Model<UserModel> {
265 id: this.id, 267 id: this.id,
266 username: this.username, 268 username: this.username,
267 email: this.email, 269 email: this.email,
268 displayNSFW: this.displayNSFW, 270 nsfwPolicy: this.nsfwPolicy,
269 autoPlayVideo: this.autoPlayVideo, 271 autoPlayVideo: this.autoPlayVideo,
270 role: this.role, 272 role: this.role,
271 roleLabel: USER_ROLE_LABELS[ this.role ], 273 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 {
95} 95}
96 96
97@Scopes({ 97@Scopes({
98 [ScopeNames.AVAILABLE_FOR_LIST]: (actorId: number, filter?: VideoFilter, withFiles?: boolean) => { 98 [ScopeNames.AVAILABLE_FOR_LIST]: (actorId: number, hideNSFW: boolean, filter?: VideoFilter, withFiles?: boolean) => {
99 const query: IFindOptions<VideoModel> = { 99 const query: IFindOptions<VideoModel> = {
100 where: { 100 where: {
101 id: { 101 id: {
@@ -161,6 +161,11 @@ enum ScopeNames {
161 }) 161 })
162 } 162 }
163 163
164 // Hide nsfw videos?
165 if (hideNSFW === true) {
166 query.where['nsfw'] = false
167 }
168
164 return query 169 return query
165 }, 170 },
166 [ScopeNames.WITH_ACCOUNT_DETAILS]: { 171 [ScopeNames.WITH_ACCOUNT_DETAILS]: {
@@ -640,7 +645,7 @@ export class VideoModel extends Model<VideoModel> {
640 }) 645 })
641 } 646 }
642 647
643 static listAccountVideosForApi (accountId: number, start: number, count: number, sort: string, withFiles = false) { 648 static listAccountVideosForApi (accountId: number, start: number, count: number, sort: string, hideNSFW: boolean, withFiles = false) {
644 const query: IFindOptions<VideoModel> = { 649 const query: IFindOptions<VideoModel> = {
645 offset: start, 650 offset: start,
646 limit: count, 651 limit: count,
@@ -669,6 +674,12 @@ export class VideoModel extends Model<VideoModel> {
669 }) 674 })
670 } 675 }
671 676
677 if (hideNSFW === true) {
678 query.where = {
679 nsfw: false
680 }
681 }
682
672 return VideoModel.findAndCountAll(query).then(({ rows, count }) => { 683 return VideoModel.findAndCountAll(query).then(({ rows, count }) => {
673 return { 684 return {
674 data: rows, 685 data: rows,
@@ -677,7 +688,7 @@ export class VideoModel extends Model<VideoModel> {
677 }) 688 })
678 } 689 }
679 690
680 static async listForApi (start: number, count: number, sort: string, filter?: VideoFilter, withFiles = false) { 691 static async listForApi (start: number, count: number, sort: string, hideNSFW: boolean, filter?: VideoFilter, withFiles = false) {
681 const query = { 692 const query = {
682 offset: start, 693 offset: start,
683 limit: count, 694 limit: count,
@@ -685,8 +696,7 @@ export class VideoModel extends Model<VideoModel> {
685 } 696 }
686 697
687 const serverActor = await getServerActor() 698 const serverActor = await getServerActor()
688 699 return VideoModel.scope({ method: [ ScopeNames.AVAILABLE_FOR_LIST, serverActor.id, hideNSFW, filter, withFiles ] })
689 return VideoModel.scope({ method: [ ScopeNames.AVAILABLE_FOR_LIST, serverActor.id, filter, withFiles ] })
690 .findAndCountAll(query) 700 .findAndCountAll(query)
691 .then(({ rows, count }) => { 701 .then(({ rows, count }) => {
692 return { 702 return {
@@ -696,7 +706,7 @@ export class VideoModel extends Model<VideoModel> {
696 }) 706 })
697 } 707 }
698 708
699 static async searchAndPopulateAccountAndServer (value: string, start: number, count: number, sort: string) { 709 static async searchAndPopulateAccountAndServer (value: string, start: number, count: number, sort: string, hideNSFW: boolean) {
700 const query: IFindOptions<VideoModel> = { 710 const query: IFindOptions<VideoModel> = {
701 offset: start, 711 offset: start,
702 limit: count, 712 limit: count,
@@ -724,7 +734,7 @@ export class VideoModel extends Model<VideoModel> {
724 734
725 const serverActor = await getServerActor() 735 const serverActor = await getServerActor()
726 736
727 return VideoModel.scope({ method: [ ScopeNames.AVAILABLE_FOR_LIST, serverActor.id ] }) 737 return VideoModel.scope({ method: [ ScopeNames.AVAILABLE_FOR_LIST, serverActor.id, hideNSFW ] })
728 .findAndCountAll(query) 738 .findAndCountAll(query)
729 .then(({ rows, count }) => { 739 .then(({ rows, count }) => {
730 return { 740 return {