1 import { FindOptions, Op, QueryTypes, Transaction } from 'sequelize'
2 import { AllowNull, BelongsTo, Column, CreatedAt, DataType, ForeignKey, Is, Model, Table, UpdatedAt } from 'sequelize-typescript'
5 MAccountVideoRateAccountUrl,
6 MAccountVideoRateAccountVideo,
7 MAccountVideoRateFormattable
8 } from '@server/types/models'
9 import { AccountVideoRate, VideoRateType } from '@shared/models'
10 import { AttributesOnly } from '@shared/typescript-utils'
11 import { isActivityPubUrlValid } from '../../helpers/custom-validators/activitypub/misc'
12 import { CONSTRAINTS_FIELDS, VIDEO_RATE_TYPES } from '../../initializers/constants'
13 import { ActorModel } from '../actor/actor'
14 import { getSort, throwIfNotValid } from '../shared'
15 import { VideoModel } from '../video/video'
16 import { ScopeNames as VideoChannelScopeNames, SummaryOptions, VideoChannelModel } from '../video/video-channel'
17 import { AccountModel } from './account'
20 Account rates per video.
23 tableName: 'accountVideoRate',
26 fields: [ 'videoId', 'accountId' ],
33 fields: [ 'accountId' ]
36 fields: [ 'videoId', 'type' ]
44 export class AccountVideoRateModel extends Model<Partial<AttributesOnly<AccountVideoRateModel>>> {
47 @Column(DataType.ENUM(...Object.values(VIDEO_RATE_TYPES)))
51 @Is('AccountVideoRateUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'url'))
52 @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEO_RATES.URL.max))
61 @ForeignKey(() => VideoModel)
65 @BelongsTo(() => VideoModel, {
73 @ForeignKey(() => AccountModel)
77 @BelongsTo(() => AccountModel, {
85 static load (accountId: number, videoId: number, transaction?: Transaction): Promise<MAccountVideoRate> {
86 const options: FindOptions = {
92 if (transaction) options.transaction = transaction
94 return AccountVideoRateModel.findOne(options)
97 static loadByAccountAndVideoOrUrl (accountId: number, videoId: number, url: string, t?: Transaction): Promise<MAccountVideoRate> {
98 const options: FindOptions = {
111 if (t) options.transaction = t
113 return AccountVideoRateModel.findOne(options)
116 static listByAccountForApi (options: {
123 const getQuery = (forCount: boolean) => {
124 const query: FindOptions = {
125 offset: options.start,
126 limit: options.count,
127 order: getSort(options.sort),
129 accountId: options.accountId
133 if (options.type) query.where['type'] = options.type
135 if (forCount !== true) {
142 model: VideoChannelModel.scope({ method: [ VideoChannelScopeNames.SUMMARY, { withAccount: true } as SummaryOptions ] }),
154 AccountVideoRateModel.count(getQuery(true)),
155 AccountVideoRateModel.findAll(getQuery(false))
156 ]).then(([ total, data ]) => ({ total, data }))
159 static listRemoteRateUrlsOfLocalVideos () {
160 const query = `SELECT "accountVideoRate".url FROM "accountVideoRate" ` +
161 `INNER JOIN account ON account.id = "accountVideoRate"."accountId" ` +
162 `INNER JOIN actor ON actor.id = account."actorId" AND actor."serverId" IS NOT NULL ` +
163 `INNER JOIN video ON video.id = "accountVideoRate"."videoId" AND video.remote IS FALSE`
165 return AccountVideoRateModel.sequelize.query<{ url: string }>(query, {
166 type: QueryTypes.SELECT,
168 }).then(rows => rows.map(r => r.url))
171 static loadLocalAndPopulateVideo (
172 rateType: VideoRateType,
176 ): Promise<MAccountVideoRateAccountVideo> {
177 const options: FindOptions = {
184 model: AccountModel.unscoped(),
188 attributes: [ 'id', 'url', 'followersUrl', 'preferredUsername' ],
189 model: ActorModel.unscoped(),
192 preferredUsername: accountName,
199 model: VideoModel.unscoped(),
204 if (t) options.transaction = t
206 return AccountVideoRateModel.findOne(options)
209 static loadByUrl (url: string, transaction: Transaction) {
210 const options: FindOptions = {
215 if (transaction) options.transaction = transaction
217 return AccountVideoRateModel.findOne(options)
220 static listAndCountAccountUrlsByVideoId (rateType: VideoRateType, videoId: number, start: number, count: number, t?: Transaction) {
231 attributes: [ 'actorId' ],
232 model: AccountModel.unscoped(),
236 attributes: [ 'url' ],
237 model: ActorModel.unscoped(),
246 AccountVideoRateModel.count(query),
247 AccountVideoRateModel.findAll<MAccountVideoRateAccountUrl>(query)
248 ]).then(([ total, data ]) => ({ total, data }))
251 toFormattedJSON (this: MAccountVideoRateFormattable): AccountVideoRate {
253 video: this.Video.toFormattedJSON(),