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'
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 loadByAccountAndVideoOrUrl (accountId: number, videoId: number, url: string, transaction?: Transaction) {
93 const options: FindOptions = {
106 if (transaction) options.transaction = transaction
108 return AccountVideoRateModel.findOne(options)
111 static listByAccountForApi (options: {
118 const query: FindOptions = {
119 offset: options.start,
120 limit: options.count,
121 order: getSort(options.sort),
123 accountId: options.accountId
131 model: VideoChannelModel.scope({ method: [VideoChannelScopeNames.SUMMARY, { withAccount: true } as SummaryOptions ] }),
138 if (options.type) query.where['type'] = options.type
140 return AccountVideoRateModel.findAndCountAll(query)
143 static loadLocalAndPopulateVideo (rateType: VideoRateType, accountName: string, videoId: number, transaction?: Transaction) {
144 const options: FindOptions = {
151 model: AccountModel.unscoped(),
155 attributes: [ 'id', 'url', 'preferredUsername' ],
156 model: ActorModel.unscoped(),
159 preferredUsername: accountName
165 model: VideoModel.unscoped(),
170 if (transaction) options.transaction = transaction
172 return AccountVideoRateModel.findOne(options)
175 static loadByUrl (url: string, transaction: Transaction) {
176 const options: FindOptions = {
181 if (transaction) options.transaction = transaction
183 return AccountVideoRateModel.findOne(options)
186 static listAndCountAccountUrlsByVideoId (rateType: VideoRateType, videoId: number, start: number, count: number, t?: Transaction) {
197 attributes: [ 'actorId' ],
198 model: AccountModel.unscoped(),
202 attributes: [ 'url' ],
203 model: ActorModel.unscoped(),
211 return AccountVideoRateModel.findAndCountAll(query)
214 static cleanOldRatesOf (videoId: number, type: VideoRateType, beforeUpdatedAt: Date) {
215 return AccountVideoRateModel.sequelize.transaction(async t => {
219 [Op.lt]: beforeUpdatedAt
224 [Op.notIn]: buildLocalAccountIdsIn()
230 const deleted = await AccountVideoRateModel.destroy(query)
239 if (type === 'like') await VideoModel.increment({ likes: -deleted }, options)
240 else if (type === 'dislike') await VideoModel.increment({ dislikes: -deleted }, options)
244 toFormattedJSON (): AccountVideoRate {
246 video: this.Video.toFormattedJSON(),