Add new follow, mention and user registered notifs
[oweals/peertube.git] / server / models / video / video-file.ts
1 import {
2   AllowNull,
3   BelongsTo,
4   Column,
5   CreatedAt,
6   DataType,
7   Default,
8   ForeignKey,
9   HasMany,
10   Is,
11   Model,
12   Table,
13   UpdatedAt
14 } from 'sequelize-typescript'
15 import {
16   isVideoFileExtnameValid,
17   isVideoFileInfoHashValid,
18   isVideoFileResolutionValid,
19   isVideoFileSizeValid,
20   isVideoFPSResolutionValid
21 } from '../../helpers/custom-validators/videos'
22 import { throwIfNotValid } from '../utils'
23 import { VideoModel } from './video'
24 import * as Sequelize from 'sequelize'
25 import { VideoRedundancyModel } from '../redundancy/video-redundancy'
26
27 @Table({
28   tableName: 'videoFile',
29   indexes: [
30     {
31       fields: [ 'videoId' ]
32     },
33     {
34       fields: [ 'infoHash' ]
35     },
36     {
37       fields: [ 'videoId', 'resolution', 'fps' ],
38       unique: true
39     }
40   ]
41 })
42 export class VideoFileModel extends Model<VideoFileModel> {
43   @CreatedAt
44   createdAt: Date
45
46   @UpdatedAt
47   updatedAt: Date
48
49   @AllowNull(false)
50   @Is('VideoFileResolution', value => throwIfNotValid(value, isVideoFileResolutionValid, 'resolution'))
51   @Column
52   resolution: number
53
54   @AllowNull(false)
55   @Is('VideoFileSize', value => throwIfNotValid(value, isVideoFileSizeValid, 'size'))
56   @Column(DataType.BIGINT)
57   size: number
58
59   @AllowNull(false)
60   @Is('VideoFileExtname', value => throwIfNotValid(value, isVideoFileExtnameValid, 'extname'))
61   @Column
62   extname: string
63
64   @AllowNull(false)
65   @Is('VideoFileSize', value => throwIfNotValid(value, isVideoFileInfoHashValid, 'info hash'))
66   @Column
67   infoHash: string
68
69   @AllowNull(false)
70   @Default(-1)
71   @Is('VideoFileFPS', value => throwIfNotValid(value, isVideoFPSResolutionValid, 'fps'))
72   @Column
73   fps: number
74
75   @ForeignKey(() => VideoModel)
76   @Column
77   videoId: number
78
79   @BelongsTo(() => VideoModel, {
80     foreignKey: {
81       allowNull: false
82     },
83     onDelete: 'CASCADE'
84   })
85   Video: VideoModel
86
87   @HasMany(() => VideoRedundancyModel, {
88     foreignKey: {
89       allowNull: false
90     },
91     onDelete: 'CASCADE',
92     hooks: true
93   })
94   RedundancyVideos: VideoRedundancyModel[]
95
96   static isInfohashExists (infoHash: string) {
97     const query = 'SELECT 1 FROM "videoFile" WHERE "infoHash" = $infoHash LIMIT 1'
98     const options = {
99       type: Sequelize.QueryTypes.SELECT,
100       bind: { infoHash },
101       raw: true
102     }
103
104     return VideoModel.sequelize.query(query, options)
105               .then(results => {
106                 return results.length === 1
107               })
108   }
109
110   static loadWithVideo (id: number) {
111     const options = {
112       include: [
113         {
114           model: VideoModel.unscoped(),
115           required: true
116         }
117       ]
118     }
119
120     return VideoFileModel.findById(id, options)
121   }
122
123   hasSameUniqueKeysThan (other: VideoFileModel) {
124     return this.fps === other.fps &&
125       this.resolution === other.resolution &&
126       this.videoId === other.videoId
127   }
128 }