X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;ds=sidebyside;f=server%2Fmodels%2Faccount%2Faccount-video-rate.ts;h=f462df4b33d78e6f498f01111862fd5acac10349;hb=c100a6142e6571312db9f6407698a21a08b593fb;hp=18762f0c55901be4bcaeb623a820bc4ee1d9147d;hpb=ae28cdf327d782e629379eee1999096ca2a5d74b;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/models/account/account-video-rate.ts b/server/models/account/account-video-rate.ts index 18762f0c5..f462df4b3 100644 --- a/server/models/account/account-video-rate.ts +++ b/server/models/account/account-video-rate.ts @@ -1,5 +1,5 @@ import { values } from 'lodash' -import { Transaction } from 'sequelize' +import { Transaction, Op } from 'sequelize' import { AllowNull, BelongsTo, Column, CreatedAt, DataType, ForeignKey, Is, Model, Table, UpdatedAt } from 'sequelize-typescript' import { IFindOptions } from 'sequelize-typescript/lib/interfaces/IFindOptions' import { VideoRateType } from '../../../shared/models/videos' @@ -7,8 +7,10 @@ import { CONSTRAINTS_FIELDS, VIDEO_RATE_TYPES } from '../../initializers' import { VideoModel } from '../video/video' import { AccountModel } from './account' import { ActorModel } from '../activitypub/actor' -import { throwIfNotValid } from '../utils' +import { throwIfNotValid, getSort } from '../utils' import { isActivityPubUrlValid } from '../../helpers/custom-validators/activitypub/misc' +import { AccountVideoRate } from '../../../shared' +import { VideoChannelModel, ScopeNames as VideoChannelScopeNames } from '../video/video-channel' /* Account rates per video. @@ -88,6 +90,38 @@ export class AccountVideoRateModel extends Model { return AccountVideoRateModel.findOne(options) } + static listByAccountForApi (options: { + start: number, + count: number, + sort: string, + type?: string, + accountId: number + }) { + const query: IFindOptions = { + offset: options.start, + limit: options.count, + order: getSort(options.sort), + where: { + accountId: options.accountId + }, + include: [ + { + model: VideoModel, + required: true, + include: [ + { + model: VideoChannelModel.scope({ method: [VideoChannelScopeNames.SUMMARY, true] }), + required: true + } + ] + } + ] + } + if (options.type) query.where['type'] = options.type + + return AccountVideoRateModel.findAndCountAll(query) + } + static loadLocalAndPopulateVideo (rateType: VideoRateType, accountName: string, videoId: number, transaction?: Transaction) { const options: IFindOptions = { where: { @@ -158,4 +192,38 @@ export class AccountVideoRateModel extends Model { return AccountVideoRateModel.findAndCountAll(query) } + + static cleanOldRatesOf (videoId: number, type: VideoRateType, beforeUpdatedAt: Date) { + return AccountVideoRateModel.sequelize.transaction(async t => { + const query = { + where: { + updatedAt: { + [Op.lt]: beforeUpdatedAt + }, + videoId, + type + }, + transaction: t + } + + const deleted = await AccountVideoRateModel.destroy(query) + + const options = { + transaction: t, + where: { + id: videoId + } + } + + if (type === 'like') await VideoModel.increment({ likes: -deleted }, options) + else if (type === 'dislike') await VideoModel.increment({ dislikes: -deleted }, options) + }) + } + + toFormattedJSON (): AccountVideoRate { + return { + video: this.Video.toFormattedJSON(), + rating: this.type + } + } }