]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | Request Video events (likes, dislikes, views...) | |
3 | */ | |
4 | ||
5 | import { values } from 'lodash' | |
6 | import * as Sequelize from 'sequelize' | |
7 | ||
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' | |
12 | import { | |
13 | RequestVideoEventInstance, | |
14 | RequestVideoEventAttributes, | |
15 | ||
16 | RequestVideoEventMethods, | |
17 | RequestsVideoEventGrouped | |
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 | |
25 | ||
26 | export 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 | ||
69 | function associate (models) { | |
70 | RequestVideoEvent.belongsTo(models.Video, { | |
71 | foreignKey: { | |
72 | name: 'videoId', | |
73 | allowNull: false | |
74 | }, | |
75 | onDelete: 'CASCADE' | |
76 | }) | |
77 | } | |
78 | ||
79 | countTotalRequests = function () { | |
80 | const query = {} | |
81 | return RequestVideoEvent.count(query) | |
82 | } | |
83 | ||
84 | listWithLimitAndRandom = 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 "Videos" ON "Videos"."authorId" = "Authors"."id" ' + | |
89 | 'INNER JOIN "RequestVideoEvents" ON "RequestVideoEvents"."videoId" = "Videos"."id"' | |
90 | ||
91 | return Pod.listRandomPodIdsWithRequest(limitPods, 'Authors', podJoins).then(podIds => { | |
92 | // We don't have friends that have requests | |
93 | if (podIds.length === 0) return [] | |
94 | ||
95 | const query = { | |
96 | order: [ | |
97 | [ 'id', 'ASC' ] | |
98 | ], | |
99 | include: [ | |
100 | { | |
101 | model: RequestVideoEvent['sequelize'].models.Video, | |
102 | include: [ | |
103 | { | |
104 | model: RequestVideoEvent['sequelize'].models.Author, | |
105 | include: [ | |
106 | { | |
107 | model: RequestVideoEvent['sequelize'].models.Pod, | |
108 | where: { | |
109 | id: { | |
110 | $in: podIds | |
111 | } | |
112 | } | |
113 | } | |
114 | ] | |
115 | } | |
116 | ] | |
117 | } | |
118 | ] | |
119 | } | |
120 | ||
121 | return RequestVideoEvent.findAll(query).then(requests => { | |
122 | const requestsGrouped = groupAndTruncateRequests(requests, limitRequestsPerPod) | |
123 | return requestsGrouped | |
124 | }) | |
125 | }) | |
126 | } | |
127 | ||
128 | removeByRequestIdsAndPod = function (ids: number[], podId: number) { | |
129 | const query = { | |
130 | where: { | |
131 | id: { | |
132 | $in: ids | |
133 | } | |
134 | }, | |
135 | include: [ | |
136 | { | |
137 | model: RequestVideoEvent['sequelize'].models.Video, | |
138 | include: [ | |
139 | { | |
140 | model: RequestVideoEvent['sequelize'].models.Author, | |
141 | where: { | |
142 | podId | |
143 | } | |
144 | } | |
145 | ] | |
146 | } | |
147 | ] | |
148 | } | |
149 | ||
150 | return RequestVideoEvent.destroy(query) | |
151 | } | |
152 | ||
153 | removeAll = function () { | |
154 | // Delete all requests | |
155 | return RequestVideoEvent.truncate({ cascade: true }) | |
156 | } | |
157 | ||
158 | // --------------------------------------------------------------------------- | |
159 | ||
160 | function groupAndTruncateRequests (events: RequestVideoEventInstance[], limitRequestsPerPod: number) { | |
161 | const eventsGrouped: RequestsVideoEventGrouped = {} | |
162 | ||
163 | events.forEach(function (event) { | |
164 | const pod = event.Video.Author.Pod | |
165 | ||
166 | if (!eventsGrouped[pod.id]) eventsGrouped[pod.id] = [] | |
167 | ||
168 | if (eventsGrouped[pod.id].length < limitRequestsPerPod) { | |
169 | eventsGrouped[pod.id].push({ | |
170 | id: event.id, | |
171 | type: event.type, | |
172 | count: event.count, | |
173 | video: event.Video, | |
174 | pod | |
175 | }) | |
176 | } | |
177 | }) | |
178 | ||
179 | return eventsGrouped | |
180 | } |