aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/models
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2018-01-12 11:47:45 +0100
committerChocobozzz <me@florianbigard.com>2018-01-12 11:47:45 +0100
commit32b2b43c06621e384c0bd1610ef0bb9f23399be7 (patch)
treec195943354838bcbe3b1993d77aaf0d3200fd19d /server/models
parent6502c3d43e512e968ad49f5a3bc9abc302471e18 (diff)
downloadPeerTube-32b2b43c06621e384c0bd1610ef0bb9f23399be7.tar.gz
PeerTube-32b2b43c06621e384c0bd1610ef0bb9f23399be7.tar.zst
PeerTube-32b2b43c06621e384c0bd1610ef0bb9f23399be7.zip
Update follower/following counts
Diffstat (limited to 'server/models')
-rw-r--r--server/models/activitypub/actor-follow.ts20
-rw-r--r--server/models/activitypub/actor.ts10
2 files changed, 30 insertions, 0 deletions
diff --git a/server/models/activitypub/actor-follow.ts b/server/models/activitypub/actor-follow.ts
index de5bb6f74..435d22db5 100644
--- a/server/models/activitypub/actor-follow.ts
+++ b/server/models/activitypub/actor-follow.ts
@@ -2,6 +2,7 @@ import * as Bluebird from 'bluebird'
2import { values } from 'lodash' 2import { values } from 'lodash'
3import * as Sequelize from 'sequelize' 3import * as Sequelize from 'sequelize'
4import { 4import {
5 AfterCreate, AfterDestroy, AfterUpdate,
5 AllowNull, BelongsTo, Column, CreatedAt, DataType, Default, ForeignKey, IsInt, Max, Model, Table, 6 AllowNull, BelongsTo, Column, CreatedAt, DataType, Default, ForeignKey, IsInt, Max, Model, Table,
6 UpdatedAt 7 UpdatedAt
7} from 'sequelize-typescript' 8} from 'sequelize-typescript'
@@ -79,6 +80,25 @@ export class ActorFollowModel extends Model<ActorFollowModel> {
79 }) 80 })
80 ActorFollowing: ActorModel 81 ActorFollowing: ActorModel
81 82
83 @AfterCreate
84 @AfterUpdate
85 static incrementFollowerAndFollowingCount (instance: ActorFollowModel) {
86 if (instance.state !== 'accepted') return
87
88 return Promise.all([
89 ActorModel.incrementFollows(instance.actorId, 'followingCount', 1),
90 ActorModel.incrementFollows(instance.targetActorId, 'followersCount', 1)
91 ])
92 }
93
94 @AfterDestroy
95 static decrementFollowerAndFollowingCount (instance: ActorFollowModel) {
96 return Promise.all([
97 ActorModel.incrementFollows(instance.actorId, 'followingCount',-1),
98 ActorModel.incrementFollows(instance.targetActorId, 'followersCount', -1)
99 ])
100 }
101
82 // Remove actor follows with a score of 0 (too many requests where they were unreachable) 102 // Remove actor follows with a score of 0 (too many requests where they were unreachable)
83 static async removeBadActorFollows () { 103 static async removeBadActorFollows () {
84 const actorFollows = await ActorFollowModel.listBadActorFollows() 104 const actorFollows = await ActorFollowModel.listBadActorFollows()
diff --git a/server/models/activitypub/actor.ts b/server/models/activitypub/actor.ts
index 17f69f7a7..b7be9c32c 100644
--- a/server/models/activitypub/actor.ts
+++ b/server/models/activitypub/actor.ts
@@ -264,6 +264,16 @@ export class ActorModel extends Model<ActorModel> {
264 return ActorModel.scope(ScopeNames.FULL).findOne(query) 264 return ActorModel.scope(ScopeNames.FULL).findOne(query)
265 } 265 }
266 266
267 static incrementFollows (id: number, column: 'followersCount' | 'followingCount', by: number) {
268 // FIXME: typings
269 return (ActorModel as any).increment(column, {
270 by,
271 where: {
272 id
273 }
274 })
275 }
276
267 toFormattedJSON () { 277 toFormattedJSON () {
268 let avatar: Avatar = null 278 let avatar: Avatar = null
269 if (this.Avatar) { 279 if (this.Avatar) {