aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/models/request.ts
diff options
context:
space:
mode:
authorChocobozzz <florian.bigard@gmail.com>2017-05-15 22:22:03 +0200
committerChocobozzz <florian.bigard@gmail.com>2017-05-20 09:57:40 +0200
commit65fcc3119c334b75dd13bcfdebf186afdc580a8f (patch)
tree4f2158c61a9b7c3f47cfa233d01413b946ee53c0 /server/models/request.ts
parentd5f345ed4cfac4e1fa84dcb4fce1cda4d32f9c73 (diff)
downloadPeerTube-65fcc3119c334b75dd13bcfdebf186afdc580a8f.tar.gz
PeerTube-65fcc3119c334b75dd13bcfdebf186afdc580a8f.tar.zst
PeerTube-65fcc3119c334b75dd13bcfdebf186afdc580a8f.zip
First typescript iteration
Diffstat (limited to 'server/models/request.ts')
-rw-r--r--server/models/request.ts135
1 files changed, 135 insertions, 0 deletions
diff --git a/server/models/request.ts b/server/models/request.ts
new file mode 100644
index 000000000..672f79d11
--- /dev/null
+++ b/server/models/request.ts
@@ -0,0 +1,135 @@
1import { values } from 'lodash'
2
3import { REQUEST_ENDPOINTS } from '../initializers'
4
5// ---------------------------------------------------------------------------
6
7module.exports = function (sequelize, DataTypes) {
8 const Request = sequelize.define('Request',
9 {
10 request: {
11 type: DataTypes.JSON,
12 allowNull: false
13 },
14 endpoint: {
15 type: DataTypes.ENUM(values(REQUEST_ENDPOINTS)),
16 allowNull: false
17 }
18 },
19 {
20 classMethods: {
21 associate,
22
23 listWithLimitAndRandom,
24
25 countTotalRequests,
26 removeAll,
27 removeWithEmptyTo
28 }
29 }
30 )
31
32 return Request
33}
34
35// ------------------------------ STATICS ------------------------------
36
37function associate (models) {
38 this.belongsToMany(models.Pod, {
39 foreignKey: {
40 name: 'requestId',
41 allowNull: false
42 },
43 through: models.RequestToPod,
44 onDelete: 'CASCADE'
45 })
46}
47
48function countTotalRequests (callback) {
49 // We need to include Pod because there are no cascade delete when a pod is removed
50 // So we could count requests that do not have existing pod anymore
51 const query = {
52 include: [ this.sequelize.models.Pod ]
53 }
54
55 return this.count(query).asCallback(callback)
56}
57
58function 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
95function removeAll (callback) {
96 // Delete all requests
97 this.truncate({ cascade: true }).asCallback(callback)
98}
99
100function removeWithEmptyTo (callback) {
101 if (!callback) callback = function () { /* empty */ }
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
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}