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 loadLocalAndPopulateVideo (
150 rateType: VideoRateType,
152 videoId: number | string,
154 ): Promise<MAccountVideoRateAccountVideo> {
155 const options: FindOptions = {
162 model: AccountModel.unscoped(),
166 attributes: [ 'id', 'url', 'followersUrl', 'preferredUsername' ],
167 model: ActorModel.unscoped(),
170 preferredUsername: accountName,
177 model: VideoModel.unscoped(),
182 if (t) options.transaction = t
184 return AccountVideoRateModel.findOne(options)
187 static loadByUrl (url: string, transaction: Transaction) {
188 const options: FindOptions = {
193 if (transaction) options.transaction = transaction
195 return AccountVideoRateModel.findOne(options)
198 static listAndCountAccountUrlsByVideoId (rateType: VideoRateType, videoId: number, start: number, count: number, t?: Transaction) {
209 attributes: [ 'actorId' ],
210 model: AccountModel.unscoped(),
214 attributes: [ 'url' ],
215 model: ActorModel.unscoped(),
223 return AccountVideoRateModel.findAndCountAll<MAccountVideoRateAccountUrl>(query)
226 static cleanOldRatesOf (videoId: number, type: VideoRateType, beforeUpdatedAt: Date) {
227 return AccountVideoRateModel.sequelize.transaction(async t => {
231 [Op.lt]: beforeUpdatedAt
236 [Op.notIn]: buildLocalAccountIdsIn()
242 await AccountVideoRateModel.destroy(query)
244 const field = type === 'like'
248 const rawQuery = `UPDATE "video" SET "${field}" = ` +
250 'SELECT COUNT(id) FROM "accountVideoRate" WHERE "accountVideoRate"."videoId" = "video"."id" AND type = :rateType' +
252 'WHERE "video"."id" = :videoId'
254 return AccountVideoRateModel.sequelize.query(rawQuery, {
256 replacements: { videoId, rateType: type },
257 type: QueryTypes.UPDATE
262 toFormattedJSON (this: MAccountVideoRateFormattable): AccountVideoRate {
264 video: this.Video.toFormattedJSON(),