]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/models/request/request-video-event.ts
Move to promises
[github/Chocobozzz/PeerTube.git] / server / models / request / 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
74889a71
C
8import { database as db } from '../../initializers/database'
9import { REQUEST_VIDEO_EVENT_TYPES } from '../../initializers'
10import { isVideoEventCountValid } from '../../helpers'
11import { addMethodsToModel } from '../utils'
e02643f3 12import {
e02643f3
C
13 RequestVideoEventInstance,
14 RequestVideoEventAttributes,
15
69818c93
C
16 RequestVideoEventMethods,
17 RequestsVideoEventGrouped
e02643f3
C
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
e4c87ec2 25
127944aa
C
26export default function (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.DataTypes) {
27 RequestVideoEvent = sequelize.define<RequestVideoEventInstance, RequestVideoEventAttributes>('RequestVideoEvent',
e4c87ec2
C
28 {
29 type: {
65fcc311 30 type: DataTypes.ENUM(values(REQUEST_VIDEO_EVENT_TYPES)),
e4c87ec2
C
31 allowNull: false
32 },
33 count: {
34 type: DataTypes.INTEGER,
35 allowNull: false,
36 validate: {
37 countValid: function (value) {
65fcc311 38 const res = isVideoEventCountValid(value)
e4c87ec2
C
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 }
e02643f3 50 ]
e4c87ec2
C
51 }
52 )
53
e02643f3
C
54 const classMethods = [
55 associate,
56
57 listWithLimitAndRandom,
58 countTotalRequests,
59 removeAll,
60 removeByRequestIdsAndPod
61 ]
62 addMethodsToModel(RequestVideoEvent, classMethods)
63
e4c87ec2
C
64 return RequestVideoEvent
65}
66
67// ------------------------------ STATICS ------------------------------
68
69function associate (models) {
e02643f3 70 RequestVideoEvent.belongsTo(models.Video, {
e4c87ec2
C
71 foreignKey: {
72 name: 'videoId',
73 allowNull: false
74 },
75 onDelete: 'CASCADE'
76 })
77}
78
6fcd19ba 79countTotalRequests = function () {
e4c87ec2 80 const query = {}
6fcd19ba 81 return RequestVideoEvent.count(query)
e4c87ec2
C
82}
83
6fcd19ba 84listWithLimitAndRandom = function (limitPods: number, limitRequestsPerPod: number) {
69818c93 85 const Pod = db.Pod
e4c87ec2
C
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
6fcd19ba 91 return Pod.listRandomPodIdsWithRequest(limitPods, 'Authors', podJoins).then(podIds => {
e4c87ec2 92 // We don't have friends that have requests
6fcd19ba 93 if (podIds.length === 0) return []
e4c87ec2
C
94
95 const query = {
d38b8281
C
96 order: [
97 [ 'id', 'ASC' ]
98 ],
e4c87ec2
C
99 include: [
100 {
e02643f3 101 model: RequestVideoEvent['sequelize'].models.Video,
e4c87ec2
C
102 include: [
103 {
e02643f3 104 model: RequestVideoEvent['sequelize'].models.Author,
e4c87ec2
C
105 include: [
106 {
e02643f3 107 model: RequestVideoEvent['sequelize'].models.Pod,
e4c87ec2
C
108 where: {
109 id: {
110 $in: podIds
111 }
112 }
113 }
114 ]
115 }
116 ]
117 }
118 ]
119 }
120
6fcd19ba 121 return RequestVideoEvent.findAll(query).then(requests => {
e4c87ec2 122 const requestsGrouped = groupAndTruncateRequests(requests, limitRequestsPerPod)
6fcd19ba 123 return requestsGrouped
e4c87ec2
C
124 })
125 })
126}
127
6fcd19ba 128removeByRequestIdsAndPod = function (ids: number[], podId: number) {
e4c87ec2
C
129 const query = {
130 where: {
131 id: {
132 $in: ids
133 }
134 },
135 include: [
136 {
e02643f3 137 model: RequestVideoEvent['sequelize'].models.Video,
e4c87ec2
C
138 include: [
139 {
e02643f3 140 model: RequestVideoEvent['sequelize'].models.Author,
e4c87ec2
C
141 where: {
142 podId
143 }
144 }
145 ]
146 }
147 ]
148 }
149
6fcd19ba 150 return RequestVideoEvent.destroy(query)
e4c87ec2
C
151}
152
6fcd19ba 153removeAll = function () {
e4c87ec2 154 // Delete all requests
6fcd19ba 155 return RequestVideoEvent.truncate({ cascade: true })
e4c87ec2
C
156}
157
158// ---------------------------------------------------------------------------
159
69818c93
C
160function groupAndTruncateRequests (events: RequestVideoEventInstance[], limitRequestsPerPod: number) {
161 const eventsGrouped: RequestsVideoEventGrouped = {}
e4c87ec2
C
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}