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