Make some fields optional when uploading a video
[oweals/peertube.git] / server / models / video / video-blacklist.ts
1 import * as Sequelize from 'sequelize'
2
3 import { SortType } from '../../helpers'
4 import { addMethodsToModel, getSortOnModel } from '../utils'
5 import { VideoInstance } from './video-interface'
6 import {
7   BlacklistedVideoInstance,
8   BlacklistedVideoAttributes,
9
10   BlacklistedVideoMethods
11 } from './video-blacklist-interface'
12
13 let BlacklistedVideo: Sequelize.Model<BlacklistedVideoInstance, BlacklistedVideoAttributes>
14 let toFormattedJSON: BlacklistedVideoMethods.ToFormattedJSON
15 let listForApi: BlacklistedVideoMethods.ListForApi
16 let loadByVideoId: BlacklistedVideoMethods.LoadByVideoId
17
18 export default function (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.DataTypes) {
19   BlacklistedVideo = sequelize.define<BlacklistedVideoInstance, BlacklistedVideoAttributes>('BlacklistedVideo',
20     {},
21     {
22       indexes: [
23         {
24           fields: [ 'videoId' ],
25           unique: true
26         }
27       ]
28     }
29   )
30
31   const classMethods = [
32     associate,
33
34     listForApi,
35     loadByVideoId
36   ]
37   const instanceMethods = [
38     toFormattedJSON
39   ]
40   addMethodsToModel(BlacklistedVideo, classMethods, instanceMethods)
41
42   return BlacklistedVideo
43 }
44
45 // ------------------------------ METHODS ------------------------------
46
47 toFormattedJSON = function (this: BlacklistedVideoInstance) {
48   let video: VideoInstance
49
50   video = this.Video
51
52   return {
53     id: this.id,
54     videoId: this.videoId,
55     createdAt: this.createdAt,
56     updatedAt: this.updatedAt,
57     name: video.name,
58     uuid: video.uuid,
59     description: video.description,
60     duration: video.duration,
61     views: video.views,
62     likes: video.likes,
63     dislikes: video.dislikes,
64     nsfw: video.nsfw
65   }
66 }
67
68 // ------------------------------ STATICS ------------------------------
69
70 function associate (models) {
71   BlacklistedVideo.belongsTo(models.Video, {
72     foreignKey: {
73       name: 'videoId',
74       allowNull: false
75     },
76     onDelete: 'CASCADE'
77   })
78 }
79
80 listForApi = function (start: number, count: number, sort: SortType) {
81   const query = {
82     offset: start,
83     limit: count,
84     order: [ getSortOnModel(sort.sortModel, sort.sortValue) ],
85     include: [ { model: BlacklistedVideo['sequelize'].models.Video } ]
86   }
87
88   return BlacklistedVideo.findAndCountAll(query).then(({ rows, count }) => {
89     return {
90       data: rows,
91       total: count
92     }
93   })
94 }
95
96 loadByVideoId = function (id: number) {
97   const query = {
98     where: {
99       videoId: id
100     }
101   }
102
103   return BlacklistedVideo.findOne(query)
104 }