From 4cb6d4578893db310297d7e118ce2fb7ecb952a3 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Thu, 4 Jan 2018 11:19:16 +0100 Subject: Add ability to delete comments --- server/models/video/video-comment.ts | 45 +++++++++++++++++++++++++++++++---- server/models/video/video.ts | 46 ++++++++++++++++++++++++++++++------ 2 files changed, 80 insertions(+), 11 deletions(-) (limited to 'server/models/video') diff --git a/server/models/video/video-comment.ts b/server/models/video/video-comment.ts index d2d8945c3..66fca2484 100644 --- a/server/models/video/video-comment.ts +++ b/server/models/video/video-comment.ts @@ -7,12 +7,14 @@ import { VideoCommentObject } from '../../../shared/models/activitypub/objects/v import { VideoComment } from '../../../shared/models/videos/video-comment.model' import { isActivityPubUrlValid } from '../../helpers/custom-validators/activitypub/misc' import { CONSTRAINTS_FIELDS } from '../../initializers' +import { sendDeleteVideoComment } from '../../lib/activitypub/send' import { AccountModel } from '../account/account' import { ActorModel } from '../activitypub/actor' import { AvatarModel } from '../avatar/avatar' import { ServerModel } from '../server/server' import { getSort, throwIfNotValid } from '../utils' import { VideoModel } from './video' +import { VideoChannelModel } from './video-channel' enum ScopeNames { WITH_ACCOUNT = 'WITH_ACCOUNT', @@ -70,7 +72,25 @@ enum ScopeNames { include: [ { model: () => VideoModel, - required: false + required: true, + include: [ + { + model: () => VideoChannelModel.unscoped(), + required: true, + include: [ + { + model: () => AccountModel, + required: true, + include: [ + { + model: () => ActorModel, + required: true + } + ] + } + ] + } + ] } ] } @@ -155,9 +175,10 @@ export class VideoCommentModel extends Model { Account: AccountModel @AfterDestroy - static sendDeleteIfOwned (instance: VideoCommentModel) { - // TODO - return undefined + static async sendDeleteIfOwned (instance: VideoCommentModel) { + if (instance.isOwned()) { + await sendDeleteVideoComment(instance, undefined) + } } static loadById (id: number, t?: Sequelize.Transaction) { @@ -198,6 +219,18 @@ export class VideoCommentModel extends Model { return VideoCommentModel.findOne(query) } + static loadByUrlAndPopulateAccount (url: string, t?: Sequelize.Transaction) { + const query: IFindOptions = { + where: { + url + } + } + + if (t !== undefined) query.transaction = t + + return VideoCommentModel.scope([ ScopeNames.WITH_ACCOUNT ]).findOne(query) + } + static listThreadsForApi (videoId: number, start: number, count: number, sort: string) { const query = { offset: start, @@ -237,6 +270,10 @@ export class VideoCommentModel extends Model { }) } + isOwned () { + return this.Account.isOwned() + } + toFormattedJSON () { return { id: this.id, diff --git a/server/models/video/video.ts b/server/models/video/video.ts index c4b716cd2..4d15c2a50 100644 --- a/server/models/video/video.ts +++ b/server/models/video/video.ts @@ -43,7 +43,8 @@ import { VideoTagModel } from './video-tag' enum ScopeNames { AVAILABLE_FOR_LIST = 'AVAILABLE_FOR_LIST', - WITH_ACCOUNT = 'WITH_ACCOUNT', + WITH_ACCOUNT_API = 'WITH_ACCOUNT_API', + WITH_ACCOUNT_DETAILS = 'WITH_ACCOUNT_DETAILS', WITH_TAGS = 'WITH_TAGS', WITH_FILES = 'WITH_FILES', WITH_SHARES = 'WITH_SHARES', @@ -62,7 +63,35 @@ enum ScopeNames { privacy: VideoPrivacy.PUBLIC } }, - [ScopeNames.WITH_ACCOUNT]: { + [ScopeNames.WITH_ACCOUNT_API]: { + include: [ + { + model: () => VideoChannelModel.unscoped(), + required: true, + include: [ + { + attributes: [ 'name' ], + model: () => AccountModel.unscoped(), + required: true, + include: [ + { + attributes: [ 'serverId' ], + model: () => ActorModel.unscoped(), + required: true, + include: [ + { + model: () => ServerModel.unscoped(), + required: false + } + ] + } + ] + } + ] + } + ] + }, + [ScopeNames.WITH_ACCOUNT_DETAILS]: { include: [ { model: () => VideoChannelModel, @@ -146,6 +175,9 @@ enum ScopeNames { }, { fields: [ 'channelId' ] + }, + { + fields: [ 'id', 'privacy' ] } ] }) @@ -461,7 +493,7 @@ export class VideoModel extends Model { order: [ getSort(sort) ] } - return VideoModel.scope([ ScopeNames.AVAILABLE_FOR_LIST, ScopeNames.WITH_ACCOUNT ]) + return VideoModel.scope([ ScopeNames.AVAILABLE_FOR_LIST, ScopeNames.WITH_ACCOUNT_API ]) .findAndCountAll(query) .then(({ rows, count }) => { return { @@ -496,7 +528,7 @@ export class VideoModel extends Model { if (t !== undefined) query.transaction = t - return VideoModel.scope([ ScopeNames.WITH_ACCOUNT, ScopeNames.WITH_FILES ]).findOne(query) + return VideoModel.scope([ ScopeNames.WITH_ACCOUNT_DETAILS, ScopeNames.WITH_FILES ]).findOne(query) } static loadByUUIDOrURL (uuid: string, url: string, t?: Sequelize.Transaction) { @@ -520,7 +552,7 @@ export class VideoModel extends Model { } return VideoModel - .scope([ ScopeNames.WITH_TAGS, ScopeNames.WITH_FILES, ScopeNames.WITH_ACCOUNT ]) + .scope([ ScopeNames.WITH_TAGS, ScopeNames.WITH_FILES, ScopeNames.WITH_ACCOUNT_DETAILS ]) .findById(id, options) } @@ -545,7 +577,7 @@ export class VideoModel extends Model { } return VideoModel - .scope([ ScopeNames.WITH_TAGS, ScopeNames.WITH_FILES, ScopeNames.WITH_ACCOUNT ]) + .scope([ ScopeNames.WITH_TAGS, ScopeNames.WITH_FILES, ScopeNames.WITH_ACCOUNT_DETAILS ]) .findOne(options) } @@ -563,7 +595,7 @@ export class VideoModel extends Model { ScopeNames.WITH_SHARES, ScopeNames.WITH_TAGS, ScopeNames.WITH_FILES, - ScopeNames.WITH_ACCOUNT, + ScopeNames.WITH_ACCOUNT_DETAILS, ScopeNames.WITH_COMMENTS ]) .findOne(options) -- cgit v1.2.3