1 import { values } from 'lodash'
2 import { Transaction, Op } from 'sequelize'
3 import { AllowNull, BelongsTo, Column, CreatedAt, DataType, ForeignKey, Is, Model, Table, UpdatedAt } from 'sequelize-typescript'
4 import { IFindOptions } from 'sequelize-typescript/lib/interfaces/IFindOptions'
5 import { VideoRateType } from '../../../shared/models/videos'
6 import { CONSTRAINTS_FIELDS, VIDEO_RATE_TYPES } from '../../initializers/constants'
7 import { VideoModel } from '../video/video'
8 import { AccountModel } from './account'
9 import { ActorModel } from '../activitypub/actor'
10 import { throwIfNotValid, getSort } from '../utils'
11 import { isActivityPubUrlValid } from '../../helpers/custom-validators/activitypub/misc'
12 import { AccountVideoRate } from '../../../shared'
13 import { VideoChannelModel, ScopeNames as VideoChannelScopeNames } from '../video/video-channel'
16 Account rates per video.
19 tableName: 'accountVideoRate',
22 fields: [ 'videoId', 'accountId' ],
29 fields: [ 'accountId' ]
32 fields: [ 'videoId', 'type' ]
40 export class AccountVideoRateModel extends Model<AccountVideoRateModel> {
43 @Column(DataType.ENUM(values(VIDEO_RATE_TYPES)))
47 @Is('AccountVideoRateUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'url'))
48 @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEO_RATES.URL.max))
57 @ForeignKey(() => VideoModel)
61 @BelongsTo(() => VideoModel, {
69 @ForeignKey(() => AccountModel)
73 @BelongsTo(() => AccountModel, {
81 static load (accountId: number, videoId: number, transaction?: Transaction) {
82 const options: IFindOptions<AccountVideoRateModel> = {
88 if (transaction) options.transaction = transaction
90 return AccountVideoRateModel.findOne(options)
93 static listByAccountForApi (options: {
100 const query: IFindOptions<AccountVideoRateModel> = {
101 offset: options.start,
102 limit: options.count,
103 order: getSort(options.sort),
105 accountId: options.accountId
113 model: VideoChannelModel.scope({ method: [VideoChannelScopeNames.SUMMARY, true] }),
120 if (options.type) query.where['type'] = options.type
122 return AccountVideoRateModel.findAndCountAll(query)
125 static loadLocalAndPopulateVideo (rateType: VideoRateType, accountName: string, videoId: number, transaction?: Transaction) {
126 const options: IFindOptions<AccountVideoRateModel> = {
133 model: AccountModel.unscoped(),
137 attributes: [ 'id', 'url', 'preferredUsername' ],
138 model: ActorModel.unscoped(),
141 preferredUsername: accountName
147 model: VideoModel.unscoped(),
152 if (transaction) options.transaction = transaction
154 return AccountVideoRateModel.findOne(options)
157 static loadByUrl (url: string, transaction: Transaction) {
158 const options: IFindOptions<AccountVideoRateModel> = {
163 if (transaction) options.transaction = transaction
165 return AccountVideoRateModel.findOne(options)
168 static listAndCountAccountUrlsByVideoId (rateType: VideoRateType, videoId: number, start: number, count: number, t?: Transaction) {
179 attributes: [ 'actorId' ],
180 model: AccountModel.unscoped(),
184 attributes: [ 'url' ],
185 model: ActorModel.unscoped(),
193 return AccountVideoRateModel.findAndCountAll(query)
196 static cleanOldRatesOf (videoId: number, type: VideoRateType, beforeUpdatedAt: Date) {
197 return AccountVideoRateModel.sequelize.transaction(async t => {
201 [Op.lt]: beforeUpdatedAt
209 const deleted = await AccountVideoRateModel.destroy(query)
218 if (type === 'like') await VideoModel.increment({ likes: -deleted }, options)
219 else if (type === 'dislike') await VideoModel.increment({ dislikes: -deleted }, options)
223 toFormattedJSON (): AccountVideoRate {
225 video: this.Video.toFormattedJSON(),