X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Fmodels%2Faccount%2Faccount.ts;h=f97519b1403eac1c5d42280aa40ea5e8bf9e6a52;hb=31c82cd914e13dbf53280d0aad0740d70c414441;hp=0905a0fb2affe7a04a41afdacd7fe619c603712b;hpb=a15871560f80e07386c1dabb8370cd2664ecfd1f;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/models/account/account.ts b/server/models/account/account.ts index 0905a0fb2..f97519b14 100644 --- a/server/models/account/account.ts +++ b/server/models/account/account.ts @@ -32,14 +32,17 @@ import { FindOptions, IncludeOptions, Op, Transaction, WhereOptions } from 'sequ import { AccountBlocklistModel } from './account-blocklist' import { ServerBlocklistModel } from '../server/server-blocklist' import { ActorFollowModel } from '../activitypub/actor-follow' -import { MAccountActor, MAccountDefault, MAccountSummaryFormattable, MAccountFormattable, MAccountAP } from '../../typings/models' +import { MAccountActor, MAccountAP, MAccountDefault, MAccountFormattable, MAccountSummaryFormattable, MAccount } from '../../types/models' import * as Bluebird from 'bluebird' +import { ModelCache } from '@server/models/model-cache' +import { VideoModel } from '../video/video' export enum ScopeNames { SUMMARY = 'SUMMARY' } export type SummaryOptions = { + actorRequired?: boolean // Default: true whereActor?: WhereOptions withAccountBlockerIds?: number[] } @@ -63,12 +66,12 @@ export type SummaryOptions = { } const query: FindOptions = { - attributes: [ 'id', 'name' ], + attributes: [ 'id', 'name', 'actorId' ], include: [ { attributes: [ 'id', 'preferredUsername', 'url', 'serverId', 'avatarId' ], model: ActorModel.unscoped(), - required: true, + required: options.actorRequired ?? true, where: whereActor, include: [ serverInclude, @@ -218,8 +221,6 @@ export class AccountModel extends Model { }) BlockedAccounts: AccountBlocklistModel[] - private static cache: { [ id: string ]: any } = {} - @BeforeDestroy static async sendDeleteIfOwned (instance: AccountModel, options) { if (!instance.Actor) { @@ -247,45 +248,43 @@ export class AccountModel extends Model { } static loadLocalByName (name: string): Bluebird { - // The server actor never change, so we can easily cache it - if (name === SERVER_ACTOR_NAME && AccountModel.cache[name]) { - return Bluebird.resolve(AccountModel.cache[name]) - } - - const query = { - where: { - [Op.or]: [ - { - userId: { - [Op.ne]: null + const fun = () => { + const query = { + where: { + [Op.or]: [ + { + userId: { + [Op.ne]: null + } + }, + { + applicationId: { + [Op.ne]: null + } } - }, + ] + }, + include: [ { - applicationId: { - [Op.ne]: null + model: ActorModel, + required: true, + where: { + preferredUsername: name } } ] - }, - include: [ - { - model: ActorModel, - required: true, - where: { - preferredUsername: name - } - } - ] - } + } - return AccountModel.findOne(query) - .then(account => { - if (name === SERVER_ACTOR_NAME) { - AccountModel.cache[name] = account - } + return AccountModel.findOne(query) + } - return account - }) + return ModelCache.Instance.doCache({ + cacheType: 'local-account-name', + key: name, + fun, + // The server actor never change, so we can easily cache it + whitelist: () => name === SERVER_ACTOR_NAME + }) } static loadByNameAndHost (name: string, host: string): Bluebird { @@ -346,6 +345,29 @@ export class AccountModel extends Model { }) } + static loadAccountIdFromVideo (videoId: number): Bluebird { + const query = { + include: [ + { + attributes: [ 'id', 'accountId' ], + model: VideoChannelModel.unscoped(), + required: true, + include: [ + { + attributes: [ 'id', 'channelId' ], + model: VideoModel.unscoped(), + where: { + id: videoId + } + } + ] + } + ] + } + + return AccountModel.findOne(query) + } + static listLocalsForSitemap (sort: string): Bluebird { const query = { attributes: [ ], @@ -367,6 +389,10 @@ export class AccountModel extends Model { .findAll(query) } + getClientUrl () { + return WEBSERVER.URL + '/accounts/' + this.Actor.getIdentifier() + } + toFormattedJSON (this: MAccountFormattable): Account { const actor = this.Actor.toFormattedJSON() const account = {