diff options
author | Rigel Kent <sendmemail@rigelk.eu> | 2020-04-16 14:22:27 +0200 |
---|---|---|
committer | Rigel Kent <par@rigelk.eu> | 2020-05-01 16:41:02 +0200 |
commit | 68d19a0ace01cb7a3550da420d27663e2db1b98d (patch) | |
tree | fcdac5341001b9e6d15ddd0ca8239372ec2b3053 /server/models/video/video.ts | |
parent | 165ee2929bc76fc7f9985ae81cc33736820c7865 (diff) | |
download | PeerTube-68d19a0ace01cb7a3550da420d27663e2db1b98d.tar.gz PeerTube-68d19a0ace01cb7a3550da420d27663e2db1b98d.tar.zst PeerTube-68d19a0ace01cb7a3550da420d27663e2db1b98d.zip |
Make sure a report doesn't get deleted upon the deletion of its video
Diffstat (limited to 'server/models/video/video.ts')
-rw-r--r-- | server/models/video/video.ts | 33 |
1 files changed, 31 insertions, 2 deletions
diff --git a/server/models/video/video.ts b/server/models/video/video.ts index 0e7505af5..2636ebd8e 100644 --- a/server/models/video/video.ts +++ b/server/models/video/video.ts | |||
@@ -628,9 +628,9 @@ export class VideoModel extends Model<VideoModel> { | |||
628 | @HasMany(() => VideoAbuseModel, { | 628 | @HasMany(() => VideoAbuseModel, { |
629 | foreignKey: { | 629 | foreignKey: { |
630 | name: 'videoId', | 630 | name: 'videoId', |
631 | allowNull: false | 631 | allowNull: true |
632 | }, | 632 | }, |
633 | onDelete: 'cascade' | 633 | onDelete: 'set null' |
634 | }) | 634 | }) |
635 | VideoAbuses: VideoAbuseModel[] | 635 | VideoAbuses: VideoAbuseModel[] |
636 | 636 | ||
@@ -798,6 +798,35 @@ export class VideoModel extends Model<VideoModel> { | |||
798 | ModelCache.Instance.invalidateCache('video', instance.id) | 798 | ModelCache.Instance.invalidateCache('video', instance.id) |
799 | } | 799 | } |
800 | 800 | ||
801 | @BeforeDestroy | ||
802 | static async saveEssentialDataToAbuses (instance: VideoModel, options) { | ||
803 | const tasks: Promise<any>[] = [] | ||
804 | |||
805 | logger.info('Saving video abuses details of video %s.', instance.url) | ||
806 | |||
807 | if (!Array.isArray(instance.VideoAbuses)) { | ||
808 | instance.VideoAbuses = await instance.$get('VideoAbuses') | ||
809 | |||
810 | if (instance.VideoAbuses.length === 0) return undefined | ||
811 | } | ||
812 | |||
813 | const details = instance.toFormattedJSON() | ||
814 | |||
815 | for (const abuse of instance.VideoAbuses) { | ||
816 | tasks.push((_ => { | ||
817 | abuse.deletedVideo = details | ||
818 | return abuse.save({ transaction: options.transaction }) | ||
819 | })()) | ||
820 | } | ||
821 | |||
822 | Promise.all(tasks) | ||
823 | .catch(err => { | ||
824 | logger.error('Some errors when saving details of video %s in its abuses before destroy hook.', instance.uuid, { err }) | ||
825 | }) | ||
826 | |||
827 | return undefined | ||
828 | } | ||
829 | |||
801 | static listLocal (): Bluebird<MVideoWithAllFiles[]> { | 830 | static listLocal (): Bluebird<MVideoWithAllFiles[]> { |
802 | const query = { | 831 | const query = { |
803 | where: { | 832 | where: { |