]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/models/request-video-event.js
Update bittorrent-tracker and standard to v9
[github/Chocobozzz/PeerTube.git] / server / models / request-video-event.js
1 'use strict'
2
3 /*
4 Request Video events (likes, dislikes, views...)
5 */
6
7 const values = require('lodash/values')
8
9 const constants = require('../initializers/constants')
10 const customVideosValidators = require('../helpers/custom-validators').videos
11
12 // ---------------------------------------------------------------------------
13
14 module.exports = function (sequelize, DataTypes) {
15 const RequestVideoEvent = sequelize.define('RequestVideoEvent',
16 {
17 type: {
18 type: DataTypes.ENUM(values(constants.REQUEST_VIDEO_EVENT_TYPES)),
19 allowNull: false
20 },
21 count: {
22 type: DataTypes.INTEGER,
23 allowNull: false,
24 validate: {
25 countValid: function (value) {
26 const res = customVideosValidators.isVideoEventCountValid(value)
27 if (res === false) throw new Error('Video event count is not valid.')
28 }
29 }
30 }
31 },
32 {
33 updatedAt: false,
34 indexes: [
35 {
36 fields: [ 'videoId' ]
37 }
38 ],
39 classMethods: {
40 associate,
41
42 listWithLimitAndRandom,
43
44 countTotalRequests,
45 removeAll,
46 removeByRequestIdsAndPod
47 }
48 }
49 )
50
51 return RequestVideoEvent
52 }
53
54 // ------------------------------ STATICS ------------------------------
55
56 function associate (models) {
57 this.belongsTo(models.Video, {
58 foreignKey: {
59 name: 'videoId',
60 allowNull: false
61 },
62 onDelete: 'CASCADE'
63 })
64 }
65
66 function countTotalRequests (callback) {
67 const query = {}
68 return this.count(query).asCallback(callback)
69 }
70
71 function listWithLimitAndRandom (limitPods, limitRequestsPerPod, callback) {
72 const self = this
73 const Pod = this.sequelize.models.Pod
74
75 // We make a join between videos and authors to find the podId of our video event requests
76 const podJoins = 'INNER JOIN "Videos" ON "Videos"."authorId" = "Authors"."id" ' +
77 'INNER JOIN "RequestVideoEvents" ON "RequestVideoEvents"."videoId" = "Videos"."id"'
78
79 Pod.listRandomPodIdsWithRequest(limitPods, 'Authors', podJoins, function (err, podIds) {
80 if (err) return callback(err)
81
82 // We don't have friends that have requests
83 if (podIds.length === 0) return callback(null, [])
84
85 const query = {
86 include: [
87 {
88 model: self.sequelize.models.Video,
89 include: [
90 {
91 model: self.sequelize.models.Author,
92 include: [
93 {
94 model: self.sequelize.models.Pod,
95 where: {
96 id: {
97 $in: podIds
98 }
99 }
100 }
101 ]
102 }
103 ]
104 }
105 ]
106 }
107
108 self.findAll(query).asCallback(function (err, requests) {
109 if (err) return callback(err)
110
111 const requestsGrouped = groupAndTruncateRequests(requests, limitRequestsPerPod)
112 return callback(err, requestsGrouped)
113 })
114 })
115 }
116
117 function removeByRequestIdsAndPod (ids, podId, callback) {
118 const query = {
119 where: {
120 id: {
121 $in: ids
122 }
123 },
124 include: [
125 {
126 model: this.sequelize.models.Video,
127 include: [
128 {
129 model: this.sequelize.models.Author,
130 where: {
131 podId
132 }
133 }
134 ]
135 }
136 ]
137 }
138
139 this.destroy(query).asCallback(callback)
140 }
141
142 function removeAll (callback) {
143 // Delete all requests
144 this.truncate({ cascade: true }).asCallback(callback)
145 }
146
147 // ---------------------------------------------------------------------------
148
149 function groupAndTruncateRequests (events, limitRequestsPerPod) {
150 const eventsGrouped = {}
151
152 events.forEach(function (event) {
153 const pod = event.Video.Author.Pod
154
155 if (!eventsGrouped[pod.id]) eventsGrouped[pod.id] = []
156
157 if (eventsGrouped[pod.id].length < limitRequestsPerPod) {
158 eventsGrouped[pod.id].push({
159 id: event.id,
160 type: event.type,
161 count: event.count,
162 video: event.Video,
163 pod
164 })
165 }
166 })
167
168 return eventsGrouped
169 }