diff options
Diffstat (limited to 'server/models')
-rw-r--r-- | server/models/account/user.ts | 4 | ||||
-rw-r--r-- | server/models/index.ts | 1 | ||||
-rw-r--r-- | server/models/oauth/oauth-token.ts | 4 | ||||
-rw-r--r-- | server/models/request/abstract-request-interface.ts | 12 | ||||
-rw-r--r-- | server/models/request/index.ts | 5 | ||||
-rw-r--r-- | server/models/request/request-interface.ts | 46 | ||||
-rw-r--r-- | server/models/request/request-to-pod-interface.ts | 23 | ||||
-rw-r--r-- | server/models/request/request-to-pod.ts | 51 | ||||
-rw-r--r-- | server/models/request/request-video-event-interface.ts | 50 | ||||
-rw-r--r-- | server/models/request/request-video-event.ts | 191 | ||||
-rw-r--r-- | server/models/request/request-video-qadu-interface.ts | 48 | ||||
-rw-r--r-- | server/models/request/request-video-qadu.ts | 159 | ||||
-rw-r--r-- | server/models/request/request.ts | 144 | ||||
-rw-r--r-- | server/models/video/index.ts | 1 |
14 files changed, 4 insertions, 735 deletions
diff --git a/server/models/account/user.ts b/server/models/account/user.ts index 1401762c5..7390baf91 100644 --- a/server/models/account/user.ts +++ b/server/models/account/user.ts | |||
@@ -164,7 +164,7 @@ toFormattedJSON = function (this: UserInstance) { | |||
164 | roleLabel: USER_ROLE_LABELS[this.role], | 164 | roleLabel: USER_ROLE_LABELS[this.role], |
165 | videoQuota: this.videoQuota, | 165 | videoQuota: this.videoQuota, |
166 | createdAt: this.createdAt, | 166 | createdAt: this.createdAt, |
167 | author: { | 167 | account: { |
168 | id: this.Account.id, | 168 | id: this.Account.id, |
169 | uuid: this.Account.uuid | 169 | uuid: this.Account.uuid |
170 | } | 170 | } |
@@ -295,7 +295,7 @@ function getOriginalVideoFileTotalFromUser (user: UserInstance) { | |||
295 | '(SELECT MAX("VideoFiles"."size") AS "size" FROM "VideoFiles" ' + | 295 | '(SELECT MAX("VideoFiles"."size") AS "size" FROM "VideoFiles" ' + |
296 | 'INNER JOIN "Videos" ON "VideoFiles"."videoId" = "Videos"."id" ' + | 296 | 'INNER JOIN "Videos" ON "VideoFiles"."videoId" = "Videos"."id" ' + |
297 | 'INNER JOIN "VideoChannels" ON "VideoChannels"."id" = "Videos"."channelId" ' + | 297 | 'INNER JOIN "VideoChannels" ON "VideoChannels"."id" = "Videos"."channelId" ' + |
298 | 'INNER JOIN "Accounts" ON "VideoChannels"."authorId" = "Accounts"."id" ' + | 298 | 'INNER JOIN "Accounts" ON "VideoChannels"."accountId" = "Accounts"."id" ' + |
299 | 'INNER JOIN "Users" ON "Accounts"."userId" = "Users"."id" ' + | 299 | 'INNER JOIN "Users" ON "Accounts"."userId" = "Users"."id" ' + |
300 | 'WHERE "Users"."id" = $userId GROUP BY "Videos"."id") t' | 300 | 'WHERE "Users"."id" = $userId GROUP BY "Videos"."id") t' |
301 | 301 | ||
diff --git a/server/models/index.ts b/server/models/index.ts index 29479e067..0aec2d3b1 100644 --- a/server/models/index.ts +++ b/server/models/index.ts | |||
@@ -2,6 +2,5 @@ export * from './application' | |||
2 | export * from './job' | 2 | export * from './job' |
3 | export * from './oauth' | 3 | export * from './oauth' |
4 | export * from './pod' | 4 | export * from './pod' |
5 | export * from './request' | ||
6 | export * from './account' | 5 | export * from './account' |
7 | export * from './video' | 6 | export * from './video' |
diff --git a/server/models/oauth/oauth-token.ts b/server/models/oauth/oauth-token.ts index dc8bcd872..c7afcc38c 100644 --- a/server/models/oauth/oauth-token.ts +++ b/server/models/oauth/oauth-token.ts | |||
@@ -131,7 +131,7 @@ getByTokenAndPopulateUser = function (bearerToken: string) { | |||
131 | model: OAuthToken['sequelize'].models.User, | 131 | model: OAuthToken['sequelize'].models.User, |
132 | include: [ | 132 | include: [ |
133 | { | 133 | { |
134 | model: OAuthToken['sequelize'].models.Author, | 134 | model: OAuthToken['sequelize'].models.Account, |
135 | required: true | 135 | required: true |
136 | } | 136 | } |
137 | ] | 137 | ] |
@@ -156,7 +156,7 @@ getByRefreshTokenAndPopulateUser = function (refreshToken: string) { | |||
156 | model: OAuthToken['sequelize'].models.User, | 156 | model: OAuthToken['sequelize'].models.User, |
157 | include: [ | 157 | include: [ |
158 | { | 158 | { |
159 | model: OAuthToken['sequelize'].models.Author, | 159 | model: OAuthToken['sequelize'].models.Account, |
160 | required: true | 160 | required: true |
161 | } | 161 | } |
162 | ] | 162 | ] |
diff --git a/server/models/request/abstract-request-interface.ts b/server/models/request/abstract-request-interface.ts deleted file mode 100644 index a384f4d27..000000000 --- a/server/models/request/abstract-request-interface.ts +++ /dev/null | |||
@@ -1,12 +0,0 @@ | |||
1 | import * as Promise from 'bluebird' | ||
2 | |||
3 | export interface AbstractRequestClass <T> { | ||
4 | countTotalRequests: () => Promise<number> | ||
5 | listWithLimitAndRandom: (limitPods: number, limitRequestsPerPod: number) => Promise<T> | ||
6 | removeWithEmptyTo: () => Promise<number> | ||
7 | removeAll: () => Promise<void> | ||
8 | } | ||
9 | |||
10 | export interface AbstractRequestToPodClass { | ||
11 | removeByRequestIdsAndPod: (ids: number[], podId: number) => Promise<number> | ||
12 | } | ||
diff --git a/server/models/request/index.ts b/server/models/request/index.ts deleted file mode 100644 index 3dd6aedc2..000000000 --- a/server/models/request/index.ts +++ /dev/null | |||
@@ -1,5 +0,0 @@ | |||
1 | export * from './abstract-request-interface' | ||
2 | export * from './request-interface' | ||
3 | export * from './request-to-pod-interface' | ||
4 | export * from './request-video-event-interface' | ||
5 | export * from './request-video-qadu-interface' | ||
diff --git a/server/models/request/request-interface.ts b/server/models/request/request-interface.ts deleted file mode 100644 index dae35651b..000000000 --- a/server/models/request/request-interface.ts +++ /dev/null | |||
@@ -1,46 +0,0 @@ | |||
1 | import * as Sequelize from 'sequelize' | ||
2 | import * as Promise from 'bluebird' | ||
3 | |||
4 | import { AbstractRequestClass } from './abstract-request-interface' | ||
5 | import { PodInstance, PodAttributes } from '../pod/pod-interface' | ||
6 | import { RequestEndpoint } from '../../../shared/models/request-scheduler.model' | ||
7 | |||
8 | export type RequestsGrouped = { | ||
9 | [ podId: number ]: { | ||
10 | request: RequestInstance, | ||
11 | pod: PodInstance | ||
12 | }[] | ||
13 | } | ||
14 | |||
15 | export namespace RequestMethods { | ||
16 | export type CountTotalRequests = () => Promise<number> | ||
17 | |||
18 | export type ListWithLimitAndRandom = (limitPods: number, limitRequestsPerPod: number) => Promise<RequestsGrouped> | ||
19 | |||
20 | export type RemoveWithEmptyTo = () => Promise<number> | ||
21 | |||
22 | export type RemoveAll = () => Promise<void> | ||
23 | } | ||
24 | |||
25 | export interface RequestClass extends AbstractRequestClass<RequestsGrouped> { | ||
26 | countTotalRequests: RequestMethods.CountTotalRequests | ||
27 | listWithLimitAndRandom: RequestMethods.ListWithLimitAndRandom | ||
28 | removeWithEmptyTo: RequestMethods.RemoveWithEmptyTo | ||
29 | removeAll: RequestMethods.RemoveAll | ||
30 | } | ||
31 | |||
32 | export interface RequestAttributes { | ||
33 | request: object | ||
34 | endpoint: RequestEndpoint | ||
35 | } | ||
36 | |||
37 | export interface RequestInstance extends RequestClass, RequestAttributes, Sequelize.Instance<RequestAttributes> { | ||
38 | id: number | ||
39 | createdAt: Date | ||
40 | updatedAt: Date | ||
41 | |||
42 | setPods: Sequelize.HasManySetAssociationsMixin<PodAttributes, number> | ||
43 | Pods: PodInstance[] | ||
44 | } | ||
45 | |||
46 | export interface RequestModel extends RequestClass, Sequelize.Model<RequestInstance, RequestAttributes> {} | ||
diff --git a/server/models/request/request-to-pod-interface.ts b/server/models/request/request-to-pod-interface.ts deleted file mode 100644 index 7ca99f4d4..000000000 --- a/server/models/request/request-to-pod-interface.ts +++ /dev/null | |||
@@ -1,23 +0,0 @@ | |||
1 | import * as Sequelize from 'sequelize' | ||
2 | import * as Promise from 'bluebird' | ||
3 | |||
4 | import { AbstractRequestToPodClass } from './abstract-request-interface' | ||
5 | |||
6 | export namespace RequestToPodMethods { | ||
7 | export type RemoveByRequestIdsAndPod = (requestsIds: number[], podId: number) => Promise<number> | ||
8 | } | ||
9 | |||
10 | export interface RequestToPodClass extends AbstractRequestToPodClass { | ||
11 | removeByRequestIdsAndPod: RequestToPodMethods.RemoveByRequestIdsAndPod | ||
12 | } | ||
13 | |||
14 | export interface RequestToPodAttributes { | ||
15 | } | ||
16 | |||
17 | export interface RequestToPodInstance extends RequestToPodClass, RequestToPodAttributes, Sequelize.Instance<RequestToPodAttributes> { | ||
18 | id: number | ||
19 | createdAt: Date | ||
20 | updatedAt: Date | ||
21 | } | ||
22 | |||
23 | export interface RequestToPodModel extends RequestToPodClass, Sequelize.Model<RequestToPodInstance, RequestToPodAttributes> {} | ||
diff --git a/server/models/request/request-to-pod.ts b/server/models/request/request-to-pod.ts deleted file mode 100644 index 4e14e918b..000000000 --- a/server/models/request/request-to-pod.ts +++ /dev/null | |||
@@ -1,51 +0,0 @@ | |||
1 | import * as Sequelize from 'sequelize' | ||
2 | |||
3 | import { addMethodsToModel } from '../utils' | ||
4 | import { | ||
5 | RequestToPodInstance, | ||
6 | RequestToPodAttributes, | ||
7 | |||
8 | RequestToPodMethods | ||
9 | } from './request-to-pod-interface' | ||
10 | |||
11 | let RequestToPod: Sequelize.Model<RequestToPodInstance, RequestToPodAttributes> | ||
12 | let removeByRequestIdsAndPod: RequestToPodMethods.RemoveByRequestIdsAndPod | ||
13 | |||
14 | export default function (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.DataTypes) { | ||
15 | RequestToPod = sequelize.define<RequestToPodInstance, RequestToPodAttributes>('RequestToPod', {}, { | ||
16 | indexes: [ | ||
17 | { | ||
18 | fields: [ 'requestId' ] | ||
19 | }, | ||
20 | { | ||
21 | fields: [ 'podId' ] | ||
22 | }, | ||
23 | { | ||
24 | fields: [ 'requestId', 'podId' ], | ||
25 | unique: true | ||
26 | } | ||
27 | ] | ||
28 | }) | ||
29 | |||
30 | const classMethods = [ | ||
31 | removeByRequestIdsAndPod | ||
32 | ] | ||
33 | addMethodsToModel(RequestToPod, classMethods) | ||
34 | |||
35 | return RequestToPod | ||
36 | } | ||
37 | |||
38 | // --------------------------------------------------------------------------- | ||
39 | |||
40 | removeByRequestIdsAndPod = function (requestsIds: number[], podId: number) { | ||
41 | const query = { | ||
42 | where: { | ||
43 | requestId: { | ||
44 | [Sequelize.Op.in]: requestsIds | ||
45 | }, | ||
46 | podId: podId | ||
47 | } | ||
48 | } | ||
49 | |||
50 | return RequestToPod.destroy(query) | ||
51 | } | ||
diff --git a/server/models/request/request-video-event-interface.ts b/server/models/request/request-video-event-interface.ts deleted file mode 100644 index 26cabe3ba..000000000 --- a/server/models/request/request-video-event-interface.ts +++ /dev/null | |||
@@ -1,50 +0,0 @@ | |||
1 | import * as Sequelize from 'sequelize' | ||
2 | import * as Promise from 'bluebird' | ||
3 | |||
4 | import { AbstractRequestClass, AbstractRequestToPodClass } from './abstract-request-interface' | ||
5 | import { VideoInstance } from '../video/video-interface' | ||
6 | import { PodInstance } from '../pod/pod-interface' | ||
7 | |||
8 | import { RequestVideoEventType } from '../../../shared/models/request-scheduler.model' | ||
9 | |||
10 | export type RequestsVideoEventGrouped = { | ||
11 | [ podId: number ]: { | ||
12 | id: number | ||
13 | type: RequestVideoEventType | ||
14 | count: number | ||
15 | video: VideoInstance | ||
16 | pod: PodInstance | ||
17 | }[] | ||
18 | } | ||
19 | |||
20 | export namespace RequestVideoEventMethods { | ||
21 | export type CountTotalRequests = () => Promise<number> | ||
22 | |||
23 | export type ListWithLimitAndRandom = (limitPods: number, limitRequestsPerPod: number) => Promise<RequestsVideoEventGrouped> | ||
24 | |||
25 | export type RemoveByRequestIdsAndPod = (ids: number[], podId: number) => Promise<number> | ||
26 | |||
27 | export type RemoveAll = () => Promise<void> | ||
28 | } | ||
29 | |||
30 | export interface RequestVideoEventClass extends AbstractRequestClass<RequestsVideoEventGrouped>, AbstractRequestToPodClass { | ||
31 | countTotalRequests: RequestVideoEventMethods.CountTotalRequests | ||
32 | listWithLimitAndRandom: RequestVideoEventMethods.ListWithLimitAndRandom | ||
33 | removeByRequestIdsAndPod: RequestVideoEventMethods.RemoveByRequestIdsAndPod | ||
34 | removeAll: RequestVideoEventMethods.RemoveAll | ||
35 | } | ||
36 | |||
37 | export interface RequestVideoEventAttributes { | ||
38 | type: RequestVideoEventType | ||
39 | count: number | ||
40 | } | ||
41 | |||
42 | export interface RequestVideoEventInstance | ||
43 | extends RequestVideoEventClass, RequestVideoEventAttributes, Sequelize.Instance<RequestVideoEventAttributes> { | ||
44 | id: number | ||
45 | |||
46 | Video: VideoInstance | ||
47 | } | ||
48 | |||
49 | export interface RequestVideoEventModel | ||
50 | extends RequestVideoEventClass, Sequelize.Model<RequestVideoEventInstance, RequestVideoEventAttributes> {} | ||
diff --git a/server/models/request/request-video-event.ts b/server/models/request/request-video-event.ts deleted file mode 100644 index 8954407e1..000000000 --- a/server/models/request/request-video-event.ts +++ /dev/null | |||
@@ -1,191 +0,0 @@ | |||
1 | /* | ||
2 | Request Video events (likes, dislikes, views...) | ||
3 | */ | ||
4 | |||
5 | import { values } from 'lodash' | ||
6 | import * as Sequelize from 'sequelize' | ||
7 | |||
8 | import { database as db } from '../../initializers/database' | ||
9 | import { REQUEST_VIDEO_EVENT_TYPES } from '../../initializers' | ||
10 | import { isVideoEventCountValid } from '../../helpers' | ||
11 | import { addMethodsToModel } from '../utils' | ||
12 | import { | ||
13 | RequestVideoEventInstance, | ||
14 | RequestVideoEventAttributes, | ||
15 | |||
16 | RequestVideoEventMethods, | ||
17 | RequestsVideoEventGrouped | ||
18 | } from './request-video-event-interface' | ||
19 | |||
20 | let RequestVideoEvent: Sequelize.Model<RequestVideoEventInstance, RequestVideoEventAttributes> | ||
21 | let countTotalRequests: RequestVideoEventMethods.CountTotalRequests | ||
22 | let listWithLimitAndRandom: RequestVideoEventMethods.ListWithLimitAndRandom | ||
23 | let removeByRequestIdsAndPod: RequestVideoEventMethods.RemoveByRequestIdsAndPod | ||
24 | let removeAll: RequestVideoEventMethods.RemoveAll | ||
25 | |||
26 | export default function (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.DataTypes) { | ||
27 | RequestVideoEvent = sequelize.define<RequestVideoEventInstance, RequestVideoEventAttributes>('RequestVideoEvent', | ||
28 | { | ||
29 | type: { | ||
30 | type: DataTypes.ENUM(values(REQUEST_VIDEO_EVENT_TYPES)), | ||
31 | allowNull: false | ||
32 | }, | ||
33 | count: { | ||
34 | type: DataTypes.INTEGER, | ||
35 | allowNull: false, | ||
36 | validate: { | ||
37 | countValid: function (value) { | ||
38 | const res = isVideoEventCountValid(value) | ||
39 | if (res === false) throw new Error('Video event count is not valid.') | ||
40 | } | ||
41 | } | ||
42 | } | ||
43 | }, | ||
44 | { | ||
45 | updatedAt: false, | ||
46 | indexes: [ | ||
47 | { | ||
48 | fields: [ 'videoId' ] | ||
49 | } | ||
50 | ] | ||
51 | } | ||
52 | ) | ||
53 | |||
54 | const classMethods = [ | ||
55 | associate, | ||
56 | |||
57 | listWithLimitAndRandom, | ||
58 | countTotalRequests, | ||
59 | removeAll, | ||
60 | removeByRequestIdsAndPod | ||
61 | ] | ||
62 | addMethodsToModel(RequestVideoEvent, classMethods) | ||
63 | |||
64 | return RequestVideoEvent | ||
65 | } | ||
66 | |||
67 | // ------------------------------ STATICS ------------------------------ | ||
68 | |||
69 | function associate (models) { | ||
70 | RequestVideoEvent.belongsTo(models.Video, { | ||
71 | foreignKey: { | ||
72 | name: 'videoId', | ||
73 | allowNull: false | ||
74 | }, | ||
75 | onDelete: 'CASCADE' | ||
76 | }) | ||
77 | } | ||
78 | |||
79 | countTotalRequests = function () { | ||
80 | const query = {} | ||
81 | return RequestVideoEvent.count(query) | ||
82 | } | ||
83 | |||
84 | listWithLimitAndRandom = function (limitPods: number, limitRequestsPerPod: number) { | ||
85 | const Pod = db.Pod | ||
86 | |||
87 | // We make a join between videos and authors to find the podId of our video event requests | ||
88 | const podJoins = 'INNER JOIN "VideoChannels" ON "VideoChannels"."authorId" = "Authors"."id" ' + | ||
89 | 'INNER JOIN "Videos" ON "Videos"."channelId" = "VideoChannels"."id" ' + | ||
90 | 'INNER JOIN "RequestVideoEvents" ON "RequestVideoEvents"."videoId" = "Videos"."id"' | ||
91 | |||
92 | return Pod.listRandomPodIdsWithRequest(limitPods, 'Authors', podJoins).then(podIds => { | ||
93 | // We don't have friends that have requests | ||
94 | if (podIds.length === 0) return [] | ||
95 | |||
96 | const query = { | ||
97 | order: [ | ||
98 | [ 'id', 'ASC' ] | ||
99 | ], | ||
100 | include: [ | ||
101 | { | ||
102 | model: RequestVideoEvent['sequelize'].models.Video, | ||
103 | include: [ | ||
104 | { | ||
105 | model: RequestVideoEvent['sequelize'].models.VideoChannel, | ||
106 | include: [ | ||
107 | { | ||
108 | model: RequestVideoEvent['sequelize'].models.Author, | ||
109 | include: [ | ||
110 | { | ||
111 | model: RequestVideoEvent['sequelize'].models.Pod, | ||
112 | where: { | ||
113 | id: { | ||
114 | [Sequelize.Op.in]: podIds | ||
115 | } | ||
116 | } | ||
117 | } | ||
118 | ] | ||
119 | } | ||
120 | ] | ||
121 | } | ||
122 | ] | ||
123 | } | ||
124 | ] | ||
125 | } | ||
126 | |||
127 | return RequestVideoEvent.findAll(query).then(requests => { | ||
128 | const requestsGrouped = groupAndTruncateRequests(requests, limitRequestsPerPod) | ||
129 | return requestsGrouped | ||
130 | }) | ||
131 | }) | ||
132 | } | ||
133 | |||
134 | removeByRequestIdsAndPod = function (ids: number[], podId: number) { | ||
135 | const query = { | ||
136 | where: { | ||
137 | id: { | ||
138 | [Sequelize.Op.in]: ids | ||
139 | } | ||
140 | }, | ||
141 | include: [ | ||
142 | { | ||
143 | model: RequestVideoEvent['sequelize'].models.Video, | ||
144 | include: [ | ||
145 | { | ||
146 | model: RequestVideoEvent['sequelize'].models.VideoChannel, | ||
147 | include: [ | ||
148 | { | ||
149 | model: RequestVideoEvent['sequelize'].models.Author, | ||
150 | where: { | ||
151 | podId | ||
152 | } | ||
153 | } | ||
154 | ] | ||
155 | } | ||
156 | ] | ||
157 | } | ||
158 | ] | ||
159 | } | ||
160 | |||
161 | return RequestVideoEvent.destroy(query) | ||
162 | } | ||
163 | |||
164 | removeAll = function () { | ||
165 | // Delete all requests | ||
166 | return RequestVideoEvent.truncate({ cascade: true }) | ||
167 | } | ||
168 | |||
169 | // --------------------------------------------------------------------------- | ||
170 | |||
171 | function groupAndTruncateRequests (events: RequestVideoEventInstance[], limitRequestsPerPod: number) { | ||
172 | const eventsGrouped: RequestsVideoEventGrouped = {} | ||
173 | |||
174 | events.forEach(event => { | ||
175 | const pod = event.Video.VideoChannel.Author.Pod | ||
176 | |||
177 | if (!eventsGrouped[pod.id]) eventsGrouped[pod.id] = [] | ||
178 | |||
179 | if (eventsGrouped[pod.id].length < limitRequestsPerPod) { | ||
180 | eventsGrouped[pod.id].push({ | ||
181 | id: event.id, | ||
182 | type: event.type, | ||
183 | count: event.count, | ||
184 | video: event.Video, | ||
185 | pod | ||
186 | }) | ||
187 | } | ||
188 | }) | ||
189 | |||
190 | return eventsGrouped | ||
191 | } | ||
diff --git a/server/models/request/request-video-qadu-interface.ts b/server/models/request/request-video-qadu-interface.ts deleted file mode 100644 index b7b7b1ecc..000000000 --- a/server/models/request/request-video-qadu-interface.ts +++ /dev/null | |||
@@ -1,48 +0,0 @@ | |||
1 | import * as Sequelize from 'sequelize' | ||
2 | import * as Promise from 'bluebird' | ||
3 | |||
4 | import { AbstractRequestClass, AbstractRequestToPodClass } from './abstract-request-interface' | ||
5 | import { VideoInstance } from '../video/video-interface' | ||
6 | import { PodInstance } from '../pod/pod-interface' | ||
7 | |||
8 | import { RequestVideoQaduType } from '../../../shared/models/request-scheduler.model' | ||
9 | |||
10 | export type RequestsVideoQaduGrouped = { | ||
11 | [ podId: number ]: { | ||
12 | request: RequestVideoQaduInstance | ||
13 | video: VideoInstance | ||
14 | pod: PodInstance | ||
15 | } | ||
16 | } | ||
17 | |||
18 | export namespace RequestVideoQaduMethods { | ||
19 | export type CountTotalRequests = () => Promise<number> | ||
20 | |||
21 | export type ListWithLimitAndRandom = (limitPods: number, limitRequestsPerPod: number) => Promise<RequestsVideoQaduGrouped> | ||
22 | |||
23 | export type RemoveByRequestIdsAndPod = (ids: number[], podId: number) => Promise<number> | ||
24 | |||
25 | export type RemoveAll = () => Promise<void> | ||
26 | } | ||
27 | |||
28 | export interface RequestVideoQaduClass extends AbstractRequestClass<RequestsVideoQaduGrouped>, AbstractRequestToPodClass { | ||
29 | countTotalRequests: RequestVideoQaduMethods.CountTotalRequests | ||
30 | listWithLimitAndRandom: RequestVideoQaduMethods.ListWithLimitAndRandom | ||
31 | removeByRequestIdsAndPod: RequestVideoQaduMethods.RemoveByRequestIdsAndPod | ||
32 | removeAll: RequestVideoQaduMethods.RemoveAll | ||
33 | } | ||
34 | |||
35 | export interface RequestVideoQaduAttributes { | ||
36 | type: RequestVideoQaduType | ||
37 | } | ||
38 | |||
39 | export interface RequestVideoQaduInstance | ||
40 | extends RequestVideoQaduClass, RequestVideoQaduAttributes, Sequelize.Instance<RequestVideoQaduAttributes> { | ||
41 | id: number | ||
42 | |||
43 | Pod: PodInstance | ||
44 | Video: VideoInstance | ||
45 | } | ||
46 | |||
47 | export interface RequestVideoQaduModel | ||
48 | extends RequestVideoQaduClass, Sequelize.Model<RequestVideoQaduInstance, RequestVideoQaduAttributes> {} | ||
diff --git a/server/models/request/request-video-qadu.ts b/server/models/request/request-video-qadu.ts deleted file mode 100644 index 93cf249b3..000000000 --- a/server/models/request/request-video-qadu.ts +++ /dev/null | |||
@@ -1,159 +0,0 @@ | |||
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 | [Sequelize.Op.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 | [Sequelize.Op.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(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 | } | ||
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 | } | ||
diff --git a/server/models/video/index.ts b/server/models/video/index.ts index dba6a5590..20d97931f 100644 --- a/server/models/video/index.ts +++ b/server/models/video/index.ts | |||
@@ -1,4 +1,3 @@ | |||
1 | export * from './author-interface' | ||
2 | export * from './tag-interface' | 1 | export * from './tag-interface' |
3 | export * from './video-abuse-interface' | 2 | export * from './video-abuse-interface' |
4 | export * from './video-blacklist-interface' | 3 | export * from './video-blacklist-interface' |