diff options
Diffstat (limited to 'server/models/request/request-video-qadu.ts')
-rw-r--r-- | server/models/request/request-video-qadu.ts | 164 |
1 files changed, 164 insertions, 0 deletions
diff --git a/server/models/request/request-video-qadu.ts b/server/models/request/request-video-qadu.ts new file mode 100644 index 000000000..da62239f5 --- /dev/null +++ b/server/models/request/request-video-qadu.ts | |||
@@ -0,0 +1,164 @@ | |||
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 | |||
12 | import { values } from 'lodash' | ||
13 | import * as Sequelize from 'sequelize' | ||
14 | |||
15 | import { database as db } from '../../initializers/database' | ||
16 | import { REQUEST_VIDEO_QADU_TYPES } from '../../initializers' | ||
17 | import { addMethodsToModel } from '../utils' | ||
18 | import { | ||
19 | RequestVideoQaduClass, | ||
20 | RequestVideoQaduInstance, | ||
21 | RequestVideoQaduAttributes, | ||
22 | |||
23 | RequestVideoQaduMethods | ||
24 | } from './request-video-qadu-interface' | ||
25 | |||
26 | let RequestVideoQadu: Sequelize.Model<RequestVideoQaduInstance, RequestVideoQaduAttributes> | ||
27 | let countTotalRequests: RequestVideoQaduMethods.CountTotalRequests | ||
28 | let listWithLimitAndRandom: RequestVideoQaduMethods.ListWithLimitAndRandom | ||
29 | let removeByRequestIdsAndPod: RequestVideoQaduMethods.RemoveByRequestIdsAndPod | ||
30 | let removeAll: RequestVideoQaduMethods.RemoveAll | ||
31 | |||
32 | export default function (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.DataTypes) { | ||
33 | RequestVideoQadu = sequelize.define<RequestVideoQaduInstance, RequestVideoQaduAttributes>('RequestVideoQadu', | ||
34 | { | ||
35 | type: { | ||
36 | type: DataTypes.ENUM(values(REQUEST_VIDEO_QADU_TYPES)), | ||
37 | allowNull: false | ||
38 | } | ||
39 | }, | ||
40 | { | ||
41 | timestamps: false, | ||
42 | indexes: [ | ||
43 | { | ||
44 | fields: [ 'podId' ] | ||
45 | }, | ||
46 | { | ||
47 | fields: [ 'videoId' ] | ||
48 | } | ||
49 | ] | ||
50 | } | ||
51 | ) | ||
52 | |||
53 | const classMethods = [ | ||
54 | associate, | ||
55 | |||
56 | listWithLimitAndRandom, | ||
57 | countTotalRequests, | ||
58 | removeAll, | ||
59 | removeByRequestIdsAndPod | ||
60 | ] | ||
61 | addMethodsToModel(RequestVideoQadu, classMethods) | ||
62 | |||
63 | return RequestVideoQadu | ||
64 | } | ||
65 | |||
66 | // ------------------------------ STATICS ------------------------------ | ||
67 | |||
68 | function associate (models) { | ||
69 | RequestVideoQadu.belongsTo(models.Pod, { | ||
70 | foreignKey: { | ||
71 | name: 'podId', | ||
72 | allowNull: false | ||
73 | }, | ||
74 | onDelete: 'CASCADE' | ||
75 | }) | ||
76 | |||
77 | RequestVideoQadu.belongsTo(models.Video, { | ||
78 | foreignKey: { | ||
79 | name: 'videoId', | ||
80 | allowNull: false | ||
81 | }, | ||
82 | onDelete: 'CASCADE' | ||
83 | }) | ||
84 | } | ||
85 | |||
86 | countTotalRequests = function (callback: RequestVideoQaduMethods.CountTotalRequestsCallback) { | ||
87 | const query = {} | ||
88 | return RequestVideoQadu.count(query).asCallback(callback) | ||
89 | } | ||
90 | |||
91 | listWithLimitAndRandom = function (limitPods: number, limitRequestsPerPod: number, callback: RequestVideoQaduMethods.ListWithLimitAndRandomCallback) { | ||
92 | const Pod = db.Pod | ||
93 | const tableJoin = '' | ||
94 | |||
95 | Pod.listRandomPodIdsWithRequest(limitPods, 'RequestVideoQadus', tableJoin, function (err, podIds) { | ||
96 | if (err) return callback(err) | ||
97 | |||
98 | // We don't have friends that have requests | ||
99 | if (podIds.length === 0) return callback(null, []) | ||
100 | |||
101 | const query = { | ||
102 | include: [ | ||
103 | { | ||
104 | model: RequestVideoQadu['sequelize'].models.Pod, | ||
105 | where: { | ||
106 | id: { | ||
107 | $in: podIds | ||
108 | } | ||
109 | } | ||
110 | }, | ||
111 | { | ||
112 | model: RequestVideoQadu['sequelize'].models.Video | ||
113 | } | ||
114 | ] | ||
115 | } | ||
116 | |||
117 | RequestVideoQadu.findAll(query).asCallback(function (err, requests) { | ||
118 | if (err) return callback(err) | ||
119 | |||
120 | const requestsGrouped = groupAndTruncateRequests(requests, limitRequestsPerPod) | ||
121 | return callback(err, requestsGrouped) | ||
122 | }) | ||
123 | }) | ||
124 | } | ||
125 | |||
126 | removeByRequestIdsAndPod = function (ids: number[], podId: number, callback: RequestVideoQaduMethods.RemoveByRequestIdsAndPodCallback) { | ||
127 | const query = { | ||
128 | where: { | ||
129 | id: { | ||
130 | $in: ids | ||
131 | }, | ||
132 | podId | ||
133 | } | ||
134 | } | ||
135 | |||
136 | RequestVideoQadu.destroy(query).asCallback(callback) | ||
137 | } | ||
138 | |||
139 | removeAll = function (callback: RequestVideoQaduMethods.RemoveAllCallback) { | ||
140 | // Delete all requests | ||
141 | RequestVideoQadu.truncate({ cascade: true }).asCallback(callback) | ||
142 | } | ||
143 | |||
144 | // --------------------------------------------------------------------------- | ||
145 | |||
146 | function groupAndTruncateRequests (requests: RequestVideoQaduInstance[], limitRequestsPerPod: number) { | ||
147 | const requestsGrouped = {} | ||
148 | |||
149 | requests.forEach(function (request) { | ||
150 | const pod = request.Pod | ||
151 | |||
152 | if (!requestsGrouped[pod.id]) requestsGrouped[pod.id] = [] | ||
153 | |||
154 | if (requestsGrouped[pod.id].length < limitRequestsPerPod) { | ||
155 | requestsGrouped[pod.id].push({ | ||
156 | request: request, | ||
157 | video: request.Video, | ||
158 | pod | ||
159 | }) | ||
160 | } | ||
161 | }) | ||
162 | |||
163 | return requestsGrouped | ||
164 | } | ||