]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/models/activitypub/actor.ts
Add peertube plugin index website models
[github/Chocobozzz/PeerTube.git] / server / models / activitypub / actor.ts
index 2fceb21ddf87cec326bfb59be39725361ce0b66e..bd6a2c8fdbf712bdfb942f4a6412de2b14663fc5 100644 (file)
@@ -7,13 +7,11 @@ import {
   Column,
   CreatedAt,
   DataType,
-  Default,
   DefaultScope,
   ForeignKey,
   HasMany,
   HasOne,
   Is,
-  IsUUID,
   Model,
   Scopes,
   Table,
@@ -30,11 +28,11 @@ import {
   isActorPublicKeyValid
 } from '../../helpers/custom-validators/activitypub/actor'
 import { isActivityPubUrlValid } from '../../helpers/custom-validators/activitypub/misc'
-import { ACTIVITY_PUB, ACTIVITY_PUB_ACTOR_TYPES, CONFIG, CONSTRAINTS_FIELDS } from '../../initializers'
+import { ACTIVITY_PUB, ACTIVITY_PUB_ACTOR_TYPES, CONSTRAINTS_FIELDS, WEBSERVER } from '../../initializers/constants'
 import { AccountModel } from '../account/account'
 import { AvatarModel } from '../avatar/avatar'
 import { ServerModel } from '../server/server'
-import { throwIfNotValid } from '../utils'
+import { isOutdated, throwIfNotValid } from '../utils'
 import { VideoChannelModel } from '../video/video-channel'
 import { ActorFollowModel } from './actor-follow'
 import { VideoModel } from '../video/video'
@@ -56,46 +54,46 @@ export const unusedActorAttributesForAPI = [
   'updatedAt'
 ]
 
-@DefaultScope({
+@DefaultScope(() => ({
   include: [
     {
-      model: () => ServerModel,
+      model: ServerModel,
       required: false
     },
     {
-      model: () => AvatarModel,
+      model: AvatarModel,
       required: false
     }
   ]
-})
-@Scopes({
+}))
+@Scopes(() => ({
   [ScopeNames.FULL]: {
     include: [
       {
-        model: () => AccountModel.unscoped(),
+        model: AccountModel.unscoped(),
         required: false
       },
       {
-        model: () => VideoChannelModel.unscoped(),
+        model: VideoChannelModel.unscoped(),
         required: false,
         include: [
           {
-            model: () => AccountModel,
+            model: AccountModel,
             required: true
           }
         ]
       },
       {
-        model: () => ServerModel,
+        model: ServerModel,
         required: false
       },
       {
-        model: () => AvatarModel,
+        model: AvatarModel,
         required: false
       }
     ]
   }
-})
+}))
 @Table({
   tableName: 'actor',
   indexes: [
@@ -119,10 +117,6 @@ export const unusedActorAttributesForAPI = [
     {
       fields: [ 'avatarId' ]
     },
-    {
-      fields: [ 'uuid' ],
-      unique: true
-    },
     {
       fields: [ 'followersUrl' ]
     }
@@ -131,15 +125,9 @@ export const unusedActorAttributesForAPI = [
 export class ActorModel extends Model<ActorModel> {
 
   @AllowNull(false)
-  @Column(DataType.ENUM(values(ACTIVITY_PUB_ACTOR_TYPES)))
+  @Column(DataType.ENUM(...values(ACTIVITY_PUB_ACTOR_TYPES)))
   type: ActivityPubActorType
 
-  @AllowNull(false)
-  @Default(DataType.UUIDV4)
-  @IsUUID(4)
-  @Column(DataType.UUID)
-  uuid: string
-
   @AllowNull(false)
   @Is('ActorPreferredUsername', value => throwIfNotValid(value, isActorPreferredUsernameValid, 'actor preferred username'))
   @Column
@@ -151,12 +139,12 @@ export class ActorModel extends Model<ActorModel> {
   url: string
 
   @AllowNull(true)
-  @Is('ActorPublicKey', value => throwIfNotValid(value, isActorPublicKeyValid, 'public key'))
+  @Is('ActorPublicKey', value => throwIfNotValid(value, isActorPublicKeyValid, 'public key', true))
   @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.PUBLIC_KEY.max))
   publicKey: string
 
   @AllowNull(true)
-  @Is('ActorPublicKey', value => throwIfNotValid(value, isActorPrivateKeyValid, 'private key'))
+  @Is('ActorPublicKey', value => throwIfNotValid(value, isActorPrivateKeyValid, 'private key', true))
   @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.PRIVATE_KEY.max))
   privateKey: string
 
@@ -280,14 +268,16 @@ export class ActorModel extends Model<ActorModel> {
               attributes: [ 'id' ],
               model: VideoChannelModel.unscoped(),
               required: true,
-              include: {
-                attributes: [ 'id' ],
-                model: VideoModel.unscoped(),
-                required: true,
-                where: {
-                  id: videoId
+              include: [
+                {
+                  attributes: [ 'id' ],
+                  model: VideoModel.unscoped(),
+                  required: true,
+                  where: {
+                    id: videoId
+                  }
                 }
-              }
+              ]
             }
           ]
         }
@@ -295,7 +285,7 @@ export class ActorModel extends Model<ActorModel> {
       transaction
     }
 
-    return ActorModel.unscoped().findOne(query as any) // FIXME: typings
+    return ActorModel.unscoped().findOne(query)
   }
 
   static isActorUrlExist (url: string) {
@@ -389,8 +379,7 @@ export class ActorModel extends Model<ActorModel> {
   }
 
   static incrementFollows (id: number, column: 'followersCount' | 'followingCount', by: number) {
-    // FIXME: typings
-    return (ActorModel as any).increment(column, {
+    return ActorModel.increment(column, {
       by,
       where: {
         id
@@ -407,7 +396,6 @@ export class ActorModel extends Model<ActorModel> {
     return {
       id: this.id,
       url: this.url,
-      uuid: this.uuid,
       name: this.preferredUsername,
       host: this.getHost(),
       hostRedundancyAllowed: this.getRedundancyAllowed(),
@@ -453,7 +441,6 @@ export class ActorModel extends Model<ActorModel> {
       endpoints: {
         sharedInbox: this.sharedInboxUrl
       },
-      uuid: this.uuid,
       publicKey: {
         id: this.getPublicKeyUrl(),
         owner: this.url,
@@ -516,7 +503,7 @@ export class ActorModel extends Model<ActorModel> {
   }
 
   getHost () {
-    return this.Server ? this.Server.host : CONFIG.WEBSERVER.HOST
+    return this.Server ? this.Server.host : WEBSERVER.HOST
   }
 
   getRedundancyAllowed () {
@@ -526,17 +513,12 @@ export class ActorModel extends Model<ActorModel> {
   getAvatarUrl () {
     if (!this.avatarId) return undefined
 
-    return CONFIG.WEBSERVER.URL + this.Avatar.getWebserverPath()
+    return WEBSERVER.URL + this.Avatar.getWebserverPath()
   }
 
   isOutdated () {
     if (this.isOwned()) return false
 
-    const now = Date.now()
-    const createdAtTime = this.createdAt.getTime()
-    const updatedAtTime = this.updatedAt.getTime()
-
-    return (now - createdAtTime) > ACTIVITY_PUB.ACTOR_REFRESH_INTERVAL &&
-      (now - updatedAtTime) > ACTIVITY_PUB.ACTOR_REFRESH_INTERVAL
+    return isOutdated(this, ACTIVITY_PUB.ACTOR_REFRESH_INTERVAL)
   }
 }