X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Fmodels%2Faccount%2Faccount-video-rate.ts;h=8aeb486d1fef684e65033d4028c1b500b649ad19;hb=8bb71f2e332726c207318e44b95e784f9740dc40;hp=d5c214ecbc69b1a9e82395404b88b5c680642aab;hpb=970ceac0a6bf4990b8924738591df4949491ec9b;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/models/account/account-video-rate.ts b/server/models/account/account-video-rate.ts index d5c214ecb..8aeb486d1 100644 --- a/server/models/account/account-video-rate.ts +++ b/server/models/account/account-video-rate.ts @@ -6,10 +6,17 @@ import { CONSTRAINTS_FIELDS, VIDEO_RATE_TYPES } from '../../initializers/constan import { VideoModel } from '../video/video' import { AccountModel } from './account' import { ActorModel } from '../activitypub/actor' -import { getSort, 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' +import * as Bluebird from 'bluebird' +import { + MAccountVideoRate, + MAccountVideoRateAccountUrl, + MAccountVideoRateAccountVideo, + MAccountVideoRateFormattable +} from '@server/typings/models/video/video-rate' /* Account rates per video. @@ -77,7 +84,7 @@ export class AccountVideoRateModel extends Model { }) Account: AccountModel - static load (accountId: number, videoId: number, transaction?: Transaction) { + static load (accountId: number, videoId: number, transaction?: Transaction): Bluebird { const options: FindOptions = { where: { accountId, @@ -89,10 +96,10 @@ export class AccountVideoRateModel extends Model { return AccountVideoRateModel.findOne(options) } - static loadByAccountAndVideoOrUrl (accountId: number, videoId: number, url: string, transaction?: Transaction) { + static loadByAccountAndVideoOrUrl (accountId: number, videoId: number, url: string, t?: Transaction): Bluebird { const options: FindOptions = { where: { - [ Op.or]: [ + [Op.or]: [ { accountId, videoId @@ -103,16 +110,16 @@ export class AccountVideoRateModel extends Model { ] } } - if (transaction) options.transaction = transaction + if (t) options.transaction = t return AccountVideoRateModel.findOne(options) } static listByAccountForApi (options: { - start: number, - count: number, - sort: string, - type?: string, + start: number + count: number + sort: string + type?: string accountId: number }) { const query: FindOptions = { @@ -128,7 +135,7 @@ export class AccountVideoRateModel extends Model { required: true, include: [ { - model: VideoChannelModel.scope({ method: [VideoChannelScopeNames.SUMMARY, { withAccount: true } as SummaryOptions ] }), + model: VideoChannelModel.scope({ method: [ VideoChannelScopeNames.SUMMARY, { withAccount: true } as SummaryOptions ] }), required: true } ] @@ -140,7 +147,12 @@ export class AccountVideoRateModel extends Model { return AccountVideoRateModel.findAndCountAll(query) } - static loadLocalAndPopulateVideo (rateType: VideoRateType, accountName: string, videoId: number, transaction?: Transaction) { + static loadLocalAndPopulateVideo ( + rateType: VideoRateType, + accountName: string, + videoId: number | string, + t?: Transaction + ): Bluebird { const options: FindOptions = { where: { videoId, @@ -152,7 +164,7 @@ export class AccountVideoRateModel extends Model { required: true, include: [ { - attributes: [ 'id', 'url', 'preferredUsername' ], + attributes: [ 'id', 'url', 'followersUrl', 'preferredUsername' ], model: ActorModel.unscoped(), required: true, where: { @@ -167,7 +179,7 @@ export class AccountVideoRateModel extends Model { } ] } - if (transaction) options.transaction = transaction + if (t) options.transaction = t return AccountVideoRateModel.findOne(options) } @@ -208,7 +220,7 @@ export class AccountVideoRateModel extends Model { ] } - return AccountVideoRateModel.findAndCountAll(query) + return AccountVideoRateModel.findAndCountAll(query) } static cleanOldRatesOf (videoId: number, type: VideoRateType, beforeUpdatedAt: Date) { @@ -219,25 +231,11 @@ export class AccountVideoRateModel extends Model { [Op.lt]: beforeUpdatedAt }, videoId, - type - }, - include: [ - { - model: AccountModel.unscoped(), - required: true, - include: [ - { - model: ActorModel.unscoped(), - required: true, - where: { - serverId: { - [Op.ne]: null - } - } - } - ] + type, + accountId: { + [Op.notIn]: buildLocalAccountIdsIn() } - ], + }, transaction: t } @@ -255,7 +253,7 @@ export class AccountVideoRateModel extends Model { }) } - toFormattedJSON (): AccountVideoRate { + toFormattedJSON (this: MAccountVideoRateFormattable): AccountVideoRate { return { video: this.Video.toFormattedJSON(), rating: this.type