]>
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 | |
74889a71 C |
8 | import { database as db } from '../../initializers/database' |
9 | import { REQUEST_VIDEO_EVENT_TYPES } from '../../initializers' | |
10 | import { isVideoEventCountValid } from '../../helpers' | |
11 | import { addMethodsToModel } from '../utils' | |
e02643f3 | 12 | import { |
e02643f3 C |
13 | RequestVideoEventInstance, |
14 | RequestVideoEventAttributes, | |
15 | ||
69818c93 C |
16 | RequestVideoEventMethods, |
17 | RequestsVideoEventGrouped | |
e02643f3 C |
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 | |
127944aa C |
26 | export default function (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.DataTypes) { |
27 | RequestVideoEvent = sequelize.define<RequestVideoEventInstance, RequestVideoEventAttributes>('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 | ||
6fcd19ba | 79 | countTotalRequests = function () { |
e4c87ec2 | 80 | const query = {} |
6fcd19ba | 81 | return RequestVideoEvent.count(query) |
e4c87ec2 C |
82 | } |
83 | ||
6fcd19ba | 84 | listWithLimitAndRandom = function (limitPods: number, limitRequestsPerPod: number) { |
69818c93 | 85 | const Pod = db.Pod |
e4c87ec2 C |
86 | |
87 | // We make a join between videos and authors to find the podId of our video event requests | |
72c7248b C |
88 | const podJoins = 'INNER JOIN "VideoChannels" ON "VideoChannels"."authorId" = "Authors"."id" ' + |
89 | 'INNER JOIN "Videos" ON "Videos"."channelId" = "VideoChannels"."id" ' + | |
e4c87ec2 C |
90 | 'INNER JOIN "RequestVideoEvents" ON "RequestVideoEvents"."videoId" = "Videos"."id"' |
91 | ||
6fcd19ba | 92 | return Pod.listRandomPodIdsWithRequest(limitPods, 'Authors', podJoins).then(podIds => { |
e4c87ec2 | 93 | // We don't have friends that have requests |
6fcd19ba | 94 | if (podIds.length === 0) return [] |
e4c87ec2 C |
95 | |
96 | const query = { | |
d38b8281 C |
97 | order: [ |
98 | [ 'id', 'ASC' ] | |
99 | ], | |
e4c87ec2 C |
100 | include: [ |
101 | { | |
e02643f3 | 102 | model: RequestVideoEvent['sequelize'].models.Video, |
e4c87ec2 C |
103 | include: [ |
104 | { | |
b869d190 | 105 | model: RequestVideoEvent['sequelize'].models.VideoChannel, |
e4c87ec2 C |
106 | include: [ |
107 | { | |
b869d190 C |
108 | model: RequestVideoEvent['sequelize'].models.Author, |
109 | include: [ | |
110 | { | |
111 | model: RequestVideoEvent['sequelize'].models.Pod, | |
112 | where: { | |
113 | id: { | |
c2962505 | 114 | [Sequelize.Op.in]: podIds |
b869d190 C |
115 | } |
116 | } | |
e4c87ec2 | 117 | } |
b869d190 | 118 | ] |
e4c87ec2 C |
119 | } |
120 | ] | |
121 | } | |
122 | ] | |
123 | } | |
124 | ] | |
125 | } | |
126 | ||
6fcd19ba | 127 | return RequestVideoEvent.findAll(query).then(requests => { |
e4c87ec2 | 128 | const requestsGrouped = groupAndTruncateRequests(requests, limitRequestsPerPod) |
6fcd19ba | 129 | return requestsGrouped |
e4c87ec2 C |
130 | }) |
131 | }) | |
132 | } | |
133 | ||
6fcd19ba | 134 | removeByRequestIdsAndPod = function (ids: number[], podId: number) { |
e4c87ec2 C |
135 | const query = { |
136 | where: { | |
137 | id: { | |
c2962505 | 138 | [Sequelize.Op.in]: ids |
e4c87ec2 C |
139 | } |
140 | }, | |
141 | include: [ | |
142 | { | |
e02643f3 | 143 | model: RequestVideoEvent['sequelize'].models.Video, |
e4c87ec2 C |
144 | include: [ |
145 | { | |
b869d190 C |
146 | model: RequestVideoEvent['sequelize'].models.VideoChannel, |
147 | include: [ | |
148 | { | |
149 | model: RequestVideoEvent['sequelize'].models.Author, | |
150 | where: { | |
151 | podId | |
152 | } | |
153 | } | |
154 | ] | |
e4c87ec2 C |
155 | } |
156 | ] | |
157 | } | |
158 | ] | |
159 | } | |
160 | ||
6fcd19ba | 161 | return RequestVideoEvent.destroy(query) |
e4c87ec2 C |
162 | } |
163 | ||
6fcd19ba | 164 | removeAll = function () { |
e4c87ec2 | 165 | // Delete all requests |
6fcd19ba | 166 | return RequestVideoEvent.truncate({ cascade: true }) |
e4c87ec2 C |
167 | } |
168 | ||
169 | // --------------------------------------------------------------------------- | |
170 | ||
69818c93 C |
171 | function groupAndTruncateRequests (events: RequestVideoEventInstance[], limitRequestsPerPod: number) { |
172 | const eventsGrouped: RequestsVideoEventGrouped = {} | |
e4c87ec2 | 173 | |
075f16ca | 174 | events.forEach(event => { |
72c7248b | 175 | const pod = event.Video.VideoChannel.Author.Pod |
e4c87ec2 C |
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 | } |