From 0f8d00e3144060270d7fe603865fccaf18649c47 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Fri, 13 Nov 2020 16:38:23 +0100 Subject: Implement video comment list in admin --- server/models/video/video-comment.ts | 132 +++++++++++++++++++++++++++++++++-- 1 file changed, 128 insertions(+), 4 deletions(-) (limited to 'server/models/video/video-comment.ts') diff --git a/server/models/video/video-comment.ts b/server/models/video/video-comment.ts index de27b3d87..70aed75d6 100644 --- a/server/models/video/video-comment.ts +++ b/server/models/video/video-comment.ts @@ -1,6 +1,6 @@ import * as Bluebird from 'bluebird' import { uniq } from 'lodash' -import { FindOptions, Op, Order, ScopeOptions, Sequelize, Transaction, WhereOptions } from 'sequelize' +import { FindAndCountOptions, FindOptions, Op, Order, ScopeOptions, Sequelize, Transaction, WhereOptions } from 'sequelize' import { AllowNull, BelongsTo, @@ -20,13 +20,14 @@ 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' -import { VideoComment } from '../../../shared/models/videos/video-comment.model' +import { VideoComment, VideoCommentAdmin } from '../../../shared/models/videos/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 { CONSTRAINTS_FIELDS, WEBSERVER } from '../../initializers/constants' import { MComment, + MCommentAdminFormattable, MCommentAP, MCommentFormattable, MCommentId, @@ -40,7 +41,14 @@ import { import { VideoCommentAbuseModel } from '../abuse/video-comment-abuse' import { AccountModel } from '../account/account' import { ActorModel, unusedActorAttributesForAPI } from '../activitypub/actor' -import { buildBlockedAccountSQL, buildBlockedAccountSQLOptimized, buildLocalAccountIdsIn, getCommentSort, throwIfNotValid } from '../utils' +import { + buildBlockedAccountSQL, + buildBlockedAccountSQLOptimized, + buildLocalAccountIdsIn, + getCommentSort, + searchAttribute, + throwIfNotValid +} from '../utils' import { VideoModel } from './video' import { VideoChannelModel } from './video-channel' @@ -303,6 +311,90 @@ 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 + + isLocal?: boolean + search?: string + searchAccount?: string + searchVideo?: string + }) { + const { start, count, sort, isLocal, search, searchAccount, searchVideo } = parameters + + const query: FindAndCountOptions = { + offset: start, + limit: count, + order: getCommentSort(sort) + } + + const where: WhereOptions = { + isDeleted: false + } + + const whereAccount: WhereOptions = {} + const whereActor: WhereOptions = {} + const whereVideo: WhereOptions = {} + + if (isLocal === true) { + Object.assign(where, { + serverId: null + }) + } else if (isLocal === false) { + Object.assign(where, { + serverId: { + [Op.ne]: null + } + }) + } + + if (search) { + Object.assign(where, searchAttribute(search, 'text')) + Object.assign(whereActor, searchAttribute(search, 'preferredUsername')) + Object.assign(whereAccount, searchAttribute(search, 'name')) + Object.assign(whereVideo, searchAttribute(search, 'name')) + } + + if (searchAccount) { + Object.assign(whereActor, searchAttribute(search, 'preferredUsername')) + Object.assign(whereAccount, searchAttribute(search, 'name')) + } + + if (searchVideo) { + Object.assign(whereVideo, searchAttribute(search, 'name')) + } + + query.include = [ + { + model: AccountModel.unscoped(), + required: !!searchAccount, + where: whereAccount, + include: [ + { + attributes: { + exclude: unusedActorAttributesForAPI + }, + model: ActorModel, // Default scope includes avatar and server + required: true, + where: whereActor + } + ] + }, + { + model: VideoModel.unscoped(), + required: true, + where: whereVideo + } + ] + + return VideoCommentModel + .findAndCountAll(query) + .then(({ rows, count }) => { + return { total: count, data: rows } + }) + } + static async listThreadsForApi (parameters: { videoId: number isVideoOwned: boolean @@ -656,19 +748,51 @@ export class VideoCommentModel extends Model { 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, 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 -- cgit v1.2.3