diff options
author | Chocobozzz <me@florianbigard.com> | 2018-08-13 16:57:13 +0200 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2018-08-14 09:27:18 +0200 |
commit | 26b7305a232e547709f433a6edf700bf495935d8 (patch) | |
tree | b5676090c61df72f864735bcc881d5ee256cffbd /server/models/video | |
parent | efc9e8450a8bbeeef9cd18e3ad6037abc0f815c3 (diff) | |
download | PeerTube-26b7305a232e547709f433a6edf700bf495935d8.tar.gz PeerTube-26b7305a232e547709f433a6edf700bf495935d8.tar.zst PeerTube-26b7305a232e547709f433a6edf700bf495935d8.zip |
Add blacklist reason field
Diffstat (limited to 'server/models/video')
-rw-r--r-- | server/models/video/video-blacklist.ts | 59 | ||||
-rw-r--r-- | server/models/video/video.ts | 30 |
2 files changed, 69 insertions, 20 deletions
diff --git a/server/models/video/video-blacklist.ts b/server/models/video/video-blacklist.ts index 26167174a..1b8a338cb 100644 --- a/server/models/video/video-blacklist.ts +++ b/server/models/video/video-blacklist.ts | |||
@@ -1,7 +1,23 @@ | |||
1 | import { BelongsTo, Column, CreatedAt, ForeignKey, Model, Table, UpdatedAt } from 'sequelize-typescript' | 1 | import { |
2 | AfterCreate, | ||
3 | AfterDestroy, | ||
4 | AllowNull, | ||
5 | BelongsTo, | ||
6 | Column, | ||
7 | CreatedAt, DataType, | ||
8 | ForeignKey, | ||
9 | Is, | ||
10 | Model, | ||
11 | Table, | ||
12 | UpdatedAt | ||
13 | } from 'sequelize-typescript' | ||
2 | import { SortType } from '../../helpers/utils' | 14 | import { SortType } from '../../helpers/utils' |
3 | import { getSortOnModel } from '../utils' | 15 | import { getSortOnModel, throwIfNotValid } from '../utils' |
4 | import { VideoModel } from './video' | 16 | import { VideoModel } from './video' |
17 | import { isVideoBlacklistReasonValid } from '../../helpers/custom-validators/video-blacklist' | ||
18 | import { Emailer } from '../../lib/emailer' | ||
19 | import { BlacklistedVideo } from '../../../shared/models/videos' | ||
20 | import { CONSTRAINTS_FIELDS } from '../../initializers' | ||
5 | 21 | ||
6 | @Table({ | 22 | @Table({ |
7 | tableName: 'videoBlacklist', | 23 | tableName: 'videoBlacklist', |
@@ -14,6 +30,11 @@ import { VideoModel } from './video' | |||
14 | }) | 30 | }) |
15 | export class VideoBlacklistModel extends Model<VideoBlacklistModel> { | 31 | export class VideoBlacklistModel extends Model<VideoBlacklistModel> { |
16 | 32 | ||
33 | @AllowNull(true) | ||
34 | @Is('VideoBlacklistReason', value => throwIfNotValid(value, isVideoBlacklistReasonValid, 'reason')) | ||
35 | @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEO_BLACKLIST.REASON.max)) | ||
36 | reason: string | ||
37 | |||
17 | @CreatedAt | 38 | @CreatedAt |
18 | createdAt: Date | 39 | createdAt: Date |
19 | 40 | ||
@@ -32,6 +53,16 @@ export class VideoBlacklistModel extends Model<VideoBlacklistModel> { | |||
32 | }) | 53 | }) |
33 | Video: VideoModel | 54 | Video: VideoModel |
34 | 55 | ||
56 | @AfterCreate | ||
57 | static sendBlacklistEmailNotification (instance: VideoBlacklistModel) { | ||
58 | return Emailer.Instance.addVideoBlacklistReportJob(instance.videoId, instance.reason) | ||
59 | } | ||
60 | |||
61 | @AfterDestroy | ||
62 | static sendUnblacklistEmailNotification (instance: VideoBlacklistModel) { | ||
63 | return Emailer.Instance.addVideoUnblacklistReportJob(instance.videoId) | ||
64 | } | ||
65 | |||
35 | static listForApi (start: number, count: number, sort: SortType) { | 66 | static listForApi (start: number, count: number, sort: SortType) { |
36 | const query = { | 67 | const query = { |
37 | offset: start, | 68 | offset: start, |
@@ -59,22 +90,26 @@ export class VideoBlacklistModel extends Model<VideoBlacklistModel> { | |||
59 | return VideoBlacklistModel.findOne(query) | 90 | return VideoBlacklistModel.findOne(query) |
60 | } | 91 | } |
61 | 92 | ||
62 | toFormattedJSON () { | 93 | toFormattedJSON (): BlacklistedVideo { |
63 | const video = this.Video | 94 | const video = this.Video |
64 | 95 | ||
65 | return { | 96 | return { |
66 | id: this.id, | 97 | id: this.id, |
67 | videoId: this.videoId, | ||
68 | createdAt: this.createdAt, | 98 | createdAt: this.createdAt, |
69 | updatedAt: this.updatedAt, | 99 | updatedAt: this.updatedAt, |
70 | name: video.name, | 100 | reason: this.reason, |
71 | uuid: video.uuid, | 101 | |
72 | description: video.description, | 102 | video: { |
73 | duration: video.duration, | 103 | id: video.id, |
74 | views: video.views, | 104 | name: video.name, |
75 | likes: video.likes, | 105 | uuid: video.uuid, |
76 | dislikes: video.dislikes, | 106 | description: video.description, |
77 | nsfw: video.nsfw | 107 | duration: video.duration, |
108 | views: video.views, | ||
109 | likes: video.likes, | ||
110 | dislikes: video.dislikes, | ||
111 | nsfw: video.nsfw | ||
112 | } | ||
78 | } | 113 | } |
79 | } | 114 | } |
80 | } | 115 | } |
diff --git a/server/models/video/video.ts b/server/models/video/video.ts index 39fe21007..f3a900bc9 100644 --- a/server/models/video/video.ts +++ b/server/models/video/video.ts | |||
@@ -93,6 +93,7 @@ import { VideoShareModel } from './video-share' | |||
93 | import { VideoTagModel } from './video-tag' | 93 | import { VideoTagModel } from './video-tag' |
94 | import { ScheduleVideoUpdateModel } from './schedule-video-update' | 94 | import { ScheduleVideoUpdateModel } from './schedule-video-update' |
95 | import { VideoCaptionModel } from './video-caption' | 95 | import { VideoCaptionModel } from './video-caption' |
96 | import { VideoBlacklistModel } from './video-blacklist' | ||
96 | 97 | ||
97 | // FIXME: Define indexes here because there is an issue with TS and Sequelize.literal when called directly in the annotation | 98 | // FIXME: Define indexes here because there is an issue with TS and Sequelize.literal when called directly in the annotation |
98 | const indexes: Sequelize.DefineIndexesOptions[] = [ | 99 | const indexes: Sequelize.DefineIndexesOptions[] = [ |
@@ -581,6 +582,15 @@ export class VideoModel extends Model<VideoModel> { | |||
581 | }) | 582 | }) |
582 | ScheduleVideoUpdate: ScheduleVideoUpdateModel | 583 | ScheduleVideoUpdate: ScheduleVideoUpdateModel |
583 | 584 | ||
585 | @HasOne(() => VideoBlacklistModel, { | ||
586 | foreignKey: { | ||
587 | name: 'videoId', | ||
588 | allowNull: false | ||
589 | }, | ||
590 | onDelete: 'cascade' | ||
591 | }) | ||
592 | VideoBlacklist: VideoBlacklistModel | ||
593 | |||
584 | @HasMany(() => VideoCaptionModel, { | 594 | @HasMany(() => VideoCaptionModel, { |
585 | foreignKey: { | 595 | foreignKey: { |
586 | name: 'videoId', | 596 | name: 'videoId', |
@@ -755,7 +765,7 @@ export class VideoModel extends Model<VideoModel> { | |||
755 | }) | 765 | }) |
756 | } | 766 | } |
757 | 767 | ||
758 | static listUserVideosForApi (accountId: number, start: number, count: number, sort: string, hideNSFW: boolean, withFiles = false) { | 768 | static listUserVideosForApi (accountId: number, start: number, count: number, sort: string, withFiles = false) { |
759 | const query: IFindOptions<VideoModel> = { | 769 | const query: IFindOptions<VideoModel> = { |
760 | offset: start, | 770 | offset: start, |
761 | limit: count, | 771 | limit: count, |
@@ -777,6 +787,10 @@ export class VideoModel extends Model<VideoModel> { | |||
777 | { | 787 | { |
778 | model: ScheduleVideoUpdateModel, | 788 | model: ScheduleVideoUpdateModel, |
779 | required: false | 789 | required: false |
790 | }, | ||
791 | { | ||
792 | model: VideoBlacklistModel, | ||
793 | required: false | ||
780 | } | 794 | } |
781 | ] | 795 | ] |
782 | } | 796 | } |
@@ -788,12 +802,6 @@ export class VideoModel extends Model<VideoModel> { | |||
788 | }) | 802 | }) |
789 | } | 803 | } |
790 | 804 | ||
791 | if (hideNSFW === true) { | ||
792 | query.where = { | ||
793 | nsfw: false | ||
794 | } | ||
795 | } | ||
796 | |||
797 | return VideoModel.findAndCountAll(query).then(({ rows, count }) => { | 805 | return VideoModel.findAndCountAll(query).then(({ rows, count }) => { |
798 | return { | 806 | return { |
799 | data: rows, | 807 | data: rows, |
@@ -1177,7 +1185,8 @@ export class VideoModel extends Model<VideoModel> { | |||
1177 | additionalAttributes: { | 1185 | additionalAttributes: { |
1178 | state?: boolean, | 1186 | state?: boolean, |
1179 | waitTranscoding?: boolean, | 1187 | waitTranscoding?: boolean, |
1180 | scheduledUpdate?: boolean | 1188 | scheduledUpdate?: boolean, |
1189 | blacklistInfo?: boolean | ||
1181 | } | 1190 | } |
1182 | }): Video { | 1191 | }): Video { |
1183 | const formattedAccount = this.VideoChannel.Account.toFormattedJSON() | 1192 | const formattedAccount = this.VideoChannel.Account.toFormattedJSON() |
@@ -1254,6 +1263,11 @@ export class VideoModel extends Model<VideoModel> { | |||
1254 | privacy: this.ScheduleVideoUpdate.privacy || undefined | 1263 | privacy: this.ScheduleVideoUpdate.privacy || undefined |
1255 | } | 1264 | } |
1256 | } | 1265 | } |
1266 | |||
1267 | if (options.additionalAttributes.blacklistInfo === true) { | ||
1268 | videoObject.blacklisted = !!this.VideoBlacklist | ||
1269 | videoObject.blacklistedReason = this.VideoBlacklist ? this.VideoBlacklist.reason : null | ||
1270 | } | ||
1257 | } | 1271 | } |
1258 | 1272 | ||
1259 | return videoObject | 1273 | return videoObject |