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 { getSort, throwIfNotValid } from '../utils'
10 import { isActivityPubUrlValid } from '../../helpers/custom-validators/activitypub/misc'
11 import { AccountVideoRate } from '../../../shared'
12 import { ScopeNames as VideoChannelScopeNames, VideoChannelModel } from '../video/video-channel'
15 Account rates per video.
18 tableName: 'accountVideoRate',
21 fields: [ 'videoId', 'accountId' ],
28 fields: [ 'accountId' ]
31 fields: [ 'videoId', 'type' ]
39 export class AccountVideoRateModel extends Model<AccountVideoRateModel> {
42 @Column(DataType.ENUM(...values(VIDEO_RATE_TYPES)))
46 @Is('AccountVideoRateUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'url'))
47 @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEO_RATES.URL.max))
56 @ForeignKey(() => VideoModel)
60 @BelongsTo(() => VideoModel, {
68 @ForeignKey(() => AccountModel)
72 @BelongsTo(() => AccountModel, {
80 static load (accountId: number, videoId: number, transaction?: Transaction) {
81 const options: FindOptions = {
87 if (transaction) options.transaction = transaction
89 return AccountVideoRateModel.findOne(options)
92 static listByAccountForApi (options: {
99 const query: FindOptions = {
100 offset: options.start,
101 limit: options.count,
102 order: getSort(options.sort),
104 accountId: options.accountId
112 model: VideoChannelModel.scope({ method: [VideoChannelScopeNames.SUMMARY, true] }),
119 if (options.type) query.where['type'] = options.type
121 return AccountVideoRateModel.findAndCountAll(query)
124 static loadLocalAndPopulateVideo (rateType: VideoRateType, accountName: string, videoId: number, transaction?: Transaction) {
125 const options: FindOptions = {
132 model: AccountModel.unscoped(),
136 attributes: [ 'id', 'url', 'preferredUsername' ],
137 model: ActorModel.unscoped(),
140 preferredUsername: accountName
146 model: VideoModel.unscoped(),
151 if (transaction) options.transaction = transaction
153 return AccountVideoRateModel.findOne(options)
156 static loadByUrl (url: string, transaction: Transaction) {
157 const options: FindOptions = {
162 if (transaction) options.transaction = transaction
164 return AccountVideoRateModel.findOne(options)
167 static listAndCountAccountUrlsByVideoId (rateType: VideoRateType, videoId: number, start: number, count: number, t?: Transaction) {
178 attributes: [ 'actorId' ],
179 model: AccountModel.unscoped(),
183 attributes: [ 'url' ],
184 model: ActorModel.unscoped(),
192 return AccountVideoRateModel.findAndCountAll(query)
195 static cleanOldRatesOf (videoId: number, type: VideoRateType, beforeUpdatedAt: Date) {
196 return AccountVideoRateModel.sequelize.transaction(async t => {
200 [Op.lt]: beforeUpdatedAt
208 const deleted = await AccountVideoRateModel.destroy(query)
217 if (type === 'like') await VideoModel.increment({ likes: -deleted }, options)
218 else if (type === 'dislike') await VideoModel.increment({ dislikes: -deleted }, options)
222 toFormattedJSON (): AccountVideoRate {
224 video: this.Video.toFormattedJSON(),