From 0251197e249cc03b65805ed6805da72bf4573529 Mon Sep 17 00:00:00 2001 From: Rigel Kent Date: Mon, 20 Apr 2020 11:20:12 +0200 Subject: [PATCH] Factorize rest-table and fix/simplify SQL --- .../followers-list.component.ts | 2 - .../following-list.component.ts | 2 - .../video-redundancies-list.component.ts | 1 - .../instance-account-blocklist.component.ts | 2 - .../instance-server-blocklist.component.ts | 2 - .../video-abuse-list.component.html | 26 ++++++++--- .../video-abuse-list.component.ts | 4 +- .../video-blacklist-list.component.ts | 2 - .../app/+admin/system/jobs/jobs.component.ts | 1 - .../users/user-list/user-list.component.ts | 1 - .../my-account-blocklist.component.ts | 1 - .../my-account-server-blocklist.component.ts | 1 - .../my-account-ownership.component.ts | 1 - .../my-account-video-imports.component.ts | 2 - client/src/app/shared/rest/rest-table.ts | 4 +- server/helpers/middlewares/video-abuses.ts | 2 +- .../migrations/0490-abuse-video.ts | 4 +- server/models/account/account-blocklist.ts | 4 +- server/models/server/server-blocklist.ts | 2 - server/models/utils.ts | 8 ++-- server/models/video/video-abuse.ts | 43 +++++++------------ server/models/video/video-blacklist.ts | 2 +- server/models/video/video.ts | 6 +-- 23 files changed, 50 insertions(+), 73 deletions(-) diff --git a/client/src/app/+admin/follows/followers-list/followers-list.component.ts b/client/src/app/+admin/follows/followers-list/followers-list.component.ts index 00f447bb2..17352a601 100644 --- a/client/src/app/+admin/follows/followers-list/followers-list.component.ts +++ b/client/src/app/+admin/follows/followers-list/followers-list.component.ts @@ -14,8 +14,6 @@ import { I18n } from '@ngx-translate/i18n-polyfill' export class FollowersListComponent extends RestTable implements OnInit { followers: ActorFollow[] = [] totalRecords = 0 - rowsPerPageOptions = [ 20, 50, 100 ] - rowsPerPage = this.rowsPerPageOptions[0] sort: SortMeta = { field: 'createdAt', order: -1 } pagination: RestPagination = { count: this.rowsPerPage, start: 0 } diff --git a/client/src/app/+admin/follows/following-list/following-list.component.ts b/client/src/app/+admin/follows/following-list/following-list.component.ts index e1b1f8aeb..6ddbf02d6 100644 --- a/client/src/app/+admin/follows/following-list/following-list.component.ts +++ b/client/src/app/+admin/follows/following-list/following-list.component.ts @@ -18,8 +18,6 @@ export class FollowingListComponent extends RestTable implements OnInit { following: ActorFollow[] = [] totalRecords = 0 - rowsPerPageOptions = [ 20, 50, 100 ] - rowsPerPage = this.rowsPerPageOptions[0] sort: SortMeta = { field: 'createdAt', order: -1 } pagination: RestPagination = { count: this.rowsPerPage, start: 0 } diff --git a/client/src/app/+admin/follows/video-redundancies-list/video-redundancies-list.component.ts b/client/src/app/+admin/follows/video-redundancies-list/video-redundancies-list.component.ts index f91800175..267a1f58e 100644 --- a/client/src/app/+admin/follows/video-redundancies-list/video-redundancies-list.component.ts +++ b/client/src/app/+admin/follows/video-redundancies-list/video-redundancies-list.component.ts @@ -20,7 +20,6 @@ export class VideoRedundanciesListComponent extends RestTable implements OnInit videoRedundancies: VideoRedundancy[] = [] totalRecords = 0 - rowsPerPage = 10 sort: SortMeta = { field: 'name', order: 1 } pagination: RestPagination = { count: this.rowsPerPage, start: 0 } diff --git a/client/src/app/+admin/moderation/instance-blocklist/instance-account-blocklist.component.ts b/client/src/app/+admin/moderation/instance-blocklist/instance-account-blocklist.component.ts index 6bd015234..73a9ae75d 100644 --- a/client/src/app/+admin/moderation/instance-blocklist/instance-account-blocklist.component.ts +++ b/client/src/app/+admin/moderation/instance-blocklist/instance-account-blocklist.component.ts @@ -14,8 +14,6 @@ import { Actor } from '@app/shared/actor/actor.model' export class InstanceAccountBlocklistComponent extends RestTable implements OnInit { blockedAccounts: AccountBlock[] = [] totalRecords = 0 - rowsPerPageOptions = [ 20, 50, 100 ] - rowsPerPage = this.rowsPerPageOptions[0] sort: SortMeta = { field: 'createdAt', order: -1 } pagination: RestPagination = { count: this.rowsPerPage, start: 0 } diff --git a/client/src/app/+admin/moderation/instance-blocklist/instance-server-blocklist.component.ts b/client/src/app/+admin/moderation/instance-blocklist/instance-server-blocklist.component.ts index 344e2d135..559c9c0b0 100644 --- a/client/src/app/+admin/moderation/instance-blocklist/instance-server-blocklist.component.ts +++ b/client/src/app/+admin/moderation/instance-blocklist/instance-server-blocklist.component.ts @@ -17,8 +17,6 @@ export class InstanceServerBlocklistComponent extends RestTable implements OnIni blockedServers: ServerBlock[] = [] totalRecords = 0 - rowsPerPageOptions = [ 20, 50, 100 ] - rowsPerPage = this.rowsPerPageOptions[0] sort: SortMeta = { field: 'createdAt', order: -1 } pagination: RestPagination = { count: this.rowsPerPage, start: 0 } diff --git a/client/src/app/+admin/moderation/video-abuse-list/video-abuse-list.component.html b/client/src/app/+admin/moderation/video-abuse-list/video-abuse-list.component.html index 6dacfe243..4afa2850b 100644 --- a/client/src/app/+admin/moderation/video-abuse-list/video-abuse-list.component.html +++ b/client/src/app/+admin/moderation/video-abuse-list/video-abuse-list.component.html @@ -20,7 +20,7 @@ Reporter Video - Created + Created State @@ -59,7 +59,9 @@ {{ videoAbuse.nth }}/{{ videoAbuse.count }} + > + {{ videoAbuse.nth }}/{{ videoAbuse.count }} +
@@ -67,7 +69,7 @@
-
by {{ videoAbuse.video.channel?.displayName }} on {{ videoAbuse.video.channel?.host }}
+
by {{ videoAbuse.video.channel?.displayName }} on {{ videoAbuse.video.channel?.host }}
@@ -75,13 +77,15 @@
-
Deleted
+
+ Deleted +
{{ videoAbuse.video.name }}
-
by {{ videoAbuse.video.channel?.displayName }} on {{ videoAbuse.video.channel?.host }}
+
by {{ videoAbuse.video.channel?.displayName }} on {{ videoAbuse.video.channel?.host }}
@@ -107,8 +111,10 @@
- +
+ +
Reporter @@ -128,6 +134,7 @@
+
Reportee @@ -147,6 +154,7 @@
+
Updated @@ -157,16 +165,20 @@ Report #{{ videoAbuse.id }}
+
Note
+
+
- The video was {{ videoAbuse.video.deleted ? 'deleted' : 'blacklisted' }} + The video was deleted + The video was blacklisted
diff --git a/client/src/app/+admin/moderation/video-abuse-list/video-abuse-list.component.ts b/client/src/app/+admin/moderation/video-abuse-list/video-abuse-list.component.ts index 2ece5dcc1..f04efa9fc 100644 --- a/client/src/app/+admin/moderation/video-abuse-list/video-abuse-list.component.ts +++ b/client/src/app/+admin/moderation/video-abuse-list/video-abuse-list.component.ts @@ -29,8 +29,6 @@ export class VideoAbuseListComponent extends RestTable implements OnInit, AfterV videoAbuses: (VideoAbuse & { moderationCommentHtml?: string, reasonHtml?: string })[] = [] totalRecords = 0 - rowsPerPageOptions = [ 20, 50, 100 ] - rowsPerPage = this.rowsPerPageOptions[0] sort: SortMeta = { field: 'createdAt', order: 1 } pagination: RestPagination = { count: this.rowsPerPage, start: 0 } @@ -199,7 +197,7 @@ export class VideoAbuseListComponent extends RestTable implements OnInit, AfterV } ngAfterViewInit () { - this.setTableFilter(this.search) + if (this.search) this.setTableFilter(this.search) } getIdentifier () { diff --git a/client/src/app/+admin/moderation/video-blacklist-list/video-blacklist-list.component.ts b/client/src/app/+admin/moderation/video-blacklist-list/video-blacklist-list.component.ts index e9925f9bf..63ecdeb9f 100644 --- a/client/src/app/+admin/moderation/video-blacklist-list/video-blacklist-list.component.ts +++ b/client/src/app/+admin/moderation/video-blacklist-list/video-blacklist-list.component.ts @@ -17,8 +17,6 @@ import { MarkdownService } from '@app/shared/renderer' export class VideoBlacklistListComponent extends RestTable implements OnInit { blacklist: (VideoBlacklist & { reasonHtml?: string })[] = [] totalRecords = 0 - rowsPerPageOptions = [ 20, 50, 100 ] - rowsPerPage = this.rowsPerPageOptions[0] sort: SortMeta = { field: 'createdAt', order: -1 } pagination: RestPagination = { count: this.rowsPerPage, start: 0 } listBlacklistTypeFilter: VideoBlacklistType = undefined diff --git a/client/src/app/+admin/system/jobs/jobs.component.ts b/client/src/app/+admin/system/jobs/jobs.component.ts index dc8343b52..4f7f7c368 100644 --- a/client/src/app/+admin/system/jobs/jobs.component.ts +++ b/client/src/app/+admin/system/jobs/jobs.component.ts @@ -40,7 +40,6 @@ export class JobsComponent extends RestTable implements OnInit { jobs: Job[] = [] totalRecords: number - rowsPerPage = 10 sort: SortMeta = { field: 'createdAt', order: -1 } pagination: RestPagination = { count: this.rowsPerPage, start: 0 } diff --git a/client/src/app/+admin/users/user-list/user-list.component.ts b/client/src/app/+admin/users/user-list/user-list.component.ts index 667a0e1fd..da50b7ed0 100644 --- a/client/src/app/+admin/users/user-list/user-list.component.ts +++ b/client/src/app/+admin/users/user-list/user-list.component.ts @@ -19,7 +19,6 @@ export class UserListComponent extends RestTable implements OnInit { users: User[] = [] totalRecords = 0 - rowsPerPage = 10 sort: SortMeta = { field: 'createdAt', order: 1 } pagination: RestPagination = { count: this.rowsPerPage, start: 0 } diff --git a/client/src/app/+my-account/my-account-blocklist/my-account-blocklist.component.ts b/client/src/app/+my-account/my-account-blocklist/my-account-blocklist.component.ts index d8e9fc514..fd1fabcdb 100644 --- a/client/src/app/+my-account/my-account-blocklist/my-account-blocklist.component.ts +++ b/client/src/app/+my-account/my-account-blocklist/my-account-blocklist.component.ts @@ -13,7 +13,6 @@ import { AccountBlock, BlocklistService } from '@app/shared/blocklist' export class MyAccountBlocklistComponent extends RestTable implements OnInit { blockedAccounts: AccountBlock[] = [] totalRecords = 0 - rowsPerPage = 10 sort: SortMeta = { field: 'createdAt', order: -1 } pagination: RestPagination = { count: this.rowsPerPage, start: 0 } diff --git a/client/src/app/+my-account/my-account-blocklist/my-account-server-blocklist.component.ts b/client/src/app/+my-account/my-account-blocklist/my-account-server-blocklist.component.ts index 9049dc69c..483c11804 100644 --- a/client/src/app/+my-account/my-account-blocklist/my-account-server-blocklist.component.ts +++ b/client/src/app/+my-account/my-account-blocklist/my-account-server-blocklist.component.ts @@ -14,7 +14,6 @@ import { BlocklistService } from '@app/shared/blocklist' export class MyAccountServerBlocklistComponent extends RestTable implements OnInit { blockedServers: ServerBlock[] = [] totalRecords = 0 - rowsPerPage = 10 sort: SortMeta = { field: 'createdAt', order: -1 } pagination: RestPagination = { count: this.rowsPerPage, start: 0 } diff --git a/client/src/app/+my-account/my-account-ownership/my-account-ownership.component.ts b/client/src/app/+my-account/my-account-ownership/my-account-ownership.component.ts index 324641be1..f0a6303d1 100644 --- a/client/src/app/+my-account/my-account-ownership/my-account-ownership.component.ts +++ b/client/src/app/+my-account/my-account-ownership/my-account-ownership.component.ts @@ -14,7 +14,6 @@ import { MyAccountAcceptOwnershipComponent } from './my-account-accept-ownership export class MyAccountOwnershipComponent extends RestTable implements OnInit { videoChangeOwnerships: VideoChangeOwnership[] = [] totalRecords = 0 - rowsPerPage = 10 sort: SortMeta = { field: 'createdAt', order: -1 } pagination: RestPagination = { count: this.rowsPerPage, start: 0 } diff --git a/client/src/app/+my-account/my-account-video-imports/my-account-video-imports.component.ts b/client/src/app/+my-account/my-account-video-imports/my-account-video-imports.component.ts index 9281f9789..4452154eb 100644 --- a/client/src/app/+my-account/my-account-video-imports/my-account-video-imports.component.ts +++ b/client/src/app/+my-account/my-account-video-imports/my-account-video-imports.component.ts @@ -2,7 +2,6 @@ import { Component, OnInit } from '@angular/core' import { RestPagination, RestTable } from '@app/shared' import { SortMeta } from 'primeng/api' import { Notifier } from '@app/core' -import { I18n } from '@ngx-translate/i18n-polyfill' import { VideoImport, VideoImportState } from '../../../../../shared/models/videos' import { VideoImportService } from '@app/shared/video-import' @@ -14,7 +13,6 @@ import { VideoImportService } from '@app/shared/video-import' export class MyAccountVideoImportsComponent extends RestTable implements OnInit { videoImports: VideoImport[] = [] totalRecords = 0 - rowsPerPage = 10 sort: SortMeta = { field: 'createdAt', order: 1 } pagination: RestPagination = { count: this.rowsPerPage, start: 0 } diff --git a/client/src/app/shared/rest/rest-table.ts b/client/src/app/shared/rest/rest-table.ts index 5954a7ba1..891fb7b2b 100644 --- a/client/src/app/shared/rest/rest-table.ts +++ b/client/src/app/shared/rest/rest-table.ts @@ -7,11 +7,13 @@ import { debounceTime, distinctUntilChanged } from 'rxjs/operators' export abstract class RestTable { abstract totalRecords: number - abstract rowsPerPage: number abstract sort: SortMeta abstract pagination: RestPagination search: string + rowsPerPageOptions = [ 10, 20, 50, 100 ] + rowsPerPage = this.rowsPerPageOptions[0] + private searchStream: Subject abstract getIdentifier (): string diff --git a/server/helpers/middlewares/video-abuses.ts b/server/helpers/middlewares/video-abuses.ts index 7553a5eb3..97a5724b6 100644 --- a/server/helpers/middlewares/video-abuses.ts +++ b/server/helpers/middlewares/video-abuses.ts @@ -7,7 +7,7 @@ async function doesVideoAbuseExist (abuseIdArg: number | string, videoUUID: stri let videoAbuse = await VideoAbuseModel.loadByIdAndVideoId(abuseId, null, videoUUID) if (!videoAbuse) { - const userId = res.locals.oauth ? res.locals.oauth.token.User.id : undefined + const userId = res.locals.oauth?.token.User.id const video = await fetchVideo(videoUUID, 'all', userId) if (video) videoAbuse = await VideoAbuseModel.loadByIdAndVideoId(abuseId, video.id) diff --git a/server/initializers/migrations/0490-abuse-video.ts b/server/initializers/migrations/0490-abuse-video.ts index 26333feb5..610307aa4 100644 --- a/server/initializers/migrations/0490-abuse-video.ts +++ b/server/initializers/migrations/0490-abuse-video.ts @@ -11,10 +11,8 @@ async function up (utils: { allowNull: true } await utils.queryInterface.addColumn('videoAbuse', 'deletedVideo', deletedVideo) - await utils.sequelize.query(`ALTER TABLE "videoAbsue" ALTER COLUMN "videoId" DROP NOT NULL;`) + await utils.sequelize.query(`ALTER TABLE "videoAbuse" ALTER COLUMN "videoId" DROP NOT NULL;`) await utils.sequelize.query(`ALTER TABLE "videoAbuse" DROP CONSTRAINT IF EXISTS "videoAbuse_videoId_fkey";`) - await utils.sequelize.query(`ALTER TABLE "videoAbuse" ADD CONSTRAINT "videoAbuse_videoId_fkey" - FOREIGN KEY ("videoId") REFERENCES video(id) ON UPDATE CASCADE ON DELETE SET NULL;`) } diff --git a/server/models/account/account-blocklist.ts b/server/models/account/account-blocklist.ts index fe2d5d010..d8a7ce4b4 100644 --- a/server/models/account/account-blocklist.ts +++ b/server/models/account/account-blocklist.ts @@ -133,8 +133,8 @@ export class AccountBlocklistModel extends Model { if (search) { Object.assign(where, { [Op.or]: [ - { ...searchAttribute(search, '$BlockedAccount.name$') }, - { ...searchAttribute(search, '$BlockedAccount.Actor.url$') } + searchAttribute(search, '$BlockedAccount.name$'), + searchAttribute(search, '$BlockedAccount.Actor.url$') ] }) } diff --git a/server/models/server/server-blocklist.ts b/server/models/server/server-blocklist.ts index 764203d2c..892024c04 100644 --- a/server/models/server/server-blocklist.ts +++ b/server/models/server/server-blocklist.ts @@ -139,8 +139,6 @@ export class ServerBlocklistModel extends Model { } } - console.log(search) - return ServerBlocklistModel .scope([ ScopeNames.WITH_ACCOUNT, ScopeNames.WITH_SERVER ]) .findAndCountAll(query) diff --git a/server/models/utils.ts b/server/models/utils.ts index 7137419a2..bdf2291f0 100644 --- a/server/models/utils.ts +++ b/server/models/utils.ts @@ -208,13 +208,15 @@ function buildDirectionAndField (value: string) { } function searchAttribute (sourceField, targetField) { - return sourceField - ? { + if (sourceField) { + return { [targetField]: { [Op.iLike]: `%${sourceField}%` } } - : {} + } else { + return {} + } } // --------------------------------------------------------------------------- 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' import { CONSTRAINTS_FIELDS, VIDEO_ABUSE_STATES } from '../../initializers/constants' import { MUserAccountId, MVideoAbuse, MVideoAbuseFormattable, MVideoAbuseVideo } from '../../typings/models' import * as Bluebird from 'bluebird' -import { literal, Op } from 'sequelize' +import { literal, Op, Sequelize } from 'sequelize' import { ThumbnailModel } from './thumbnail' import { VideoBlacklistModel } from './video-blacklist' import { ScopeNames as VideoChannelScopeNames, SummaryOptions, VideoChannelModel } from './video-channel' @@ -31,7 +31,7 @@ export enum ScopeNames { searchVideo?: string searchVideoChannel?: string serverAccountId: number - userAccountId: any + userAccountId: number }) => { let where = { reporterAccountId: { @@ -45,28 +45,28 @@ export enum ScopeNames { { [Op.and]: [ { videoId: { [Op.not]: null } }, - { '$Video.name$': { [Op.iLike]: `%${options.search}%` } } + searchAttribute(options.search, '$Video.name$') ] }, { [Op.and]: [ { videoId: { [Op.not]: null } }, - { '$Video.VideoChannel.name$': { [Op.iLike]: `%${options.search}%` } } + searchAttribute(options.search, '$Video.VideoChannel.name$') ] }, { [Op.and]: [ { deletedVideo: { [Op.not]: null } }, - { deletedVideo: { name: { [Op.iLike]: `%${options.search}%` } } } + { deletedVideo: searchAttribute(options.search, 'name') } ] }, { [Op.and]: [ { deletedVideo: { [Op.not]: null } }, - { deletedVideo: { channel: { displayName: { [Op.iLike]: `%${options.search}%` } } } } + { deletedVideo: { channel: searchAttribute(options.search, 'displayName') } } ] }, - { '$Account.name$': { [Op.iLike]: `%${options.search}%` } } + searchAttribute(options.search, '$Account.name$') ] }) } @@ -77,13 +77,9 @@ export enum ScopeNames { [ literal( '(' + - 'SELECT t.count ' + - 'FROM ( ' + - 'SELECT id, ' + - 'count(id) OVER (PARTITION BY "videoId") ' + - 'FROM "videoAbuse" ' + - ') t ' + - 'WHERE t.id = "VideoAbuseModel".id ' + + 'SELECT count(*) ' + + 'FROM "videoAbuse" ' + + 'WHERE "videoId" = "VideoAbuseModel"."videoId" ' + ')' ), 'countReportsForVideo' @@ -118,20 +114,11 @@ export enum ScopeNames { [ literal( '(' + - 'WITH ' + - 'ids AS ( ' + - 'SELECT "account"."id" ' + - 'FROM "account" ' + - 'INNER JOIN "videoChannel" ON "videoChannel"."accountId" = "account"."id" ' + - 'INNER JOIN "video" ON "video"."channelId" = "videoChannel"."id" ' + - 'WHERE "video"."id" = "VideoAbuseModel"."videoId" ' + - ') ' + - 'SELECT count("videoAbuse"."id") ' + + 'SELECT count(DISTINCT "videoAbuse"."id") ' + 'FROM "videoAbuse" ' + 'INNER JOIN "video" ON "video"."id" = "videoAbuse"."videoId" ' + 'INNER JOIN "videoChannel" ON "videoChannel"."id" = "video"."channelId" ' + - 'INNER JOIN "account" ON "videoChannel"."accountId" = "account"."id" ' + - 'INNER JOIN ids ON "account"."id" = ids.id ' + + 'INNER JOIN "account" ON "videoChannel"."accountId" = "Video->VideoChannel"."accountId" ' + ')' ), 'countReportsForReportee' @@ -142,19 +129,19 @@ export enum ScopeNames { { model: AccountModel, required: true, - where: { ...searchAttribute(options.searchReporter, 'name') } + where: searchAttribute(options.searchReporter, 'name') }, { model: VideoModel, required: false, - where: { ...searchAttribute(options.searchVideo, 'name') }, + where: searchAttribute(options.searchVideo, 'name'), include: [ { model: ThumbnailModel }, { model: VideoChannelModel.scope({ method: [ VideoChannelScopeNames.SUMMARY, { withAccount: true } as SummaryOptions ] }), - where: { ...searchAttribute(options.searchVideoChannel, 'name') } + where: searchAttribute(options.searchVideoChannel, 'name') }, { 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 { { model: VideoModel, required: true, - where: { ...searchAttribute(search, 'name') }, + where: searchAttribute(search, 'name'), include: [ { 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 { const details = instance.toFormattedDetailsJSON() for (const abuse of instance.VideoAbuses) { - tasks.push((_ => { - abuse.deletedVideo = details - return abuse.save({ transaction: options.transaction }) - })()) + abuse.deletedVideo = details + tasks.push(abuse.save({ transaction: options.transaction })) } Promise.all(tasks)