aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/models
diff options
context:
space:
mode:
Diffstat (limited to 'server/models')
-rw-r--r--server/models/account/account.ts2
-rw-r--r--server/models/activitypub/actor-follow.ts13
-rw-r--r--server/models/activitypub/actor.ts27
-rw-r--r--server/models/video/video-caption.ts2
-rw-r--r--server/models/video/video-channel.ts2
-rw-r--r--server/models/video/video.ts2
6 files changed, 30 insertions, 18 deletions
diff --git a/server/models/account/account.ts b/server/models/account/account.ts
index a757b7203..8a0ffeb63 100644
--- a/server/models/account/account.ts
+++ b/server/models/account/account.ts
@@ -223,7 +223,7 @@ export class AccountModel extends Model<AccountModel> {
223 @BeforeDestroy 223 @BeforeDestroy
224 static async sendDeleteIfOwned (instance: AccountModel, options) { 224 static async sendDeleteIfOwned (instance: AccountModel, options) {
225 if (!instance.Actor) { 225 if (!instance.Actor) {
226 instance.Actor = await instance.$get('Actor', { transaction: options.transaction }) as ActorModel 226 instance.Actor = await instance.$get('Actor', { transaction: options.transaction })
227 } 227 }
228 228
229 await ActorFollowModel.removeFollowsOf(instance.Actor.id, options.transaction) 229 await ActorFollowModel.removeFollowsOf(instance.Actor.id, options.transaction)
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) {
diff --git a/server/models/video/video-caption.ts b/server/models/video/video-caption.ts
index ad5801768..eeb2a4afd 100644
--- a/server/models/video/video-caption.ts
+++ b/server/models/video/video-caption.ts
@@ -79,7 +79,7 @@ export class VideoCaptionModel extends Model<VideoCaptionModel> {
79 @BeforeDestroy 79 @BeforeDestroy
80 static async removeFiles (instance: VideoCaptionModel) { 80 static async removeFiles (instance: VideoCaptionModel) {
81 if (!instance.Video) { 81 if (!instance.Video) {
82 instance.Video = await instance.$get('Video') as VideoModel 82 instance.Video = await instance.$get('Video')
83 } 83 }
84 84
85 if (instance.isOwned()) { 85 if (instance.isOwned()) {
diff --git a/server/models/video/video-channel.ts b/server/models/video/video-channel.ts
index 05545bd9d..e10adcb3a 100644
--- a/server/models/video/video-channel.ts
+++ b/server/models/video/video-channel.ts
@@ -249,7 +249,7 @@ export class VideoChannelModel extends Model<VideoChannelModel> {
249 @BeforeDestroy 249 @BeforeDestroy
250 static async sendDeleteIfOwned (instance: VideoChannelModel, options) { 250 static async sendDeleteIfOwned (instance: VideoChannelModel, options) {
251 if (!instance.Actor) { 251 if (!instance.Actor) {
252 instance.Actor = await instance.$get('Actor', { transaction: options.transaction }) as ActorModel 252 instance.Actor = await instance.$get('Actor', { transaction: options.transaction })
253 } 253 }
254 254
255 if (instance.Actor.isOwned()) { 255 if (instance.Actor.isOwned()) {
diff --git a/server/models/video/video.ts b/server/models/video/video.ts
index cd3245ee4..ac8c81ddf 100644
--- a/server/models/video/video.ts
+++ b/server/models/video/video.ts
@@ -1061,7 +1061,7 @@ export class VideoModel extends Model<VideoModel> {
1061 1061
1062 if (instance.isOwned()) { 1062 if (instance.isOwned()) {
1063 if (!Array.isArray(instance.VideoFiles)) { 1063 if (!Array.isArray(instance.VideoFiles)) {
1064 instance.VideoFiles = await instance.$get('VideoFiles') as VideoFileModel[] 1064 instance.VideoFiles = await instance.$get('VideoFiles')
1065 } 1065 }
1066 1066
1067 // Remove physical files and torrents 1067 // Remove physical files and torrents