Factorize rest-table and fix/simplify SQL

pull/2711/head
Rigel Kent 2020-04-20 11:20:12 +02:00 committed by Rigel Kent
parent b8cf27c0f8
commit 0251197e24
23 changed files with 50 additions and 73 deletions

View File

@ -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 }

View File

@ -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 }

View File

@ -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 }

View File

@ -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 }

View File

@ -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 }

View File

@ -20,7 +20,7 @@
<th style="width: 40px;"></th>
<th style="width: 20%;" pResizableColumn i18n>Reporter</th>
<th i18n>Video</th>
<th style="width:190px;" i18n pSortableColumn="createdAt">Created <p-sortIcon field="createdAt"></p-sortIcon></th>
<th style="width: 190px;" i18n pSortableColumn="createdAt">Created <p-sortIcon field="createdAt"></p-sortIcon></th>
<th i18n pSortableColumn="state" style="width: 80px;">State <p-sortIcon field="state"></p-sortIcon></th>
<th style="width: 120px;"></th>
</tr>
@ -59,7 +59,9 @@
<span
class="video-table-video-image-label" *ngIf="videoAbuse.count > 1"
i18n-title title="This video has been reported multiple times."
>{{ videoAbuse.nth }}/{{ videoAbuse.count }}</span>
>
{{ videoAbuse.nth }}/{{ videoAbuse.count }}
</span>
</div>
<div class="video-table-video-text">
<div>
@ -67,7 +69,7 @@
<span *ngIf="!videoAbuse.video.blacklisted" class="glyphicon glyphicon-new-window"></span>
<span *ngIf="videoAbuse.video.blacklisted" i18n-title title="Video was blacklisted" class="glyphicon glyphicon-ban-circle"></span>
</div>
<div class="text-muted">by {{ videoAbuse.video.channel?.displayName }} on {{ videoAbuse.video.channel?.host }} </div>
<div class="text-muted" i18n>by {{ videoAbuse.video.channel?.displayName }} on {{ videoAbuse.video.channel?.host }} </div>
</div>
</div>
</a>
@ -75,13 +77,15 @@
<td *ngIf="videoAbuse.video.deleted" class="c-hand" [pRowToggler]="videoAbuse">
<div class="video-table-video" i18n-title title="Video was deleted">
<div class="video-table-video-image"><span i18n>Deleted</span></div>
<div class="video-table-video-image">
<span i18n>Deleted</span>
</div>
<div class="video-table-video-text">
<div>
{{ videoAbuse.video.name }}
<span class="glyphicon glyphicon-trash"></span>
</div>
<div class="text-muted">by {{ videoAbuse.video.channel?.displayName }} on {{ videoAbuse.video.channel?.host }} </div>
<div class="text-muted" i18n>by {{ videoAbuse.video.channel?.displayName }} on {{ videoAbuse.video.channel?.host }} </div>
</div>
</div>
</td>
@ -107,8 +111,10 @@
<tr>
<td class="expand-cell" colspan="6">
<div class="d-flex moderation-expanded">
<!-- report metadata -->
<!-- report left part (report details) -->
<div class="col-8">
<!-- report metadata -->
<div class="d-flex">
<span class="col-3 moderation-expanded-label" i18n>Reporter</span>
<span class="col-9 moderation-expanded-text">
@ -128,6 +134,7 @@
</a>
</span>
</div>
<div class="d-flex">
<span class="col-3 moderation-expanded-label" i18n>Reportee</span>
<span class="col-9 moderation-expanded-text">
@ -147,6 +154,7 @@
</a>
</span>
</div>
<div class="d-flex">
<span class="col-3 moderation-expanded-label" i18n>Updated</span>
<time class="col-9 moderation-expanded-text video-details-date-updated">{{ videoAbuse.updatedAt | date: 'medium' }}</time>
@ -157,16 +165,20 @@
<span class="col-3 moderation-expanded-label" i18n>Report #{{ videoAbuse.id }}</span>
<span class="col-9 moderation-expanded-text" [innerHTML]="videoAbuse.reasonHtml"></span>
</div>
<div class="mt-3 d-flex" *ngIf="videoAbuse.moderationComment">
<span class="col-3 moderation-expanded-label" i18n>Note</span>
<span class="col-9 moderation-expanded-text" [innerHTML]="videoAbuse.moderationCommentHtml"></span>
</div>
</div>
<!-- report right part (video details) -->
<div class="col-4">
<div class="screenratio">
<div *ngIf="videoAbuse.video.deleted || videoAbuse.video.blacklisted">
<span i18n>The video was {{ videoAbuse.video.deleted ? 'deleted' : 'blacklisted' }}</span>
<span i18n *ngIf="videoAbuse.video.deleted">The video was deleted</span>
<span i18n *ngIf="!videoAbuse.video.deleted">The video was blacklisted</span>
</div>
<div *ngIf="!videoAbuse.video.deleted && !videoAbuse.video.blacklisted" [innerHTML]="videoAbuse.embedHtml"></div>
</div>

View File

@ -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 () {

View File

@ -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

View File

@ -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 }

View File

@ -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 }

View File

@ -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 }

View File

@ -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 }

View File

@ -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 }

View File

@ -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 }

View File

@ -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<string>
abstract getIdentifier (): string

View File

@ -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)

View File

@ -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;`)
}

View File

@ -133,8 +133,8 @@ export class AccountBlocklistModel extends Model<AccountBlocklistModel> {
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$')
]
})
}

View File

@ -139,8 +139,6 @@ export class ServerBlocklistModel extends Model<ServerBlocklistModel> {
}
}
console.log(search)
return ServerBlocklistModel
.scope([ ScopeNames.WITH_ACCOUNT, ScopeNames.WITH_SERVER ])
.findAndCountAll<MServerBlocklistAccountServer>(query)

View File

@ -208,13 +208,15 @@ function buildDirectionAndField (value: string) {
}
function searchAttribute (sourceField, targetField) {
return sourceField
? {
if (sourceField) {
return {
[targetField]: {
[Op.iLike]: `%${sourceField}%`
}
}
: {}
} else {
return {}
}
}
// ---------------------------------------------------------------------------

View File

@ -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' ],

View File

@ -78,7 +78,7 @@ export class VideoBlacklistModel extends Model<VideoBlacklistModel> {
{
model: VideoModel,
required: true,
where: { ...searchAttribute(search, 'name') },
where: searchAttribute(search, 'name'),
include: [
{
model: VideoChannelModel.scope({ method: [ VideoChannelScopeNames.SUMMARY, { withAccount: true } as SummaryOptions ] }),

View File

@ -813,10 +813,8 @@ export class VideoModel extends Model<VideoModel> {
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)