]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/models/request/request-video-event.ts
Add video channels
[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
72c7248b
C
88 const podJoins = 'INNER JOIN "VideoChannels" ON "VideoChannels"."authorId" = "Authors"."id" ' +
89 'INNER JOIN "Videos" ON "Videos"."channelId" = "VideoChannels"."id" ' +
e4c87ec2
C
90 'INNER JOIN "RequestVideoEvents" ON "RequestVideoEvents"."videoId" = "Videos"."id"'
91
6fcd19ba 92 return Pod.listRandomPodIdsWithRequest(limitPods, 'Authors', podJoins).then(podIds => {
e4c87ec2 93 // We don't have friends that have requests
6fcd19ba 94 if (podIds.length === 0) return []
e4c87ec2
C
95
96 const query = {
d38b8281
C
97 order: [
98 [ 'id', 'ASC' ]
99 ],
e4c87ec2
C
100 include: [
101 {
e02643f3 102 model: RequestVideoEvent['sequelize'].models.Video,
e4c87ec2
C
103 include: [
104 {
e02643f3 105 model: RequestVideoEvent['sequelize'].models.Author,
e4c87ec2
C
106 include: [
107 {
e02643f3 108 model: RequestVideoEvent['sequelize'].models.Pod,
e4c87ec2
C
109 where: {
110 id: {
111 $in: podIds
112 }
113 }
114 }
115 ]
116 }
117 ]
118 }
119 ]
120 }
121
6fcd19ba 122 return RequestVideoEvent.findAll(query).then(requests => {
e4c87ec2 123 const requestsGrouped = groupAndTruncateRequests(requests, limitRequestsPerPod)
6fcd19ba 124 return requestsGrouped
e4c87ec2
C
125 })
126 })
127}
128
6fcd19ba 129removeByRequestIdsAndPod = function (ids: number[], podId: number) {
e4c87ec2
C
130 const query = {
131 where: {
132 id: {
133 $in: ids
134 }
135 },
136 include: [
137 {
e02643f3 138 model: RequestVideoEvent['sequelize'].models.Video,
e4c87ec2
C
139 include: [
140 {
e02643f3 141 model: RequestVideoEvent['sequelize'].models.Author,
e4c87ec2
C
142 where: {
143 podId
144 }
145 }
146 ]
147 }
148 ]
149 }
150
6fcd19ba 151 return RequestVideoEvent.destroy(query)
e4c87ec2
C
152}
153
6fcd19ba 154removeAll = function () {
e4c87ec2 155 // Delete all requests
6fcd19ba 156 return RequestVideoEvent.truncate({ cascade: true })
e4c87ec2
C
157}
158
159// ---------------------------------------------------------------------------
160
69818c93
C
161function groupAndTruncateRequests (events: RequestVideoEventInstance[], limitRequestsPerPod: number) {
162 const eventsGrouped: RequestsVideoEventGrouped = {}
e4c87ec2 163
075f16ca 164 events.forEach(event => {
72c7248b 165 const pod = event.Video.VideoChannel.Author.Pod
e4c87ec2
C
166
167 if (!eventsGrouped[pod.id]) eventsGrouped[pod.id] = []
168
169 if (eventsGrouped[pod.id].length < limitRequestsPerPod) {
170 eventsGrouped[pod.id].push({
171 id: event.id,
172 type: event.type,
173 count: event.count,
174 video: event.Video,
175 pod
176 })
177 }
178 })
179
180 return eventsGrouped
181}