X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Fmodels%2Faccount%2Faccount-video-rate.ts;h=4bd8114cf6e2a718a781b89a25fd8b4d42d64bc6;hb=13176a07a95984a53cc59aec5217f2ce9806d1bc;hp=e5d39582bdf7698f9b36e3adf1a5e7764b054be5;hpb=2ba92871319d7af63472c1380664a9f9eeb1c690;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/models/account/account-video-rate.ts b/server/models/account/account-video-rate.ts index e5d39582b..4bd8114cf 100644 --- a/server/models/account/account-video-rate.ts +++ b/server/models/account/account-video-rate.ts @@ -1,14 +1,15 @@ import { values } from 'lodash' -import { Transaction, Op } from 'sequelize' +import { FindOptions, Op, Transaction } 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' -import { CONSTRAINTS_FIELDS, VIDEO_RATE_TYPES } from '../../initializers' +import { CONSTRAINTS_FIELDS, VIDEO_RATE_TYPES } from '../../initializers/constants' import { VideoModel } from '../video/video' import { AccountModel } from './account' import { ActorModel } from '../activitypub/actor' -import { throwIfNotValid } from '../utils' +import { buildLocalAccountIdsIn, getSort, throwIfNotValid } from '../utils' import { isActivityPubUrlValid } from '../../helpers/custom-validators/activitypub/misc' +import { AccountVideoRate } from '../../../shared' +import { ScopeNames as VideoChannelScopeNames, SummaryOptions, VideoChannelModel } from '../video/video-channel' /* Account rates per video. @@ -38,7 +39,7 @@ import { isActivityPubUrlValid } from '../../helpers/custom-validators/activityp export class AccountVideoRateModel extends Model { @AllowNull(false) - @Column(DataType.ENUM(values(VIDEO_RATE_TYPES))) + @Column(DataType.ENUM(...values(VIDEO_RATE_TYPES))) type: VideoRateType @AllowNull(false) @@ -77,7 +78,7 @@ export class AccountVideoRateModel extends Model { Account: AccountModel static load (accountId: number, videoId: number, transaction?: Transaction) { - const options: IFindOptions = { + const options: FindOptions = { where: { accountId, videoId @@ -88,8 +89,59 @@ export class AccountVideoRateModel extends Model { return AccountVideoRateModel.findOne(options) } + static loadByAccountAndVideoOrUrl (accountId: number, videoId: number, url: string, transaction?: Transaction) { + const options: FindOptions = { + where: { + [ Op.or]: [ + { + accountId, + videoId + }, + { + url + } + ] + } + } + if (transaction) options.transaction = transaction + + return AccountVideoRateModel.findOne(options) + } + + static listByAccountForApi (options: { + start: number, + count: number, + sort: string, + type?: string, + accountId: number + }) { + const query: FindOptions = { + 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, { withAccount: true } as SummaryOptions ] }), + 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 = { + const options: FindOptions = { where: { videoId, type: rateType @@ -121,7 +173,7 @@ export class AccountVideoRateModel extends Model { } static loadByUrl (url: string, transaction: Transaction) { - const options: IFindOptions = { + const options: FindOptions = { where: { url } @@ -167,7 +219,10 @@ export class AccountVideoRateModel extends Model { [Op.lt]: beforeUpdatedAt }, videoId, - type + type, + accountId: { + [Op.notIn]: buildLocalAccountIdsIn() + } }, transaction: t } @@ -185,4 +240,11 @@ export class AccountVideoRateModel extends Model { else if (type === 'dislike') await VideoModel.increment({ dislikes: -deleted }, options) }) } + + toFormattedJSON (): AccountVideoRate { + return { + video: this.Video.toFormattedJSON(), + rating: this.type + } + } }