aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/models/request-video-qadu.js
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-video-qadu.js
parentd5f345ed4cfac4e1fa84dcb4fce1cda4d32f9c73 (diff)
downloadPeerTube-65fcc3119c334b75dd13bcfdebf186afdc580a8f.tar.gz
PeerTube-65fcc3119c334b75dd13bcfdebf186afdc580a8f.tar.zst
PeerTube-65fcc3119c334b75dd13bcfdebf186afdc580a8f.zip
First typescript iteration
Diffstat (limited to 'server/models/request-video-qadu.js')
-rw-r--r--server/models/request-video-qadu.js151
1 files changed, 0 insertions, 151 deletions
diff --git a/server/models/request-video-qadu.js b/server/models/request-video-qadu.js
deleted file mode 100644
index 5d88738aa..000000000
--- a/server/models/request-video-qadu.js
+++ /dev/null
@@ -1,151 +0,0 @@
1'use strict'
2
3/*
4 Request Video for Quick And Dirty Updates like:
5 - views
6 - likes
7 - dislikes
8
9 We can't put it in the same system than basic requests for efficiency.
10 Moreover we don't want to slow down the basic requests with a lot of views/likes/dislikes requests.
11 So we put it an independant request scheduler.
12*/
13
14const values = require('lodash/values')
15
16const constants = require('../initializers/constants')
17
18// ---------------------------------------------------------------------------
19
20module.exports = function (sequelize, DataTypes) {
21 const RequestVideoQadu = sequelize.define('RequestVideoQadu',
22 {
23 type: {
24 type: DataTypes.ENUM(values(constants.REQUEST_VIDEO_QADU_TYPES)),
25 allowNull: false
26 }
27 },
28 {
29 timestamps: false,
30 indexes: [
31 {
32 fields: [ 'podId' ]
33 },
34 {
35 fields: [ 'videoId' ]
36 }
37 ],
38 classMethods: {
39 associate,
40
41 listWithLimitAndRandom,
42
43 countTotalRequests,
44 removeAll,
45 removeByRequestIdsAndPod
46 }
47 }
48 )
49
50 return RequestVideoQadu
51}
52
53// ------------------------------ STATICS ------------------------------
54
55function associate (models) {
56 this.belongsTo(models.Pod, {
57 foreignKey: {
58 name: 'podId',
59 allowNull: false
60 },
61 onDelete: 'CASCADE'
62 })
63
64 this.belongsTo(models.Video, {
65 foreignKey: {
66 name: 'videoId',
67 allowNull: false
68 },
69 onDelete: 'CASCADE'
70 })
71}
72
73function countTotalRequests (callback) {
74 const query = {}
75 return this.count(query).asCallback(callback)
76}
77
78function listWithLimitAndRandom (limitPods, limitRequestsPerPod, callback) {
79 const self = this
80 const Pod = this.sequelize.models.Pod
81
82 Pod.listRandomPodIdsWithRequest(limitPods, 'RequestVideoQadus', function (err, podIds) {
83 if (err) return callback(err)
84
85 // We don't have friends that have requests
86 if (podIds.length === 0) return callback(null, [])
87
88 const query = {
89 include: [
90 {
91 model: self.sequelize.models.Pod,
92 where: {
93 id: {
94 $in: podIds
95 }
96 }
97 },
98 {
99 model: self.sequelize.models.Video
100 }
101 ]
102 }
103
104 self.findAll(query).asCallback(function (err, requests) {
105 if (err) return callback(err)
106
107 const requestsGrouped = groupAndTruncateRequests(requests, limitRequestsPerPod)
108 return callback(err, requestsGrouped)
109 })
110 })
111}
112
113function removeByRequestIdsAndPod (ids, podId, callback) {
114 const query = {
115 where: {
116 id: {
117 $in: ids
118 },
119 podId
120 }
121 }
122
123 this.destroy(query).asCallback(callback)
124}
125
126function removeAll (callback) {
127 // Delete all requests
128 this.truncate({ cascade: true }).asCallback(callback)
129}
130
131// ---------------------------------------------------------------------------
132
133function groupAndTruncateRequests (requests, limitRequestsPerPod) {
134 const requestsGrouped = {}
135
136 requests.forEach(function (request) {
137 const pod = request.Pod
138
139 if (!requestsGrouped[pod.id]) requestsGrouped[pod.id] = []
140
141 if (requestsGrouped[pod.id].length < limitRequestsPerPod) {
142 requestsGrouped[pod.id].push({
143 request: request,
144 video: request.Video,
145 pod
146 })
147 }
148 })
149
150 return requestsGrouped
151}