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