]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/models/request-video-qadu.ts
Type functions
[github/Chocobozzz/PeerTube.git] / server / models / request-video-qadu.ts
CommitLineData
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 12import { values } from 'lodash'
e02643f3 13import * as Sequelize from 'sequelize'
9e167724 14
69818c93 15import { database as db } from '../initializers/database'
65fcc311 16import { REQUEST_VIDEO_QADU_TYPES } from '../initializers'
e02643f3
C
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
9e167724 31
e02643f3
C
32export default function (sequelize, DataTypes) {
33 RequestVideoQadu = sequelize.define('RequestVideoQadu',
9e167724
C
34 {
35 type: {
65fcc311 36 type: DataTypes.ENUM(values(REQUEST_VIDEO_QADU_TYPES)),
9e167724
C
37 allowNull: false
38 }
39 },
40 {
41 timestamps: false,
42 indexes: [
43 {
44 fields: [ 'podId' ]
45 },
46 {
47 fields: [ 'videoId' ]
48 }
e02643f3 49 ]
9e167724
C
50 }
51 )
52
e02643f3
C
53 const classMethods = [
54 associate,
55
56 listWithLimitAndRandom,
57 countTotalRequests,
58 removeAll,
59 removeByRequestIdsAndPod
60 ]
61 addMethodsToModel(RequestVideoQadu, classMethods)
62
9e167724
C
63 return RequestVideoQadu
64}
65
66// ------------------------------ STATICS ------------------------------
67
68function associate (models) {
e02643f3 69 RequestVideoQadu.belongsTo(models.Pod, {
9e167724
C
70 foreignKey: {
71 name: 'podId',
72 allowNull: false
73 },
74 onDelete: 'CASCADE'
75 })
76
e02643f3 77 RequestVideoQadu.belongsTo(models.Video, {
9e167724
C
78 foreignKey: {
79 name: 'videoId',
80 allowNull: false
81 },
82 onDelete: 'CASCADE'
83 })
84}
85
69818c93 86countTotalRequests = function (callback: RequestVideoQaduMethods.CountTotalRequestsCallback) {
e4c87ec2 87 const query = {}
e02643f3 88 return RequestVideoQadu.count(query).asCallback(callback)
9e167724
C
89}
90
69818c93
C
91listWithLimitAndRandom = function (limitPods: number, limitRequestsPerPod: number, callback: RequestVideoQaduMethods.ListWithLimitAndRandomCallback) {
92 const Pod = db.Pod
93 const tableJoin = ''
9e167724 94
69818c93 95 Pod.listRandomPodIdsWithRequest(limitPods, 'RequestVideoQadus', tableJoin, function (err, podIds) {
9e167724
C
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 {
e02643f3 104 model: RequestVideoQadu['sequelize'].models.Pod,
9e167724
C
105 where: {
106 id: {
107 $in: podIds
108 }
109 }
110 },
111 {
e02643f3 112 model: RequestVideoQadu['sequelize'].models.Video
9e167724
C
113 }
114 ]
115 }
116
e02643f3 117 RequestVideoQadu.findAll(query).asCallback(function (err, requests) {
9e167724
C
118 if (err) return callback(err)
119
120 const requestsGrouped = groupAndTruncateRequests(requests, limitRequestsPerPod)
121 return callback(err, requestsGrouped)
122 })
123 })
124}
125
69818c93 126removeByRequestIdsAndPod = function (ids: number[], podId: number, callback: RequestVideoQaduMethods.RemoveByRequestIdsAndPodCallback) {
9e167724
C
127 const query = {
128 where: {
129 id: {
130 $in: ids
131 },
132 podId
133 }
134 }
135
e02643f3 136 RequestVideoQadu.destroy(query).asCallback(callback)
9e167724
C
137}
138
69818c93 139removeAll = function (callback: RequestVideoQaduMethods.RemoveAllCallback) {
9e167724 140 // Delete all requests
e02643f3 141 RequestVideoQadu.truncate({ cascade: true }).asCallback(callback)
9e167724
C
142}
143
144// ---------------------------------------------------------------------------
145
69818c93 146function groupAndTruncateRequests (requests: RequestVideoQaduInstance[], limitRequestsPerPod: number) {
9e167724
C
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}