aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/models
diff options
context:
space:
mode:
authorRigel Kent <sendmemail@rigelk.eu>2020-04-19 14:11:40 +0200
committerRigel Kent <par@rigelk.eu>2020-05-01 16:41:02 +0200
commite0a929179a9dc76e035ca7fda2b61d5ff46afbc5 (patch)
tree1e6615c612ad6995dcb1c3619342dbbc7db35034 /server/models
parentaeb1bed9835b3b092832160245080d4023c14d91 (diff)
downloadPeerTube-e0a929179a9dc76e035ca7fda2b61d5ff46afbc5.tar.gz
PeerTube-e0a929179a9dc76e035ca7fda2b61d5ff46afbc5.tar.zst
PeerTube-e0a929179a9dc76e035ca7fda2b61d5ff46afbc5.zip
Add filter inputs for blacklisted videos and muted accounts/servers
Diffstat (limited to 'server/models')
-rw-r--r--server/models/account/account-blocklist.ts32
-rw-r--r--server/models/server/server-blocklist.ts17
-rw-r--r--server/models/utils.ts15
-rw-r--r--server/models/video/video-abuse.ts18
-rw-r--r--server/models/video/video-blacklist.ts13
5 files changed, 70 insertions, 25 deletions
diff --git a/server/models/account/account-blocklist.ts b/server/models/account/account-blocklist.ts
index e2f66d733..fe2d5d010 100644
--- a/server/models/account/account-blocklist.ts
+++ b/server/models/account/account-blocklist.ts
@@ -1,6 +1,6 @@
1import { BelongsTo, Column, CreatedAt, ForeignKey, Model, Scopes, Table, UpdatedAt } from 'sequelize-typescript' 1import { BelongsTo, Column, CreatedAt, ForeignKey, Model, Scopes, Table, UpdatedAt } from 'sequelize-typescript'
2import { AccountModel } from './account' 2import { AccountModel } from './account'
3import { getSort } from '../utils' 3import { getSort, searchAttribute } from '../utils'
4import { AccountBlock } from '../../../shared/models/blocklist' 4import { AccountBlock } from '../../../shared/models/blocklist'
5import { Op } from 'sequelize' 5import { Op } from 'sequelize'
6import * as Bluebird from 'bluebird' 6import * as Bluebird from 'bluebird'
@@ -111,16 +111,36 @@ export class AccountBlocklistModel extends Model<AccountBlocklistModel> {
111 return AccountBlocklistModel.findOne(query) 111 return AccountBlocklistModel.findOne(query)
112 } 112 }
113 113
114 static listForApi (accountId: number, start: number, count: number, sort: string) { 114 static listForApi (parameters: {
115 start: number
116 count: number
117 sort: string
118 search?: string
119 accountId: number
120 }) {
121 const { start, count, sort, search, accountId } = parameters
122
115 const query = { 123 const query = {
116 offset: start, 124 offset: start,
117 limit: count, 125 limit: count,
118 order: getSort(sort), 126 order: getSort(sort)
119 where: { 127 }
120 accountId 128
121 } 129 const where = {
130 accountId
122 } 131 }
123 132
133 if (search) {
134 Object.assign(where, {
135 [Op.or]: [
136 { ...searchAttribute(search, '$BlockedAccount.name$') },
137 { ...searchAttribute(search, '$BlockedAccount.Actor.url$') }
138 ]
139 })
140 }
141
142 Object.assign(query, { where })
143
124 return AccountBlocklistModel 144 return AccountBlocklistModel
125 .scope([ ScopeNames.WITH_ACCOUNTS ]) 145 .scope([ ScopeNames.WITH_ACCOUNTS ])
126 .findAndCountAll<MAccountBlocklistAccounts>(query) 146 .findAndCountAll<MAccountBlocklistAccounts>(query)
diff --git a/server/models/server/server-blocklist.ts b/server/models/server/server-blocklist.ts
index 883ae47ab..764203d2c 100644
--- a/server/models/server/server-blocklist.ts
+++ b/server/models/server/server-blocklist.ts
@@ -2,7 +2,7 @@ import { BelongsTo, Column, CreatedAt, ForeignKey, Model, Scopes, Table, Updated
2import { AccountModel } from '../account/account' 2import { AccountModel } from '../account/account'
3import { ServerModel } from './server' 3import { ServerModel } from './server'
4import { ServerBlock } from '../../../shared/models/blocklist' 4import { ServerBlock } from '../../../shared/models/blocklist'
5import { getSort } from '../utils' 5import { getSort, searchAttribute } from '../utils'
6import * as Bluebird from 'bluebird' 6import * as Bluebird from 'bluebird'
7import { MServerBlocklist, MServerBlocklistAccountServer, MServerBlocklistFormattable } from '@server/typings/models' 7import { MServerBlocklist, MServerBlocklistAccountServer, MServerBlocklistFormattable } from '@server/typings/models'
8import { Op } from 'sequelize' 8import { Op } from 'sequelize'
@@ -120,16 +120,27 @@ export class ServerBlocklistModel extends Model<ServerBlocklistModel> {
120 return ServerBlocklistModel.findOne(query) 120 return ServerBlocklistModel.findOne(query)
121 } 121 }
122 122
123 static listForApi (accountId: number, start: number, count: number, sort: string) { 123 static listForApi (parameters: {
124 start: number
125 count: number
126 sort: string
127 search?: string
128 accountId: number
129 }) {
130 const { start, count, sort, search, accountId } = parameters
131
124 const query = { 132 const query = {
125 offset: start, 133 offset: start,
126 limit: count, 134 limit: count,
127 order: getSort(sort), 135 order: getSort(sort),
128 where: { 136 where: {
129 accountId 137 accountId,
138 ...searchAttribute(search, '$BlockedServer.host$')
130 } 139 }
131 } 140 }
132 141
142 console.log(search)
143
133 return ServerBlocklistModel 144 return ServerBlocklistModel
134 .scope([ ScopeNames.WITH_ACCOUNT, ScopeNames.WITH_SERVER ]) 145 .scope([ ScopeNames.WITH_ACCOUNT, ScopeNames.WITH_SERVER ])
135 .findAndCountAll<MServerBlocklistAccountServer>(query) 146 .findAndCountAll<MServerBlocklistAccountServer>(query)
diff --git a/server/models/utils.ts b/server/models/utils.ts
index 06ff05864..7137419a2 100644
--- a/server/models/utils.ts
+++ b/server/models/utils.ts
@@ -1,7 +1,7 @@
1import { Model, Sequelize } from 'sequelize-typescript' 1import { Model, Sequelize } from 'sequelize-typescript'
2import validator from 'validator' 2import validator from 'validator'
3import { Col } from 'sequelize/types/lib/utils' 3import { Col } from 'sequelize/types/lib/utils'
4import { literal, OrderItem } from 'sequelize' 4import { literal, OrderItem, Op } from 'sequelize'
5 5
6type Primitive = string | Function | number | boolean | Symbol | undefined | null 6type Primitive = string | Function | number | boolean | Symbol | undefined | null
7type DeepOmitHelper<T, K extends keyof T> = { 7type DeepOmitHelper<T, K extends keyof T> = {
@@ -207,6 +207,16 @@ function buildDirectionAndField (value: string) {
207 return { direction, field } 207 return { direction, field }
208} 208}
209 209
210function searchAttribute (sourceField, targetField) {
211 return sourceField
212 ? {
213 [targetField]: {
214 [Op.iLike]: `%${sourceField}%`
215 }
216 }
217 : {}
218}
219
210// --------------------------------------------------------------------------- 220// ---------------------------------------------------------------------------
211 221
212export { 222export {
@@ -228,7 +238,8 @@ export {
228 parseAggregateResult, 238 parseAggregateResult,
229 getFollowsSort, 239 getFollowsSort,
230 buildDirectionAndField, 240 buildDirectionAndField,
231 createSafeIn 241 createSafeIn,
242 searchAttribute
232} 243}
233 244
234// --------------------------------------------------------------------------- 245// ---------------------------------------------------------------------------
diff --git a/server/models/video/video-abuse.ts b/server/models/video/video-abuse.ts
index d68608ca6..e8c3bd823 100644
--- a/server/models/video/video-abuse.ts
+++ b/server/models/video/video-abuse.ts
@@ -9,7 +9,7 @@ import {
9 isVideoAbuseStateValid 9 isVideoAbuseStateValid
10} from '../../helpers/custom-validators/video-abuses' 10} from '../../helpers/custom-validators/video-abuses'
11import { AccountModel } from '../account/account' 11import { AccountModel } from '../account/account'
12import { buildBlockedAccountSQL, getSort, throwIfNotValid } from '../utils' 12import { buildBlockedAccountSQL, getSort, throwIfNotValid, searchAttribute } from '../utils'
13import { VideoModel } from './video' 13import { VideoModel } from './video'
14import { VideoAbuseState, VideoDetails } from '../../../shared' 14import { VideoAbuseState, VideoDetails } from '../../../shared'
15import { CONSTRAINTS_FIELDS, VIDEO_ABUSE_STATES } from '../../initializers/constants' 15import { CONSTRAINTS_FIELDS, VIDEO_ABUSE_STATES } from '../../initializers/constants'
@@ -17,8 +17,8 @@ import { MUserAccountId, MVideoAbuse, MVideoAbuseFormattable, MVideoAbuseVideo }
17import * as Bluebird from 'bluebird' 17import * as Bluebird from 'bluebird'
18import { literal, Op } from 'sequelize' 18import { literal, Op } from 'sequelize'
19import { ThumbnailModel } from './thumbnail' 19import { ThumbnailModel } from './thumbnail'
20import { VideoChannelModel } from './video-channel'
21import { VideoBlacklistModel } from './video-blacklist' 20import { VideoBlacklistModel } from './video-blacklist'
21import { ScopeNames as VideoChannelScopeNames, SummaryOptions, VideoChannelModel } from './video-channel'
22 22
23export enum ScopeNames { 23export enum ScopeNames {
24 FOR_API = 'FOR_API' 24 FOR_API = 'FOR_API'
@@ -33,12 +33,6 @@ export enum ScopeNames {
33 serverAccountId: number 33 serverAccountId: number
34 userAccountId: any 34 userAccountId: any
35 }) => { 35 }) => {
36 const search = (sourceField, targetField) => sourceField ? ({
37 [targetField]: {
38 [Op.iLike]: `%${sourceField}%`
39 }
40 }) : {}
41
42 let where = { 36 let where = {
43 reporterAccountId: { 37 reporterAccountId: {
44 [Op.notIn]: literal('(' + buildBlockedAccountSQL(options.serverAccountId, options.userAccountId) + ')') 38 [Op.notIn]: literal('(' + buildBlockedAccountSQL(options.serverAccountId, options.userAccountId) + ')')
@@ -148,19 +142,19 @@ export enum ScopeNames {
148 { 142 {
149 model: AccountModel, 143 model: AccountModel,
150 required: true, 144 required: true,
151 where: { ...search(options.searchReporter, 'name') } 145 where: { ...searchAttribute(options.searchReporter, 'name') }
152 }, 146 },
153 { 147 {
154 model: VideoModel, 148 model: VideoModel,
155 required: false, 149 required: false,
156 where: { ...search(options.searchVideo, 'name') }, 150 where: { ...searchAttribute(options.searchVideo, 'name') },
157 include: [ 151 include: [
158 { 152 {
159 model: ThumbnailModel 153 model: ThumbnailModel
160 }, 154 },
161 { 155 {
162 model: VideoChannelModel.scope([ 'WITH_ACTOR', 'WITH_ACCOUNT' ]), 156 model: VideoChannelModel.scope({ method: [ VideoChannelScopeNames.SUMMARY, { withAccount: true } as SummaryOptions ] }),
163 where: { ...search(options.searchVideoChannel, 'name') } 157 where: { ...searchAttribute(options.searchVideoChannel, 'name') }
164 }, 158 },
165 { 159 {
166 attributes: [ 'id', 'reason', 'unfederated' ], 160 attributes: [ 'id', 'reason', 'unfederated' ],
diff --git a/server/models/video/video-blacklist.ts b/server/models/video/video-blacklist.ts
index 694983cb3..680eba471 100644
--- a/server/models/video/video-blacklist.ts
+++ b/server/models/video/video-blacklist.ts
@@ -1,5 +1,5 @@
1import { AllowNull, BelongsTo, Column, CreatedAt, DataType, Default, ForeignKey, Is, Model, Table, UpdatedAt } from 'sequelize-typescript' 1import { AllowNull, BelongsTo, Column, CreatedAt, DataType, Default, ForeignKey, Is, Model, Table, UpdatedAt } from 'sequelize-typescript'
2import { getBlacklistSort, SortType, throwIfNotValid } from '../utils' 2import { getBlacklistSort, SortType, throwIfNotValid, searchAttribute } from '../utils'
3import { VideoModel } from './video' 3import { VideoModel } from './video'
4import { ScopeNames as VideoChannelScopeNames, SummaryOptions, VideoChannelModel } from './video-channel' 4import { ScopeNames as VideoChannelScopeNames, SummaryOptions, VideoChannelModel } from './video-channel'
5import { isVideoBlacklistReasonValid, isVideoBlacklistTypeValid } from '../../helpers/custom-validators/video-blacklist' 5import { isVideoBlacklistReasonValid, isVideoBlacklistTypeValid } from '../../helpers/custom-validators/video-blacklist'
@@ -54,7 +54,15 @@ export class VideoBlacklistModel extends Model<VideoBlacklistModel> {
54 }) 54 })
55 Video: VideoModel 55 Video: VideoModel
56 56
57 static listForApi (start: number, count: number, sort: SortType, type?: VideoBlacklistType) { 57 static listForApi (parameters: {
58 start: number
59 count: number
60 sort: SortType
61 search?: string
62 type?: VideoBlacklistType
63 }) {
64 const { start, count, sort, search, type } = parameters
65
58 function buildBaseQuery (): FindOptions { 66 function buildBaseQuery (): FindOptions {
59 return { 67 return {
60 offset: start, 68 offset: start,
@@ -70,6 +78,7 @@ export class VideoBlacklistModel extends Model<VideoBlacklistModel> {
70 { 78 {
71 model: VideoModel, 79 model: VideoModel,
72 required: true, 80 required: true,
81 where: { ...searchAttribute(search, 'name') },
73 include: [ 82 include: [
74 { 83 {
75 model: VideoChannelModel.scope({ method: [ VideoChannelScopeNames.SUMMARY, { withAccount: true } as SummaryOptions ] }), 84 model: VideoChannelModel.scope({ method: [ VideoChannelScopeNames.SUMMARY, { withAccount: true } as SummaryOptions ] }),