]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/models/request.js
Server: make a basic "quick and dirty update" for videos
[github/Chocobozzz/PeerTube.git] / server / models / request.js
CommitLineData
9f10b292
C
1'use strict'
2
67bf9b96 3const values = require('lodash/values')
9f10b292 4
f0f5567b 5const constants = require('../initializers/constants')
9f10b292 6
00057e85 7// ---------------------------------------------------------------------------
9f10b292 8
feb4bdfd
C
9module.exports = function (sequelize, DataTypes) {
10 const Request = sequelize.define('Request',
11 {
12 request: {
67bf9b96
C
13 type: DataTypes.JSON,
14 allowNull: false
feb4bdfd
C
15 },
16 endpoint: {
67bf9b96
C
17 type: DataTypes.ENUM(values(constants.REQUEST_ENDPOINTS)),
18 allowNull: false
feb4bdfd
C
19 }
20 },
4b08096b 21 {
feb4bdfd
C
22 classMethods: {
23 associate,
24
c1a7ab7f
C
25 listWithLimitAndRandom,
26
feb4bdfd 27 countTotalRequests,
c1a7ab7f
C
28 removeAll,
29 removeWithEmptyTo
feb4bdfd 30 }
4b08096b 31 }
feb4bdfd 32 )
528a9efa 33
feb4bdfd
C
34 return Request
35}
00057e85
C
36
37// ------------------------------ STATICS ------------------------------
38
feb4bdfd
C
39function associate (models) {
40 this.belongsToMany(models.Pod, {
41 foreignKey: {
42 name: 'requestId',
43 allowNull: false
44 },
45 through: models.RequestToPod,
46 onDelete: 'CASCADE'
47 })
48}
49
feb4bdfd
C
50function countTotalRequests (callback) {
51 const query = {
52 include: [ this.sequelize.models.Pod ]
53 }
54
55 return this.count(query).asCallback(callback)
56}
57
bd14d16a 58function listWithLimitAndRandom (limitPods, limitRequestsPerPod, callback) {
43666d61 59 const self = this
bd14d16a 60 const Pod = this.sequelize.models.Pod
43666d61 61
9e167724 62 Pod.listRandomPodIdsWithRequest(limitPods, 'RequestToPods', function (err, podIds) {
43666d61
C
63 if (err) return callback(err)
64
bd14d16a
C
65 // We don't have friends that have requests
66 if (podIds.length === 0) return callback(null, [])
43666d61 67
9e167724 68 // The first x requests of these pods
bd14d16a 69 // It is very important to sort by id ASC to keep the requests order!
feb4bdfd
C
70 const query = {
71 order: [
72 [ 'id', 'ASC' ]
73 ],
bd14d16a
C
74 include: [
75 {
76 model: self.sequelize.models.Pod,
77 where: {
78 id: {
79 $in: podIds
80 }
81 }
82 }
83 ]
feb4bdfd
C
84 }
85
bd14d16a
C
86 self.findAll(query).asCallback(function (err, requests) {
87 if (err) return callback(err)
88
89 const requestsGrouped = groupAndTruncateRequests(requests, limitRequestsPerPod)
90 return callback(err, requestsGrouped)
91 })
92 })
93}
94
00057e85 95function removeAll (callback) {
feb4bdfd 96 // Delete all requests
7920c273 97 this.truncate({ cascade: true }).asCallback(callback)
00057e85
C
98}
99
100function removeWithEmptyTo (callback) {
101 if (!callback) callback = function () {}
102
feb4bdfd
C
103 const query = {
104 where: {
105 id: {
106 $notIn: [
107 this.sequelize.literal('SELECT "requestId" FROM "RequestToPods"')
108 ]
109 }
110 }
111 }
112
113 this.destroy(query).asCallback(callback)
00057e85 114}
c1a7ab7f
C
115
116// ---------------------------------------------------------------------------
117
118function groupAndTruncateRequests (requests, limitRequestsPerPod) {
119 const requestsGrouped = {}
120
121 requests.forEach(function (request) {
122 request.Pods.forEach(function (pod) {
123 if (!requestsGrouped[pod.id]) requestsGrouped[pod.id] = []
124
125 if (requestsGrouped[pod.id].length < limitRequestsPerPod) {
126 requestsGrouped[pod.id].push({
127 request,
128 pod
129 })
130 }
131 })
132 })
133
134 return requestsGrouped
135}