]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/models/pod.js
Server: add database field validations
[github/Chocobozzz/PeerTube.git] / server / models / pod.js
1 'use strict'
2
3 const map = require('lodash/map')
4
5 const constants = require('../initializers/constants')
6 const customPodsValidators = require('../helpers/custom-validators').pods
7
8 // ---------------------------------------------------------------------------
9
10 module.exports = function (sequelize, DataTypes) {
11 const Pod = sequelize.define('Pod',
12 {
13 host: {
14 type: DataTypes.STRING,
15 allowNull: false,
16 validate: {
17 isHost: function (value) {
18 const res = customPodsValidators.isHostValid(value)
19 if (res === false) throw new Error('Host not valid.')
20 }
21 }
22 },
23 publicKey: {
24 type: DataTypes.STRING(5000),
25 allowNull: false
26 },
27 score: {
28 type: DataTypes.INTEGER,
29 defaultValue: constants.FRIEND_SCORE.BASE,
30 allowNull: false,
31 validate: {
32 isInt: true,
33 max: constants.FRIEND_SCORE.MAX
34 }
35 }
36 },
37 {
38 classMethods: {
39 associate,
40
41 countAll,
42 incrementScores,
43 list,
44 listAllIds,
45 listBadPods,
46 load,
47 loadByHost,
48 removeAll
49 },
50 instanceMethods: {
51 toFormatedJSON
52 }
53 }
54 )
55
56 return Pod
57 }
58
59 // ------------------------------ METHODS ------------------------------
60
61 function toFormatedJSON () {
62 const json = {
63 id: this.id,
64 host: this.host,
65 score: this.score,
66 createdAt: this.createdAt
67 }
68
69 return json
70 }
71
72 // ------------------------------ Statics ------------------------------
73
74 function associate (models) {
75 this.belongsToMany(models.Request, {
76 foreignKey: 'podId',
77 through: models.RequestToPod,
78 onDelete: 'cascade'
79 })
80 }
81
82 function countAll (callback) {
83 return this.count().asCallback(callback)
84 }
85
86 function incrementScores (ids, value, callback) {
87 if (!callback) callback = function () {}
88
89 const update = {
90 score: this.sequelize.literal('score +' + value)
91 }
92
93 const options = {
94 where: {
95 id: {
96 $in: ids
97 }
98 },
99 // In this case score is a literal and not an integer so we do not validate it
100 validate: false
101 }
102
103 return this.update(update, options).asCallback(callback)
104 }
105
106 function list (callback) {
107 return this.findAll().asCallback(callback)
108 }
109
110 function listAllIds (callback) {
111 const query = {
112 attributes: [ 'id' ]
113 }
114
115 return this.findAll(query).asCallback(function (err, pods) {
116 if (err) return callback(err)
117
118 return callback(null, map(pods, 'id'))
119 })
120 }
121
122 function listBadPods (callback) {
123 const query = {
124 where: {
125 score: { $lte: 0 }
126 }
127 }
128
129 return this.findAll(query).asCallback(callback)
130 }
131
132 function load (id, callback) {
133 return this.findById(id).asCallback(callback)
134 }
135
136 function loadByHost (host, callback) {
137 const query = {
138 where: {
139 host: host
140 }
141 }
142
143 return this.findOne(query).asCallback(callback)
144 }
145
146 function removeAll (callback) {
147 return this.destroy().asCallback(callback)
148 }