X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Fmodels%2Fvideo%2Fvideo-comment.ts;h=de27b3d875fb3745682f5a056405162b5298f85f;hb=97816649b793bdd0f3df64631ae0ef7cf3d7461c;hp=ba09522cceb492ca30fcf5ae7cc758cf9cda718b;hpb=696d83fd1377486dd03cc1bd02a21d9b6ddd9fcd;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/models/video/video-comment.ts b/server/models/video/video-comment.ts index ba09522cc..de27b3d87 100644 --- a/server/models/video/video-comment.ts +++ b/server/models/video/video-comment.ts @@ -1,9 +1,22 @@ import * as Bluebird from 'bluebird' import { uniq } from 'lodash' -import { FindOptions, Op, Order, ScopeOptions, Sequelize, Transaction } from 'sequelize' -import { AllowNull, BelongsTo, Column, CreatedAt, DataType, ForeignKey, Is, Model, Scopes, Table, UpdatedAt } from 'sequelize-typescript' +import { FindOptions, Op, Order, ScopeOptions, Sequelize, Transaction, WhereOptions } from 'sequelize' +import { + AllowNull, + BelongsTo, + Column, + CreatedAt, + DataType, + ForeignKey, + HasMany, + Is, + Model, + Scopes, + Table, + UpdatedAt +} from 'sequelize-typescript' import { getServerActor } from '@server/models/application/application' -import { MAccount, MAccountId, MUserAccountId } from '@server/typings/models' +import { MAccount, MAccountId, MUserAccountId } from '@server/types/models' import { VideoPrivacy } from '@shared/models' import { ActivityTagObject, ActivityTombstoneObject } from '../../../shared/models/activitypub/objects/common-objects' import { VideoCommentObject } from '../../../shared/models/activitypub/objects/video-comment-object' @@ -23,14 +36,15 @@ import { MCommentOwnerVideoFeed, MCommentOwnerVideoReply, MVideoImmutable -} from '../../typings/models/video' +} from '../../types/models/video' +import { VideoCommentAbuseModel } from '../abuse/video-comment-abuse' import { AccountModel } from '../account/account' import { ActorModel, unusedActorAttributesForAPI } from '../activitypub/actor' -import { buildBlockedAccountSQL, buildLocalAccountIdsIn, getCommentSort, throwIfNotValid } from '../utils' +import { buildBlockedAccountSQL, buildBlockedAccountSQLOptimized, buildLocalAccountIdsIn, getCommentSort, throwIfNotValid } from '../utils' import { VideoModel } from './video' import { VideoChannelModel } from './video-channel' -enum ScopeNames { +export enum ScopeNames { WITH_ACCOUNT = 'WITH_ACCOUNT', WITH_ACCOUNT_FOR_API = 'WITH_ACCOUNT_FOR_API', WITH_IN_REPLY_TO = 'WITH_IN_REPLY_TO', @@ -144,6 +158,11 @@ enum ScopeNames { }, { fields: [ 'accountId' ] + }, + { + fields: [ + { name: 'createdAt', order: 'DESC' } + ] } ] }) @@ -219,6 +238,15 @@ export class VideoCommentModel extends Model { }) Account: AccountModel + @HasMany(() => VideoCommentAbuseModel, { + foreignKey: { + name: 'videoCommentId', + allowNull: true + }, + onDelete: 'set null' + }) + CommentAbuses: VideoCommentAbuseModel[] + static loadById (id: number, t?: Transaction): Bluebird { const query: FindOptions = { where: { @@ -422,8 +450,32 @@ export class VideoCommentModel extends Model { return VideoCommentModel.findAndCountAll(query) } - static async listForFeed (start: number, count: number, videoId?: number): Promise { + static async listForFeed (parameters: { + start: number + count: number + videoId?: number + accountId?: number + videoChannelId?: number + }): Promise { const serverActor = await getServerActor() + const { start, count, videoId, accountId, videoChannelId } = parameters + + const whereAnd: WhereOptions[] = buildBlockedAccountSQLOptimized( + '"VideoCommentModel"."accountId"', + [ serverActor.Account.id, '"Video->VideoChannel"."accountId"' ] + ) + + if (accountId) { + whereAnd.push({ + [Op.eq]: accountId + }) + } + + const accountWhere = { + [Op.and]: whereAnd + } + + const videoChannelWhere = videoChannelId ? { id: videoChannelId } : undefined const query = { order: [ [ 'createdAt', 'DESC' ] ] as Order, @@ -431,11 +483,7 @@ export class VideoCommentModel extends Model { limit: count, where: { deletedAt: null, - accountId: { - [Op.notIn]: Sequelize.literal( - '(' + buildBlockedAccountSQL([ serverActor.Account.id, '"Video->VideoChannel"."accountId"' ]) + ')' - ) - } + accountId: accountWhere }, include: [ { @@ -449,7 +497,8 @@ export class VideoCommentModel extends Model { { attributes: [ 'accountId' ], model: VideoChannelModel.unscoped(), - required: true + required: true, + where: videoChannelWhere } ] } @@ -607,7 +656,7 @@ export class VideoCommentModel extends Model { id: this.id, url: this.url, text: this.text, - threadId: this.originCommentId || this.id, + threadId: this.getThreadId(), inReplyToCommentId: this.inReplyToCommentId || null, videoId: this.videoId, createdAt: this.createdAt,