diff options
author | Chocobozzz <florian.bigard@gmail.com> | 2017-06-16 09:45:46 +0200 |
---|---|---|
committer | Chocobozzz <florian.bigard@gmail.com> | 2017-06-16 09:45:46 +0200 |
commit | 74889a71fe687dda74f2a687653122327807af36 (patch) | |
tree | e938e8b6401b74fbec80513a877d9967f2c0dbcd /server/models/request/request-video-event.ts | |
parent | 15a302943d84bc0978b84fe33110c4daa451d311 (diff) | |
download | PeerTube-74889a71fe687dda74f2a687653122327807af36.tar.gz PeerTube-74889a71fe687dda74f2a687653122327807af36.tar.zst PeerTube-74889a71fe687dda74f2a687653122327807af36.zip |
Reorganize model files
Diffstat (limited to 'server/models/request/request-video-event.ts')
-rw-r--r-- | server/models/request/request-video-event.ts | 185 |
1 files changed, 185 insertions, 0 deletions
diff --git a/server/models/request/request-video-event.ts b/server/models/request/request-video-event.ts new file mode 100644 index 000000000..f552ef50b --- /dev/null +++ b/server/models/request/request-video-event.ts | |||
@@ -0,0 +1,185 @@ | |||
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 | RequestVideoEventClass, | ||
14 | RequestVideoEventInstance, | ||
15 | RequestVideoEventAttributes, | ||
16 | |||
17 | RequestVideoEventMethods, | ||
18 | RequestsVideoEventGrouped | ||
19 | } from './request-video-event-interface' | ||
20 | |||
21 | let RequestVideoEvent: Sequelize.Model<RequestVideoEventInstance, RequestVideoEventAttributes> | ||
22 | let countTotalRequests: RequestVideoEventMethods.CountTotalRequests | ||
23 | let listWithLimitAndRandom: RequestVideoEventMethods.ListWithLimitAndRandom | ||
24 | let removeByRequestIdsAndPod: RequestVideoEventMethods.RemoveByRequestIdsAndPod | ||
25 | let removeAll: RequestVideoEventMethods.RemoveAll | ||
26 | |||
27 | export default function (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.DataTypes) { | ||
28 | RequestVideoEvent = sequelize.define<RequestVideoEventInstance, RequestVideoEventAttributes>('RequestVideoEvent', | ||
29 | { | ||
30 | type: { | ||
31 | type: DataTypes.ENUM(values(REQUEST_VIDEO_EVENT_TYPES)), | ||
32 | allowNull: false | ||
33 | }, | ||
34 | count: { | ||
35 | type: DataTypes.INTEGER, | ||
36 | allowNull: false, | ||
37 | validate: { | ||
38 | countValid: function (value) { | ||
39 | const res = isVideoEventCountValid(value) | ||
40 | if (res === false) throw new Error('Video event count is not valid.') | ||
41 | } | ||
42 | } | ||
43 | } | ||
44 | }, | ||
45 | { | ||
46 | updatedAt: false, | ||
47 | indexes: [ | ||
48 | { | ||
49 | fields: [ 'videoId' ] | ||
50 | } | ||
51 | ] | ||
52 | } | ||
53 | ) | ||
54 | |||
55 | const classMethods = [ | ||
56 | associate, | ||
57 | |||
58 | listWithLimitAndRandom, | ||
59 | countTotalRequests, | ||
60 | removeAll, | ||
61 | removeByRequestIdsAndPod | ||
62 | ] | ||
63 | addMethodsToModel(RequestVideoEvent, classMethods) | ||
64 | |||
65 | return RequestVideoEvent | ||
66 | } | ||
67 | |||
68 | // ------------------------------ STATICS ------------------------------ | ||
69 | |||
70 | function associate (models) { | ||
71 | RequestVideoEvent.belongsTo(models.Video, { | ||
72 | foreignKey: { | ||
73 | name: 'videoId', | ||
74 | allowNull: false | ||
75 | }, | ||
76 | onDelete: 'CASCADE' | ||
77 | }) | ||
78 | } | ||
79 | |||
80 | countTotalRequests = function (callback: RequestVideoEventMethods.CountTotalRequestsCallback) { | ||
81 | const query = {} | ||
82 | return RequestVideoEvent.count(query).asCallback(callback) | ||
83 | } | ||
84 | |||
85 | listWithLimitAndRandom = function (limitPods: number, limitRequestsPerPod: number, callback: RequestVideoEventMethods.ListWithLimitAndRandomCallback) { | ||
86 | const Pod = db.Pod | ||
87 | |||
88 | // We make a join between videos and authors to find the podId of our video event requests | ||
89 | const podJoins = 'INNER JOIN "Videos" ON "Videos"."authorId" = "Authors"."id" ' + | ||
90 | 'INNER JOIN "RequestVideoEvents" ON "RequestVideoEvents"."videoId" = "Videos"."id"' | ||
91 | |||
92 | Pod.listRandomPodIdsWithRequest(limitPods, 'Authors', podJoins, function (err, podIds) { | ||
93 | if (err) return callback(err) | ||
94 | |||
95 | // We don't have friends that have requests | ||
96 | if (podIds.length === 0) return callback(null, []) | ||
97 | |||
98 | const query = { | ||
99 | order: [ | ||
100 | [ 'id', 'ASC' ] | ||
101 | ], | ||
102 | include: [ | ||
103 | { | ||
104 | model: RequestVideoEvent['sequelize'].models.Video, | ||
105 | include: [ | ||
106 | { | ||
107 | model: RequestVideoEvent['sequelize'].models.Author, | ||
108 | include: [ | ||
109 | { | ||
110 | model: RequestVideoEvent['sequelize'].models.Pod, | ||
111 | where: { | ||
112 | id: { | ||
113 | $in: podIds | ||
114 | } | ||
115 | } | ||
116 | } | ||
117 | ] | ||
118 | } | ||
119 | ] | ||
120 | } | ||
121 | ] | ||
122 | } | ||
123 | |||
124 | RequestVideoEvent.findAll(query).asCallback(function (err, requests) { | ||
125 | if (err) return callback(err) | ||
126 | |||
127 | const requestsGrouped = groupAndTruncateRequests(requests, limitRequestsPerPod) | ||
128 | return callback(err, requestsGrouped) | ||
129 | }) | ||
130 | }) | ||
131 | } | ||
132 | |||
133 | removeByRequestIdsAndPod = function (ids: number[], podId: number, callback: RequestVideoEventMethods.RemoveByRequestIdsAndPodCallback) { | ||
134 | const query = { | ||
135 | where: { | ||
136 | id: { | ||
137 | $in: ids | ||
138 | } | ||
139 | }, | ||
140 | include: [ | ||
141 | { | ||
142 | model: RequestVideoEvent['sequelize'].models.Video, | ||
143 | include: [ | ||
144 | { | ||
145 | model: RequestVideoEvent['sequelize'].models.Author, | ||
146 | where: { | ||
147 | podId | ||
148 | } | ||
149 | } | ||
150 | ] | ||
151 | } | ||
152 | ] | ||
153 | } | ||
154 | |||
155 | RequestVideoEvent.destroy(query).asCallback(callback) | ||
156 | } | ||
157 | |||
158 | removeAll = function (callback: RequestVideoEventMethods.RemoveAllCallback) { | ||
159 | // Delete all requests | ||
160 | RequestVideoEvent.truncate({ cascade: true }).asCallback(callback) | ||
161 | } | ||
162 | |||
163 | // --------------------------------------------------------------------------- | ||
164 | |||
165 | function groupAndTruncateRequests (events: RequestVideoEventInstance[], limitRequestsPerPod: number) { | ||
166 | const eventsGrouped: RequestsVideoEventGrouped = {} | ||
167 | |||
168 | events.forEach(function (event) { | ||
169 | const pod = event.Video.Author.Pod | ||
170 | |||
171 | if (!eventsGrouped[pod.id]) eventsGrouped[pod.id] = [] | ||
172 | |||
173 | if (eventsGrouped[pod.id].length < limitRequestsPerPod) { | ||
174 | eventsGrouped[pod.id].push({ | ||
175 | id: event.id, | ||
176 | type: event.type, | ||
177 | count: event.count, | ||
178 | video: event.Video, | ||
179 | pod | ||
180 | }) | ||
181 | } | ||
182 | }) | ||
183 | |||
184 | return eventsGrouped | ||
185 | } | ||