aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/models/video
diff options
context:
space:
mode:
authorRigel Kent <sendmemail@rigelk.eu>2020-04-20 11:20:12 +0200
committerRigel Kent <par@rigelk.eu>2020-05-01 16:41:02 +0200
commit0251197e249cc03b65805ed6805da72bf4573529 (patch)
tree3aae202fd1ea24bac5cd7f2c380ffe50aae189a0 /server/models/video
parentb8cf27c0f86d205a279d03b83e0e6728f46da67f (diff)
downloadPeerTube-0251197e249cc03b65805ed6805da72bf4573529.tar.gz
PeerTube-0251197e249cc03b65805ed6805da72bf4573529.tar.zst
PeerTube-0251197e249cc03b65805ed6805da72bf4573529.zip
Factorize rest-table and fix/simplify SQL
Diffstat (limited to 'server/models/video')
-rw-r--r--server/models/video/video-abuse.ts43
-rw-r--r--server/models/video/video-blacklist.ts2
-rw-r--r--server/models/video/video.ts6
3 files changed, 18 insertions, 33 deletions
diff --git a/server/models/video/video-abuse.ts b/server/models/video/video-abuse.ts
index e8c3bd823..71b519cd9 100644
--- a/server/models/video/video-abuse.ts
+++ b/server/models/video/video-abuse.ts
@@ -15,7 +15,7 @@ import { VideoAbuseState, VideoDetails } from '../../../shared'
15import { CONSTRAINTS_FIELDS, VIDEO_ABUSE_STATES } from '../../initializers/constants' 15import { CONSTRAINTS_FIELDS, VIDEO_ABUSE_STATES } from '../../initializers/constants'
16import { MUserAccountId, MVideoAbuse, MVideoAbuseFormattable, MVideoAbuseVideo } from '../../typings/models' 16import { MUserAccountId, MVideoAbuse, MVideoAbuseFormattable, MVideoAbuseVideo } from '../../typings/models'
17import * as Bluebird from 'bluebird' 17import * as Bluebird from 'bluebird'
18import { literal, Op } from 'sequelize' 18import { literal, Op, Sequelize } from 'sequelize'
19import { ThumbnailModel } from './thumbnail' 19import { ThumbnailModel } from './thumbnail'
20import { VideoBlacklistModel } from './video-blacklist' 20import { VideoBlacklistModel } from './video-blacklist'
21import { ScopeNames as VideoChannelScopeNames, SummaryOptions, VideoChannelModel } from './video-channel' 21import { ScopeNames as VideoChannelScopeNames, SummaryOptions, VideoChannelModel } from './video-channel'
@@ -31,7 +31,7 @@ export enum ScopeNames {
31 searchVideo?: string 31 searchVideo?: string
32 searchVideoChannel?: string 32 searchVideoChannel?: string
33 serverAccountId: number 33 serverAccountId: number
34 userAccountId: any 34 userAccountId: number
35 }) => { 35 }) => {
36 let where = { 36 let where = {
37 reporterAccountId: { 37 reporterAccountId: {
@@ -45,28 +45,28 @@ export enum ScopeNames {
45 { 45 {
46 [Op.and]: [ 46 [Op.and]: [
47 { videoId: { [Op.not]: null } }, 47 { videoId: { [Op.not]: null } },
48 { '$Video.name$': { [Op.iLike]: `%${options.search}%` } } 48 searchAttribute(options.search, '$Video.name$')
49 ] 49 ]
50 }, 50 },
51 { 51 {
52 [Op.and]: [ 52 [Op.and]: [
53 { videoId: { [Op.not]: null } }, 53 { videoId: { [Op.not]: null } },
54 { '$Video.VideoChannel.name$': { [Op.iLike]: `%${options.search}%` } } 54 searchAttribute(options.search, '$Video.VideoChannel.name$')
55 ] 55 ]
56 }, 56 },
57 { 57 {
58 [Op.and]: [ 58 [Op.and]: [
59 { deletedVideo: { [Op.not]: null } }, 59 { deletedVideo: { [Op.not]: null } },
60 { deletedVideo: { name: { [Op.iLike]: `%${options.search}%` } } } 60 { deletedVideo: searchAttribute(options.search, 'name') }
61 ] 61 ]
62 }, 62 },
63 { 63 {
64 [Op.and]: [ 64 [Op.and]: [
65 { deletedVideo: { [Op.not]: null } }, 65 { deletedVideo: { [Op.not]: null } },
66 { deletedVideo: { channel: { displayName: { [Op.iLike]: `%${options.search}%` } } } } 66 { deletedVideo: { channel: searchAttribute(options.search, 'displayName') } }
67 ] 67 ]
68 }, 68 },
69 { '$Account.name$': { [Op.iLike]: `%${options.search}%` } } 69 searchAttribute(options.search, '$Account.name$')
70 ] 70 ]
71 }) 71 })
72 } 72 }
@@ -77,13 +77,9 @@ export enum ScopeNames {
77 [ 77 [
78 literal( 78 literal(
79 '(' + 79 '(' +
80 'SELECT t.count ' + 80 'SELECT count(*) ' +
81 'FROM ( ' + 81 'FROM "videoAbuse" ' +
82 'SELECT id, ' + 82 'WHERE "videoId" = "VideoAbuseModel"."videoId" ' +
83 'count(id) OVER (PARTITION BY "videoId") ' +
84 'FROM "videoAbuse" ' +
85 ') t ' +
86 'WHERE t.id = "VideoAbuseModel".id ' +
87 ')' 83 ')'
88 ), 84 ),
89 'countReportsForVideo' 85 'countReportsForVideo'
@@ -118,20 +114,11 @@ export enum ScopeNames {
118 [ 114 [
119 literal( 115 literal(
120 '(' + 116 '(' +
121 'WITH ' + 117 'SELECT count(DISTINCT "videoAbuse"."id") ' +
122 'ids AS ( ' +
123 'SELECT "account"."id" ' +
124 'FROM "account" ' +
125 'INNER JOIN "videoChannel" ON "videoChannel"."accountId" = "account"."id" ' +
126 'INNER JOIN "video" ON "video"."channelId" = "videoChannel"."id" ' +
127 'WHERE "video"."id" = "VideoAbuseModel"."videoId" ' +
128 ') ' +
129 'SELECT count("videoAbuse"."id") ' +
130 'FROM "videoAbuse" ' + 118 'FROM "videoAbuse" ' +
131 'INNER JOIN "video" ON "video"."id" = "videoAbuse"."videoId" ' + 119 'INNER JOIN "video" ON "video"."id" = "videoAbuse"."videoId" ' +
132 'INNER JOIN "videoChannel" ON "videoChannel"."id" = "video"."channelId" ' + 120 'INNER JOIN "videoChannel" ON "videoChannel"."id" = "video"."channelId" ' +
133 'INNER JOIN "account" ON "videoChannel"."accountId" = "account"."id" ' + 121 'INNER JOIN "account" ON "videoChannel"."accountId" = "Video->VideoChannel"."accountId" ' +
134 'INNER JOIN ids ON "account"."id" = ids.id ' +
135 ')' 122 ')'
136 ), 123 ),
137 'countReportsForReportee' 124 'countReportsForReportee'
@@ -142,19 +129,19 @@ export enum ScopeNames {
142 { 129 {
143 model: AccountModel, 130 model: AccountModel,
144 required: true, 131 required: true,
145 where: { ...searchAttribute(options.searchReporter, 'name') } 132 where: searchAttribute(options.searchReporter, 'name')
146 }, 133 },
147 { 134 {
148 model: VideoModel, 135 model: VideoModel,
149 required: false, 136 required: false,
150 where: { ...searchAttribute(options.searchVideo, 'name') }, 137 where: searchAttribute(options.searchVideo, 'name'),
151 include: [ 138 include: [
152 { 139 {
153 model: ThumbnailModel 140 model: ThumbnailModel
154 }, 141 },
155 { 142 {
156 model: VideoChannelModel.scope({ method: [ VideoChannelScopeNames.SUMMARY, { withAccount: true } as SummaryOptions ] }), 143 model: VideoChannelModel.scope({ method: [ VideoChannelScopeNames.SUMMARY, { withAccount: true } as SummaryOptions ] }),
157 where: { ...searchAttribute(options.searchVideoChannel, 'name') } 144 where: searchAttribute(options.searchVideoChannel, 'name')
158 }, 145 },
159 { 146 {
160 attributes: [ 'id', 'reason', 'unfederated' ], 147 attributes: [ 'id', 'reason', 'unfederated' ],
diff --git a/server/models/video/video-blacklist.ts b/server/models/video/video-blacklist.ts
index 680eba471..8cbfe362e 100644
--- a/server/models/video/video-blacklist.ts
+++ b/server/models/video/video-blacklist.ts
@@ -78,7 +78,7 @@ export class VideoBlacklistModel extends Model<VideoBlacklistModel> {
78 { 78 {
79 model: VideoModel, 79 model: VideoModel,
80 required: true, 80 required: true,
81 where: { ...searchAttribute(search, 'name') }, 81 where: searchAttribute(search, 'name'),
82 include: [ 82 include: [
83 { 83 {
84 model: VideoChannelModel.scope({ method: [ VideoChannelScopeNames.SUMMARY, { withAccount: true } as SummaryOptions ] }), 84 model: VideoChannelModel.scope({ method: [ VideoChannelScopeNames.SUMMARY, { withAccount: true } as SummaryOptions ] }),
diff --git a/server/models/video/video.ts b/server/models/video/video.ts
index f32216e90..ccb9d64ca 100644
--- a/server/models/video/video.ts
+++ b/server/models/video/video.ts
@@ -813,10 +813,8 @@ export class VideoModel extends Model<VideoModel> {
813 const details = instance.toFormattedDetailsJSON() 813 const details = instance.toFormattedDetailsJSON()
814 814
815 for (const abuse of instance.VideoAbuses) { 815 for (const abuse of instance.VideoAbuses) {
816 tasks.push((_ => { 816 abuse.deletedVideo = details
817 abuse.deletedVideo = details 817 tasks.push(abuse.save({ transaction: options.transaction }))
818 return abuse.save({ transaction: options.transaction })
819 })())
820 } 818 }
821 819
822 Promise.all(tasks) 820 Promise.all(tasks)