1 import { difference, values } from 'lodash'
2 import { IncludeOptions, Op, QueryTypes, Transaction, WhereOptions } from 'sequelize'
20 } from 'sequelize-typescript'
21 import { isActivityPubUrlValid } from '@server/helpers/custom-validators/activitypub/misc'
22 import { afterCommitIfTransaction } from '@server/helpers/database-utils'
23 import { getServerActor } from '@server/models/application/application'
25 MActorFollowActorsDefault,
26 MActorFollowActorsDefaultSubscription,
27 MActorFollowFollowingHost,
28 MActorFollowFormattable,
29 MActorFollowSubscriptions
30 } from '@server/types/models'
31 import { AttributesOnly } from '@shared/core-utils'
32 import { ActivityPubActorType } from '@shared/models'
33 import { FollowState } from '../../../shared/models/actors'
34 import { ActorFollow } from '../../../shared/models/actors/follow.model'
35 import { logger } from '../../helpers/logger'
36 import { ACTOR_FOLLOW_SCORE, CONSTRAINTS_FIELDS, FOLLOW_STATES, SERVER_ACTOR_NAME } from '../../initializers/constants'
37 import { AccountModel } from '../account/account'
38 import { ServerModel } from '../server/server'
39 import { doesExist } from '../shared/query'
40 import { createSafeIn, getFollowsSort, getSort, searchAttribute, throwIfNotValid } from '../utils'
41 import { VideoChannelModel } from '../video/video-channel'
42 import { ActorModel, unusedActorAttributesForAPI } from './actor'
45 tableName: 'actorFollow',
51 fields: [ 'targetActorId' ]
54 fields: [ 'actorId', 'targetActorId' ],
66 export class ActorFollowModel extends Model<Partial<AttributesOnly<ActorFollowModel>>> {
69 @Column(DataType.ENUM(...values(FOLLOW_STATES)))
73 @Default(ACTOR_FOLLOW_SCORE.BASE)
75 @Max(ACTOR_FOLLOW_SCORE.MAX)
79 // Allow null because we added this column in PeerTube v3, and don't want to generate fake URLs of remote follows
81 @Is('ActorFollowUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'url'))
82 @Column(DataType.STRING(CONSTRAINTS_FIELDS.COMMONS.URL.max))
91 @ForeignKey(() => ActorModel)
95 @BelongsTo(() => ActorModel, {
103 ActorFollower: ActorModel
105 @ForeignKey(() => ActorModel)
107 targetActorId: number
109 @BelongsTo(() => ActorModel, {
111 name: 'targetActorId',
114 as: 'ActorFollowing',
117 ActorFollowing: ActorModel
121 static incrementFollowerAndFollowingCount (instance: ActorFollowModel, options: any) {
122 return afterCommitIfTransaction(options.transaction, () => {
124 ActorModel.rebuildFollowsCount(instance.actorId, 'following'),
125 ActorModel.rebuildFollowsCount(instance.targetActorId, 'followers')
131 static decrementFollowerAndFollowingCount (instance: ActorFollowModel, options: any) {
132 return afterCommitIfTransaction(options.transaction, () => {
134 ActorModel.rebuildFollowsCount(instance.actorId, 'following'),
135 ActorModel.rebuildFollowsCount(instance.targetActorId, 'followers')
140 static removeFollowsOf (actorId: number, t?: Transaction) {
148 targetActorId: actorId
155 return ActorFollowModel.destroy(query)
158 // Remove actor follows with a score of 0 (too many requests where they were unreachable)
159 static async removeBadActorFollows () {
160 const actorFollows = await ActorFollowModel.listBadActorFollows()
162 const actorFollowsRemovePromises = actorFollows.map(actorFollow => actorFollow.destroy())
163 await Promise.all(actorFollowsRemovePromises)
165 const numberOfActorFollowsRemoved = actorFollows.length
167 if (numberOfActorFollowsRemoved) logger.info('Removed bad %d actor follows.', numberOfActorFollowsRemoved)
170 static isFollowedBy (actorId: number, followerActorId: number) {
171 const query = 'SELECT 1 FROM "actorFollow" WHERE "actorId" = $followerActorId AND "targetActorId" = $actorId LIMIT 1'
173 return doesExist(query, { actorId, followerActorId })
176 static loadByActorAndTarget (actorId: number, targetActorId: number, t?: Transaction): Promise<MActorFollowActorsDefault> {
180 targetActorId: targetActorId
197 return ActorFollowModel.findOne(query)
200 static loadByActorAndTargetNameAndHostForAPI (
205 ): Promise<MActorFollowActorsDefaultSubscription> {
206 const actorFollowingPartInclude: IncludeOptions = {
209 as: 'ActorFollowing',
211 preferredUsername: targetName
215 model: VideoChannelModel.unscoped(),
221 if (targetHost === null) {
222 actorFollowingPartInclude.where['serverId'] = null
224 actorFollowingPartInclude.include.push({
238 actorFollowingPartInclude,
248 return ActorFollowModel.findOne(query)
251 static listSubscribedIn (actorId: number, targets: { name: string, host?: string }[]): Promise<MActorFollowFollowingHost[]> {
252 const whereTab = targets
258 $preferredUsername$: t.name
270 $preferredUsername$: t.name
280 attributes: [ 'id' ],
293 attributes: [ 'preferredUsername' ],
294 model: ActorModel.unscoped(),
296 as: 'ActorFollowing',
299 attributes: [ 'host' ],
300 model: ServerModel.unscoped(),
308 return ActorFollowModel.findAll(query)
311 static listFollowingForApi (options: {
317 actorType?: ActivityPubActorType
320 const { id, start, count, sort, search, state, actorType } = options
322 const followWhere = state ? { state } : {}
323 const followingWhere: WhereOptions = {}
326 Object.assign(followWhere, {
328 searchAttribute(options.search, '$ActorFollowing.preferredUsername$'),
329 searchAttribute(options.search, '$ActorFollowing.Server.host$')
335 Object.assign(followingWhere, { type: actorType })
342 order: getFollowsSort(sort),
355 as: 'ActorFollowing',
357 where: followingWhere,
368 return ActorFollowModel.findAndCountAll<MActorFollowActorsDefault>(query)
369 .then(({ rows, count }) => {
377 static listFollowersForApi (options: {
383 actorType?: ActivityPubActorType
386 const { actorId, start, count, sort, search, state, actorType } = options
388 const followWhere = state ? { state } : {}
389 const followerWhere: WhereOptions = {}
392 Object.assign(followWhere, {
394 searchAttribute(search, '$ActorFollower.preferredUsername$'),
395 searchAttribute(search, '$ActorFollower.Server.host$')
401 Object.assign(followerWhere, { type: actorType })
408 order: getFollowsSort(sort),
415 where: followerWhere,
425 as: 'ActorFollowing',
434 return ActorFollowModel.findAndCountAll<MActorFollowActorsDefault>(query)
435 .then(({ rows, count }) => {
443 static listSubscriptionsForApi (options: {
450 const { actorId, start, count, sort } = options
455 if (options.search) {
456 Object.assign(where, {
458 searchAttribute(options.search, '$ActorFollowing.preferredUsername$'),
459 searchAttribute(options.search, '$ActorFollowing.VideoChannel.name$')
469 order: getSort(sort),
473 attributes: [ 'id' ],
474 model: ActorModel.unscoped(),
475 as: 'ActorFollowing',
479 model: VideoChannelModel.unscoped(),
484 exclude: unusedActorAttributesForAPI
490 model: AccountModel.unscoped(),
495 exclude: unusedActorAttributesForAPI
509 return ActorFollowModel.findAndCountAll<MActorFollowSubscriptions>(query)
510 .then(({ rows, count }) => {
512 data: rows.map(r => r.ActorFollowing.VideoChannel),
518 static async keepUnfollowedInstance (hosts: string[]) {
519 const followerId = (await getServerActor()).id
522 attributes: [ 'id' ],
528 attributes: [ 'id' ],
529 model: ActorModel.unscoped(),
531 as: 'ActorFollowing',
533 preferredUsername: SERVER_ACTOR_NAME
537 attributes: [ 'host' ],
538 model: ServerModel.unscoped(),
551 const res = await ActorFollowModel.findAll(query)
552 const followedHosts = res.map(row => row.ActorFollowing.Server.host)
554 return difference(hosts, followedHosts)
557 static listAcceptedFollowerUrlsForAP (actorIds: number[], t: Transaction, start?: number, count?: number) {
558 return ActorFollowModel.createListAcceptedFollowForApiQuery('followers', actorIds, t, start, count)
561 static listAcceptedFollowerSharedInboxUrls (actorIds: number[], t: Transaction) {
562 return ActorFollowModel.createListAcceptedFollowForApiQuery(
573 static listAcceptedFollowingUrlsForApi (actorIds: number[], t: Transaction, start?: number, count?: number) {
574 return ActorFollowModel.createListAcceptedFollowForApiQuery('following', actorIds, t, start, count)
577 static async getStats () {
578 const serverActor = await getServerActor()
580 const totalInstanceFollowing = await ActorFollowModel.count({
582 actorId: serverActor.id
586 const totalInstanceFollowers = await ActorFollowModel.count({
588 targetActorId: serverActor.id
593 totalInstanceFollowing,
594 totalInstanceFollowers
598 static updateScore (inboxUrl: string, value: number, t?: Transaction) {
599 const query = `UPDATE "actorFollow" SET "score" = LEAST("score" + ${value}, ${ACTOR_FOLLOW_SCORE.MAX}) ` +
601 'SELECT "actorFollow"."id" FROM "actorFollow" ' +
602 'INNER JOIN "actor" ON "actor"."id" = "actorFollow"."actorId" ' +
603 `WHERE "actor"."inboxUrl" = '${inboxUrl}' OR "actor"."sharedInboxUrl" = '${inboxUrl}'` +
607 type: QueryTypes.BULKUPDATE,
611 return ActorFollowModel.sequelize.query(query, options)
614 static async updateScoreByFollowingServers (serverIds: number[], value: number, t?: Transaction) {
615 if (serverIds.length === 0) return
617 const me = await getServerActor()
618 const serverIdsString = createSafeIn(ActorFollowModel.sequelize, serverIds)
620 const query = `UPDATE "actorFollow" SET "score" = LEAST("score" + ${value}, ${ACTOR_FOLLOW_SCORE.MAX}) ` +
622 'SELECT "actorFollow"."id" FROM "actorFollow" ' +
623 'INNER JOIN "actor" ON "actor"."id" = "actorFollow"."targetActorId" ' +
624 `WHERE "actorFollow"."actorId" = ${me.Account.actorId} ` + // I'm the follower
625 `AND "actor"."serverId" IN (${serverIdsString})` + // Criteria on followings
629 type: QueryTypes.BULKUPDATE,
633 return ActorFollowModel.sequelize.query(query, options)
636 private static async createListAcceptedFollowForApiQuery (
637 type: 'followers' | 'following',
645 let firstJoin: string
646 let secondJoin: string
648 if (type === 'followers') {
649 firstJoin = 'targetActorId'
650 secondJoin = 'actorId'
652 firstJoin = 'actorId'
653 secondJoin = 'targetActorId'
656 const selections: string[] = []
657 if (distinct === true) selections.push(`DISTINCT("Follows"."${columnUrl}") AS "selectionUrl"`)
658 else selections.push(`"Follows"."${columnUrl}" AS "selectionUrl"`)
660 selections.push('COUNT(*) AS "total"')
662 const tasks: Promise<any>[] = []
664 for (const selection of selections) {
665 let query = 'SELECT ' + selection + ' FROM "actor" ' +
666 'INNER JOIN "actorFollow" ON "actorFollow"."' + firstJoin + '" = "actor"."id" ' +
667 'INNER JOIN "actor" AS "Follows" ON "actorFollow"."' + secondJoin + '" = "Follows"."id" ' +
668 `WHERE "actor"."id" = ANY ($actorIds) AND "actorFollow"."state" = 'accepted' AND "Follows"."${columnUrl}" IS NOT NULL `
670 if (count !== undefined) query += 'LIMIT ' + count
671 if (start !== undefined) query += ' OFFSET ' + start
675 type: QueryTypes.SELECT,
678 tasks.push(ActorFollowModel.sequelize.query(query, options))
681 const [ followers, [ dataTotal ] ] = await Promise.all(tasks)
682 const urls: string[] = followers.map(f => f.selectionUrl)
686 total: dataTotal ? parseInt(dataTotal.total, 10) : 0
690 private static listBadActorFollows () {
700 return ActorFollowModel.findAll(query)
703 toFormattedJSON (this: MActorFollowFormattable): ActorFollow {
704 const follower = this.ActorFollower.toFormattedJSON()
705 const following = this.ActorFollowing.toFormattedJSON()
713 createdAt: this.createdAt,
714 updatedAt: this.updatedAt