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') 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 From f1273314593a4a7dc7ec9594ce0c6c3ae8f62b34 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Mon, 16 Nov 2020 11:55:17 +0100 Subject: Add admin view to manage comments --- server/models/video/video-comment.ts | 84 ++++++++++++++++++++---------------- 1 file changed, 46 insertions(+), 38 deletions(-) (limited to 'server/models/video') diff --git a/server/models/video/video-comment.ts b/server/models/video/video-comment.ts index 70aed75d6..ed4a345eb 100644 --- a/server/models/video/video-comment.ts +++ b/server/models/video/video-comment.ts @@ -323,14 +323,8 @@ export class VideoCommentModel extends Model { }) { const { start, count, sort, isLocal, search, searchAccount, searchVideo } = parameters - const query: FindAndCountOptions = { - offset: start, - limit: count, - order: getCommentSort(sort) - } - const where: WhereOptions = { - isDeleted: false + deletedAt: null } const whereAccount: WhereOptions = {} @@ -338,11 +332,11 @@ export class VideoCommentModel extends Model { const whereVideo: WhereOptions = {} if (isLocal === true) { - Object.assign(where, { + Object.assign(whereActor, { serverId: null }) } else if (isLocal === false) { - Object.assign(where, { + Object.assign(whereActor, { serverId: { [Op.ne]: null } @@ -350,43 +344,57 @@ export class VideoCommentModel extends Model { } 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')) + Object.assign(where, { + [Op.or]: [ + searchAttribute(search, 'text'), + searchAttribute(search, '$Account.Actor.preferredUsername$'), + searchAttribute(search, '$Account.name$'), + searchAttribute(search, '$Video.name$') + ] + }) } if (searchAccount) { - Object.assign(whereActor, searchAttribute(search, 'preferredUsername')) - Object.assign(whereAccount, searchAttribute(search, 'name')) + Object.assign(whereActor, { + [Op.or]: [ + searchAttribute(searchAccount, '$Account.Actor.preferredUsername$'), + searchAttribute(searchAccount, '$Account.name$') + ] + }) } if (searchVideo) { - Object.assign(whereVideo, searchAttribute(search, 'name')) + Object.assign(whereVideo, searchAttribute(searchVideo, '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 - } - ] + const query: FindAndCountOptions = { + offset: start, + limit: count, + order: getCommentSort(sort), + where, + include: [ + { + model: AccountModel.unscoped(), + required: true, + 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) -- cgit v1.2.3