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/video/video-rate'
10 import { AttributesOnly } from '@shared/core-utils'
11 import { AccountVideoRate } from '../../../shared'
12 import { VideoRateType } from '../../../shared/models/videos'
13 import { isActivityPubUrlValid } from '../../helpers/custom-validators/activitypub/misc'
14 import { CONSTRAINTS_FIELDS, VIDEO_RATE_TYPES } from '../../initializers/constants'
15 import { ActorModel } from '../actor/actor'
16 import { buildLocalAccountIdsIn, getSort, throwIfNotValid } from '../utils'
17 import { VideoModel } from '../video/video'
18 import { ScopeNames as VideoChannelScopeNames, SummaryOptions, VideoChannelModel } from '../video/video-channel'
19 import { AccountModel } from './account'
22 Account rates per video.
25 tableName: 'accountVideoRate',
28 fields: [ 'videoId', 'accountId' ],
35 fields: [ 'accountId' ]
38 fields: [ 'videoId', 'type' ]
46 export class AccountVideoRateModel extends Model<Partial<AttributesOnly<AccountVideoRateModel>>> {
49 @Column(DataType.ENUM(...values(VIDEO_RATE_TYPES)))
53 @Is('AccountVideoRateUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'url'))
54 @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEO_RATES.URL.max))
63 @ForeignKey(() => VideoModel)
67 @BelongsTo(() => VideoModel, {
75 @ForeignKey(() => AccountModel)
79 @BelongsTo(() => AccountModel, {
87 static load (accountId: number, videoId: number, transaction?: Transaction): Promise<MAccountVideoRate> {
88 const options: FindOptions = {
94 if (transaction) options.transaction = transaction
96 return AccountVideoRateModel.findOne(options)
99 static loadByAccountAndVideoOrUrl (accountId: number, videoId: number, url: string, t?: Transaction): Promise<MAccountVideoRate> {
100 const options: FindOptions = {
113 if (t) options.transaction = t
115 return AccountVideoRateModel.findOne(options)
118 static listByAccountForApi (options: {
125 const query: FindOptions = {
126 offset: options.start,
127 limit: options.count,
128 order: getSort(options.sort),
130 accountId: options.accountId
138 model: VideoChannelModel.scope({ method: [ VideoChannelScopeNames.SUMMARY, { withAccount: true } as SummaryOptions ] }),
145 if (options.type) query.where['type'] = options.type
147 return AccountVideoRateModel.findAndCountAll(query)
150 static listRemoteRateUrlsOfLocalVideos () {
151 const query = `SELECT "accountVideoRate".url FROM "accountVideoRate" ` +
152 `INNER JOIN account ON account.id = "accountVideoRate"."accountId" ` +
153 `INNER JOIN actor ON actor.id = account."actorId" AND actor."serverId" IS NOT NULL ` +
154 `INNER JOIN video ON video.id = "accountVideoRate"."videoId" AND video.remote IS FALSE`
156 return AccountVideoRateModel.sequelize.query<{ url: string }>(query, {
157 type: QueryTypes.SELECT,
159 }).then(rows => rows.map(r => r.url))
162 static loadLocalAndPopulateVideo (
163 rateType: VideoRateType,
167 ): Promise<MAccountVideoRateAccountVideo> {
168 const options: FindOptions = {
175 model: AccountModel.unscoped(),
179 attributes: [ 'id', 'url', 'followersUrl', 'preferredUsername' ],
180 model: ActorModel.unscoped(),
183 preferredUsername: accountName,
190 model: VideoModel.unscoped(),
195 if (t) options.transaction = t
197 return AccountVideoRateModel.findOne(options)
200 static loadByUrl (url: string, transaction: Transaction) {
201 const options: FindOptions = {
206 if (transaction) options.transaction = transaction
208 return AccountVideoRateModel.findOne(options)
211 static listAndCountAccountUrlsByVideoId (rateType: VideoRateType, videoId: number, start: number, count: number, t?: Transaction) {
222 attributes: [ 'actorId' ],
223 model: AccountModel.unscoped(),
227 attributes: [ 'url' ],
228 model: ActorModel.unscoped(),
236 return AccountVideoRateModel.findAndCountAll<MAccountVideoRateAccountUrl>(query)
239 static cleanOldRatesOf (videoId: number, type: VideoRateType, beforeUpdatedAt: Date) {
240 return AccountVideoRateModel.sequelize.transaction(async t => {
244 [Op.lt]: beforeUpdatedAt
249 [Op.notIn]: buildLocalAccountIdsIn()
255 await AccountVideoRateModel.destroy(query)
257 return VideoModel.updateRatesOf(videoId, type, t)
261 toFormattedJSON (this: MAccountVideoRateFormattable): AccountVideoRate {
263 video: this.Video.toFormattedJSON(),