]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/models/activitypub/actor-follow.ts
Don't show videos of remote instance after unfollow
[github/Chocobozzz/PeerTube.git] / server / models / activitypub / actor-follow.ts
index 78a65a0ff58cdaf8f937e08993cfd1c6510ad935..ced48154705187f57271454407e817c37edd9367 100644 (file)
@@ -2,8 +2,8 @@ import * as Bluebird from 'bluebird'
 import { values } from 'lodash'
 import * as Sequelize from 'sequelize'
 import {
-  AllowNull, BelongsTo, Column, CreatedAt, DataType, Default, ForeignKey, IsInt, Max, Model, Table,
-  UpdatedAt
+  AfterCreate, AfterDestroy, AfterUpdate, AllowNull, BelongsTo, Column, CreatedAt, DataType, Default, ForeignKey, IsInt, Max, Model,
+  Table, UpdatedAt
 } from 'sequelize-typescript'
 import { FollowState } from '../../../shared/models/actors'
 import { AccountFollow } from '../../../shared/models/actors/follow.model'
@@ -79,6 +79,25 @@ export class ActorFollowModel extends Model<ActorFollowModel> {
   })
   ActorFollowing: ActorModel
 
+  @AfterCreate
+  @AfterUpdate
+  static incrementFollowerAndFollowingCount (instance: ActorFollowModel) {
+    if (instance.state !== 'accepted') return undefined
+
+    return Promise.all([
+      ActorModel.incrementFollows(instance.actorId, 'followingCount', 1),
+      ActorModel.incrementFollows(instance.targetActorId, 'followersCount', 1)
+    ])
+  }
+
+  @AfterDestroy
+  static decrementFollowerAndFollowingCount (instance: ActorFollowModel) {
+    return Promise.all([
+      ActorModel.incrementFollows(instance.actorId, 'followingCount',-1),
+      ActorModel.incrementFollows(instance.targetActorId, 'followersCount', -1)
+    ])
+  }
+
   // Remove actor follows with a score of 0 (too many requests where they were unreachable)
   static async removeBadActorFollows () {
     const actorFollows = await ActorFollowModel.listBadActorFollows()
@@ -163,6 +182,34 @@ export class ActorFollowModel extends Model<ActorFollowModel> {
     return ActorFollowModel.findOne(query)
   }
 
+  static loadByFollowerInbox (url: string, t?: Sequelize.Transaction) {
+    const query = {
+      where: {
+        state: 'accepted'
+      },
+      include: [
+        {
+          model: ActorModel,
+          required: true,
+          as: 'ActorFollower',
+          where: {
+            [Sequelize.Op.or]: [
+              {
+                inboxUrl: url
+              },
+              {
+                sharedInboxUrl: url
+              }
+            ]
+          }
+        }
+      ],
+      transaction: t
+    } as any // FIXME: typings does not work
+
+    return ActorFollowModel.findOne(query)
+  }
+
   static listFollowingForApi (id: number, start: number, count: number, sort: string) {
     const query = {
       distinct: true,
@@ -307,7 +354,7 @@ export class ActorFollowModel extends Model<ActorFollowModel> {
   private static incrementScores (inboxUrls: string[], value: number, t: Sequelize.Transaction) {
     const inboxUrlsString = inboxUrls.map(url => `'${url}'`).join(',')
 
-    const query = 'UPDATE "actorFollow" SET "score" = "score" +' + value + ' ' +
+    const query = `UPDATE "actorFollow" SET "score" = LEAST("score" + ${value}, ${ACTOR_FOLLOW_SCORE.MAX}) ` +
       'WHERE id IN (' +
         'SELECT "actorFollow"."id" FROM "actorFollow" ' +
         'INNER JOIN "actor" ON "actor"."id" = "actorFollow"."actorId" ' +