aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/models/request-video-event.js
diff options
context:
space:
mode:
authorChocobozzz <florian.bigard@gmail.com>2017-02-26 18:57:33 +0100
committerChocobozzz <florian.bigard@gmail.com>2017-02-26 20:01:26 +0100
commite4c87ec26962e359d1c70b03ed188a3f19d6a25b (patch)
tree26fe20e6f600bc6f6f569dde2171b0a2346b135c /server/models/request-video-event.js
parent9e167724f7e933f41d9ea2e1c31772bf4c560a28 (diff)
downloadPeerTube-e4c87ec26962e359d1c70b03ed188a3f19d6a25b.tar.gz
PeerTube-e4c87ec26962e359d1c70b03ed188a3f19d6a25b.tar.zst
PeerTube-e4c87ec26962e359d1c70b03ed188a3f19d6a25b.zip
Server: implement video views
Diffstat (limited to 'server/models/request-video-event.js')
-rw-r--r--server/models/request-video-event.js169
1 files changed, 169 insertions, 0 deletions
diff --git a/server/models/request-video-event.js b/server/models/request-video-event.js
new file mode 100644
index 000000000..ef3ebcb3a
--- /dev/null
+++ b/server/models/request-video-event.js
@@ -0,0 +1,169 @@
1'use strict'
2
3/*
4 Request Video events (likes, dislikes, views...)
5*/
6
7const values = require('lodash/values')
8
9const constants = require('../initializers/constants')
10const customVideosValidators = require('../helpers/custom-validators').videos
11
12// ---------------------------------------------------------------------------
13
14module.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
56function associate (models) {
57 this.belongsTo(models.Video, {
58 foreignKey: {
59 name: 'videoId',
60 allowNull: false
61 },
62 onDelete: 'CASCADE'
63 })
64}
65
66function countTotalRequests (callback) {
67 const query = {}
68 return this.count(query).asCallback(callback)
69}
70
71function 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
117function 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
142function removeAll (callback) {
143 // Delete all requests
144 this.truncate({ cascade: true }).asCallback(callback)
145}
146
147// ---------------------------------------------------------------------------
148
149function 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}