]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/models/activitypub/actor.ts
Fix videos list user NSFW policy
[github/Chocobozzz/PeerTube.git] / server / models / activitypub / actor.ts
index f231dba742df7d940a46cf48ff49f63383057021..f8bb593239dbbe47792f193ad66766d394f9f945 100644 (file)
@@ -3,7 +3,6 @@ import { extname } from 'path'
 import * as Sequelize from 'sequelize'
 import {
   AllowNull,
-  BeforeDestroy,
   BelongsTo,
   Column,
   CreatedAt,
@@ -38,13 +37,24 @@ import { ServerModel } from '../server/server'
 import { throwIfNotValid } from '../utils'
 import { VideoChannelModel } from '../video/video-channel'
 import { ActorFollowModel } from './actor-follow'
-import { logger } from '../../helpers/logger'
-import { sendDeleteActor } from '../../lib/activitypub/send'
 
 enum ScopeNames {
   FULL = 'FULL'
 }
 
+export const unusedActorAttributesForAPI = [
+  'publicKey',
+  'privateKey',
+  'inboxUrl',
+  'outboxUrl',
+  'sharedInboxUrl',
+  'followersUrl',
+  'followingUrl',
+  'url',
+  'createdAt',
+  'updatedAt'
+]
+
 @DefaultScope({
   include: [
     {
@@ -66,7 +76,13 @@ enum ScopeNames {
       },
       {
         model: () => VideoChannelModel.unscoped(),
-        required: false
+        required: false,
+        include: [
+          {
+            model: () => AccountModel,
+            required: true
+          }
+        ]
       },
       {
         model: () => ServerModel,
@@ -93,6 +109,9 @@ enum ScopeNames {
     {
       fields: [ 'inboxUrl', 'sharedInboxUrl' ]
     },
+    {
+      fields: [ 'sharedInboxUrl' ]
+    },
     {
       fields: [ 'serverId' ]
     },
@@ -227,32 +246,38 @@ export class ActorModel extends Model<ActorModel> {
 
   @HasOne(() => AccountModel, {
     foreignKey: {
-      allowNull: false
-    }
+      allowNull: true
+    },
+    onDelete: 'cascade',
+    hooks: true
   })
   Account: AccountModel
 
   @HasOne(() => VideoChannelModel, {
     foreignKey: {
-      allowNull: false
-    }
+      allowNull: true
+    },
+    onDelete: 'cascade',
+    hooks: true
   })
   VideoChannel: VideoChannelModel
 
-  @BeforeDestroy
-  static async sendDeleteIfOwned (instance: ActorModel, options) {
-    if (instance.isOwned()) {
-      logger.debug('Sending delete of actor %s.', instance.url)
-      return sendDeleteActor(instance, options.transaction)
-    }
-
-    return undefined
-  }
-
   static load (id: number) {
     return ActorModel.unscoped().findById(id)
   }
 
+  static isActorUrlExist (url: string) {
+    const query = {
+      raw: true,
+      where: {
+        url
+      }
+    }
+
+    return ActorModel.unscoped().findOne(query)
+      .then(a => !!a)
+  }
+
   static listByFollowersUrls (followersUrls: string[], transaction?: Sequelize.Transaction) {
     const query = {
       where: {
@@ -266,12 +291,13 @@ export class ActorModel extends Model<ActorModel> {
     return ActorModel.scope(ScopeNames.FULL).findAll(query)
   }
 
-  static loadLocalByName (preferredUsername: string) {
+  static loadLocalByName (preferredUsername: string, transaction?: Sequelize.Transaction) {
     const query = {
       where: {
         preferredUsername,
         serverId: null
-      }
+      },
+      transaction
     }
 
     return ActorModel.scope(ScopeNames.FULL).findOne(query)
@@ -297,6 +323,29 @@ export class ActorModel extends Model<ActorModel> {
   }
 
   static loadByUrl (url: string, transaction?: Sequelize.Transaction) {
+    const query = {
+      where: {
+        url
+      },
+      transaction,
+      include: [
+        {
+          attributes: [ 'id' ],
+          model: AccountModel.unscoped(),
+          required: false
+        },
+        {
+          attributes: [ 'id' ],
+          model: VideoChannelModel.unscoped(),
+          required: false
+        }
+      ]
+    }
+
+    return ActorModel.unscoped().findOne(query)
+  }
+
+  static loadByUrlAndPopulateAccountAndChannel (url: string, transaction?: Sequelize.Transaction) {
     const query = {
       where: {
         url
@@ -317,45 +366,6 @@ export class ActorModel extends Model<ActorModel> {
     })
   }
 
-  static async getActorsFollowerSharedInboxUrls (actors: ActorModel[], t: Sequelize.Transaction) {
-    const query = {
-      // attribute: [],
-      where: {
-        id: {
-          [Sequelize.Op.in]: actors.map(a => a.id)
-        }
-      },
-      include: [
-        {
-          // attributes: [ ],
-          model: ActorFollowModel.unscoped(),
-          required: true,
-          as: 'ActorFollowers',
-          where: {
-            state: 'accepted'
-          },
-          include: [
-            {
-              attributes: [ 'sharedInboxUrl' ],
-              model: ActorModel.unscoped(),
-              as: 'ActorFollower',
-              required: true
-            }
-          ]
-        }
-      ],
-      transaction: t
-    }
-
-    const hash: { [ id: number ]: string[] } = {}
-    const res = await ActorModel.findAll(query)
-    for (const actor of res) {
-      hash[actor.id] = actor.ActorFollowers.map(follow => follow.ActorFollower.sharedInboxUrl)
-    }
-
-    return hash
-  }
-
   toFormattedJSON () {
     let avatar: Avatar = null
     if (this.Avatar) {
@@ -368,6 +378,7 @@ export class ActorModel extends Model<ActorModel> {
       uuid: this.uuid,
       name: this.preferredUsername,
       host: this.getHost(),
+      hostRedundancyAllowed: this.getRedundancyAllowed(),
       followingCount: this.followingCount,
       followersCount: this.followersCount,
       avatar,
@@ -463,10 +474,18 @@ export class ActorModel extends Model<ActorModel> {
     return 'acct:' + this.preferredUsername + '@' + this.getHost()
   }
 
+  getIdentifier () {
+    return this.Server ? `${this.preferredUsername}@${this.Server.host}` : this.preferredUsername
+  }
+
   getHost () {
     return this.Server ? this.Server.host : CONFIG.WEBSERVER.HOST
   }
 
+  getRedundancyAllowed () {
+    return this.Server ? this.Server.redundancyAllowed : false
+  }
+
   getAvatarUrl () {
     if (!this.avatarId) return undefined