]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/models/activitypub/actor.ts
Translated using Weblate (Catalan)
[github/Chocobozzz/PeerTube.git] / server / models / activitypub / actor.ts
index 007647ced187f20c46ddf850543e684d1f7eb7d7..00e8dc9541614b04a6f154742d66cb39bac8ed56 100644 (file)
@@ -16,7 +16,7 @@ import {
   Table,
   UpdatedAt
 } from 'sequelize-typescript'
-import { ActivityPubActorType } from '../../../shared/models/activitypub'
+import { ActivityIconObject, ActivityPubActorType } from '../../../shared/models/activitypub'
 import { Avatar } from '../../../shared/models/avatars/avatar.model'
 import { activityPubContextify } from '../../helpers/activitypub'
 import {
@@ -43,7 +43,7 @@ import {
   MActorFull,
   MActorHost,
   MActorServer,
-  MActorSummaryFormattable,
+  MActorSummaryFormattable, MActorUrl,
   MActorWithInboxes
 } from '../../typings/models'
 import * as Bluebird from 'bluebird'
@@ -276,7 +276,8 @@ export class ActorModel extends Model<ActorModel> {
   })
   VideoChannel: VideoChannelModel
 
-  private static cache: { [ id: string ]: any } = {}
+  private static localNameCache: { [ id: string ]: any } = {}
+  private static localUrlCache: { [ id: string ]: any } = {}
 
   static load (id: number): Bluebird<MActor> {
     return ActorModel.unscoped().findByPk(id)
@@ -334,7 +335,7 @@ export class ActorModel extends Model<ActorModel> {
     const query = {
       where: {
         followersUrl: {
-          [ Op.in ]: followersUrls
+          [Op.in]: followersUrls
         }
       },
       transaction
@@ -345,8 +346,8 @@ export class ActorModel extends Model<ActorModel> {
 
   static loadLocalByName (preferredUsername: string, transaction?: Transaction): Bluebird<MActorFull> {
     // The server actor never change, so we can easily cache it
-    if (preferredUsername === SERVER_ACTOR_NAME && ActorModel.cache[preferredUsername]) {
-      return Bluebird.resolve(ActorModel.cache[preferredUsername])
+    if (preferredUsername === SERVER_ACTOR_NAME && ActorModel.localNameCache[preferredUsername]) {
+      return Bluebird.resolve(ActorModel.localNameCache[preferredUsername])
     }
 
     const query = {
@@ -361,7 +362,33 @@ export class ActorModel extends Model<ActorModel> {
                      .findOne(query)
                      .then(actor => {
                        if (preferredUsername === SERVER_ACTOR_NAME) {
-                         ActorModel.cache[ preferredUsername ] = actor
+                         ActorModel.localNameCache[preferredUsername] = actor
+                       }
+
+                       return actor
+                     })
+  }
+
+  static loadLocalUrlByName (preferredUsername: string, transaction?: Transaction): Bluebird<MActorUrl> {
+    // The server actor never change, so we can easily cache it
+    if (preferredUsername === SERVER_ACTOR_NAME && ActorModel.localUrlCache[preferredUsername]) {
+      return Bluebird.resolve(ActorModel.localUrlCache[preferredUsername])
+    }
+
+    const query = {
+      attributes: [ 'url' ],
+      where: {
+        preferredUsername,
+        serverId: null
+      },
+      transaction
+    }
+
+    return ActorModel.unscoped()
+                     .findOne(query)
+                     .then(actor => {
+                       if (preferredUsername === SERVER_ACTOR_NAME) {
+                         ActorModel.localUrlCache[preferredUsername] = actor
                        }
 
                        return actor
@@ -473,9 +500,11 @@ export class ActorModel extends Model<ActorModel> {
   }
 
   toActivityPubObject (this: MActorAP, name: string) {
-    let icon = undefined
+    let icon: ActivityIconObject
+
     if (this.avatarId) {
       const extension = extname(this.Avatar.filename)
+
       icon = {
         type: 'Image',
         mediaType: extension === '.png' ? 'image/png' : 'image/jpeg',