mirror of
https://github.com/Chocobozzz/PeerTube.git
synced 2025-02-25 18:55:32 -06:00
Try to fix subscriptions inconsistencies
This commit is contained in:
@@ -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)
|
||||
])
|
||||
}
|
||||
|
||||
|
||||
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user