X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Fmodels%2Faccount%2Faccount.ts;h=a0081f25995cfefc34b4d4cc5d442e925d428be6;hb=3f87a46f1dd01485e4a28422f74c3a1b0f56729f;hp=0905a0fb2affe7a04a41afdacd7fe619c603712b;hpb=7cd1b12c19d0589d1d692ed0571ca0800f028aea;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/models/account/account.ts b/server/models/account/account.ts index 0905a0fb2..a0081f259 100644 --- a/server/models/account/account.ts +++ b/server/models/account/account.ts @@ -32,8 +32,9 @@ 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 } from '../../typings/models' import * as Bluebird from 'bluebird' +import { ModelCache } from '@server/models/model-cache' export enum ScopeNames { SUMMARY = 'SUMMARY' @@ -218,8 +219,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 +246,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 {