Try to fix subscriptions inconsistencies

This commit is contained in:
Chocobozzz
2020-01-08 15:11:38 +01:00
parent 440d39c52d
commit e612209767
12 changed files with 37 additions and 28 deletions

View File

@@ -36,6 +36,7 @@ import {
MActorFollowSubscriptions
} from '@server/typings/models'
import { ActivityPubActorType } from '@shared/models'
import { afterCommitIfTransaction } from '@server/helpers/database-utils'
@Table({
tableName: 'actorFollow',
@@ -104,20 +105,20 @@ export class ActorFollowModel extends Model<ActorFollowModel> {
@AfterCreate
@AfterUpdate
static incrementFollowerAndFollowingCount (instance: ActorFollowModel) {
static incrementFollowerAndFollowingCount (instance: ActorFollowModel, options: any) {
if (instance.state !== 'accepted') return undefined
return Promise.all([
ActorModel.incrementFollows(instance.actorId, 'followingCount', 1),
ActorModel.incrementFollows(instance.targetActorId, 'followersCount', 1)
ActorModel.rebuildFollowsCount(instance.actorId, 'following', options.transaction),
ActorModel.rebuildFollowsCount(instance.targetActorId, 'followers', options.transaction)
])
}
@AfterDestroy
static decrementFollowerAndFollowingCount (instance: ActorFollowModel) {
static decrementFollowerAndFollowingCount (instance: ActorFollowModel, options: any) {
return Promise.all([
ActorModel.incrementFollows(instance.actorId, 'followingCount',-1),
ActorModel.incrementFollows(instance.targetActorId, 'followersCount', -1)
ActorModel.rebuildFollowsCount(instance.actorId, 'following', options.transaction),
ActorModel.rebuildFollowsCount(instance.targetActorId, 'followers', options.transaction)
])
}

View File

@@ -47,7 +47,7 @@ import {
MActorWithInboxes
} from '../../typings/models'
import * as Bluebird from 'bluebird'
import { Op, Transaction } from 'sequelize'
import { Op, Transaction, literal } from 'sequelize'
enum ScopeNames {
FULL = 'FULL'
@@ -421,13 +421,24 @@ export class ActorModel extends Model<ActorModel> {
return ActorModel.scope(ScopeNames.FULL).findOne(query)
}
static incrementFollows (id: number, column: 'followersCount' | 'followingCount', by: number) {
return ActorModel.increment(column, {
by,
where: {
id
}
})
static rebuildFollowsCount (ofId: number, type: 'followers' | 'following', transaction?: Transaction) {
const sanitizedOfId = parseInt(ofId + '', 10)
const where = { id: sanitizedOfId }
let columnToUpdate: string
let columnOfCount: string
if (type === 'followers') {
columnToUpdate = 'followersCount'
columnOfCount = 'targetActorId'
} else {
columnToUpdate = 'followingCount'
columnOfCount = 'actorId'
}
return ActorModel.update({
[columnToUpdate]: literal(`(SELECT COUNT(*) FROM "actorFollow" WHERE "${columnOfCount}" = ${sanitizedOfId})`)
}, { where, transaction })
}
getSharedInbox (this: MActorWithInboxes) {