]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/models/request/request.ts
Move to promises
[github/Chocobozzz/PeerTube.git] / server / models / request / request.ts
CommitLineData
65fcc311 1import { values } from 'lodash'
e02643f3 2import * as Sequelize from 'sequelize'
9f10b292 3
74889a71
C
4import { database as db } from '../../initializers/database'
5import { REQUEST_ENDPOINTS } from '../../initializers'
6import { addMethodsToModel } from '../utils'
e02643f3 7import {
e02643f3
C
8 RequestInstance,
9 RequestAttributes,
10
69818c93
C
11 RequestMethods,
12 RequestsGrouped
e02643f3
C
13} from './request-interface'
14
15let Request: Sequelize.Model<RequestInstance, RequestAttributes>
16let countTotalRequests: RequestMethods.CountTotalRequests
17let listWithLimitAndRandom: RequestMethods.ListWithLimitAndRandom
18let removeWithEmptyTo: RequestMethods.RemoveWithEmptyTo
19let removeAll: RequestMethods.RemoveAll
9f10b292 20
127944aa
C
21export default function (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.DataTypes) {
22 Request = sequelize.define<RequestInstance, RequestAttributes>('Request',
feb4bdfd
C
23 {
24 request: {
67bf9b96
C
25 type: DataTypes.JSON,
26 allowNull: false
feb4bdfd
C
27 },
28 endpoint: {
65fcc311 29 type: DataTypes.ENUM(values(REQUEST_ENDPOINTS)),
67bf9b96 30 allowNull: false
feb4bdfd 31 }
4b08096b 32 }
feb4bdfd 33 )
528a9efa 34
e02643f3
C
35 const classMethods = [
36 associate,
37
38 listWithLimitAndRandom,
39
40 countTotalRequests,
41 removeAll,
42 removeWithEmptyTo
43 ]
44 addMethodsToModel(Request, classMethods)
45
feb4bdfd
C
46 return Request
47}
00057e85
C
48
49// ------------------------------ STATICS ------------------------------
50
feb4bdfd 51function associate (models) {
e02643f3 52 Request.belongsToMany(models.Pod, {
feb4bdfd
C
53 foreignKey: {
54 name: 'requestId',
55 allowNull: false
56 },
57 through: models.RequestToPod,
58 onDelete: 'CASCADE'
59 })
60}
61
6fcd19ba 62countTotalRequests = function () {
e4c87ec2
C
63 // We need to include Pod because there are no cascade delete when a pod is removed
64 // So we could count requests that do not have existing pod anymore
feb4bdfd 65 const query = {
e02643f3 66 include: [ Request['sequelize'].models.Pod ]
feb4bdfd
C
67 }
68
6fcd19ba 69 return Request.count(query)
feb4bdfd
C
70}
71
6fcd19ba 72listWithLimitAndRandom = function (limitPods: number, limitRequestsPerPod: number) {
69818c93
C
73 const Pod = db.Pod
74 const tableJoin = ''
43666d61 75
6fcd19ba 76 return Pod.listRandomPodIdsWithRequest(limitPods, 'RequestToPods', tableJoin).then(podIds => {
bd14d16a 77 // We don't have friends that have requests
6fcd19ba 78 if (podIds.length === 0) return []
43666d61 79
9e167724 80 // The first x requests of these pods
bd14d16a 81 // It is very important to sort by id ASC to keep the requests order!
feb4bdfd
C
82 const query = {
83 order: [
84 [ 'id', 'ASC' ]
85 ],
bd14d16a
C
86 include: [
87 {
e02643f3 88 model: Request['sequelize'].models.Pod,
bd14d16a
C
89 where: {
90 id: {
91 $in: podIds
92 }
93 }
94 }
95 ]
feb4bdfd
C
96 }
97
6fcd19ba 98 return Request.findAll(query).then(requests => {
bd14d16a
C
99
100 const requestsGrouped = groupAndTruncateRequests(requests, limitRequestsPerPod)
6fcd19ba 101 return requestsGrouped
bd14d16a
C
102 })
103 })
104}
105
6fcd19ba 106removeAll = function () {
feb4bdfd 107 // Delete all requests
6fcd19ba 108 return Request.truncate({ cascade: true })
00057e85
C
109}
110
6fcd19ba 111removeWithEmptyTo = function () {
feb4bdfd
C
112 const query = {
113 where: {
114 id: {
115 $notIn: [
e02643f3 116 Sequelize.literal('SELECT "requestId" FROM "RequestToPods"')
feb4bdfd
C
117 ]
118 }
119 }
120 }
121
6fcd19ba 122 return Request.destroy(query)
00057e85 123}
c1a7ab7f
C
124
125// ---------------------------------------------------------------------------
126
69818c93
C
127function groupAndTruncateRequests (requests: RequestInstance[], limitRequestsPerPod: number) {
128 const requestsGrouped: RequestsGrouped = {}
c1a7ab7f
C
129
130 requests.forEach(function (request) {
131 request.Pods.forEach(function (pod) {
132 if (!requestsGrouped[pod.id]) requestsGrouped[pod.id] = []
133
134 if (requestsGrouped[pod.id].length < limitRequestsPerPod) {
135 requestsGrouped[pod.id].push({
136 request,
137 pod
138 })
139 }
140 })
141 })
142
143 return requestsGrouped
144}