]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame_incremental - server/models/request/request-video-event.ts
Move to promises
[github/Chocobozzz/PeerTube.git] / server / models / request / request-video-event.ts
... / ...
CommitLineData
1/*
2 Request Video events (likes, dislikes, views...)
3*/
4
5import { values } from 'lodash'
6import * as Sequelize from 'sequelize'
7
8import { database as db } from '../../initializers/database'
9import { REQUEST_VIDEO_EVENT_TYPES } from '../../initializers'
10import { isVideoEventCountValid } from '../../helpers'
11import { addMethodsToModel } from '../utils'
12import {
13 RequestVideoEventInstance,
14 RequestVideoEventAttributes,
15
16 RequestVideoEventMethods,
17 RequestsVideoEventGrouped
18} from './request-video-event-interface'
19
20let RequestVideoEvent: Sequelize.Model<RequestVideoEventInstance, RequestVideoEventAttributes>
21let countTotalRequests: RequestVideoEventMethods.CountTotalRequests
22let listWithLimitAndRandom: RequestVideoEventMethods.ListWithLimitAndRandom
23let removeByRequestIdsAndPod: RequestVideoEventMethods.RemoveByRequestIdsAndPod
24let removeAll: RequestVideoEventMethods.RemoveAll
25
26export 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
69function associate (models) {
70 RequestVideoEvent.belongsTo(models.Video, {
71 foreignKey: {
72 name: 'videoId',
73 allowNull: false
74 },
75 onDelete: 'CASCADE'
76 })
77}
78
79countTotalRequests = function () {
80 const query = {}
81 return RequestVideoEvent.count(query)
82}
83
84listWithLimitAndRandom = 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 "Videos" ON "Videos"."authorId" = "Authors"."id" ' +
89 'INNER JOIN "RequestVideoEvents" ON "RequestVideoEvents"."videoId" = "Videos"."id"'
90
91 return Pod.listRandomPodIdsWithRequest(limitPods, 'Authors', podJoins).then(podIds => {
92 // We don't have friends that have requests
93 if (podIds.length === 0) return []
94
95 const query = {
96 order: [
97 [ 'id', 'ASC' ]
98 ],
99 include: [
100 {
101 model: RequestVideoEvent['sequelize'].models.Video,
102 include: [
103 {
104 model: RequestVideoEvent['sequelize'].models.Author,
105 include: [
106 {
107 model: RequestVideoEvent['sequelize'].models.Pod,
108 where: {
109 id: {
110 $in: podIds
111 }
112 }
113 }
114 ]
115 }
116 ]
117 }
118 ]
119 }
120
121 return RequestVideoEvent.findAll(query).then(requests => {
122 const requestsGrouped = groupAndTruncateRequests(requests, limitRequestsPerPod)
123 return requestsGrouped
124 })
125 })
126}
127
128removeByRequestIdsAndPod = function (ids: number[], podId: number) {
129 const query = {
130 where: {
131 id: {
132 $in: ids
133 }
134 },
135 include: [
136 {
137 model: RequestVideoEvent['sequelize'].models.Video,
138 include: [
139 {
140 model: RequestVideoEvent['sequelize'].models.Author,
141 where: {
142 podId
143 }
144 }
145 ]
146 }
147 ]
148 }
149
150 return RequestVideoEvent.destroy(query)
151}
152
153removeAll = function () {
154 // Delete all requests
155 return RequestVideoEvent.truncate({ cascade: true })
156}
157
158// ---------------------------------------------------------------------------
159
160function groupAndTruncateRequests (events: RequestVideoEventInstance[], limitRequestsPerPod: number) {
161 const eventsGrouped: RequestsVideoEventGrouped = {}
162
163 events.forEach(function (event) {
164 const pod = event.Video.Author.Pod
165
166 if (!eventsGrouped[pod.id]) eventsGrouped[pod.id] = []
167
168 if (eventsGrouped[pod.id].length < limitRequestsPerPod) {
169 eventsGrouped[pod.id].push({
170 id: event.id,
171 type: event.type,
172 count: event.count,
173 video: event.Video,
174 pod
175 })
176 }
177 })
178
179 return eventsGrouped
180}