]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/models/video/video-blacklist.ts
Use global uuid instead of remoteId for videos
[github/Chocobozzz/PeerTube.git] / server / models / video / video-blacklist.ts
CommitLineData
e02643f3
C
1import * as Sequelize from 'sequelize'
2
74889a71 3import { addMethodsToModel, getSort } from '../utils'
e02643f3 4import {
e02643f3
C
5 BlacklistedVideoInstance,
6 BlacklistedVideoAttributes,
7
8 BlacklistedVideoMethods
9} from './video-blacklist-interface'
10
11let BlacklistedVideo: Sequelize.Model<BlacklistedVideoInstance, BlacklistedVideoAttributes>
12let toFormatedJSON: BlacklistedVideoMethods.ToFormatedJSON
13let countTotal: BlacklistedVideoMethods.CountTotal
14let list: BlacklistedVideoMethods.List
15let listForApi: BlacklistedVideoMethods.ListForApi
16let loadById: BlacklistedVideoMethods.LoadById
17let loadByVideoId: BlacklistedVideoMethods.LoadByVideoId
18
127944aa
C
19export default function (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.DataTypes) {
20 BlacklistedVideo = sequelize.define<BlacklistedVideoInstance, BlacklistedVideoAttributes>('BlacklistedVideo',
198b205c
GS
21 {},
22 {
23 indexes: [
24 {
25 fields: [ 'videoId' ],
26 unique: true
27 }
e02643f3 28 ]
198b205c
GS
29 }
30 )
31
e02643f3
C
32 const classMethods = [
33 associate,
34
35 countTotal,
36 list,
37 listForApi,
38 loadById,
39 loadByVideoId
40 ]
41 const instanceMethods = [
42 toFormatedJSON
43 ]
44 addMethodsToModel(BlacklistedVideo, classMethods, instanceMethods)
45
198b205c
GS
46 return BlacklistedVideo
47}
48
49// ------------------------------ METHODS ------------------------------
50
70c065d6 51toFormatedJSON = function (this: BlacklistedVideoInstance) {
198b205c
GS
52 return {
53 id: this.id,
54 videoId: this.videoId,
55 createdAt: this.createdAt
56 }
57}
58
59// ------------------------------ STATICS ------------------------------
60
61function associate (models) {
e02643f3 62 BlacklistedVideo.belongsTo(models.Video, {
0a6658fd
C
63 foreignKey: {
64 name: 'videoId',
65 allowNull: false
66 },
67 onDelete: 'CASCADE'
198b205c
GS
68 })
69}
70
6fcd19ba
C
71countTotal = function () {
72 return BlacklistedVideo.count()
198b205c
GS
73}
74
6fcd19ba
C
75list = function () {
76 return BlacklistedVideo.findAll()
198b205c
GS
77}
78
6fcd19ba 79listForApi = function (start: number, count: number, sort: string) {
198b205c
GS
80 const query = {
81 offset: start,
82 limit: count,
65fcc311 83 order: [ getSort(sort) ]
198b205c
GS
84 }
85
6fcd19ba
C
86 return BlacklistedVideo.findAndCountAll(query).then(({ rows, count }) => {
87 return {
88 data: rows,
89 total: count
90 }
198b205c
GS
91 })
92}
93
6fcd19ba
C
94loadById = function (id: number) {
95 return BlacklistedVideo.findById(id)
198b205c
GS
96}
97
0a6658fd 98loadByVideoId = function (id: number) {
198b205c
GS
99 const query = {
100 where: {
101 videoId: id
102 }
103 }
104
6fcd19ba 105 return BlacklistedVideo.findOne(query)
198b205c 106}