1 import { values } from 'lodash'
2 import { Transaction } from 'sequelize'
3 import { AllowNull, BelongsTo, Column, CreatedAt, DataType, ForeignKey, Is, Model, Table, UpdatedAt } from 'sequelize-typescript'
4 import { IFindOptions } from 'sequelize-typescript/lib/interfaces/IFindOptions'
5 import { VideoRateType } from '../../../shared/models/videos'
6 import { CONSTRAINTS_FIELDS, VIDEO_RATE_TYPES } from '../../initializers'
7 import { VideoModel } from '../video/video'
8 import { AccountModel } from './account'
9 import { ActorModel } from '../activitypub/actor'
10 import { throwIfNotValid } from '../utils'
11 import { isActivityPubUrlValid } from '../../helpers/custom-validators/activitypub/misc'
14 Account rates per video.
17 tableName: 'accountVideoRate',
20 fields: [ 'videoId', 'accountId' ],
27 fields: [ 'accountId' ]
30 fields: [ 'videoId', 'type' ]
38 export class AccountVideoRateModel extends Model<AccountVideoRateModel> {
41 @Column(DataType.ENUM(values(VIDEO_RATE_TYPES)))
45 @Is('AccountVideoRateUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'url'))
46 @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEO_RATES.URL.max))
55 @ForeignKey(() => VideoModel)
59 @BelongsTo(() => VideoModel, {
67 @ForeignKey(() => AccountModel)
71 @BelongsTo(() => AccountModel, {
79 static load (accountId: number, videoId: number, transaction?: Transaction) {
80 const options: IFindOptions<AccountVideoRateModel> = {
86 if (transaction) options.transaction = transaction
88 return AccountVideoRateModel.findOne(options)
91 static loadLocalAndPopulateVideo (rateType: VideoRateType, accountName: string, videoId: number, transaction?: Transaction) {
92 const options: IFindOptions<AccountVideoRateModel> = {
99 model: AccountModel.unscoped(),
103 attributes: [ 'id', 'url', 'preferredUsername' ],
104 model: ActorModel.unscoped(),
107 preferredUsername: accountName
113 model: VideoModel.unscoped(),
118 if (transaction) options.transaction = transaction
120 return AccountVideoRateModel.findOne(options)
123 static loadByUrl (url: string, transaction: Transaction) {
124 const options: IFindOptions<AccountVideoRateModel> = {
129 if (transaction) options.transaction = transaction
131 return AccountVideoRateModel.findOne(options)
134 static listAndCountAccountUrlsByVideoId (rateType: VideoRateType, videoId: number, start: number, count: number, t?: Transaction) {
145 attributes: [ 'actorId' ],
146 model: AccountModel.unscoped(),
150 attributes: [ 'url' ],
151 model: ActorModel.unscoped(),
159 return AccountVideoRateModel.findAndCountAll(query)