]>
Commit | Line | Data |
---|---|---|
9e167724 C |
1 | /* |
2 | Request Video for Quick And Dirty Updates like: | |
3 | - views | |
4 | - likes | |
5 | - dislikes | |
6 | ||
7 | We can't put it in the same system than basic requests for efficiency. | |
8 | Moreover we don't want to slow down the basic requests with a lot of views/likes/dislikes requests. | |
9 | So we put it an independant request scheduler. | |
10 | */ | |
11 | ||
65fcc311 | 12 | import { values } from 'lodash' |
e02643f3 | 13 | import * as Sequelize from 'sequelize' |
9e167724 | 14 | |
74889a71 C |
15 | import { database as db } from '../../initializers/database' |
16 | import { REQUEST_VIDEO_QADU_TYPES } from '../../initializers' | |
17 | import { addMethodsToModel } from '../utils' | |
e02643f3 | 18 | import { |
e02643f3 C |
19 | RequestVideoQaduInstance, |
20 | RequestVideoQaduAttributes, | |
21 | ||
22 | RequestVideoQaduMethods | |
23 | } from './request-video-qadu-interface' | |
24 | ||
25 | let RequestVideoQadu: Sequelize.Model<RequestVideoQaduInstance, RequestVideoQaduAttributes> | |
26 | let countTotalRequests: RequestVideoQaduMethods.CountTotalRequests | |
27 | let listWithLimitAndRandom: RequestVideoQaduMethods.ListWithLimitAndRandom | |
28 | let removeByRequestIdsAndPod: RequestVideoQaduMethods.RemoveByRequestIdsAndPod | |
29 | let removeAll: RequestVideoQaduMethods.RemoveAll | |
9e167724 | 30 | |
127944aa C |
31 | export default function (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.DataTypes) { |
32 | RequestVideoQadu = sequelize.define<RequestVideoQaduInstance, RequestVideoQaduAttributes>('RequestVideoQadu', | |
9e167724 C |
33 | { |
34 | type: { | |
65fcc311 | 35 | type: DataTypes.ENUM(values(REQUEST_VIDEO_QADU_TYPES)), |
9e167724 C |
36 | allowNull: false |
37 | } | |
38 | }, | |
39 | { | |
40 | timestamps: false, | |
41 | indexes: [ | |
42 | { | |
43 | fields: [ 'podId' ] | |
44 | }, | |
45 | { | |
46 | fields: [ 'videoId' ] | |
47 | } | |
e02643f3 | 48 | ] |
9e167724 C |
49 | } |
50 | ) | |
51 | ||
e02643f3 C |
52 | const classMethods = [ |
53 | associate, | |
54 | ||
55 | listWithLimitAndRandom, | |
56 | countTotalRequests, | |
57 | removeAll, | |
58 | removeByRequestIdsAndPod | |
59 | ] | |
60 | addMethodsToModel(RequestVideoQadu, classMethods) | |
61 | ||
9e167724 C |
62 | return RequestVideoQadu |
63 | } | |
64 | ||
65 | // ------------------------------ STATICS ------------------------------ | |
66 | ||
67 | function associate (models) { | |
e02643f3 | 68 | RequestVideoQadu.belongsTo(models.Pod, { |
9e167724 C |
69 | foreignKey: { |
70 | name: 'podId', | |
71 | allowNull: false | |
72 | }, | |
73 | onDelete: 'CASCADE' | |
74 | }) | |
75 | ||
e02643f3 | 76 | RequestVideoQadu.belongsTo(models.Video, { |
9e167724 C |
77 | foreignKey: { |
78 | name: 'videoId', | |
79 | allowNull: false | |
80 | }, | |
81 | onDelete: 'CASCADE' | |
82 | }) | |
83 | } | |
84 | ||
6fcd19ba | 85 | countTotalRequests = function () { |
e4c87ec2 | 86 | const query = {} |
6fcd19ba | 87 | return RequestVideoQadu.count(query) |
9e167724 C |
88 | } |
89 | ||
6fcd19ba | 90 | listWithLimitAndRandom = function (limitPods: number, limitRequestsPerPod: number) { |
69818c93 C |
91 | const Pod = db.Pod |
92 | const tableJoin = '' | |
9e167724 | 93 | |
6fcd19ba | 94 | return Pod.listRandomPodIdsWithRequest(limitPods, 'RequestVideoQadus', tableJoin).then(podIds => { |
9e167724 | 95 | // We don't have friends that have requests |
6fcd19ba | 96 | if (podIds.length === 0) return [] |
9e167724 C |
97 | |
98 | const query = { | |
99 | include: [ | |
100 | { | |
e02643f3 | 101 | model: RequestVideoQadu['sequelize'].models.Pod, |
9e167724 C |
102 | where: { |
103 | id: { | |
104 | $in: podIds | |
105 | } | |
106 | } | |
107 | }, | |
108 | { | |
e02643f3 | 109 | model: RequestVideoQadu['sequelize'].models.Video |
9e167724 C |
110 | } |
111 | ] | |
112 | } | |
113 | ||
6fcd19ba | 114 | return RequestVideoQadu.findAll(query).then(requests => { |
9e167724 | 115 | const requestsGrouped = groupAndTruncateRequests(requests, limitRequestsPerPod) |
6fcd19ba | 116 | return requestsGrouped |
9e167724 C |
117 | }) |
118 | }) | |
119 | } | |
120 | ||
6fcd19ba | 121 | removeByRequestIdsAndPod = function (ids: number[], podId: number) { |
9e167724 C |
122 | const query = { |
123 | where: { | |
124 | id: { | |
125 | $in: ids | |
126 | }, | |
127 | podId | |
128 | } | |
129 | } | |
130 | ||
6fcd19ba | 131 | return RequestVideoQadu.destroy(query) |
9e167724 C |
132 | } |
133 | ||
6fcd19ba | 134 | removeAll = function () { |
9e167724 | 135 | // Delete all requests |
6fcd19ba | 136 | return RequestVideoQadu.truncate({ cascade: true }) |
9e167724 C |
137 | } |
138 | ||
139 | // --------------------------------------------------------------------------- | |
140 | ||
69818c93 | 141 | function groupAndTruncateRequests (requests: RequestVideoQaduInstance[], limitRequestsPerPod: number) { |
9e167724 C |
142 | const requestsGrouped = {} |
143 | ||
075f16ca | 144 | requests.forEach(request => { |
9e167724 C |
145 | const pod = request.Pod |
146 | ||
147 | if (!requestsGrouped[pod.id]) requestsGrouped[pod.id] = [] | |
148 | ||
149 | if (requestsGrouped[pod.id].length < limitRequestsPerPod) { | |
150 | requestsGrouped[pod.id].push({ | |
151 | request: request, | |
152 | video: request.Video, | |
153 | pod | |
154 | }) | |
155 | } | |
156 | }) | |
157 | ||
158 | return requestsGrouped | |
159 | } |