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