]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/models/activitypub/actor.ts
Begin advanced search
[github/Chocobozzz/PeerTube.git] / server / models / activitypub / actor.ts
index 17f69f7a7aaa157caef66c60695211be230d0f48..38a689fea5da9ac7481d3c5480c5b188077bdfd8 100644 (file)
@@ -2,14 +2,31 @@ import { values } from 'lodash'
 import { extname } from 'path'
 import * as Sequelize from 'sequelize'
 import {
-  AllowNull, BelongsTo, Column, CreatedAt, DataType, Default, DefaultScope, ForeignKey, HasMany, HasOne, Is, IsUUID, Model, Scopes,
-  Table, UpdatedAt
+  AllowNull,
+  BelongsTo,
+  Column,
+  CreatedAt,
+  DataType,
+  Default,
+  DefaultScope,
+  ForeignKey,
+  HasMany,
+  HasOne,
+  Is,
+  IsUUID,
+  Model,
+  Scopes,
+  Table,
+  UpdatedAt
 } from 'sequelize-typescript'
 import { ActivityPubActorType } from '../../../shared/models/activitypub'
 import { Avatar } from '../../../shared/models/avatars/avatar.model'
 import { activityPubContextify } from '../../helpers/activitypub'
 import {
-  isActorFollowersCountValid, isActorFollowingCountValid, isActorPreferredUsernameValid, isActorPrivateKeyValid,
+  isActorFollowersCountValid,
+  isActorFollowingCountValid,
+  isActorPreferredUsernameValid,
+  isActorPrivateKeyValid,
   isActorPublicKeyValid
 } from '../../helpers/custom-validators/activitypub/actor'
 import { isActivityPubUrlValid } from '../../helpers/custom-validators/activitypub/misc'
@@ -71,6 +88,12 @@ enum ScopeNames {
     },
     {
       fields: [ 'inboxUrl', 'sharedInboxUrl' ]
+    },
+    {
+      fields: [ 'serverId' ]
+    },
+    {
+      fields: [ 'avatarId' ]
     }
   ]
 })
@@ -155,7 +178,8 @@ export class ActorModel extends Model<ActorModel> {
     foreignKey: {
       allowNull: true
     },
-    onDelete: 'set null'
+    onDelete: 'set null',
+    hooks: true
   })
   Avatar: AvatarModel
 
@@ -166,17 +190,17 @@ export class ActorModel extends Model<ActorModel> {
     },
     onDelete: 'cascade'
   })
-  AccountFollowing: ActorFollowModel[]
+  ActorFollowing: ActorFollowModel[]
 
   @HasMany(() => ActorFollowModel, {
     foreignKey: {
       name: 'targetActorId',
       allowNull: false
     },
-    as: 'followers',
+    as: 'ActorFollowers',
     onDelete: 'cascade'
   })
-  AccountFollowers: ActorFollowModel[]
+  ActorFollowers: ActorFollowModel[]
 
   @ForeignKey(() => ServerModel)
   @Column
@@ -194,7 +218,8 @@ export class ActorModel extends Model<ActorModel> {
     foreignKey: {
       allowNull: true
     },
-    onDelete: 'cascade'
+    onDelete: 'cascade',
+    hooks: true
   })
   Account: AccountModel
 
@@ -202,7 +227,8 @@ export class ActorModel extends Model<ActorModel> {
     foreignKey: {
       allowNull: true
     },
-    onDelete: 'cascade'
+    onDelete: 'cascade',
+    hooks: true
   })
   VideoChannel: VideoChannelModel
 
@@ -264,6 +290,55 @@ export class ActorModel extends Model<ActorModel> {
     return ActorModel.scope(ScopeNames.FULL).findOne(query)
   }
 
+  static incrementFollows (id: number, column: 'followersCount' | 'followingCount', by: number) {
+    // FIXME: typings
+    return (ActorModel as any).increment(column, {
+      by,
+      where: {
+        id
+      }
+    })
+  }
+
+  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) {
@@ -334,10 +409,12 @@ export class ActorModel extends Model<ActorModel> {
       attributes: [ 'sharedInboxUrl' ],
       include: [
         {
-          model: ActorFollowModel,
+          attribute: [],
+          model: ActorFollowModel.unscoped(),
           required: true,
-          as: 'followers',
+          as: 'ActorFollowing',
           where: {
+            state: 'accepted',
             targetActorId: this.id
           }
         }