]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/models/request/request-video-qadu.ts
Remove any typing from server
[github/Chocobozzz/PeerTube.git] / server / models / request / request-video-qadu.ts
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 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
30
31 export default function (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.DataTypes) {
32 RequestVideoQadu = sequelize.define<RequestVideoQaduInstance, RequestVideoQaduAttributes>('RequestVideoQadu',
33 {
34 type: {
35 type: DataTypes.ENUM(values(REQUEST_VIDEO_QADU_TYPES)),
36 allowNull: false
37 }
38 },
39 {
40 timestamps: false,
41 indexes: [
42 {
43 fields: [ 'podId' ]
44 },
45 {
46 fields: [ 'videoId' ]
47 }
48 ]
49 }
50 )
51
52 const classMethods = [
53 associate,
54
55 listWithLimitAndRandom,
56 countTotalRequests,
57 removeAll,
58 removeByRequestIdsAndPod
59 ]
60 addMethodsToModel(RequestVideoQadu, classMethods)
61
62 return RequestVideoQadu
63 }
64
65 // ------------------------------ STATICS ------------------------------
66
67 function associate (models) {
68 RequestVideoQadu.belongsTo(models.Pod, {
69 foreignKey: {
70 name: 'podId',
71 allowNull: false
72 },
73 onDelete: 'CASCADE'
74 })
75
76 RequestVideoQadu.belongsTo(models.Video, {
77 foreignKey: {
78 name: 'videoId',
79 allowNull: false
80 },
81 onDelete: 'CASCADE'
82 })
83 }
84
85 countTotalRequests = function () {
86 const query = {}
87 return RequestVideoQadu.count(query)
88 }
89
90 listWithLimitAndRandom = function (limitPods: number, limitRequestsPerPod: number) {
91 const Pod = db.Pod
92 const tableJoin = ''
93
94 return Pod.listRandomPodIdsWithRequest(limitPods, 'RequestVideoQadus', tableJoin).then(podIds => {
95 // We don't have friends that have requests
96 if (podIds.length === 0) return []
97
98 const query = {
99 include: [
100 {
101 model: RequestVideoQadu['sequelize'].models.Pod,
102 where: {
103 id: {
104 $in: podIds
105 }
106 }
107 },
108 {
109 model: RequestVideoQadu['sequelize'].models.Video
110 }
111 ]
112 }
113
114 return RequestVideoQadu.findAll(query).then(requests => {
115 const requestsGrouped = groupAndTruncateRequests(requests, limitRequestsPerPod)
116 return requestsGrouped
117 })
118 })
119 }
120
121 removeByRequestIdsAndPod = function (ids: number[], podId: number) {
122 const query = {
123 where: {
124 id: {
125 $in: ids
126 },
127 podId
128 }
129 }
130
131 return RequestVideoQadu.destroy(query)
132 }
133
134 removeAll = function () {
135 // Delete all requests
136 return RequestVideoQadu.truncate({ cascade: true })
137 }
138
139 // ---------------------------------------------------------------------------
140
141 function groupAndTruncateRequests (requests: RequestVideoQaduInstance[], limitRequestsPerPod: number) {
142 const requestsGrouped = {}
143
144 requests.forEach(function (request) {
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 }