]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame_incremental - server/models/request-video-event.ts
require -> import
[github/Chocobozzz/PeerTube.git] / server / models / 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 { REQUEST_VIDEO_EVENT_TYPES } from '../initializers'
9import { isVideoEventCountValid } from '../helpers'
10
11import { addMethodsToModel } from './utils'
12import {
13 RequestVideoEventClass,
14 RequestVideoEventInstance,
15 RequestVideoEventAttributes,
16
17 RequestVideoEventMethods
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, DataTypes) {
27 RequestVideoEvent = sequelize.define('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 (callback) {
80 const query = {}
81 return RequestVideoEvent.count(query).asCallback(callback)
82}
83
84listWithLimitAndRandom = function (limitPods, limitRequestsPerPod, callback) {
85 const Pod = RequestVideoEvent['sequelize'].models.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 Pod.listRandomPodIdsWithRequest(limitPods, 'Authors', podJoins, function (err, podIds) {
92 if (err) return callback(err)
93
94 // We don't have friends that have requests
95 if (podIds.length === 0) return callback(null, [])
96
97 const query = {
98 order: [
99 [ 'id', 'ASC' ]
100 ],
101 include: [
102 {
103 model: RequestVideoEvent['sequelize'].models.Video,
104 include: [
105 {
106 model: RequestVideoEvent['sequelize'].models.Author,
107 include: [
108 {
109 model: RequestVideoEvent['sequelize'].models.Pod,
110 where: {
111 id: {
112 $in: podIds
113 }
114 }
115 }
116 ]
117 }
118 ]
119 }
120 ]
121 }
122
123 RequestVideoEvent.findAll(query).asCallback(function (err, requests) {
124 if (err) return callback(err)
125
126 const requestsGrouped = groupAndTruncateRequests(requests, limitRequestsPerPod)
127 return callback(err, requestsGrouped)
128 })
129 })
130}
131
132removeByRequestIdsAndPod = function (ids, podId, callback) {
133 const query = {
134 where: {
135 id: {
136 $in: ids
137 }
138 },
139 include: [
140 {
141 model: RequestVideoEvent['sequelize'].models.Video,
142 include: [
143 {
144 model: RequestVideoEvent['sequelize'].models.Author,
145 where: {
146 podId
147 }
148 }
149 ]
150 }
151 ]
152 }
153
154 RequestVideoEvent.destroy(query).asCallback(callback)
155}
156
157removeAll = function (callback) {
158 // Delete all requests
159 RequestVideoEvent.truncate({ cascade: true }).asCallback(callback)
160}
161
162// ---------------------------------------------------------------------------
163
164function groupAndTruncateRequests (events, limitRequestsPerPod) {
165 const eventsGrouped = {}
166
167 events.forEach(function (event) {
168 const pod = event.Video.Author.Pod
169
170 if (!eventsGrouped[pod.id]) eventsGrouped[pod.id] = []
171
172 if (eventsGrouped[pod.id].length < limitRequestsPerPod) {
173 eventsGrouped[pod.id].push({
174 id: event.id,
175 type: event.type,
176 count: event.count,
177 video: event.Video,
178 pod
179 })
180 }
181 })
182
183 return eventsGrouped
184}