]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/models/request-video-event.ts
Better models define typing
[github/Chocobozzz/PeerTube.git] / server / models / request-video-event.ts
CommitLineData
e4c87ec2
C
1/*
2 Request Video events (likes, dislikes, views...)
3*/
4
65fcc311 5import { values } from 'lodash'
e02643f3 6import * as Sequelize from 'sequelize'
e4c87ec2 7
69818c93 8import { database as db } from '../initializers/database'
65fcc311
C
9import { REQUEST_VIDEO_EVENT_TYPES } from '../initializers'
10import { isVideoEventCountValid } from '../helpers'
e02643f3
C
11import { addMethodsToModel } from './utils'
12import {
13 RequestVideoEventClass,
14 RequestVideoEventInstance,
15 RequestVideoEventAttributes,
16
69818c93
C
17 RequestVideoEventMethods,
18 RequestsVideoEventGrouped
e02643f3
C
19} from './request-video-event-interface'
20
21let RequestVideoEvent: Sequelize.Model<RequestVideoEventInstance, RequestVideoEventAttributes>
22let countTotalRequests: RequestVideoEventMethods.CountTotalRequests
23let listWithLimitAndRandom: RequestVideoEventMethods.ListWithLimitAndRandom
24let removeByRequestIdsAndPod: RequestVideoEventMethods.RemoveByRequestIdsAndPod
25let removeAll: RequestVideoEventMethods.RemoveAll
e4c87ec2 26
127944aa
C
27export default function (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.DataTypes) {
28 RequestVideoEvent = sequelize.define<RequestVideoEventInstance, RequestVideoEventAttributes>('RequestVideoEvent',
e4c87ec2
C
29 {
30 type: {
65fcc311 31 type: DataTypes.ENUM(values(REQUEST_VIDEO_EVENT_TYPES)),
e4c87ec2
C
32 allowNull: false
33 },
34 count: {
35 type: DataTypes.INTEGER,
36 allowNull: false,
37 validate: {
38 countValid: function (value) {
65fcc311 39 const res = isVideoEventCountValid(value)
e4c87ec2
C
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 }
e02643f3 51 ]
e4c87ec2
C
52 }
53 )
54
e02643f3
C
55 const classMethods = [
56 associate,
57
58 listWithLimitAndRandom,
59 countTotalRequests,
60 removeAll,
61 removeByRequestIdsAndPod
62 ]
63 addMethodsToModel(RequestVideoEvent, classMethods)
64
e4c87ec2
C
65 return RequestVideoEvent
66}
67
68// ------------------------------ STATICS ------------------------------
69
70function associate (models) {
e02643f3 71 RequestVideoEvent.belongsTo(models.Video, {
e4c87ec2
C
72 foreignKey: {
73 name: 'videoId',
74 allowNull: false
75 },
76 onDelete: 'CASCADE'
77 })
78}
79
69818c93 80countTotalRequests = function (callback: RequestVideoEventMethods.CountTotalRequestsCallback) {
e4c87ec2 81 const query = {}
e02643f3 82 return RequestVideoEvent.count(query).asCallback(callback)
e4c87ec2
C
83}
84
69818c93
C
85listWithLimitAndRandom = function (limitPods: number, limitRequestsPerPod: number, callback: RequestVideoEventMethods.ListWithLimitAndRandomCallback) {
86 const Pod = db.Pod
e4c87ec2
C
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 = {
d38b8281
C
99 order: [
100 [ 'id', 'ASC' ]
101 ],
e4c87ec2
C
102 include: [
103 {
e02643f3 104 model: RequestVideoEvent['sequelize'].models.Video,
e4c87ec2
C
105 include: [
106 {
e02643f3 107 model: RequestVideoEvent['sequelize'].models.Author,
e4c87ec2
C
108 include: [
109 {
e02643f3 110 model: RequestVideoEvent['sequelize'].models.Pod,
e4c87ec2
C
111 where: {
112 id: {
113 $in: podIds
114 }
115 }
116 }
117 ]
118 }
119 ]
120 }
121 ]
122 }
123
e02643f3 124 RequestVideoEvent.findAll(query).asCallback(function (err, requests) {
e4c87ec2
C
125 if (err) return callback(err)
126
127 const requestsGrouped = groupAndTruncateRequests(requests, limitRequestsPerPod)
128 return callback(err, requestsGrouped)
129 })
130 })
131}
132
69818c93 133removeByRequestIdsAndPod = function (ids: number[], podId: number, callback: RequestVideoEventMethods.RemoveByRequestIdsAndPodCallback) {
e4c87ec2
C
134 const query = {
135 where: {
136 id: {
137 $in: ids
138 }
139 },
140 include: [
141 {
e02643f3 142 model: RequestVideoEvent['sequelize'].models.Video,
e4c87ec2
C
143 include: [
144 {
e02643f3 145 model: RequestVideoEvent['sequelize'].models.Author,
e4c87ec2
C
146 where: {
147 podId
148 }
149 }
150 ]
151 }
152 ]
153 }
154
e02643f3 155 RequestVideoEvent.destroy(query).asCallback(callback)
e4c87ec2
C
156}
157
69818c93 158removeAll = function (callback: RequestVideoEventMethods.RemoveAllCallback) {
e4c87ec2 159 // Delete all requests
e02643f3 160 RequestVideoEvent.truncate({ cascade: true }).asCallback(callback)
e4c87ec2
C
161}
162
163// ---------------------------------------------------------------------------
164
69818c93
C
165function groupAndTruncateRequests (events: RequestVideoEventInstance[], limitRequestsPerPod: number) {
166 const eventsGrouped: RequestsVideoEventGrouped = {}
e4c87ec2
C
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}