import { values } from 'lodash'
import * as Sequelize from 'sequelize'
import {
- AllowNull, BelongsTo, Column, CreatedAt, DataType, Default, ForeignKey, IsInt, Max, Model, Table,
- UpdatedAt
+ AfterCreate, AfterDestroy, AfterUpdate, AllowNull, BelongsTo, Column, CreatedAt, DataType, Default, ForeignKey, IsInt, Max, Model,
+ Table, UpdatedAt
} from 'sequelize-typescript'
import { FollowState } from '../../../shared/models/actors'
import { AccountFollow } from '../../../shared/models/actors/follow.model'
import { logger } from '../../helpers/logger'
+import { getServerActor } from '../../helpers/utils'
import { ACTOR_FOLLOW_SCORE } from '../../initializers'
import { FOLLOW_STATES } from '../../initializers/constants'
import { ServerModel } from '../server/server'
})
ActorFollowing: ActorModel
+ @AfterCreate
+ @AfterUpdate
+ static incrementFollowerAndFollowingCount (instance: ActorFollowModel) {
+ if (instance.state !== 'accepted') return undefined
+
+ return Promise.all([
+ ActorModel.incrementFollows(instance.actorId, 'followingCount', 1),
+ ActorModel.incrementFollows(instance.targetActorId, 'followersCount', 1)
+ ])
+ }
+
+ @AfterDestroy
+ static decrementFollowerAndFollowingCount (instance: ActorFollowModel) {
+ return Promise.all([
+ ActorModel.incrementFollows(instance.actorId, 'followingCount',-1),
+ ActorModel.incrementFollows(instance.targetActorId, 'followersCount', -1)
+ ])
+ }
+
// Remove actor follows with a score of 0 (too many requests where they were unreachable)
static async removeBadActorFollows () {
const actorFollows = await ActorFollowModel.listBadActorFollows()
if (numberOfActorFollowsRemoved) logger.info('Removed bad %d actor follows.', numberOfActorFollowsRemoved)
}
- static updateActorFollowsScoreAndRemoveBadOnes (goodInboxes: string[], badInboxes: string[], t: Sequelize.Transaction) {
+ static updateActorFollowsScore (goodInboxes: string[], badInboxes: string[], t: Sequelize.Transaction) {
if (goodInboxes.length === 0 && badInboxes.length === 0) return
logger.info('Updating %d good actor follows and %d bad actor follows scores.', goodInboxes.length, badInboxes.length)
if (goodInboxes.length !== 0) {
ActorFollowModel.incrementScores(goodInboxes, ACTOR_FOLLOW_SCORE.BONUS, t)
- .catch(err => logger.error('Cannot increment scores of good actor follows.', err))
+ .catch(err => logger.error('Cannot increment scores of good actor follows.', { err }))
}
if (badInboxes.length !== 0) {
ActorFollowModel.incrementScores(badInboxes, ACTOR_FOLLOW_SCORE.PENALTY, t)
- .catch(err => logger.error('Cannot decrement scores of bad actor follows.', err))
+ .catch(err => logger.error('Cannot decrement scores of bad actor follows.', { err }))
}
}
return ActorFollowModel.findOne(query)
}
- static loadByFollowerInbox (url: string, t?: Sequelize.Transaction) {
- const query = {
- where: {
- state: 'accepted'
- },
- include: [
- {
- model: ActorModel,
- required: true,
- as: 'ActorFollower',
- where: {
- [Sequelize.Op.or]: [
- {
- inboxUrl: url
- },
- {
- sharedInboxUrl: url
- }
- ]
- }
- }
- ],
- transaction: t
- } as any // FIXME: typings does not work
-
- return ActorFollowModel.findOne(query)
- }
-
static listFollowingForApi (id: number, start: number, count: number, sort: string) {
const query = {
distinct: true,
offset: start,
limit: count,
- order: [ getSort(sort) ],
+ order: getSort(sort),
include: [
{
model: ActorModel,
distinct: true,
offset: start,
limit: count,
- order: [ getSort(sort) ],
+ order: getSort(sort),
include: [
{
model: ActorModel,
return ActorFollowModel.createListAcceptedFollowForApiQuery('following', actorIds, t, start, count)
}
+ static async getStats () {
+ const serverActor = await getServerActor()
+
+ const totalInstanceFollowing = await ActorFollowModel.count({
+ where: {
+ actorId: serverActor.id
+ }
+ })
+
+ const totalInstanceFollowers = await ActorFollowModel.count({
+ where: {
+ targetActorId: serverActor.id
+ }
+ })
+
+ return {
+ totalInstanceFollowing,
+ totalInstanceFollowers
+ }
+ }
+
private static async createListAcceptedFollowForApiQuery (
type: 'followers' | 'following',
actorIds: number[],
tasks.push(ActorFollowModel.sequelize.query(query, options))
}
- const [ followers, [ { total } ] ] = await
- Promise.all(tasks)
+ const [ followers, [ { total } ] ] = await Promise.all(tasks)
const urls: string[] = followers.map(f => f.url)
return {
score: {
[Sequelize.Op.lte]: 0
}
- }
+ },
+ logging: false
}
return ActorFollowModel.findAll(query)