]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/models/request-video-qadu.js
Update README
[github/Chocobozzz/PeerTube.git] / server / models / request-video-qadu.js
CommitLineData
9e167724
C
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) {
e4c87ec2 74 const query = {}
9e167724
C
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}