]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/models/account/account-video-rate.ts
Cleaner warning of IP address leaking on embedded videos (#2034)
[github/Chocobozzz/PeerTube.git] / server / models / account / account-video-rate.ts
CommitLineData
65fcc311 1import { values } from 'lodash'
1735c825 2import { FindOptions, Op, Transaction } from 'sequelize'
5c6d985f 3import { AllowNull, BelongsTo, Column, CreatedAt, DataType, ForeignKey, Is, Model, Table, UpdatedAt } from 'sequelize-typescript'
3fd3ab2d 4import { VideoRateType } from '../../../shared/models/videos'
74dc3bca 5import { CONSTRAINTS_FIELDS, VIDEO_RATE_TYPES } from '../../initializers/constants'
3fd3ab2d
C
6import { VideoModel } from '../video/video'
7import { AccountModel } from './account'
8fffe21a 8import { ActorModel } from '../activitypub/actor'
6b9c966f 9import { buildLocalAccountIdsIn, getSort, throwIfNotValid } from '../utils'
5c6d985f 10import { isActivityPubUrlValid } from '../../helpers/custom-validators/activitypub/misc'
c100a614 11import { AccountVideoRate } from '../../../shared'
bfbd9128 12import { ScopeNames as VideoChannelScopeNames, SummaryOptions, VideoChannelModel } from '../video/video-channel'
d38b8281 13
3fd3ab2d
C
14/*
15 Account rates per video.
16*/
17@Table({
18 tableName: 'accountVideoRate',
19 indexes: [
d38b8281 20 {
3fd3ab2d
C
21 fields: [ 'videoId', 'accountId' ],
22 unique: true
8cd72bd3
C
23 },
24 {
25 fields: [ 'videoId' ]
26 },
27 {
28 fields: [ 'accountId' ]
29 },
30 {
31 fields: [ 'videoId', 'type' ]
5c6d985f
C
32 },
33 {
34 fields: [ 'url' ],
35 unique: true
d38b8281 36 }
3fd3ab2d
C
37 ]
38})
39export class AccountVideoRateModel extends Model<AccountVideoRateModel> {
d38b8281 40
3fd3ab2d 41 @AllowNull(false)
1735c825 42 @Column(DataType.ENUM(...values(VIDEO_RATE_TYPES)))
3fd3ab2d 43 type: VideoRateType
e02643f3 44
5c6d985f
C
45 @AllowNull(false)
46 @Is('AccountVideoRateUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'url'))
47 @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEO_RATES.URL.max))
48 url: string
49
3fd3ab2d
C
50 @CreatedAt
51 createdAt: Date
e02643f3 52
3fd3ab2d
C
53 @UpdatedAt
54 updatedAt: Date
d38b8281 55
3fd3ab2d
C
56 @ForeignKey(() => VideoModel)
57 @Column
58 videoId: number
d38b8281 59
3fd3ab2d 60 @BelongsTo(() => VideoModel, {
d38b8281 61 foreignKey: {
d38b8281
C
62 allowNull: false
63 },
64 onDelete: 'CASCADE'
65 })
3fd3ab2d 66 Video: VideoModel
d38b8281 67
3fd3ab2d
C
68 @ForeignKey(() => AccountModel)
69 @Column
70 accountId: number
71
72 @BelongsTo(() => AccountModel, {
d38b8281 73 foreignKey: {
d38b8281
C
74 allowNull: false
75 },
76 onDelete: 'CASCADE'
77 })
3fd3ab2d 78 Account: AccountModel
d38b8281 79
5c6d985f 80 static load (accountId: number, videoId: number, transaction?: Transaction) {
1735c825 81 const options: FindOptions = {
3fd3ab2d
C
82 where: {
83 accountId,
84 videoId
85 }
d38b8281 86 }
3fd3ab2d 87 if (transaction) options.transaction = transaction
d38b8281 88
3fd3ab2d
C
89 return AccountVideoRateModel.findOne(options)
90 }
8fffe21a 91
970ceac0
C
92 static loadByAccountAndVideoOrUrl (accountId: number, videoId: number, url: string, transaction?: Transaction) {
93 const options: FindOptions = {
94 where: {
95 [ Op.or]: [
96 {
97 accountId,
98 videoId
99 },
100 {
101 url
102 }
103 ]
104 }
105 }
106 if (transaction) options.transaction = transaction
107
108 return AccountVideoRateModel.findOne(options)
109 }
110
c100a614
YB
111 static listByAccountForApi (options: {
112 start: number,
113 count: number,
114 sort: string,
115 type?: string,
116 accountId: number
117 }) {
1735c825 118 const query: FindOptions = {
c100a614
YB
119 offset: options.start,
120 limit: options.count,
121 order: getSort(options.sort),
122 where: {
123 accountId: options.accountId
124 },
125 include: [
126 {
127 model: VideoModel,
128 required: true,
129 include: [
130 {
bfbd9128 131 model: VideoChannelModel.scope({ method: [VideoChannelScopeNames.SUMMARY, { withAccount: true } as SummaryOptions ] }),
c100a614
YB
132 required: true
133 }
134 ]
135 }
136 ]
137 }
138 if (options.type) query.where['type'] = options.type
139
140 return AccountVideoRateModel.findAndCountAll(query)
141 }
142
5c6d985f 143 static loadLocalAndPopulateVideo (rateType: VideoRateType, accountName: string, videoId: number, transaction?: Transaction) {
1735c825 144 const options: FindOptions = {
5c6d985f
C
145 where: {
146 videoId,
147 type: rateType
148 },
149 include: [
150 {
151 model: AccountModel.unscoped(),
152 required: true,
153 include: [
154 {
155 attributes: [ 'id', 'url', 'preferredUsername' ],
156 model: ActorModel.unscoped(),
157 required: true,
158 where: {
159 preferredUsername: accountName
160 }
161 }
162 ]
163 },
164 {
165 model: VideoModel.unscoped(),
166 required: true
167 }
168 ]
169 }
170 if (transaction) options.transaction = transaction
171
172 return AccountVideoRateModel.findOne(options)
173 }
174
175 static loadByUrl (url: string, transaction: Transaction) {
1735c825 176 const options: FindOptions = {
5c6d985f
C
177 where: {
178 url
179 }
180 }
181 if (transaction) options.transaction = transaction
182
183 return AccountVideoRateModel.findOne(options)
184 }
185
8fffe21a
C
186 static listAndCountAccountUrlsByVideoId (rateType: VideoRateType, videoId: number, start: number, count: number, t?: Transaction) {
187 const query = {
9a4a9b6c
C
188 offset: start,
189 limit: count,
8fffe21a
C
190 where: {
191 videoId,
192 type: rateType
193 },
194 transaction: t,
195 include: [
196 {
197 attributes: [ 'actorId' ],
198 model: AccountModel.unscoped(),
199 required: true,
200 include: [
201 {
202 attributes: [ 'url' ],
203 model: ActorModel.unscoped(),
204 required: true
205 }
206 ]
207 }
208 ]
209 }
210
211 return AccountVideoRateModel.findAndCountAll(query)
212 }
2ba92871
C
213
214 static cleanOldRatesOf (videoId: number, type: VideoRateType, beforeUpdatedAt: Date) {
215 return AccountVideoRateModel.sequelize.transaction(async t => {
216 const query = {
217 where: {
218 updatedAt: {
219 [Op.lt]: beforeUpdatedAt
220 },
221 videoId,
6b9c966f
C
222 type,
223 accountId: {
224 [Op.notIn]: buildLocalAccountIdsIn()
970ceac0 225 }
6b9c966f 226 },
2ba92871
C
227 transaction: t
228 }
229
230 const deleted = await AccountVideoRateModel.destroy(query)
231
232 const options = {
233 transaction: t,
234 where: {
235 id: videoId
236 }
237 }
238
239 if (type === 'like') await VideoModel.increment({ likes: -deleted }, options)
240 else if (type === 'dislike') await VideoModel.increment({ dislikes: -deleted }, options)
241 })
242 }
c100a614
YB
243
244 toFormattedJSON (): AccountVideoRate {
245 return {
246 video: this.Video.toFormattedJSON(),
247 rating: this.type
248 }
249 }
d38b8281 250}