]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/models/video/video-blacklist.ts
Handle blacklist (#84)
[github/Chocobozzz/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 countTotal: BlacklistedVideoMethods.CountTotal
16 let list: BlacklistedVideoMethods.List
17 let listForApi: BlacklistedVideoMethods.ListForApi
18 let loadById: BlacklistedVideoMethods.LoadById
19 let loadByVideoId: BlacklistedVideoMethods.LoadByVideoId
20
21 export default function (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.DataTypes) {
22 BlacklistedVideo = sequelize.define<BlacklistedVideoInstance, BlacklistedVideoAttributes>('BlacklistedVideo',
23 {},
24 {
25 indexes: [
26 {
27 fields: [ 'videoId' ],
28 unique: true
29 }
30 ]
31 }
32 )
33
34 const classMethods = [
35 associate,
36
37 countTotal,
38 list,
39 listForApi,
40 loadById,
41 loadByVideoId
42 ]
43 const instanceMethods = [
44 toFormattedJSON
45 ]
46 addMethodsToModel(BlacklistedVideo, classMethods, instanceMethods)
47
48 return BlacklistedVideo
49 }
50
51 // ------------------------------ METHODS ------------------------------
52
53 toFormattedJSON = function (this: BlacklistedVideoInstance) {
54 let video: VideoInstance
55
56 video = this.Video
57
58 return {
59 id: this.id,
60 videoId: this.videoId,
61 createdAt: this.createdAt,
62 updatedAt: this.updatedAt,
63 name: video.name,
64 uuid: video.uuid,
65 description: video.description,
66 duration: video.duration,
67 views: video.views,
68 likes: video.likes,
69 dislikes: video.dislikes,
70 nsfw: video.nsfw
71 }
72 }
73
74 // ------------------------------ STATICS ------------------------------
75
76 function associate (models) {
77 BlacklistedVideo.belongsTo(models.Video, {
78 foreignKey: {
79 name: 'videoId',
80 allowNull: false
81 },
82 onDelete: 'CASCADE'
83 })
84 }
85
86 countTotal = function () {
87 return BlacklistedVideo.count()
88 }
89
90 list = function () {
91 return BlacklistedVideo.findAll()
92 }
93
94 listForApi = function (start: number, count: number, sort: SortType) {
95 const query = {
96 offset: start,
97 limit: count,
98 order: [ getSortOnModel(sort.sortModel, sort.sortValue) ],
99 include: [ { model: BlacklistedVideo['sequelize'].models.Video } ]
100 }
101
102 return BlacklistedVideo.findAndCountAll(query).then(({ rows, count }) => {
103 return {
104 data: rows,
105 total: count
106 }
107 })
108 }
109
110 loadById = function (id: number) {
111 return BlacklistedVideo.findById(id)
112 }
113
114 loadByVideoId = function (id: number) {
115 const query = {
116 where: {
117 videoId: id
118 }
119 }
120
121 return BlacklistedVideo.findOne(query)
122 }