1 import * as Bluebird from 'bluebird'
2 import { values } from 'lodash'
3 import * as Sequelize from 'sequelize'
20 } from 'sequelize-typescript'
21 import { FollowState } from '../../../shared/models/actors'
22 import { AccountFollow } from '../../../shared/models/actors/follow.model'
23 import { logger } from '../../helpers/logger'
24 import { getServerActor } from '../../helpers/utils'
25 import { ACTOR_FOLLOW_SCORE } from '../../initializers'
26 import { FOLLOW_STATES } from '../../initializers/constants'
27 import { ServerModel } from '../server/server'
28 import { getSort } from '../utils'
29 import { ActorModel, unusedActorAttributesForAPI } from './actor'
30 import { VideoChannelModel } from '../video/video-channel'
31 import { IIncludeOptions } from '../../../node_modules/sequelize-typescript/lib/interfaces/IIncludeOptions'
32 import { AccountModel } from '../account/account'
35 tableName: 'actorFollow',
41 fields: [ 'targetActorId' ]
44 fields: [ 'actorId', 'targetActorId' ],
52 export class ActorFollowModel extends Model<ActorFollowModel> {
55 @Column(DataType.ENUM(values(FOLLOW_STATES)))
59 @Default(ACTOR_FOLLOW_SCORE.BASE)
61 @Max(ACTOR_FOLLOW_SCORE.MAX)
71 @ForeignKey(() => ActorModel)
75 @BelongsTo(() => ActorModel, {
83 ActorFollower: ActorModel
85 @ForeignKey(() => ActorModel)
89 @BelongsTo(() => ActorModel, {
91 name: 'targetActorId',
97 ActorFollowing: ActorModel
101 static incrementFollowerAndFollowingCount (instance: ActorFollowModel) {
102 if (instance.state !== 'accepted') return undefined
105 ActorModel.incrementFollows(instance.actorId, 'followingCount', 1),
106 ActorModel.incrementFollows(instance.targetActorId, 'followersCount', 1)
111 static decrementFollowerAndFollowingCount (instance: ActorFollowModel) {
113 ActorModel.incrementFollows(instance.actorId, 'followingCount',-1),
114 ActorModel.incrementFollows(instance.targetActorId, 'followersCount', -1)
118 // Remove actor follows with a score of 0 (too many requests where they were unreachable)
119 static async removeBadActorFollows () {
120 const actorFollows = await ActorFollowModel.listBadActorFollows()
122 const actorFollowsRemovePromises = actorFollows.map(actorFollow => actorFollow.destroy())
123 await Promise.all(actorFollowsRemovePromises)
125 const numberOfActorFollowsRemoved = actorFollows.length
127 if (numberOfActorFollowsRemoved) logger.info('Removed bad %d actor follows.', numberOfActorFollowsRemoved)
130 static updateActorFollowsScore (goodInboxes: string[], badInboxes: string[], t: Sequelize.Transaction | undefined) {
131 if (goodInboxes.length === 0 && badInboxes.length === 0) return
133 logger.info('Updating %d good actor follows and %d bad actor follows scores.', goodInboxes.length, badInboxes.length)
135 if (goodInboxes.length !== 0) {
136 ActorFollowModel.incrementScores(goodInboxes, ACTOR_FOLLOW_SCORE.BONUS, t)
137 .catch(err => logger.error('Cannot increment scores of good actor follows.', { err }))
140 if (badInboxes.length !== 0) {
141 ActorFollowModel.incrementScores(badInboxes, ACTOR_FOLLOW_SCORE.PENALTY, t)
142 .catch(err => logger.error('Cannot decrement scores of bad actor follows.', { err }))
146 static loadByActorAndTarget (actorId: number, targetActorId: number, t?: Sequelize.Transaction) {
150 targetActorId: targetActorId
167 return ActorFollowModel.findOne(query)
170 static loadByActorAndTargetNameAndHostForAPI (actorId: number, targetName: string, targetHost: string, t?: Sequelize.Transaction) {
171 const actorFollowingPartInclude: IIncludeOptions = {
174 as: 'ActorFollowing',
176 preferredUsername: targetName
180 model: VideoChannelModel.unscoped(),
186 if (targetHost === null) {
187 actorFollowingPartInclude.where['serverId'] = null
189 actorFollowingPartInclude.include.push({
203 actorFollowingPartInclude,
213 return ActorFollowModel.findOne(query)
215 if (result && result.ActorFollowing.VideoChannel) {
216 result.ActorFollowing.VideoChannel.Actor = result.ActorFollowing
223 static listSubscribedIn (actorId: number, targets: { name: string, host?: string }[]) {
224 const whereTab = targets
228 [ Sequelize.Op.and ]: [
230 '$preferredUsername$': t.name
240 [ Sequelize.Op.and ]: [
242 '$preferredUsername$': t.name
254 [ Sequelize.Op.and ]: [
256 [ Sequelize.Op.or ]: whereTab
265 attributes: [ 'preferredUsername' ],
266 model: ActorModel.unscoped(),
268 as: 'ActorFollowing',
271 attributes: [ 'host' ],
272 model: ServerModel.unscoped(),
280 return ActorFollowModel.findAll(query)
283 static listFollowingForApi (id: number, start: number, count: number, sort: string) {
288 order: getSort(sort),
300 as: 'ActorFollowing',
302 include: [ ServerModel ]
307 return ActorFollowModel.findAndCountAll(query)
308 .then(({ rows, count }) => {
316 static listSubscriptionsForApi (id: number, start: number, count: number, sort: string) {
322 order: getSort(sort),
329 exclude: unusedActorAttributesForAPI
332 as: 'ActorFollowing',
336 model: VideoChannelModel,
341 exclude: unusedActorAttributesForAPI
352 exclude: unusedActorAttributesForAPI
366 return ActorFollowModel.findAndCountAll(query)
367 .then(({ rows, count }) => {
369 data: rows.map(r => r.ActorFollowing.VideoChannel),
375 static listFollowersForApi (id: number, start: number, count: number, sort: string) {
380 order: getSort(sort),
386 include: [ ServerModel ]
390 as: 'ActorFollowing',
399 return ActorFollowModel.findAndCountAll(query)
400 .then(({ rows, count }) => {
408 static listAcceptedFollowerUrlsForApi (actorIds: number[], t: Sequelize.Transaction, start?: number, count?: number) {
409 return ActorFollowModel.createListAcceptedFollowForApiQuery('followers', actorIds, t, start, count)
412 static listAcceptedFollowerSharedInboxUrls (actorIds: number[], t: Sequelize.Transaction) {
413 return ActorFollowModel.createListAcceptedFollowForApiQuery(
424 static listAcceptedFollowingUrlsForApi (actorIds: number[], t: Sequelize.Transaction, start?: number, count?: number) {
425 return ActorFollowModel.createListAcceptedFollowForApiQuery('following', actorIds, t, start, count)
428 static async getStats () {
429 const serverActor = await getServerActor()
431 const totalInstanceFollowing = await ActorFollowModel.count({
433 actorId: serverActor.id
437 const totalInstanceFollowers = await ActorFollowModel.count({
439 targetActorId: serverActor.id
444 totalInstanceFollowing,
445 totalInstanceFollowers
449 private static async createListAcceptedFollowForApiQuery (
450 type: 'followers' | 'following',
452 t: Sequelize.Transaction,
458 let firstJoin: string
459 let secondJoin: string
461 if (type === 'followers') {
462 firstJoin = 'targetActorId'
463 secondJoin = 'actorId'
465 firstJoin = 'actorId'
466 secondJoin = 'targetActorId'
469 const selections: string[] = []
470 if (distinct === true) selections.push('DISTINCT("Follows"."' + columnUrl + '") AS "url"')
471 else selections.push('"Follows"."' + columnUrl + '" AS "url"')
473 selections.push('COUNT(*) AS "total"')
475 const tasks: Bluebird<any>[] = []
477 for (let selection of selections) {
478 let query = 'SELECT ' + selection + ' FROM "actor" ' +
479 'INNER JOIN "actorFollow" ON "actorFollow"."' + firstJoin + '" = "actor"."id" ' +
480 'INNER JOIN "actor" AS "Follows" ON "actorFollow"."' + secondJoin + '" = "Follows"."id" ' +
481 'WHERE "actor"."id" = ANY ($actorIds) AND "actorFollow"."state" = \'accepted\' '
483 if (count !== undefined) query += 'LIMIT ' + count
484 if (start !== undefined) query += ' OFFSET ' + start
488 type: Sequelize.QueryTypes.SELECT,
491 tasks.push(ActorFollowModel.sequelize.query(query, options))
494 const [ followers, [ { total } ] ] = await Promise.all(tasks)
495 const urls: string[] = followers.map(f => f.url)
499 total: parseInt(total, 10)
503 private static incrementScores (inboxUrls: string[], value: number, t: Sequelize.Transaction | undefined) {
504 const inboxUrlsString = inboxUrls.map(url => `'${url}'`).join(',')
506 const query = `UPDATE "actorFollow" SET "score" = LEAST("score" + ${value}, ${ACTOR_FOLLOW_SCORE.MAX}) ` +
508 'SELECT "actorFollow"."id" FROM "actorFollow" ' +
509 'INNER JOIN "actor" ON "actor"."id" = "actorFollow"."actorId" ' +
510 'WHERE "actor"."inboxUrl" IN (' + inboxUrlsString + ') OR "actor"."sharedInboxUrl" IN (' + inboxUrlsString + ')' +
513 const options = t ? {
514 type: Sequelize.QueryTypes.BULKUPDATE,
518 return ActorFollowModel.sequelize.query(query, options)
521 private static listBadActorFollows () {
525 [Sequelize.Op.lte]: 0
531 return ActorFollowModel.findAll(query)
534 toFormattedJSON (): AccountFollow {
535 const follower = this.ActorFollower.toFormattedJSON()
536 const following = this.ActorFollowing.toFormattedJSON()
544 createdAt: this.createdAt,
545 updatedAt: this.updatedAt