]>
Commit | Line | Data |
---|---|---|
e4c87ec2 C |
1 | /* |
2 | Request Video events (likes, dislikes, views...) | |
3 | */ | |
4 | ||
65fcc311 | 5 | import { values } from 'lodash' |
e02643f3 | 6 | import * as Sequelize from 'sequelize' |
e4c87ec2 | 7 | |
65fcc311 C |
8 | import { REQUEST_VIDEO_EVENT_TYPES } from '../initializers' |
9 | import { isVideoEventCountValid } from '../helpers' | |
e4c87ec2 | 10 | |
e02643f3 C |
11 | import { addMethodsToModel } from './utils' |
12 | import { | |
13 | RequestVideoEventClass, | |
14 | RequestVideoEventInstance, | |
15 | RequestVideoEventAttributes, | |
16 | ||
17 | RequestVideoEventMethods | |
18 | } from './request-video-event-interface' | |
19 | ||
20 | let RequestVideoEvent: Sequelize.Model<RequestVideoEventInstance, RequestVideoEventAttributes> | |
21 | let countTotalRequests: RequestVideoEventMethods.CountTotalRequests | |
22 | let listWithLimitAndRandom: RequestVideoEventMethods.ListWithLimitAndRandom | |
23 | let removeByRequestIdsAndPod: RequestVideoEventMethods.RemoveByRequestIdsAndPod | |
24 | let removeAll: RequestVideoEventMethods.RemoveAll | |
e4c87ec2 | 25 | |
e02643f3 C |
26 | export default function (sequelize, DataTypes) { |
27 | RequestVideoEvent = sequelize.define('RequestVideoEvent', | |
e4c87ec2 C |
28 | { |
29 | type: { | |
65fcc311 | 30 | type: DataTypes.ENUM(values(REQUEST_VIDEO_EVENT_TYPES)), |
e4c87ec2 C |
31 | allowNull: false |
32 | }, | |
33 | count: { | |
34 | type: DataTypes.INTEGER, | |
35 | allowNull: false, | |
36 | validate: { | |
37 | countValid: function (value) { | |
65fcc311 | 38 | const res = isVideoEventCountValid(value) |
e4c87ec2 C |
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 | } | |
e02643f3 | 50 | ] |
e4c87ec2 C |
51 | } |
52 | ) | |
53 | ||
e02643f3 C |
54 | const classMethods = [ |
55 | associate, | |
56 | ||
57 | listWithLimitAndRandom, | |
58 | countTotalRequests, | |
59 | removeAll, | |
60 | removeByRequestIdsAndPod | |
61 | ] | |
62 | addMethodsToModel(RequestVideoEvent, classMethods) | |
63 | ||
e4c87ec2 C |
64 | return RequestVideoEvent |
65 | } | |
66 | ||
67 | // ------------------------------ STATICS ------------------------------ | |
68 | ||
69 | function associate (models) { | |
e02643f3 | 70 | RequestVideoEvent.belongsTo(models.Video, { |
e4c87ec2 C |
71 | foreignKey: { |
72 | name: 'videoId', | |
73 | allowNull: false | |
74 | }, | |
75 | onDelete: 'CASCADE' | |
76 | }) | |
77 | } | |
78 | ||
e02643f3 | 79 | countTotalRequests = function (callback) { |
e4c87ec2 | 80 | const query = {} |
e02643f3 | 81 | return RequestVideoEvent.count(query).asCallback(callback) |
e4c87ec2 C |
82 | } |
83 | ||
e02643f3 C |
84 | listWithLimitAndRandom = function (limitPods, limitRequestsPerPod, callback) { |
85 | const Pod = RequestVideoEvent['sequelize'].models.Pod | |
e4c87ec2 C |
86 | |
87 | // We make a join between videos and authors to find the podId of our video event requests | |
88 | const podJoins = 'INNER JOIN "Videos" ON "Videos"."authorId" = "Authors"."id" ' + | |
89 | 'INNER JOIN "RequestVideoEvents" ON "RequestVideoEvents"."videoId" = "Videos"."id"' | |
90 | ||
91 | Pod.listRandomPodIdsWithRequest(limitPods, 'Authors', podJoins, function (err, podIds) { | |
92 | if (err) return callback(err) | |
93 | ||
94 | // We don't have friends that have requests | |
95 | if (podIds.length === 0) return callback(null, []) | |
96 | ||
97 | const query = { | |
d38b8281 C |
98 | order: [ |
99 | [ 'id', 'ASC' ] | |
100 | ], | |
e4c87ec2 C |
101 | include: [ |
102 | { | |
e02643f3 | 103 | model: RequestVideoEvent['sequelize'].models.Video, |
e4c87ec2 C |
104 | include: [ |
105 | { | |
e02643f3 | 106 | model: RequestVideoEvent['sequelize'].models.Author, |
e4c87ec2 C |
107 | include: [ |
108 | { | |
e02643f3 | 109 | model: RequestVideoEvent['sequelize'].models.Pod, |
e4c87ec2 C |
110 | where: { |
111 | id: { | |
112 | $in: podIds | |
113 | } | |
114 | } | |
115 | } | |
116 | ] | |
117 | } | |
118 | ] | |
119 | } | |
120 | ] | |
121 | } | |
122 | ||
e02643f3 | 123 | RequestVideoEvent.findAll(query).asCallback(function (err, requests) { |
e4c87ec2 C |
124 | if (err) return callback(err) |
125 | ||
126 | const requestsGrouped = groupAndTruncateRequests(requests, limitRequestsPerPod) | |
127 | return callback(err, requestsGrouped) | |
128 | }) | |
129 | }) | |
130 | } | |
131 | ||
e02643f3 | 132 | removeByRequestIdsAndPod = function (ids, podId, callback) { |
e4c87ec2 C |
133 | const query = { |
134 | where: { | |
135 | id: { | |
136 | $in: ids | |
137 | } | |
138 | }, | |
139 | include: [ | |
140 | { | |
e02643f3 | 141 | model: RequestVideoEvent['sequelize'].models.Video, |
e4c87ec2 C |
142 | include: [ |
143 | { | |
e02643f3 | 144 | model: RequestVideoEvent['sequelize'].models.Author, |
e4c87ec2 C |
145 | where: { |
146 | podId | |
147 | } | |
148 | } | |
149 | ] | |
150 | } | |
151 | ] | |
152 | } | |
153 | ||
e02643f3 | 154 | RequestVideoEvent.destroy(query).asCallback(callback) |
e4c87ec2 C |
155 | } |
156 | ||
e02643f3 | 157 | removeAll = function (callback) { |
e4c87ec2 | 158 | // Delete all requests |
e02643f3 | 159 | RequestVideoEvent.truncate({ cascade: true }).asCallback(callback) |
e4c87ec2 C |
160 | } |
161 | ||
162 | // --------------------------------------------------------------------------- | |
163 | ||
164 | function groupAndTruncateRequests (events, limitRequestsPerPod) { | |
165 | const eventsGrouped = {} | |
166 | ||
167 | events.forEach(function (event) { | |
168 | const pod = event.Video.Author.Pod | |
169 | ||
170 | if (!eventsGrouped[pod.id]) eventsGrouped[pod.id] = [] | |
171 | ||
172 | if (eventsGrouped[pod.id].length < limitRequestsPerPod) { | |
173 | eventsGrouped[pod.id].push({ | |
174 | id: event.id, | |
175 | type: event.type, | |
176 | count: event.count, | |
177 | video: event.Video, | |
178 | pod | |
179 | }) | |
180 | } | |
181 | }) | |
182 | ||
183 | return eventsGrouped | |
184 | } |