diff options
Diffstat (limited to 'server/models/video')
-rw-r--r-- | server/models/video/video-comment.ts | 65 | ||||
-rw-r--r-- | server/models/video/video.ts | 4 |
2 files changed, 66 insertions, 3 deletions
diff --git a/server/models/video/video-comment.ts b/server/models/video/video-comment.ts index 90625d987..fb6078ed8 100644 --- a/server/models/video/video-comment.ts +++ b/server/models/video/video-comment.ts | |||
@@ -1,7 +1,22 @@ | |||
1 | import * as Bluebird from 'bluebird' | 1 | import * as Bluebird from 'bluebird' |
2 | import { uniq } from 'lodash' | 2 | import { uniq } from 'lodash' |
3 | import { FindOptions, Op, Order, ScopeOptions, Sequelize, Transaction } from 'sequelize' | 3 | import { FindOptions, Op, Order, ScopeOptions, Sequelize, Transaction } from 'sequelize' |
4 | import { AllowNull, BelongsTo, Column, CreatedAt, DataType, ForeignKey, Is, Model, Scopes, Table, UpdatedAt } from 'sequelize-typescript' | 4 | import { |
5 | AllowNull, | ||
6 | BeforeDestroy, | ||
7 | BelongsTo, | ||
8 | Column, | ||
9 | CreatedAt, | ||
10 | DataType, | ||
11 | ForeignKey, | ||
12 | HasMany, | ||
13 | Is, | ||
14 | Model, | ||
15 | Scopes, | ||
16 | Table, | ||
17 | UpdatedAt | ||
18 | } from 'sequelize-typescript' | ||
19 | import { logger } from '@server/helpers/logger' | ||
5 | import { getServerActor } from '@server/models/application/application' | 20 | import { getServerActor } from '@server/models/application/application' |
6 | import { MAccount, MAccountId, MUserAccountId } from '@server/types/models' | 21 | import { MAccount, MAccountId, MUserAccountId } from '@server/types/models' |
7 | import { VideoPrivacy } from '@shared/models' | 22 | import { VideoPrivacy } from '@shared/models' |
@@ -24,6 +39,7 @@ import { | |||
24 | MCommentOwnerVideoReply, | 39 | MCommentOwnerVideoReply, |
25 | MVideoImmutable | 40 | MVideoImmutable |
26 | } from '../../types/models/video' | 41 | } from '../../types/models/video' |
42 | import { VideoCommentAbuseModel } from '../abuse/video-comment-abuse' | ||
27 | import { AccountModel } from '../account/account' | 43 | import { AccountModel } from '../account/account' |
28 | import { ActorModel, unusedActorAttributesForAPI } from '../activitypub/actor' | 44 | import { ActorModel, unusedActorAttributesForAPI } from '../activitypub/actor' |
29 | import { buildBlockedAccountSQL, buildLocalAccountIdsIn, getCommentSort, throwIfNotValid } from '../utils' | 45 | import { buildBlockedAccountSQL, buildLocalAccountIdsIn, getCommentSort, throwIfNotValid } from '../utils' |
@@ -224,6 +240,53 @@ export class VideoCommentModel extends Model<VideoCommentModel> { | |||
224 | }) | 240 | }) |
225 | Account: AccountModel | 241 | Account: AccountModel |
226 | 242 | ||
243 | @HasMany(() => VideoCommentAbuseModel, { | ||
244 | foreignKey: { | ||
245 | name: 'commentId', | ||
246 | allowNull: true | ||
247 | }, | ||
248 | onDelete: 'set null' | ||
249 | }) | ||
250 | CommentAbuses: VideoCommentAbuseModel[] | ||
251 | |||
252 | @BeforeDestroy | ||
253 | static async saveEssentialDataToAbuses (instance: VideoCommentModel, options) { | ||
254 | const tasks: Promise<any>[] = [] | ||
255 | |||
256 | if (!Array.isArray(instance.CommentAbuses)) { | ||
257 | instance.CommentAbuses = await instance.$get('CommentAbuses') | ||
258 | |||
259 | if (instance.CommentAbuses.length === 0) return undefined | ||
260 | } | ||
261 | |||
262 | if (!instance.Video) { | ||
263 | instance.Video = await instance.$get('Video') | ||
264 | } | ||
265 | |||
266 | logger.info('Saving video comment %s for abuse.', instance.url) | ||
267 | |||
268 | const details = Object.assign(instance.toFormattedJSON(), { | ||
269 | Video: { | ||
270 | id: instance.Video.id, | ||
271 | name: instance.Video.name, | ||
272 | uuid: instance.Video.uuid | ||
273 | } | ||
274 | }) | ||
275 | |||
276 | for (const abuse of instance.CommentAbuses) { | ||
277 | abuse.deletedComment = details | ||
278 | |||
279 | tasks.push(abuse.save({ transaction: options.transaction })) | ||
280 | } | ||
281 | |||
282 | Promise.all(tasks) | ||
283 | .catch(err => { | ||
284 | logger.error('Some errors when saving details of comment %s in its abuses before destroy hook.', instance.url, { err }) | ||
285 | }) | ||
286 | |||
287 | return undefined | ||
288 | } | ||
289 | |||
227 | static loadById (id: number, t?: Transaction): Bluebird<MComment> { | 290 | static loadById (id: number, t?: Transaction): Bluebird<MComment> { |
228 | const query: FindOptions = { | 291 | const query: FindOptions = { |
229 | where: { | 292 | where: { |
diff --git a/server/models/video/video.ts b/server/models/video/video.ts index 272bba0e1..43609587c 100644 --- a/server/models/video/video.ts +++ b/server/models/video/video.ts | |||
@@ -803,14 +803,14 @@ export class VideoModel extends Model<VideoModel> { | |||
803 | static async saveEssentialDataToAbuses (instance: VideoModel, options) { | 803 | static async saveEssentialDataToAbuses (instance: VideoModel, options) { |
804 | const tasks: Promise<any>[] = [] | 804 | const tasks: Promise<any>[] = [] |
805 | 805 | ||
806 | logger.info('Saving video abuses details of video %s.', instance.url) | ||
807 | |||
808 | if (!Array.isArray(instance.VideoAbuses)) { | 806 | if (!Array.isArray(instance.VideoAbuses)) { |
809 | instance.VideoAbuses = await instance.$get('VideoAbuses') | 807 | instance.VideoAbuses = await instance.$get('VideoAbuses') |
810 | 808 | ||
811 | if (instance.VideoAbuses.length === 0) return undefined | 809 | if (instance.VideoAbuses.length === 0) return undefined |
812 | } | 810 | } |
813 | 811 | ||
812 | logger.info('Saving video abuses details of video %s.', instance.url) | ||
813 | |||
814 | const details = instance.toFormattedDetailsJSON() | 814 | const details = instance.toFormattedDetailsJSON() |
815 | 815 | ||
816 | for (const abuse of instance.VideoAbuses) { | 816 | for (const abuse of instance.VideoAbuses) { |