X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Fmodels%2Fvideo%2Fvideo-comment.ts;h=ff514280936bb50cc39291293e2358f1bfe2c68a;hb=d5b0313c0c6f1cb2ce1510f0317f8fbdef663145;hp=2d60c6a30218527ce70b26e8c0b31ee76af2a892;hpb=7b51ede977c299a74728171d8c124bcc4cbba6ea;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/models/video/video-comment.ts b/server/models/video/video-comment.ts index 2d60c6a30..ff5142809 100644 --- a/server/models/video/video-comment.ts +++ b/server/models/video/video-comment.ts @@ -1,5 +1,4 @@ -import { uniq } from 'lodash' -import { FindOptions, Op, Order, QueryTypes, ScopeOptions, Sequelize, Transaction, WhereOptions } from 'sequelize' +import { FindOptions, Op, Order, QueryTypes, Sequelize, Transaction } from 'sequelize' import { AllowNull, BelongsTo, @@ -16,7 +15,7 @@ import { } from 'sequelize-typescript' import { getServerActor } from '@server/models/application/application' import { MAccount, MAccountId, MUserAccountId } from '@server/types/models' -import { VideoPrivacy } from '@shared/models' +import { pick, uniqify } from '@shared/core-utils' import { AttributesOnly } from '@shared/typescript-utils' import { ActivityTagObject, ActivityTombstoneObject } from '../../../shared/models/activitypub/objects/common-objects' import { VideoCommentObject } from '../../../shared/models/activitypub/objects/video-comment-object' @@ -40,61 +39,19 @@ import { } from '../../types/models/video' import { VideoCommentAbuseModel } from '../abuse/video-comment-abuse' import { AccountModel } from '../account/account' -import { ActorModel, unusedActorAttributesForAPI } from '../actor/actor' -import { - buildBlockedAccountSQL, - buildBlockedAccountSQLOptimized, - buildLocalAccountIdsIn, - getCommentSort, - searchAttribute, - throwIfNotValid -} from '../utils' +import { ActorModel } from '../actor/actor' +import { buildLocalAccountIdsIn, buildSQLAttributes, throwIfNotValid } from '../shared' +import { ListVideoCommentsOptions, VideoCommentListQueryBuilder } from './sql/comment/video-comment-list-query-builder' import { VideoModel } from './video' import { VideoChannelModel } from './video-channel' export enum ScopeNames { WITH_ACCOUNT = 'WITH_ACCOUNT', - WITH_ACCOUNT_FOR_API = 'WITH_ACCOUNT_FOR_API', WITH_IN_REPLY_TO = 'WITH_IN_REPLY_TO', - WITH_VIDEO = 'WITH_VIDEO', - ATTRIBUTES_FOR_API = 'ATTRIBUTES_FOR_API' + WITH_VIDEO = 'WITH_VIDEO' } @Scopes(() => ({ - [ScopeNames.ATTRIBUTES_FOR_API]: (blockerAccountIds: number[]) => { - return { - attributes: { - include: [ - [ - Sequelize.literal( - '(' + - 'WITH "blocklist" AS (' + buildBlockedAccountSQL(blockerAccountIds) + ')' + - 'SELECT COUNT("replies"."id") ' + - 'FROM "videoComment" AS "replies" ' + - 'WHERE "replies"."originCommentId" = "VideoCommentModel"."id" ' + - 'AND "deletedAt" IS NULL ' + - 'AND "accountId" NOT IN (SELECT "id" FROM "blocklist")' + - ')' - ), - 'totalReplies' - ], - [ - Sequelize.literal( - '(' + - 'SELECT COUNT("replies"."id") ' + - 'FROM "videoComment" AS "replies" ' + - 'INNER JOIN "video" ON "video"."id" = "replies"."videoId" ' + - 'INNER JOIN "videoChannel" ON "videoChannel"."id" = "video"."channelId" ' + - 'WHERE "replies"."originCommentId" = "VideoCommentModel"."id" ' + - 'AND "replies"."accountId" = "videoChannel"."accountId"' + - ')' - ), - 'totalRepliesFromVideoAuthor' - ] - ] - } - } as FindOptions - }, [ScopeNames.WITH_ACCOUNT]: { include: [ { @@ -102,22 +59,6 @@ export enum ScopeNames { } ] }, - [ScopeNames.WITH_ACCOUNT_FOR_API]: { - include: [ - { - model: AccountModel.unscoped(), - include: [ - { - attributes: { - exclude: unusedActorAttributesForAPI - }, - model: ActorModel, // Default scope includes avatar and server - required: true - } - ] - } - ] - }, [ScopeNames.WITH_IN_REPLY_TO]: { include: [ { @@ -251,6 +192,18 @@ export class VideoCommentModel extends Model { const query: FindOptions = { where: { @@ -312,94 +265,25 @@ export class VideoCommentModel extends Model { - return { - offset: start, - limit: count, - order: getCommentSort(sort), - where, - include: [ - { - model: AccountModel.unscoped(), - required: true, - where: whereAccount, - include: [ - { - attributes: { - exclude: unusedActorAttributesForAPI - }, - model: forCount === true - ? ActorModel.unscoped() // Default scope includes avatar and server - : ActorModel, - required: true, - where: whereActor - } - ] - }, - { - model: VideoModel.unscoped(), - required: true, - where: whereVideo - } - ] - } + selectType: 'api', + notDeleted: true } return Promise.all([ - VideoCommentModel.count(getQuery(true)), - VideoCommentModel.findAll(getQuery(false)) - ]).then(([ total, data ]) => ({ total, data })) + new VideoCommentListQueryBuilder(VideoCommentModel.sequelize, queryOptions).listComments(), + new VideoCommentListQueryBuilder(VideoCommentModel.sequelize, queryOptions).countComments() + ]).then(([ rows, count ]) => { + return { total: count, data: rows } + }) } static async listThreadsForApi (parameters: { @@ -410,67 +294,40 @@ export class VideoCommentModel extends Model(), + new VideoCommentListQueryBuilder(VideoCommentModel.sequelize, countOptions).countComments(), + new VideoCommentListQueryBuilder(VideoCommentModel.sequelize, notDeletedCountOptions).countComments() ]).then(([ rows, count, totalNotDeletedComments ]) => { return { total: count, data: rows, totalNotDeletedComments } }) @@ -478,54 +335,29 @@ export class VideoCommentModel extends Model ({ total, data })) + new VideoCommentListQueryBuilder(VideoCommentModel.sequelize, queryOptions).listComments(), + new VideoCommentListQueryBuilder(VideoCommentModel.sequelize, queryOptions).countComments() + ]).then(([ rows, count ]) => { + return { total: count, data: rows } + }) } static listThreadParentComments (comment: MCommentId, t: Transaction, order: 'ASC' | 'DESC' = 'ASC'): Promise { @@ -553,31 +385,31 @@ export class VideoCommentModel extends Model(query) - ]).then(([ total, data ]) => ({ total, data })) + new VideoCommentListQueryBuilder(VideoCommentModel.sequelize, queryOptions).listComments(), + new VideoCommentListQueryBuilder(VideoCommentModel.sequelize, queryOptions).countComments() + ]).then(([ rows, count ]) => { + return { total: count, data: rows } + }) } static async listForFeed (parameters: { @@ -586,108 +418,47 @@ export class VideoCommentModel extends Model { - const serverActor = await getServerActor() - const { start, count, videoId, accountId, videoChannelId } = parameters - - const whereAnd: WhereOptions[] = buildBlockedAccountSQLOptimized( - '"VideoCommentModel"."accountId"', - [ serverActor.Account.id, '"Video->VideoChannel"."accountId"' ] - ) + }) { + const blockerAccountIds = await VideoCommentModel.buildBlockerAccountIds({ user: null }) - if (accountId) { - whereAnd.push({ - accountId - }) - } + const queryOptions: ListVideoCommentsOptions = { + ...pick(parameters, [ 'start', 'count', 'accountId', 'videoId', 'videoChannelId' ]), - const accountWhere = { - [Op.and]: whereAnd - } + selectType: 'feed', - const videoChannelWhere = videoChannelId ? { id: videoChannelId } : undefined + sort: '-createdAt', + onPublicVideo: true, + notDeleted: true, - const query = { - order: [ [ 'createdAt', 'DESC' ] ] as Order, - offset: start, - limit: count, - where: { - deletedAt: null, - accountId: accountWhere - }, - include: [ - { - attributes: [ 'name', 'uuid' ], - model: VideoModel.unscoped(), - required: true, - where: { - privacy: VideoPrivacy.PUBLIC - }, - include: [ - { - attributes: [ 'accountId' ], - model: VideoChannelModel.unscoped(), - required: true, - where: videoChannelWhere - } - ] - } - ] + blockerAccountIds } - if (videoId) query.where['videoId'] = videoId - - return VideoCommentModel - .scope([ ScopeNames.WITH_ACCOUNT ]) - .findAll(query) + return new VideoCommentListQueryBuilder(VideoCommentModel.sequelize, queryOptions).listComments() } static listForBulkDelete (ofAccount: MAccount, filter: { onVideosOfAccount?: MAccountId } = {}) { - const accountWhere = filter.onVideosOfAccount - ? { id: filter.onVideosOfAccount.id } - : {} + const queryOptions: ListVideoCommentsOptions = { + selectType: 'comment-only', - const query = { - limit: 1000, - where: { - deletedAt: null, - accountId: ofAccount.id - }, - include: [ - { - model: VideoModel, - required: true, - include: [ - { - model: VideoChannelModel, - required: true, - include: [ - { - model: AccountModel, - required: true, - where: accountWhere - } - ] - } - ] - } - ] + accountId: ofAccount.id, + videoAccountOwnerId: filter.onVideosOfAccount?.id, + + notDeleted: true, + count: 5000 } - return VideoCommentModel - .scope([ ScopeNames.WITH_ACCOUNT ]) - .findAll(query) + return new VideoCommentListQueryBuilder(VideoCommentModel.sequelize, queryOptions).listComments() } static async getStats () { const totalLocalVideoComments = await VideoCommentModel.count({ include: [ { - model: AccountModel, + model: AccountModel.unscoped(), required: true, include: [ { - model: ActorModel, + model: ActorModel.unscoped(), required: true, where: { serverId: null @@ -744,9 +515,7 @@ export class VideoCommentModel extends Model { + const { user } = options const serverActor = await getServerActor() const blockerAccountIds = [ serverActor.Account.id ] if (user) blockerAccountIds.push(user.Account.id) - if (isVideoOwned) { - const videoOwnerAccount = await AccountModel.loadAccountIdFromVideo(videoId) - blockerAccountIds.push(videoOwnerAccount.id) - } - return blockerAccountIds } }