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'
14 import { MAccountVideoRate, MAccountVideoRateAccountUrl, MAccountVideoRateAccountVideo } from '@server/typings/models/video/video-rate'
17 Account rates per video.
20 tableName: 'accountVideoRate',
23 fields: [ 'videoId', 'accountId' ],
30 fields: [ 'accountId' ]
33 fields: [ 'videoId', 'type' ]
41 export class AccountVideoRateModel extends Model<AccountVideoRateModel> {
44 @Column(DataType.ENUM(...values(VIDEO_RATE_TYPES)))
48 @Is('AccountVideoRateUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'url'))
49 @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEO_RATES.URL.max))
58 @ForeignKey(() => VideoModel)
62 @BelongsTo(() => VideoModel, {
70 @ForeignKey(() => AccountModel)
74 @BelongsTo(() => AccountModel, {
82 static load (accountId: number, videoId: number, transaction?: Transaction): Bluebird<MAccountVideoRate> {
83 const options: FindOptions = {
89 if (transaction) options.transaction = transaction
91 return AccountVideoRateModel.findOne(options)
94 static loadByAccountAndVideoOrUrl (accountId: number, videoId: number, url: string, t?: Transaction): Bluebird<MAccountVideoRate> {
95 const options: FindOptions = {
108 if (t) options.transaction = t
110 return AccountVideoRateModel.findOne(options)
113 static listByAccountForApi (options: {
120 const query: FindOptions = {
121 offset: options.start,
122 limit: options.count,
123 order: getSort(options.sort),
125 accountId: options.accountId
133 model: VideoChannelModel.scope({ method: [VideoChannelScopeNames.SUMMARY, { withAccount: true } as SummaryOptions ] }),
140 if (options.type) query.where['type'] = options.type
142 return AccountVideoRateModel.findAndCountAll(query)
145 static loadLocalAndPopulateVideo (
146 rateType: VideoRateType,
150 ): Bluebird<MAccountVideoRateAccountVideo> {
151 const options: FindOptions = {
158 model: AccountModel.unscoped(),
162 attributes: [ 'id', 'url', 'followersUrl', 'preferredUsername' ],
163 model: ActorModel.unscoped(),
166 preferredUsername: accountName
172 model: VideoModel.unscoped(),
177 if (t) options.transaction = t
179 return AccountVideoRateModel.findOne(options)
182 static loadByUrl (url: string, transaction: Transaction) {
183 const options: FindOptions = {
188 if (transaction) options.transaction = transaction
190 return AccountVideoRateModel.findOne(options)
193 static listAndCountAccountUrlsByVideoId (rateType: VideoRateType, videoId: number, start: number, count: number, t?: Transaction) {
204 attributes: [ 'actorId' ],
205 model: AccountModel.unscoped(),
209 attributes: [ 'url' ],
210 model: ActorModel.unscoped(),
218 return AccountVideoRateModel.findAndCountAll<MAccountVideoRateAccountUrl>(query)
221 static cleanOldRatesOf (videoId: number, type: VideoRateType, beforeUpdatedAt: Date) {
222 return AccountVideoRateModel.sequelize.transaction(async t => {
226 [Op.lt]: beforeUpdatedAt
231 [Op.notIn]: buildLocalAccountIdsIn()
237 const deleted = await AccountVideoRateModel.destroy(query)
246 if (type === 'like') await VideoModel.increment({ likes: -deleted }, options)
247 else if (type === 'dislike') await VideoModel.increment({ dislikes: -deleted }, options)
251 toFormattedJSON (): AccountVideoRate {
253 video: this.Video.toFormattedJSON(),