aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/models/activitypub
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2020-01-08 15:11:38 +0100
committerChocobozzz <me@florianbigard.com>2020-01-08 15:44:41 +0100
commite612209767ebe1deb0af7688c96b7f979bb52b44 (patch)
treec5be9241e41a098f5452fe3cd187e2305d8047e3 /server/models/activitypub
parent440d39c52d4efb878b6a2e21584d6b8f52072f27 (diff)
downloadPeerTube-e612209767ebe1deb0af7688c96b7f979bb52b44.tar.gz
PeerTube-e612209767ebe1deb0af7688c96b7f979bb52b44.tar.zst
PeerTube-e612209767ebe1deb0af7688c96b7f979bb52b44.zip
Try to fix subscriptions inconsistencies
Diffstat (limited to 'server/models/activitypub')
-rw-r--r--server/models/activitypub/actor-follow.ts13
-rw-r--r--server/models/activitypub/actor.ts27
2 files changed, 26 insertions, 14 deletions
diff --git a/server/models/activitypub/actor-follow.ts b/server/models/activitypub/actor-follow.ts
index c65b975d2..f21d2b8a2 100644
--- a/server/models/activitypub/actor-follow.ts
+++ b/server/models/activitypub/actor-follow.ts
@@ -36,6 +36,7 @@ import {
36 MActorFollowSubscriptions 36 MActorFollowSubscriptions
37} from '@server/typings/models' 37} from '@server/typings/models'
38import { ActivityPubActorType } from '@shared/models' 38import { ActivityPubActorType } from '@shared/models'
39import { afterCommitIfTransaction } from '@server/helpers/database-utils'
39 40
40@Table({ 41@Table({
41 tableName: 'actorFollow', 42 tableName: 'actorFollow',
@@ -104,20 +105,20 @@ export class ActorFollowModel extends Model<ActorFollowModel> {
104 105
105 @AfterCreate 106 @AfterCreate
106 @AfterUpdate 107 @AfterUpdate
107 static incrementFollowerAndFollowingCount (instance: ActorFollowModel) { 108 static incrementFollowerAndFollowingCount (instance: ActorFollowModel, options: any) {
108 if (instance.state !== 'accepted') return undefined 109 if (instance.state !== 'accepted') return undefined
109 110
110 return Promise.all([ 111 return Promise.all([
111 ActorModel.incrementFollows(instance.actorId, 'followingCount', 1), 112 ActorModel.rebuildFollowsCount(instance.actorId, 'following', options.transaction),
112 ActorModel.incrementFollows(instance.targetActorId, 'followersCount', 1) 113 ActorModel.rebuildFollowsCount(instance.targetActorId, 'followers', options.transaction)
113 ]) 114 ])
114 } 115 }
115 116
116 @AfterDestroy 117 @AfterDestroy
117 static decrementFollowerAndFollowingCount (instance: ActorFollowModel) { 118 static decrementFollowerAndFollowingCount (instance: ActorFollowModel, options: any) {
118 return Promise.all([ 119 return Promise.all([
119 ActorModel.incrementFollows(instance.actorId, 'followingCount',-1), 120 ActorModel.rebuildFollowsCount(instance.actorId, 'following', options.transaction),
120 ActorModel.incrementFollows(instance.targetActorId, 'followersCount', -1) 121 ActorModel.rebuildFollowsCount(instance.targetActorId, 'followers', options.transaction)
121 ]) 122 ])
122 } 123 }
123 124
diff --git a/server/models/activitypub/actor.ts b/server/models/activitypub/actor.ts
index 58b52ffb1..007647ced 100644
--- a/server/models/activitypub/actor.ts
+++ b/server/models/activitypub/actor.ts
@@ -47,7 +47,7 @@ import {
47 MActorWithInboxes 47 MActorWithInboxes
48} from '../../typings/models' 48} from '../../typings/models'
49import * as Bluebird from 'bluebird' 49import * as Bluebird from 'bluebird'
50import { Op, Transaction } from 'sequelize' 50import { Op, Transaction, literal } from 'sequelize'
51 51
52enum ScopeNames { 52enum ScopeNames {
53 FULL = 'FULL' 53 FULL = 'FULL'
@@ -421,13 +421,24 @@ export class ActorModel extends Model<ActorModel> {
421 return ActorModel.scope(ScopeNames.FULL).findOne(query) 421 return ActorModel.scope(ScopeNames.FULL).findOne(query)
422 } 422 }
423 423
424 static incrementFollows (id: number, column: 'followersCount' | 'followingCount', by: number) { 424 static rebuildFollowsCount (ofId: number, type: 'followers' | 'following', transaction?: Transaction) {
425 return ActorModel.increment(column, { 425 const sanitizedOfId = parseInt(ofId + '', 10)
426 by, 426 const where = { id: sanitizedOfId }
427 where: { 427
428 id 428 let columnToUpdate: string
429 } 429 let columnOfCount: string
430 }) 430
431 if (type === 'followers') {
432 columnToUpdate = 'followersCount'
433 columnOfCount = 'targetActorId'
434 } else {
435 columnToUpdate = 'followingCount'
436 columnOfCount = 'actorId'
437 }
438
439 return ActorModel.update({
440 [columnToUpdate]: literal(`(SELECT COUNT(*) FROM "actorFollow" WHERE "${columnOfCount}" = ${sanitizedOfId})`)
441 }, { where, transaction })
431 } 442 }
432 443
433 getSharedInbox (this: MActorWithInboxes) { 444 getSharedInbox (this: MActorWithInboxes) {