X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Fmodels%2Fvideo%2Fvideo-comment.ts;h=ff514280936bb50cc39291293e2358f1bfe2c68a;hb=d5b0313c0c6f1cb2ce1510f0317f8fbdef663145;hp=b33c33d5efa002bc52f618584cefcfe508fa70a8;hpb=7024e9120b381b5b3201212f5a18f5cdc14e15ff;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/models/video/video-comment.ts b/server/models/video/video-comment.ts index b33c33d5e..ff5142809 100644 --- a/server/models/video/video-comment.ts +++ b/server/models/video/video-comment.ts @@ -1,22 +1,32 @@ -import { AllowNull, BelongsTo, Column, CreatedAt, DataType, ForeignKey, Is, Model, Scopes, Table, UpdatedAt } from 'sequelize-typescript' +import { FindOptions, Op, Order, QueryTypes, Sequelize, Transaction } 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/types/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' -import { VideoComment } from '../../../shared/models/videos/video-comment.model' -import { isActivityPubUrlValid } from '../../helpers/custom-validators/activitypub/misc' -import { CONSTRAINTS_FIELDS, WEBSERVER } from '../../initializers/constants' -import { AccountModel } from '../account/account' -import { ActorModel } from '../activitypub/actor' -import { buildBlockedAccountSQL, buildLocalAccountIdsIn, getCommentSort, throwIfNotValid } from '../utils' -import { VideoModel } from './video' -import { VideoChannelModel } from './video-channel' -import { getServerActor } from '../../helpers/utils' +import { VideoComment, VideoCommentAdmin } from '../../../shared/models/videos/comment/video-comment.model' import { actorNameAlphabet } from '../../helpers/custom-validators/activitypub/actor' +import { isActivityPubUrlValid } from '../../helpers/custom-validators/activitypub/misc' import { regexpCapture } from '../../helpers/regexp' -import { uniq } from 'lodash' -import { FindOptions, Op, Order, ScopeOptions, Sequelize, Transaction } from 'sequelize' -import * as Bluebird from 'bluebird' +import { CONSTRAINTS_FIELDS, WEBSERVER } from '../../initializers/constants' import { MComment, + MCommentAdminFormattable, MCommentAP, MCommentFormattable, MCommentId, @@ -24,56 +34,24 @@ import { MCommentOwnerReplyVideoLight, MCommentOwnerVideo, MCommentOwnerVideoFeed, - MCommentOwnerVideoReply -} from '../../typings/models/video' -import { MUserAccountId } from '@server/typings/models' + MCommentOwnerVideoReply, + MVideoImmutable +} from '../../types/models/video' +import { VideoCommentAbuseModel } from '../abuse/video-comment-abuse' +import { AccountModel } from '../account/account' +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' -enum ScopeNames { +export enum ScopeNames { WITH_ACCOUNT = 'WITH_ACCOUNT', 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]: (serverAccountId: number, userAccountId?: number) => { - return { - attributes: { - include: [ - [ - Sequelize.literal( - '(' + - 'WITH "blocklist" AS (' + buildBlockedAccountSQL(serverAccountId, userAccountId) + ')' + - 'SELECT COUNT("replies"."id") - (' + - 'SELECT COUNT("replies"."id") ' + - 'FROM "videoComment" AS "replies" ' + - 'WHERE "replies"."originCommentId" = "VideoCommentModel"."id" ' + - 'AND "accountId" IN (SELECT "id" FROM "blocklist")' + - ')' + - 'FROM "videoComment" AS "replies" ' + - 'WHERE "replies"."originCommentId" = "VideoCommentModel"."id" ' + - '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: [ { @@ -125,10 +103,15 @@ enum ScopeNames { }, { fields: [ 'accountId' ] + }, + { + fields: [ + { name: 'createdAt', order: 'DESC' } + ] } ] }) -export class VideoCommentModel extends Model { +export class VideoCommentModel extends Model>> { @CreatedAt createdAt: Date @@ -200,7 +183,28 @@ export class VideoCommentModel extends Model { }) Account: AccountModel - static loadById (id: number, t?: Transaction): Bluebird { + @HasMany(() => VideoCommentAbuseModel, { + foreignKey: { + name: 'videoCommentId', + allowNull: true + }, + onDelete: 'set null' + }) + CommentAbuses: VideoCommentAbuseModel[] + + // --------------------------------------------------------------------------- + + static getSQLAttributes (tableName: string, aliasPrefix = '') { + return buildSQLAttributes({ + model: this, + tableName, + aliasPrefix + }) + } + + // --------------------------------------------------------------------------- + + static loadById (id: number, t?: Transaction): Promise { const query: FindOptions = { where: { id @@ -212,7 +216,7 @@ export class VideoCommentModel extends Model { return VideoCommentModel.findOne(query) } - static loadByIdAndPopulateVideoAndAccountAndReply (id: number, t?: Transaction): Bluebird { + static loadByIdAndPopulateVideoAndAccountAndReply (id: number, t?: Transaction): Promise { const query: FindOptions = { where: { id @@ -226,7 +230,7 @@ export class VideoCommentModel extends Model { .findOne(query) } - static loadByUrlAndPopulateAccountAndVideo (url: string, t?: Transaction): Bluebird { + static loadByUrlAndPopulateAccountAndVideo (url: string, t?: Transaction): Promise { const query: FindOptions = { where: { url @@ -238,7 +242,7 @@ export class VideoCommentModel extends Model { return VideoCommentModel.scope([ ScopeNames.WITH_ACCOUNT, ScopeNames.WITH_VIDEO ]).findOne(query) } - static loadByUrlAndPopulateReplyAndVideoUrlAndAccount (url: string, t?: Transaction): Bluebird { + static loadByUrlAndPopulateReplyAndVideoUrlAndAccount (url: string, t?: Transaction): Promise { const query: FindOptions = { where: { url @@ -256,47 +260,77 @@ export class VideoCommentModel extends Model { return VideoCommentModel.scope([ ScopeNames.WITH_IN_REPLY_TO, ScopeNames.WITH_ACCOUNT ]).findOne(query) } + static listCommentsForApi (parameters: { + start: number + count: number + sort: string + + onLocalVideo?: boolean + isLocal?: boolean + search?: string + searchAccount?: string + searchVideo?: string + }) { + const queryOptions: ListVideoCommentsOptions = { + ...pick(parameters, [ 'start', 'count', 'sort', 'isLocal', 'search', 'searchVideo', 'searchAccount', 'onLocalVideo' ]), + + selectType: 'api', + notDeleted: true + } + + return Promise.all([ + new VideoCommentListQueryBuilder(VideoCommentModel.sequelize, queryOptions).listComments(), + new VideoCommentListQueryBuilder(VideoCommentModel.sequelize, queryOptions).countComments() + ]).then(([ rows, count ]) => { + return { total: count, data: rows } + }) + } + static async listThreadsForApi (parameters: { videoId: number + isVideoOwned: boolean start: number count: number sort: string user?: MUserAccountId }) { - const { videoId, start, count, sort, user } = parameters + const { videoId, user } = parameters - const serverActor = await getServerActor() - const serverAccountId = serverActor.Account.id - const userAccountId = user ? user.Account.id : undefined + const blockerAccountIds = await VideoCommentModel.buildBlockerAccountIds({ user }) - const query = { - offset: start, - limit: count, - order: getCommentSort(sort), - where: { - videoId, - inReplyToCommentId: null, - accountId: { - [Op.notIn]: Sequelize.literal( - '(' + buildBlockedAccountSQL(serverAccountId, userAccountId) + ')' - ) - } - } + const commonOptions: ListVideoCommentsOptions = { + selectType: 'api', + videoId, + blockerAccountIds } - const scopes: (string | ScopeOptions)[] = [ - ScopeNames.WITH_ACCOUNT, - { - method: [ ScopeNames.ATTRIBUTES_FOR_API, serverAccountId, userAccountId ] - } - ] + const listOptions: ListVideoCommentsOptions = { + ...commonOptions, + ...pick(parameters, [ 'sort', 'start', 'count' ]), - return VideoCommentModel - .scope(scopes) - .findAndCountAll(query) - .then(({ rows, count }) => { - return { total: count, data: rows } - }) + isThread: true, + includeReplyCounters: true + } + + const countOptions: ListVideoCommentsOptions = { + ...commonOptions, + + isThread: true + } + + const notDeletedCountOptions: ListVideoCommentsOptions = { + ...commonOptions, + + notDeleted: true + } + + return Promise.all([ + new VideoCommentListQueryBuilder(VideoCommentModel.sequelize, listOptions).listComments(), + new VideoCommentListQueryBuilder(VideoCommentModel.sequelize, countOptions).countComments(), + new VideoCommentListQueryBuilder(VideoCommentModel.sequelize, notDeletedCountOptions).countComments() + ]).then(([ rows, count, totalNotDeletedComments ]) => { + return { total: count, data: rows, totalNotDeletedComments } + }) } static async listThreadCommentsForApi (parameters: { @@ -304,44 +338,29 @@ export class VideoCommentModel extends Model { threadId: number user?: MUserAccountId }) { - const { videoId, threadId, user } = parameters + const { user } = parameters - const serverActor = await getServerActor() - const serverAccountId = serverActor.Account.id - const userAccountId = user ? user.Account.id : undefined + const blockerAccountIds = await VideoCommentModel.buildBlockerAccountIds({ user }) - const query = { - order: [ [ 'createdAt', 'ASC' ], [ 'updatedAt', 'ASC' ] ] as Order, - where: { - videoId, - [Op.or]: [ - { id: threadId }, - { originCommentId: threadId } - ], - accountId: { - [Op.notIn]: Sequelize.literal( - '(' + buildBlockedAccountSQL(serverAccountId, userAccountId) + ')' - ) - } - } - } + const queryOptions: ListVideoCommentsOptions = { + ...pick(parameters, [ 'videoId', 'threadId' ]), - const scopes: any[] = [ - ScopeNames.WITH_ACCOUNT, - { - method: [ ScopeNames.ATTRIBUTES_FOR_API, serverAccountId, userAccountId ] - } - ] + selectType: 'api', + sort: 'createdAt', - return VideoCommentModel - .scope(scopes) - .findAndCountAll(query) - .then(({ rows, count }) => { - return { total: count, data: rows } - }) + blockerAccountIds, + includeReplyCounters: true + } + + return Promise.all([ + 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'): Bluebird { + static listThreadParentComments (comment: MCommentId, t: Transaction, order: 'ASC' | 'DESC' = 'ASC'): Promise { const query = { order: [ [ 'createdAt', order ] ] as Order, where: { @@ -366,51 +385,80 @@ export class VideoCommentModel extends Model { .findAll(query) } - static listAndCountByVideoId (videoId: number, start: number, count: number, t?: Transaction, order: 'ASC' | 'DESC' = 'ASC') { - const query = { - order: [ [ 'createdAt', order ] ] as Order, - offset: start, - limit: count, - where: { - videoId - }, - transaction: t + static async listAndCountByVideoForAP (parameters: { + video: MVideoImmutable + start: number + count: number + }) { + const { video } = parameters + + const blockerAccountIds = await VideoCommentModel.buildBlockerAccountIds({ user: null }) + + const queryOptions: ListVideoCommentsOptions = { + ...pick(parameters, [ 'start', 'count' ]), + + selectType: 'comment-only', + videoId: video.id, + sort: 'createdAt', + + blockerAccountIds } - return VideoCommentModel.findAndCountAll(query) + return Promise.all([ + new VideoCommentListQueryBuilder(VideoCommentModel.sequelize, queryOptions).listComments(), + new VideoCommentListQueryBuilder(VideoCommentModel.sequelize, queryOptions).countComments() + ]).then(([ rows, count ]) => { + return { total: count, data: rows } + }) } - static listForFeed (start: number, count: number, videoId?: number): Bluebird { - const query = { - order: [ [ 'createdAt', 'DESC' ] ] as Order, - offset: start, - limit: count, - where: {}, - include: [ - { - attributes: [ 'name', 'uuid' ], - model: VideoModel.unscoped(), - required: true - } - ] + static async listForFeed (parameters: { + start: number + count: number + videoId?: number + accountId?: number + videoChannelId?: number + }) { + const blockerAccountIds = await VideoCommentModel.buildBlockerAccountIds({ user: null }) + + const queryOptions: ListVideoCommentsOptions = { + ...pick(parameters, [ 'start', 'count', 'accountId', 'videoId', 'videoChannelId' ]), + + selectType: 'feed', + + sort: '-createdAt', + onPublicVideo: true, + notDeleted: true, + + blockerAccountIds } - if (videoId) query.where['videoId'] = videoId + return new VideoCommentListQueryBuilder(VideoCommentModel.sequelize, queryOptions).listComments() + } - return VideoCommentModel - .scope([ ScopeNames.WITH_ACCOUNT ]) - .findAll(query) + static listForBulkDelete (ofAccount: MAccount, filter: { onVideosOfAccount?: MAccountId } = {}) { + const queryOptions: ListVideoCommentsOptions = { + selectType: 'comment-only', + + accountId: ofAccount.id, + videoAccountOwnerId: filter.onVideosOfAccount?.id, + + notDeleted: true, + count: 5000 + } + + 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 @@ -428,6 +476,18 @@ export class VideoCommentModel extends Model { } } + static listRemoteCommentUrlsOfLocalVideos () { + const query = `SELECT "videoComment".url FROM "videoComment" ` + + `INNER JOIN account ON account.id = "videoComment"."accountId" ` + + `INNER JOIN actor ON actor.id = "account"."actorId" AND actor."serverId" IS NOT NULL ` + + `INNER JOIN video ON video.id = "videoComment"."videoId" AND video.remote IS FALSE` + + return VideoCommentModel.sequelize.query<{ url: string }>(query, { + type: QueryTypes.SELECT, + raw: true + }).then(rows => rows.map(r => r.url)) + } + static cleanOldCommentsOf (videoId: number, beforeUpdatedAt: Date) { const query = { where: { @@ -437,7 +497,9 @@ export class VideoCommentModel extends Model { videoId, accountId: { [Op.notIn]: buildLocalAccountIdsIn() - } + }, + // Do not delete Tombstones + deletedAt: null } } @@ -453,13 +515,17 @@ export class VideoCommentModel extends Model { } isOwned () { - if (!this.Account) { - return false - } + if (!this.Account) return false return this.Account.isOwned() } + markAsDeleted () { + this.text = '' + this.deletedAt = new Date() + this.accountId = null + } + isDeleted () { return this.deletedAt !== null } @@ -499,7 +565,7 @@ export class VideoCommentModel extends Model { ) } - return uniq(result) + return uniqify(result) } toFormattedJSON (this: MCommentFormattable) { @@ -507,19 +573,51 @@ 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, updatedAt: this.updatedAt, deletedAt: this.deletedAt, + isDeleted: this.isDeleted(), + totalRepliesFromVideoAuthor: this.get('totalRepliesFromVideoAuthor') || 0, totalReplies: this.get('totalReplies') || 0, - account: this.Account ? this.Account.toFormattedJSON() : null + + account: this.Account + ? this.Account.toFormattedJSON() + : null } as VideoComment } + toFormattedAdminJSON (this: MCommentAdminFormattable) { + return { + id: this.id, + url: this.url, + text: this.text, + + threadId: this.getThreadId(), + inReplyToCommentId: this.inReplyToCommentId || null, + videoId: this.videoId, + + createdAt: this.createdAt, + updatedAt: this.updatedAt, + + video: { + id: this.Video.id, + uuid: this.Video.uuid, + name: this.Video.name + }, + + account: this.Account + ? this.Account.toFormattedJSON() + : null + } as VideoCommentAdmin + } + toActivityPubObject (this: MCommentAP, threadParentComments: MCommentOwner[]): VideoCommentObject | ActivityTombstoneObject { let inReplyTo: string // New thread, so in AS we reply to the video @@ -557,7 +655,10 @@ export class VideoCommentModel extends Model { return { type: 'Note' as 'Note', id: this.url, + content: this.text, + mediaType: 'text/markdown', + inReplyTo, updated: this.updatedAt.toISOString(), published: this.createdAt.toISOString(), @@ -566,4 +667,17 @@ export class VideoCommentModel extends Model { tag } } + + private static async buildBlockerAccountIds (options: { + user: MUserAccountId + }): Promise { + const { user } = options + + const serverActor = await getServerActor() + const blockerAccountIds = [ serverActor.Account.id ] + + if (user) blockerAccountIds.push(user.Account.id) + + return blockerAccountIds + } }