aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/models/request/request-video-qadu.ts
diff options
context:
space:
mode:
authorChocobozzz <florian.bigard@gmail.com>2017-06-16 09:45:46 +0200
committerChocobozzz <florian.bigard@gmail.com>2017-06-16 09:45:46 +0200
commit74889a71fe687dda74f2a687653122327807af36 (patch)
treee938e8b6401b74fbec80513a877d9967f2c0dbcd /server/models/request/request-video-qadu.ts
parent15a302943d84bc0978b84fe33110c4daa451d311 (diff)
downloadPeerTube-74889a71fe687dda74f2a687653122327807af36.tar.gz
PeerTube-74889a71fe687dda74f2a687653122327807af36.tar.zst
PeerTube-74889a71fe687dda74f2a687653122327807af36.zip
Reorganize model files
Diffstat (limited to 'server/models/request/request-video-qadu.ts')
-rw-r--r--server/models/request/request-video-qadu.ts164
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
12import { values } from 'lodash'
13import * as Sequelize from 'sequelize'
14
15import { database as db } from '../../initializers/database'
16import { REQUEST_VIDEO_QADU_TYPES } from '../../initializers'
17import { addMethodsToModel } from '../utils'
18import {
19 RequestVideoQaduClass,
20 RequestVideoQaduInstance,
21 RequestVideoQaduAttributes,
22
23 RequestVideoQaduMethods
24} from './request-video-qadu-interface'
25
26let RequestVideoQadu: Sequelize.Model<RequestVideoQaduInstance, RequestVideoQaduAttributes>
27let countTotalRequests: RequestVideoQaduMethods.CountTotalRequests
28let listWithLimitAndRandom: RequestVideoQaduMethods.ListWithLimitAndRandom
29let removeByRequestIdsAndPod: RequestVideoQaduMethods.RemoveByRequestIdsAndPod
30let removeAll: RequestVideoQaduMethods.RemoveAll
31
32export 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
68function 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
86countTotalRequests = function (callback: RequestVideoQaduMethods.CountTotalRequestsCallback) {
87 const query = {}
88 return RequestVideoQadu.count(query).asCallback(callback)
89}
90
91listWithLimitAndRandom = 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
126removeByRequestIdsAndPod = 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
139removeAll = function (callback: RequestVideoQaduMethods.RemoveAllCallback) {
140 // Delete all requests
141 RequestVideoQadu.truncate({ cascade: true }).asCallback(callback)
142}
143
144// ---------------------------------------------------------------------------
145
146function 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}