diff options
author | Chocobozzz <florian.bigard@gmail.com> | 2017-05-22 20:58:25 +0200 |
---|---|---|
committer | Chocobozzz <florian.bigard@gmail.com> | 2017-05-25 17:32:16 +0200 |
commit | e02643f32e4c97ca307f8fc5b69be79c40d70a3b (patch) | |
tree | b7f6269913cd5a0e4f26a9461a043deb0c168be0 /server/models/request-video-event.ts | |
parent | 65fcc3119c334b75dd13bcfdebf186afdc580a8f (diff) | |
download | PeerTube-e02643f32e4c97ca307f8fc5b69be79c40d70a3b.tar.gz PeerTube-e02643f32e4c97ca307f8fc5b69be79c40d70a3b.tar.zst PeerTube-e02643f32e4c97ca307f8fc5b69be79c40d70a3b.zip |
Type models
Diffstat (limited to 'server/models/request-video-event.ts')
-rw-r--r-- | server/models/request-video-event.ts | 72 |
1 files changed, 43 insertions, 29 deletions
diff --git a/server/models/request-video-event.ts b/server/models/request-video-event.ts index c61525029..234e2a8a9 100644 --- a/server/models/request-video-event.ts +++ b/server/models/request-video-event.ts | |||
@@ -3,14 +3,28 @@ | |||
3 | */ | 3 | */ |
4 | 4 | ||
5 | import { values } from 'lodash' | 5 | import { values } from 'lodash' |
6 | import * as Sequelize from 'sequelize' | ||
6 | 7 | ||
7 | import { REQUEST_VIDEO_EVENT_TYPES } from '../initializers' | 8 | import { REQUEST_VIDEO_EVENT_TYPES } from '../initializers' |
8 | import { isVideoEventCountValid } from '../helpers' | 9 | import { isVideoEventCountValid } from '../helpers' |
9 | 10 | ||
10 | // --------------------------------------------------------------------------- | 11 | import { addMethodsToModel } from './utils' |
12 | import { | ||
13 | RequestVideoEventClass, | ||
14 | RequestVideoEventInstance, | ||
15 | RequestVideoEventAttributes, | ||
16 | |||
17 | RequestVideoEventMethods | ||
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 | ||
11 | 25 | ||
12 | module.exports = function (sequelize, DataTypes) { | 26 | export default function (sequelize, DataTypes) { |
13 | const RequestVideoEvent = sequelize.define('RequestVideoEvent', | 27 | RequestVideoEvent = sequelize.define('RequestVideoEvent', |
14 | { | 28 | { |
15 | type: { | 29 | type: { |
16 | type: DataTypes.ENUM(values(REQUEST_VIDEO_EVENT_TYPES)), | 30 | type: DataTypes.ENUM(values(REQUEST_VIDEO_EVENT_TYPES)), |
@@ -33,26 +47,27 @@ module.exports = function (sequelize, DataTypes) { | |||
33 | { | 47 | { |
34 | fields: [ 'videoId' ] | 48 | fields: [ 'videoId' ] |
35 | } | 49 | } |
36 | ], | 50 | ] |
37 | classMethods: { | ||
38 | associate, | ||
39 | |||
40 | listWithLimitAndRandom, | ||
41 | |||
42 | countTotalRequests, | ||
43 | removeAll, | ||
44 | removeByRequestIdsAndPod | ||
45 | } | ||
46 | } | 51 | } |
47 | ) | 52 | ) |
48 | 53 | ||
54 | const classMethods = [ | ||
55 | associate, | ||
56 | |||
57 | listWithLimitAndRandom, | ||
58 | countTotalRequests, | ||
59 | removeAll, | ||
60 | removeByRequestIdsAndPod | ||
61 | ] | ||
62 | addMethodsToModel(RequestVideoEvent, classMethods) | ||
63 | |||
49 | return RequestVideoEvent | 64 | return RequestVideoEvent |
50 | } | 65 | } |
51 | 66 | ||
52 | // ------------------------------ STATICS ------------------------------ | 67 | // ------------------------------ STATICS ------------------------------ |
53 | 68 | ||
54 | function associate (models) { | 69 | function associate (models) { |
55 | this.belongsTo(models.Video, { | 70 | RequestVideoEvent.belongsTo(models.Video, { |
56 | foreignKey: { | 71 | foreignKey: { |
57 | name: 'videoId', | 72 | name: 'videoId', |
58 | allowNull: false | 73 | allowNull: false |
@@ -61,14 +76,13 @@ function associate (models) { | |||
61 | }) | 76 | }) |
62 | } | 77 | } |
63 | 78 | ||
64 | function countTotalRequests (callback) { | 79 | countTotalRequests = function (callback) { |
65 | const query = {} | 80 | const query = {} |
66 | return this.count(query).asCallback(callback) | 81 | return RequestVideoEvent.count(query).asCallback(callback) |
67 | } | 82 | } |
68 | 83 | ||
69 | function listWithLimitAndRandom (limitPods, limitRequestsPerPod, callback) { | 84 | listWithLimitAndRandom = function (limitPods, limitRequestsPerPod, callback) { |
70 | const self = this | 85 | const Pod = RequestVideoEvent['sequelize'].models.Pod |
71 | const Pod = this.sequelize.models.Pod | ||
72 | 86 | ||
73 | // We make a join between videos and authors to find the podId of our video event requests | 87 | // We make a join between videos and authors to find the podId of our video event requests |
74 | const podJoins = 'INNER JOIN "Videos" ON "Videos"."authorId" = "Authors"."id" ' + | 88 | const podJoins = 'INNER JOIN "Videos" ON "Videos"."authorId" = "Authors"."id" ' + |
@@ -86,13 +100,13 @@ function listWithLimitAndRandom (limitPods, limitRequestsPerPod, callback) { | |||
86 | ], | 100 | ], |
87 | include: [ | 101 | include: [ |
88 | { | 102 | { |
89 | model: self.sequelize.models.Video, | 103 | model: RequestVideoEvent['sequelize'].models.Video, |
90 | include: [ | 104 | include: [ |
91 | { | 105 | { |
92 | model: self.sequelize.models.Author, | 106 | model: RequestVideoEvent['sequelize'].models.Author, |
93 | include: [ | 107 | include: [ |
94 | { | 108 | { |
95 | model: self.sequelize.models.Pod, | 109 | model: RequestVideoEvent['sequelize'].models.Pod, |
96 | where: { | 110 | where: { |
97 | id: { | 111 | id: { |
98 | $in: podIds | 112 | $in: podIds |
@@ -106,7 +120,7 @@ function listWithLimitAndRandom (limitPods, limitRequestsPerPod, callback) { | |||
106 | ] | 120 | ] |
107 | } | 121 | } |
108 | 122 | ||
109 | self.findAll(query).asCallback(function (err, requests) { | 123 | RequestVideoEvent.findAll(query).asCallback(function (err, requests) { |
110 | if (err) return callback(err) | 124 | if (err) return callback(err) |
111 | 125 | ||
112 | const requestsGrouped = groupAndTruncateRequests(requests, limitRequestsPerPod) | 126 | const requestsGrouped = groupAndTruncateRequests(requests, limitRequestsPerPod) |
@@ -115,7 +129,7 @@ function listWithLimitAndRandom (limitPods, limitRequestsPerPod, callback) { | |||
115 | }) | 129 | }) |
116 | } | 130 | } |
117 | 131 | ||
118 | function removeByRequestIdsAndPod (ids, podId, callback) { | 132 | removeByRequestIdsAndPod = function (ids, podId, callback) { |
119 | const query = { | 133 | const query = { |
120 | where: { | 134 | where: { |
121 | id: { | 135 | id: { |
@@ -124,10 +138,10 @@ function removeByRequestIdsAndPod (ids, podId, callback) { | |||
124 | }, | 138 | }, |
125 | include: [ | 139 | include: [ |
126 | { | 140 | { |
127 | model: this.sequelize.models.Video, | 141 | model: RequestVideoEvent['sequelize'].models.Video, |
128 | include: [ | 142 | include: [ |
129 | { | 143 | { |
130 | model: this.sequelize.models.Author, | 144 | model: RequestVideoEvent['sequelize'].models.Author, |
131 | where: { | 145 | where: { |
132 | podId | 146 | podId |
133 | } | 147 | } |
@@ -137,12 +151,12 @@ function removeByRequestIdsAndPod (ids, podId, callback) { | |||
137 | ] | 151 | ] |
138 | } | 152 | } |
139 | 153 | ||
140 | this.destroy(query).asCallback(callback) | 154 | RequestVideoEvent.destroy(query).asCallback(callback) |
141 | } | 155 | } |
142 | 156 | ||
143 | function removeAll (callback) { | 157 | removeAll = function (callback) { |
144 | // Delete all requests | 158 | // Delete all requests |
145 | this.truncate({ cascade: true }).asCallback(callback) | 159 | RequestVideoEvent.truncate({ cascade: true }).asCallback(callback) |
146 | } | 160 | } |
147 | 161 | ||
148 | // --------------------------------------------------------------------------- | 162 | // --------------------------------------------------------------------------- |