]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/models/account/account.ts
Update default user theme to instance-default
[github/Chocobozzz/PeerTube.git] / server / models / account / account.ts
index 0905a0fb2affe7a04a41afdacd7fe619c603712b..a0081f25995cfefc34b4d4cc5d442e925d428be6 100644 (file)
@@ -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<AccountModel> {
   })
   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<AccountModel> {
   }
 
   static loadLocalByName (name: string): Bluebird<MAccountDefault> {
-    // 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<MAccountDefault> {