1 import { values } from 'lodash'
2 import { FindOptions, Op, Transaction } from 'sequelize'
3 import { AllowNull, BelongsTo, Column, CreatedAt, DataType, ForeignKey, Is, Model, Table, UpdatedAt } from 'sequelize-typescript'
4 import { VideoRateType } from '../../../shared/models/videos'
5 import { CONSTRAINTS_FIELDS, VIDEO_RATE_TYPES } from '../../initializers/constants'
6 import { VideoModel } from '../video/video'
7 import { AccountModel } from './account'
8 import { ActorModel } from '../activitypub/actor'
9 import { buildLocalAccountIdsIn, getSort, throwIfNotValid } from '../utils'
10 import { isActivityPubUrlValid } from '../../helpers/custom-validators/activitypub/misc'
11 import { AccountVideoRate } from '../../../shared'
12 import { ScopeNames as VideoChannelScopeNames, SummaryOptions, VideoChannelModel } from '../video/video-channel'
13 import * as Bluebird from 'bluebird'
16 MAccountVideoRateAccountUrl,
17 MAccountVideoRateAccountVideo,
18 MAccountVideoRateFormattable
19 } from '@server/typings/models/video/video-rate'
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<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): Bluebird<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): Bluebird<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 loadLocalAndPopulateVideo (
151 rateType: VideoRateType,
153 videoId: number | string,
155 ): Bluebird<MAccountVideoRateAccountVideo> {
156 const options: FindOptions = {
163 model: AccountModel.unscoped(),
167 attributes: [ 'id', 'url', 'followersUrl', 'preferredUsername' ],
168 model: ActorModel.unscoped(),
171 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 const deleted = await AccountVideoRateModel.destroy(query)
251 if (type === 'like') await VideoModel.increment({ likes: -deleted }, options)
252 else if (type === 'dislike') await VideoModel.increment({ dislikes: -deleted }, options)
256 toFormattedJSON (this: MAccountVideoRateFormattable): AccountVideoRate {
258 video: this.Video.toFormattedJSON(),