aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/models
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
parent9e167724f7e933f41d9ea2e1c31772bf4c560a28 (diff)
downloadPeerTube-e4c87ec26962e359d1c70b03ed188a3f19d6a25b.tar.gz
PeerTube-e4c87ec26962e359d1c70b03ed188a3f19d6a25b.tar.zst
PeerTube-e4c87ec26962e359d1c70b03ed188a3f19d6a25b.zip
Server: implement video views
Diffstat (limited to 'server/models')
-rw-r--r--server/models/pod.js9
-rw-r--r--server/models/request-video-event.js169
-rw-r--r--server/models/request-video-qadu.js5
-rw-r--r--server/models/request.js2
4 files changed, 179 insertions, 6 deletions
diff --git a/server/models/pod.js b/server/models/pod.js
index 14814708e..8e2d488e1 100644
--- a/server/models/pod.js
+++ b/server/models/pod.js
@@ -148,7 +148,12 @@ function listAllIds (transaction, callback) {
148 }) 148 })
149} 149}
150 150
151function listRandomPodIdsWithRequest (limit, tableRequestPod, callback) { 151function listRandomPodIdsWithRequest (limit, tableWithPods, tableWithPodsJoins, callback) {
152 if (!callback) {
153 callback = tableWithPodsJoins
154 tableWithPodsJoins = ''
155 }
156
152 const self = this 157 const self = this
153 158
154 self.count().asCallback(function (err, count) { 159 self.count().asCallback(function (err, count) {
@@ -170,7 +175,7 @@ function listRandomPodIdsWithRequest (limit, tableRequestPod, callback) {
170 where: { 175 where: {
171 id: { 176 id: {
172 $in: [ 177 $in: [
173 this.sequelize.literal('SELECT "podId" FROM "' + tableRequestPod + '"') 178 this.sequelize.literal(`SELECT DISTINCT "${tableWithPods}"."podId" FROM "${tableWithPods}" ${tableWithPodsJoins}`)
174 ] 179 ]
175 } 180 }
176 } 181 }
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}
diff --git a/server/models/request-video-qadu.js b/server/models/request-video-qadu.js
index 7010fc992..5d88738aa 100644
--- a/server/models/request-video-qadu.js
+++ b/server/models/request-video-qadu.js
@@ -71,10 +71,7 @@ function associate (models) {
71} 71}
72 72
73function countTotalRequests (callback) { 73function countTotalRequests (callback) {
74 const query = { 74 const query = {}
75 include: [ this.sequelize.models.Pod ]
76 }
77
78 return this.count(query).asCallback(callback) 75 return this.count(query).asCallback(callback)
79} 76}
80 77
diff --git a/server/models/request.js b/server/models/request.js
index de73501fc..3a047f7ee 100644
--- a/server/models/request.js
+++ b/server/models/request.js
@@ -48,6 +48,8 @@ function associate (models) {
48} 48}
49 49
50function countTotalRequests (callback) { 50function countTotalRequests (callback) {
51 // We need to include Pod because there are no cascade delete when a pod is removed
52 // So we could count requests that do not have existing pod anymore
51 const query = { 53 const query = {
52 include: [ this.sequelize.models.Pod ] 54 include: [ this.sequelize.models.Pod ]
53 } 55 }