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