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 { AccountVideoRate } from '../../../shared'
11 import { VideoRateType } from '../../../shared/models/videos'
12 import { isActivityPubUrlValid } from '../../helpers/custom-validators/activitypub/misc'
13 import { CONSTRAINTS_FIELDS, VIDEO_RATE_TYPES } from '../../initializers/constants'
14 import { ActorModel } from '../activitypub/actor'
15 import { buildLocalAccountIdsIn, 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 {
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 query: FindOptions = {
125 offset: options.start,
126 limit: options.count,
127 order: getSort(options.sort),
129 accountId: options.accountId
137 model: VideoChannelModel.scope({ method: [ VideoChannelScopeNames.SUMMARY, { withAccount: true } as SummaryOptions ] }),
144 if (options.type) query.where['type'] = options.type
146 return AccountVideoRateModel.findAndCountAll(query)
149 static listRemoteRateUrlsOfLocalVideos () {
150 const query = `SELECT "accountVideoRate".url FROM "accountVideoRate" ` +
151 `INNER JOIN account ON account.id = "accountVideoRate"."accountId" ` +
152 `INNER JOIN actor ON actor.id = account."actorId" AND actor."serverId" IS NOT NULL ` +
153 `INNER JOIN video ON video.id = "accountVideoRate"."videoId" AND video.remote IS FALSE`
155 return AccountVideoRateModel.sequelize.query<{ url: string }>(query, {
156 type: QueryTypes.SELECT,
158 }).then(rows => rows.map(r => r.url))
161 static loadLocalAndPopulateVideo (
162 rateType: VideoRateType,
166 ): Promise<MAccountVideoRateAccountVideo> {
167 const options: FindOptions = {
174 model: AccountModel.unscoped(),
178 attributes: [ 'id', 'url', 'followersUrl', 'preferredUsername' ],
179 model: ActorModel.unscoped(),
182 preferredUsername: accountName,
189 model: VideoModel.unscoped(),
194 if (t) options.transaction = t
196 return AccountVideoRateModel.findOne(options)
199 static loadByUrl (url: string, transaction: Transaction) {
200 const options: FindOptions = {
205 if (transaction) options.transaction = transaction
207 return AccountVideoRateModel.findOne(options)
210 static listAndCountAccountUrlsByVideoId (rateType: VideoRateType, videoId: number, start: number, count: number, t?: Transaction) {
221 attributes: [ 'actorId' ],
222 model: AccountModel.unscoped(),
226 attributes: [ 'url' ],
227 model: ActorModel.unscoped(),
235 return AccountVideoRateModel.findAndCountAll<MAccountVideoRateAccountUrl>(query)
238 static cleanOldRatesOf (videoId: number, type: VideoRateType, beforeUpdatedAt: Date) {
239 return AccountVideoRateModel.sequelize.transaction(async t => {
243 [Op.lt]: beforeUpdatedAt
248 [Op.notIn]: buildLocalAccountIdsIn()
254 await AccountVideoRateModel.destroy(query)
256 return VideoModel.updateRatesOf(videoId, type, t)
260 toFormattedJSON (this: MAccountVideoRateFormattable): AccountVideoRate {
262 video: this.Video.toFormattedJSON(),