aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/models/request/request-video-event.ts
diff options
context:
space:
mode:
authorChocobozzz <florian.bigard@gmail.com>2017-11-10 14:48:08 +0100
committerChocobozzz <florian.bigard@gmail.com>2017-11-27 19:40:51 +0100
commit38fa2065831b5f55be0d7f30f19a62c967397208 (patch)
tree4a986465e3a88c85bc6a8b5fc992e0f2edd63ef0 /server/models/request/request-video-event.ts
parent0d0e8dd0904b380b70e19ebcb4763d65601c4632 (diff)
downloadPeerTube-38fa2065831b5f55be0d7f30f19a62c967397208.tar.gz
PeerTube-38fa2065831b5f55be0d7f30f19a62c967397208.tar.zst
PeerTube-38fa2065831b5f55be0d7f30f19a62c967397208.zip
Remove references to author
Diffstat (limited to 'server/models/request/request-video-event.ts')
-rw-r--r--server/models/request/request-video-event.ts191
1 files changed, 0 insertions, 191 deletions
diff --git a/server/models/request/request-video-event.ts b/server/models/request/request-video-event.ts
deleted file mode 100644
index 8954407e1..000000000
--- a/server/models/request/request-video-event.ts
+++ /dev/null
@@ -1,191 +0,0 @@
1/*
2 Request Video events (likes, dislikes, views...)
3*/
4
5import { values } from 'lodash'
6import * as Sequelize from 'sequelize'
7
8import { database as db } from '../../initializers/database'
9import { REQUEST_VIDEO_EVENT_TYPES } from '../../initializers'
10import { isVideoEventCountValid } from '../../helpers'
11import { addMethodsToModel } from '../utils'
12import {
13 RequestVideoEventInstance,
14 RequestVideoEventAttributes,
15
16 RequestVideoEventMethods,
17 RequestsVideoEventGrouped
18} from './request-video-event-interface'
19
20let RequestVideoEvent: Sequelize.Model<RequestVideoEventInstance, RequestVideoEventAttributes>
21let countTotalRequests: RequestVideoEventMethods.CountTotalRequests
22let listWithLimitAndRandom: RequestVideoEventMethods.ListWithLimitAndRandom
23let removeByRequestIdsAndPod: RequestVideoEventMethods.RemoveByRequestIdsAndPod
24let removeAll: RequestVideoEventMethods.RemoveAll
25
26export default function (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.DataTypes) {
27 RequestVideoEvent = sequelize.define<RequestVideoEventInstance, RequestVideoEventAttributes>('RequestVideoEvent',
28 {
29 type: {
30 type: DataTypes.ENUM(values(REQUEST_VIDEO_EVENT_TYPES)),
31 allowNull: false
32 },
33 count: {
34 type: DataTypes.INTEGER,
35 allowNull: false,
36 validate: {
37 countValid: function (value) {
38 const res = isVideoEventCountValid(value)
39 if (res === false) throw new Error('Video event count is not valid.')
40 }
41 }
42 }
43 },
44 {
45 updatedAt: false,
46 indexes: [
47 {
48 fields: [ 'videoId' ]
49 }
50 ]
51 }
52 )
53
54 const classMethods = [
55 associate,
56
57 listWithLimitAndRandom,
58 countTotalRequests,
59 removeAll,
60 removeByRequestIdsAndPod
61 ]
62 addMethodsToModel(RequestVideoEvent, classMethods)
63
64 return RequestVideoEvent
65}
66
67// ------------------------------ STATICS ------------------------------
68
69function associate (models) {
70 RequestVideoEvent.belongsTo(models.Video, {
71 foreignKey: {
72 name: 'videoId',
73 allowNull: false
74 },
75 onDelete: 'CASCADE'
76 })
77}
78
79countTotalRequests = function () {
80 const query = {}
81 return RequestVideoEvent.count(query)
82}
83
84listWithLimitAndRandom = function (limitPods: number, limitRequestsPerPod: number) {
85 const Pod = db.Pod
86
87 // We make a join between videos and authors to find the podId of our video event requests
88 const podJoins = 'INNER JOIN "VideoChannels" ON "VideoChannels"."authorId" = "Authors"."id" ' +
89 'INNER JOIN "Videos" ON "Videos"."channelId" = "VideoChannels"."id" ' +
90 'INNER JOIN "RequestVideoEvents" ON "RequestVideoEvents"."videoId" = "Videos"."id"'
91
92 return Pod.listRandomPodIdsWithRequest(limitPods, 'Authors', podJoins).then(podIds => {
93 // We don't have friends that have requests
94 if (podIds.length === 0) return []
95
96 const query = {
97 order: [
98 [ 'id', 'ASC' ]
99 ],
100 include: [
101 {
102 model: RequestVideoEvent['sequelize'].models.Video,
103 include: [
104 {
105 model: RequestVideoEvent['sequelize'].models.VideoChannel,
106 include: [
107 {
108 model: RequestVideoEvent['sequelize'].models.Author,
109 include: [
110 {
111 model: RequestVideoEvent['sequelize'].models.Pod,
112 where: {
113 id: {
114 [Sequelize.Op.in]: podIds
115 }
116 }
117 }
118 ]
119 }
120 ]
121 }
122 ]
123 }
124 ]
125 }
126
127 return RequestVideoEvent.findAll(query).then(requests => {
128 const requestsGrouped = groupAndTruncateRequests(requests, limitRequestsPerPod)
129 return requestsGrouped
130 })
131 })
132}
133
134removeByRequestIdsAndPod = function (ids: number[], podId: number) {
135 const query = {
136 where: {
137 id: {
138 [Sequelize.Op.in]: ids
139 }
140 },
141 include: [
142 {
143 model: RequestVideoEvent['sequelize'].models.Video,
144 include: [
145 {
146 model: RequestVideoEvent['sequelize'].models.VideoChannel,
147 include: [
148 {
149 model: RequestVideoEvent['sequelize'].models.Author,
150 where: {
151 podId
152 }
153 }
154 ]
155 }
156 ]
157 }
158 ]
159 }
160
161 return RequestVideoEvent.destroy(query)
162}
163
164removeAll = function () {
165 // Delete all requests
166 return RequestVideoEvent.truncate({ cascade: true })
167}
168
169// ---------------------------------------------------------------------------
170
171function groupAndTruncateRequests (events: RequestVideoEventInstance[], limitRequestsPerPod: number) {
172 const eventsGrouped: RequestsVideoEventGrouped = {}
173
174 events.forEach(event => {
175 const pod = event.Video.VideoChannel.Author.Pod
176
177 if (!eventsGrouped[pod.id]) eventsGrouped[pod.id] = []
178
179 if (eventsGrouped[pod.id].length < limitRequestsPerPod) {
180 eventsGrouped[pod.id].push({
181 id: event.id,
182 type: event.type,
183 count: event.count,
184 video: event.Video,
185 pod
186 })
187 }
188 })
189
190 return eventsGrouped
191}