]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/models/activitypub/actor-follow.ts
Disable sql prettifier by default
[github/Chocobozzz/PeerTube.git] / server / models / activitypub / actor-follow.ts
index 3e85cc329e9569b2a0226602056782c7d4d11778..ce6a4e2674c75ddd6aad3decd89138af9ea28905 100644 (file)
@@ -1,5 +1,5 @@
-import * as Bluebird from 'bluebird'
 import { difference, values } from 'lodash'
+import { IncludeOptions, Op, QueryTypes, Transaction, WhereOptions } from 'sequelize'
 import {
   AfterCreate,
   AfterDestroy,
@@ -11,22 +11,16 @@ import {
   DataType,
   Default,
   ForeignKey,
+  Is,
   IsInt,
   Max,
   Model,
   Table,
   UpdatedAt
 } from 'sequelize-typescript'
-import { FollowState } from '../../../shared/models/actors'
-import { ActorFollow } from '../../../shared/models/actors/follow.model'
-import { logger } from '../../helpers/logger'
-import { ACTOR_FOLLOW_SCORE, FOLLOW_STATES, SERVER_ACTOR_NAME } from '../../initializers/constants'
-import { ServerModel } from '../server/server'
-import { createSafeIn, getFollowsSort, getSort } from '../utils'
-import { ActorModel, unusedActorAttributesForAPI } from './actor'
-import { VideoChannelModel } from '../video/video-channel'
-import { AccountModel } from '../account/account'
-import { IncludeOptions, Op, QueryTypes, Transaction, WhereOptions } from 'sequelize'
+import { isActivityPubUrlValid } from '@server/helpers/custom-validators/activitypub/misc'
+import { getServerActor } from '@server/models/application/application'
+import { VideoModel } from '@server/models/video/video'
 import {
   MActorFollowActorsDefault,
   MActorFollowActorsDefaultSubscription,
@@ -35,8 +29,15 @@ import {
   MActorFollowSubscriptions
 } from '@server/types/models'
 import { ActivityPubActorType } from '@shared/models'
-import { VideoModel } from '@server/models/video/video'
-import { getServerActor } from '@server/models/application/application'
+import { FollowState } from '../../../shared/models/actors'
+import { ActorFollow } from '../../../shared/models/actors/follow.model'
+import { logger } from '../../helpers/logger'
+import { ACTOR_FOLLOW_SCORE, CONSTRAINTS_FIELDS, FOLLOW_STATES, SERVER_ACTOR_NAME } from '../../initializers/constants'
+import { AccountModel } from '../account/account'
+import { ServerModel } from '../server/server'
+import { createSafeIn, getFollowsSort, getSort, searchAttribute, throwIfNotValid } from '../utils'
+import { VideoChannelModel } from '../video/video-channel'
+import { ActorModel, unusedActorAttributesForAPI } from './actor'
 
 @Table({
   tableName: 'actorFollow',
@@ -53,10 +54,14 @@ import { getServerActor } from '@server/models/application/application'
     },
     {
       fields: [ 'score' ]
+    },
+    {
+      fields: [ 'url' ],
+      unique: true
     }
   ]
 })
-export class ActorFollowModel extends Model<ActorFollowModel> {
+export class ActorFollowModel extends Model {
 
   @AllowNull(false)
   @Column(DataType.ENUM(...values(FOLLOW_STATES)))
@@ -69,6 +74,12 @@ export class ActorFollowModel extends Model<ActorFollowModel> {
   @Column
   score: number
 
+  // Allow null because we added this column in PeerTube v3, and don't want to generate fake URLs of remote follows
+  @AllowNull(true)
+  @Is('ActorFollowUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'url'))
+  @Column(DataType.STRING(CONSTRAINTS_FIELDS.COMMONS.URL.max))
+  url: string
+
   @CreatedAt
   createdAt: Date
 
@@ -164,7 +175,7 @@ export class ActorFollowModel extends Model<ActorFollowModel> {
                      .then(results => results.length === 1)
   }
 
-  static loadByActorAndTarget (actorId: number, targetActorId: number, t?: Transaction): Bluebird<MActorFollowActorsDefault> {
+  static loadByActorAndTarget (actorId: number, targetActorId: number, t?: Transaction): Promise<MActorFollowActorsDefault> {
     const query = {
       where: {
         actorId,
@@ -193,7 +204,7 @@ export class ActorFollowModel extends Model<ActorFollowModel> {
     targetName: string,
     targetHost: string,
     t?: Transaction
-  ): Bluebird<MActorFollowActorsDefaultSubscription> {
+  ): Promise<MActorFollowActorsDefaultSubscription> {
     const actorFollowingPartInclude: IncludeOptions = {
       model: ActorModel,
       required: true,
@@ -246,7 +257,7 @@ export class ActorFollowModel extends Model<ActorFollowModel> {
       })
   }
 
-  static listSubscribedIn (actorId: number, targets: { name: string, host?: string }[]): Bluebird<MActorFollowFollowingHost[]> {
+  static listSubscribedIn (actorId: number, targets: { name: string, host?: string }[]): Promise<MActorFollowFollowingHost[]> {
     const whereTab = targets
       .map(t => {
         if (t.host) {
@@ -275,7 +286,7 @@ export class ActorFollowModel extends Model<ActorFollowModel> {
       })
 
     const query = {
-      attributes: [],
+      attributes: [ 'id' ],
       where: {
         [Op.and]: [
           {
@@ -440,16 +451,34 @@ export class ActorFollowModel extends Model<ActorFollowModel> {
                            })
   }
 
-  static listSubscriptionsForApi (actorId: number, start: number, count: number, sort: string) {
+  static listSubscriptionsForApi (options: {
+    actorId: number
+    start: number
+    count: number
+    sort: string
+    search?: string
+  }) {
+    const { actorId, start, count, sort } = options
+    const where = {
+      actorId: actorId
+    }
+
+    if (options.search) {
+      Object.assign(where, {
+        [Op.or]: [
+          searchAttribute(options.search, '$ActorFollowing.preferredUsername$'),
+          searchAttribute(options.search, '$ActorFollowing.VideoChannel.name$')
+        ]
+      })
+    }
+
     const query = {
       attributes: [],
       distinct: true,
       offset: start,
       limit: count,
       order: getSort(sort),
-      where: {
-        actorId: actorId
-      },
+      where,
       include: [
         {
           attributes: [ 'id' ],
@@ -641,7 +670,7 @@ export class ActorFollowModel extends Model<ActorFollowModel> {
 
     selections.push('COUNT(*) AS "total"')
 
-    const tasks: Bluebird<any>[] = []
+    const tasks: Promise<any>[] = []
 
     for (const selection of selections) {
       let query = 'SELECT ' + selection + ' FROM "actor" ' +