1 import { values } from 'lodash'
2 import { FindOptions, Op, QueryTypes, Transaction } from 'sequelize'
3 import { AllowNull, BelongsTo, Column, CreatedAt, DataType, ForeignKey, Is, Model, Table, UpdatedAt } from 'sequelize-typescript'
6 MAccountVideoRateAccountUrl,
7 MAccountVideoRateAccountVideo,
8 MAccountVideoRateFormattable
9 } from '@server/types/models'
10 import { AccountVideoRate, VideoRateType } from '@shared/models'
11 import { AttributesOnly } from '@shared/typescript-utils'
12 import { isActivityPubUrlValid } from '../../helpers/custom-validators/activitypub/misc'
13 import { CONSTRAINTS_FIELDS, VIDEO_RATE_TYPES } from '../../initializers/constants'
14 import { ActorModel } from '../actor/actor'
15 import { getSort, throwIfNotValid } from '../utils'
16 import { VideoModel } from '../video/video'
17 import { ScopeNames as VideoChannelScopeNames, SummaryOptions, VideoChannelModel } from '../video/video-channel'
18 import { AccountModel } from './account'
21 Account rates per video.
24 tableName: 'accountVideoRate',
27 fields: [ 'videoId', 'accountId' ],
34 fields: [ 'accountId' ]
37 fields: [ 'videoId', 'type' ]
45 export class AccountVideoRateModel extends Model<Partial<AttributesOnly<AccountVideoRateModel>>> {
48 @Column(DataType.ENUM(...values(VIDEO_RATE_TYPES)))
52 @Is('AccountVideoRateUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'url'))
53 @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEO_RATES.URL.max))
62 @ForeignKey(() => VideoModel)
66 @BelongsTo(() => VideoModel, {
74 @ForeignKey(() => AccountModel)
78 @BelongsTo(() => AccountModel, {
86 static load (accountId: number, videoId: number, transaction?: Transaction): Promise<MAccountVideoRate> {
87 const options: FindOptions = {
93 if (transaction) options.transaction = transaction
95 return AccountVideoRateModel.findOne(options)
98 static loadByAccountAndVideoOrUrl (accountId: number, videoId: number, url: string, t?: Transaction): Promise<MAccountVideoRate> {
99 const options: FindOptions = {
112 if (t) options.transaction = t
114 return AccountVideoRateModel.findOne(options)
117 static listByAccountForApi (options: {
124 const getQuery = (forCount: boolean) => {
125 const query: FindOptions = {
126 offset: options.start,
127 limit: options.count,
128 order: getSort(options.sort),
130 accountId: options.accountId
134 if (options.type) query.where['type'] = options.type
136 if (forCount !== true) {
143 model: VideoChannelModel.scope({ method: [ VideoChannelScopeNames.SUMMARY, { withAccount: true } as SummaryOptions ] }),
155 AccountVideoRateModel.count(getQuery(true)),
156 AccountVideoRateModel.findAll(getQuery(false))
157 ]).then(([ total, data ]) => ({ total, data }))
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`
166 return AccountVideoRateModel.sequelize.query<{ url: string }>(query, {
167 type: QueryTypes.SELECT,
169 }).then(rows => rows.map(r => r.url))
172 static loadLocalAndPopulateVideo (
173 rateType: VideoRateType,
177 ): Promise<MAccountVideoRateAccountVideo> {
178 const options: FindOptions = {
185 model: AccountModel.unscoped(),
189 attributes: [ 'id', 'url', 'followersUrl', 'preferredUsername' ],
190 model: ActorModel.unscoped(),
193 preferredUsername: accountName,
200 model: VideoModel.unscoped(),
205 if (t) options.transaction = t
207 return AccountVideoRateModel.findOne(options)
210 static loadByUrl (url: string, transaction: Transaction) {
211 const options: FindOptions = {
216 if (transaction) options.transaction = transaction
218 return AccountVideoRateModel.findOne(options)
221 static listAndCountAccountUrlsByVideoId (rateType: VideoRateType, videoId: number, start: number, count: number, t?: Transaction) {
232 attributes: [ 'actorId' ],
233 model: AccountModel.unscoped(),
237 attributes: [ 'url' ],
238 model: ActorModel.unscoped(),
247 AccountVideoRateModel.count(query),
248 AccountVideoRateModel.findAll<MAccountVideoRateAccountUrl>(query)
249 ]).then(([ total, data ]) => ({ total, data }))
252 toFormattedJSON (this: MAccountVideoRateFormattable): AccountVideoRate {
254 video: this.Video.toFormattedJSON(),