]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/models/request.js
de73501fcb9d2e07a58d9939b41c756e6434c076
[github/Chocobozzz/PeerTube.git] / server / models / request.js
1 'use strict'
2
3 const values = require('lodash/values')
4
5 const constants = require('../initializers/constants')
6
7 // ---------------------------------------------------------------------------
8
9 module.exports = function (sequelize, DataTypes) {
10 const Request = sequelize.define('Request',
11 {
12 request: {
13 type: DataTypes.JSON,
14 allowNull: false
15 },
16 endpoint: {
17 type: DataTypes.ENUM(values(constants.REQUEST_ENDPOINTS)),
18 allowNull: false
19 }
20 },
21 {
22 classMethods: {
23 associate,
24
25 listWithLimitAndRandom,
26
27 countTotalRequests,
28 removeAll,
29 removeWithEmptyTo
30 }
31 }
32 )
33
34 return Request
35 }
36
37 // ------------------------------ STATICS ------------------------------
38
39 function 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
50 function countTotalRequests (callback) {
51 const query = {
52 include: [ this.sequelize.models.Pod ]
53 }
54
55 return this.count(query).asCallback(callback)
56 }
57
58 function listWithLimitAndRandom (limitPods, limitRequestsPerPod, callback) {
59 const self = this
60 const Pod = this.sequelize.models.Pod
61
62 Pod.listRandomPodIdsWithRequest(limitPods, 'RequestToPods', function (err, podIds) {
63 if (err) return callback(err)
64
65 // We don't have friends that have requests
66 if (podIds.length === 0) return callback(null, [])
67
68 // The first x requests of these pods
69 // It is very important to sort by id ASC to keep the requests order!
70 const query = {
71 order: [
72 [ 'id', 'ASC' ]
73 ],
74 include: [
75 {
76 model: self.sequelize.models.Pod,
77 where: {
78 id: {
79 $in: podIds
80 }
81 }
82 }
83 ]
84 }
85
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
95 function removeAll (callback) {
96 // Delete all requests
97 this.truncate({ cascade: true }).asCallback(callback)
98 }
99
100 function removeWithEmptyTo (callback) {
101 if (!callback) callback = function () {}
102
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)
114 }
115
116 // ---------------------------------------------------------------------------
117
118 function 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 }