diff options
author | Chocobozzz <florian.bigard@gmail.com> | 2017-11-10 14:48:08 +0100 |
---|---|---|
committer | Chocobozzz <florian.bigard@gmail.com> | 2017-11-27 19:40:51 +0100 |
commit | 38fa2065831b5f55be0d7f30f19a62c967397208 (patch) | |
tree | 4a986465e3a88c85bc6a8b5fc992e0f2edd63ef0 /server/models/request/request.ts | |
parent | 0d0e8dd0904b380b70e19ebcb4763d65601c4632 (diff) | |
download | PeerTube-38fa2065831b5f55be0d7f30f19a62c967397208.tar.gz PeerTube-38fa2065831b5f55be0d7f30f19a62c967397208.tar.zst PeerTube-38fa2065831b5f55be0d7f30f19a62c967397208.zip |
Remove references to author
Diffstat (limited to 'server/models/request/request.ts')
-rw-r--r-- | server/models/request/request.ts | 144 |
1 files changed, 0 insertions, 144 deletions
diff --git a/server/models/request/request.ts b/server/models/request/request.ts deleted file mode 100644 index 71118a947..000000000 --- a/server/models/request/request.ts +++ /dev/null | |||
@@ -1,144 +0,0 @@ | |||
1 | import { values } from 'lodash' | ||
2 | import * as Sequelize from 'sequelize' | ||
3 | |||
4 | import { database as db } from '../../initializers/database' | ||
5 | import { REQUEST_ENDPOINTS } from '../../initializers' | ||
6 | import { addMethodsToModel } from '../utils' | ||
7 | import { | ||
8 | RequestInstance, | ||
9 | RequestAttributes, | ||
10 | |||
11 | RequestMethods, | ||
12 | RequestsGrouped | ||
13 | } from './request-interface' | ||
14 | |||
15 | let Request: Sequelize.Model<RequestInstance, RequestAttributes> | ||
16 | let countTotalRequests: RequestMethods.CountTotalRequests | ||
17 | let listWithLimitAndRandom: RequestMethods.ListWithLimitAndRandom | ||
18 | let removeWithEmptyTo: RequestMethods.RemoveWithEmptyTo | ||
19 | let removeAll: RequestMethods.RemoveAll | ||
20 | |||
21 | export default function (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.DataTypes) { | ||
22 | Request = sequelize.define<RequestInstance, RequestAttributes>('Request', | ||
23 | { | ||
24 | request: { | ||
25 | type: DataTypes.JSON, | ||
26 | allowNull: false | ||
27 | }, | ||
28 | endpoint: { | ||
29 | type: DataTypes.ENUM(values(REQUEST_ENDPOINTS)), | ||
30 | allowNull: false | ||
31 | } | ||
32 | } | ||
33 | ) | ||
34 | |||
35 | const classMethods = [ | ||
36 | associate, | ||
37 | |||
38 | listWithLimitAndRandom, | ||
39 | |||
40 | countTotalRequests, | ||
41 | removeAll, | ||
42 | removeWithEmptyTo | ||
43 | ] | ||
44 | addMethodsToModel(Request, classMethods) | ||
45 | |||
46 | return Request | ||
47 | } | ||
48 | |||
49 | // ------------------------------ STATICS ------------------------------ | ||
50 | |||
51 | function associate (models) { | ||
52 | Request.belongsToMany(models.Pod, { | ||
53 | foreignKey: { | ||
54 | name: 'requestId', | ||
55 | allowNull: false | ||
56 | }, | ||
57 | through: models.RequestToPod, | ||
58 | onDelete: 'CASCADE' | ||
59 | }) | ||
60 | } | ||
61 | |||
62 | countTotalRequests = function () { | ||
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 | ||
65 | const query = { | ||
66 | include: [ Request['sequelize'].models.Pod ] | ||
67 | } | ||
68 | |||
69 | return Request.count(query) | ||
70 | } | ||
71 | |||
72 | listWithLimitAndRandom = function (limitPods: number, limitRequestsPerPod: number) { | ||
73 | const Pod = db.Pod | ||
74 | const tableJoin = '' | ||
75 | |||
76 | return Pod.listRandomPodIdsWithRequest(limitPods, 'RequestToPods', tableJoin).then(podIds => { | ||
77 | // We don't have friends that have requests | ||
78 | if (podIds.length === 0) return [] | ||
79 | |||
80 | // The first x requests of these pods | ||
81 | // It is very important to sort by id ASC to keep the requests order! | ||
82 | const query = { | ||
83 | order: [ | ||
84 | [ 'id', 'ASC' ] | ||
85 | ], | ||
86 | include: [ | ||
87 | { | ||
88 | model: Request['sequelize'].models.Pod, | ||
89 | where: { | ||
90 | id: { | ||
91 | [Sequelize.Op.in]: podIds | ||
92 | } | ||
93 | } | ||
94 | } | ||
95 | ] | ||
96 | } | ||
97 | |||
98 | return Request.findAll(query).then(requests => { | ||
99 | |||
100 | const requestsGrouped = groupAndTruncateRequests(requests, limitRequestsPerPod) | ||
101 | return requestsGrouped | ||
102 | }) | ||
103 | }) | ||
104 | } | ||
105 | |||
106 | removeAll = function () { | ||
107 | // Delete all requests | ||
108 | return Request.truncate({ cascade: true }) | ||
109 | } | ||
110 | |||
111 | removeWithEmptyTo = function () { | ||
112 | const query = { | ||
113 | where: { | ||
114 | id: { | ||
115 | [Sequelize.Op.notIn]: [ | ||
116 | Sequelize.literal('SELECT "requestId" FROM "RequestToPods"') | ||
117 | ] | ||
118 | } | ||
119 | } | ||
120 | } | ||
121 | |||
122 | return Request.destroy(query) | ||
123 | } | ||
124 | |||
125 | // --------------------------------------------------------------------------- | ||
126 | |||
127 | function groupAndTruncateRequests (requests: RequestInstance[], limitRequestsPerPod: number) { | ||
128 | const requestsGrouped: RequestsGrouped = {} | ||
129 | |||
130 | requests.forEach(request => { | ||
131 | request.Pods.forEach(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 | } | ||