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 { getServerActor } from '@server/models/application/application'
23 import { VideoModel } from '@server/models/video/video'
25 MActorFollowActorsDefault,
26 MActorFollowActorsDefaultSubscription,
27 MActorFollowFollowingHost,
28 MActorFollowFormattable,
29 MActorFollowSubscriptions
30 } from '@server/types/models'
31 import { ActivityPubActorType } from '@shared/models'
32 import { FollowState } from '../../../shared/models/actors'
33 import { ActorFollow } from '../../../shared/models/actors/follow.model'
34 import { logger } from '../../helpers/logger'
35 import { ACTOR_FOLLOW_SCORE, CONSTRAINTS_FIELDS, FOLLOW_STATES, SERVER_ACTOR_NAME } from '../../initializers/constants'
36 import { AccountModel } from '../account/account'
37 import { ServerModel } from '../server/server'
38 import { createSafeIn, getFollowsSort, getSort, searchAttribute, throwIfNotValid } from '../utils'
39 import { VideoChannelModel } from '../video/video-channel'
40 import { ActorModel, unusedActorAttributesForAPI } from './actor'
43 tableName: 'actorFollow',
49 fields: [ 'targetActorId' ]
52 fields: [ 'actorId', 'targetActorId' ],
64 export class ActorFollowModel extends Model {
67 @Column(DataType.ENUM(...values(FOLLOW_STATES)))
71 @Default(ACTOR_FOLLOW_SCORE.BASE)
73 @Max(ACTOR_FOLLOW_SCORE.MAX)
77 // Allow null because we added this column in PeerTube v3, and don't want to generate fake URLs of remote follows
79 @Is('ActorFollowUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'url'))
80 @Column(DataType.STRING(CONSTRAINTS_FIELDS.COMMONS.URL.max))
89 @ForeignKey(() => ActorModel)
93 @BelongsTo(() => ActorModel, {
101 ActorFollower: ActorModel
103 @ForeignKey(() => ActorModel)
105 targetActorId: number
107 @BelongsTo(() => ActorModel, {
109 name: 'targetActorId',
112 as: 'ActorFollowing',
115 ActorFollowing: ActorModel
119 static incrementFollowerAndFollowingCount (instance: ActorFollowModel, options: any) {
120 if (instance.state !== 'accepted') return undefined
123 ActorModel.rebuildFollowsCount(instance.actorId, 'following', options.transaction),
124 ActorModel.rebuildFollowsCount(instance.targetActorId, 'followers', options.transaction)
129 static decrementFollowerAndFollowingCount (instance: ActorFollowModel, options: any) {
131 ActorModel.rebuildFollowsCount(instance.actorId, 'following', options.transaction),
132 ActorModel.rebuildFollowsCount(instance.targetActorId, 'followers', options.transaction)
136 static removeFollowsOf (actorId: number, t?: Transaction) {
144 targetActorId: actorId
151 return ActorFollowModel.destroy(query)
154 // Remove actor follows with a score of 0 (too many requests where they were unreachable)
155 static async removeBadActorFollows () {
156 const actorFollows = await ActorFollowModel.listBadActorFollows()
158 const actorFollowsRemovePromises = actorFollows.map(actorFollow => actorFollow.destroy())
159 await Promise.all(actorFollowsRemovePromises)
161 const numberOfActorFollowsRemoved = actorFollows.length
163 if (numberOfActorFollowsRemoved) logger.info('Removed bad %d actor follows.', numberOfActorFollowsRemoved)
166 static isFollowedBy (actorId: number, followerActorId: number) {
167 const query = 'SELECT 1 FROM "actorFollow" WHERE "actorId" = $followerActorId AND "targetActorId" = $actorId LIMIT 1'
169 type: QueryTypes.SELECT as QueryTypes.SELECT,
170 bind: { actorId, followerActorId },
174 return VideoModel.sequelize.query(query, options)
175 .then(results => results.length === 1)
178 static loadByActorAndTarget (actorId: number, targetActorId: number, t?: Transaction): Promise<MActorFollowActorsDefault> {
182 targetActorId: targetActorId
199 return ActorFollowModel.findOne(query)
202 static loadByActorAndTargetNameAndHostForAPI (
207 ): Promise<MActorFollowActorsDefaultSubscription> {
208 const actorFollowingPartInclude: IncludeOptions = {
211 as: 'ActorFollowing',
213 preferredUsername: targetName
217 model: VideoChannelModel.unscoped(),
223 if (targetHost === null) {
224 actorFollowingPartInclude.where['serverId'] = null
226 actorFollowingPartInclude.include.push({
240 actorFollowingPartInclude,
250 return ActorFollowModel.findOne(query)
252 if (result?.ActorFollowing.VideoChannel) {
253 result.ActorFollowing.VideoChannel.Actor = result.ActorFollowing
260 static listSubscribedIn (actorId: number, targets: { name: string, host?: string }[]): Promise<MActorFollowFollowingHost[]> {
261 const whereTab = targets
267 $preferredUsername$: t.name
279 $preferredUsername$: t.name
289 attributes: [ 'id' ],
302 attributes: [ 'preferredUsername' ],
303 model: ActorModel.unscoped(),
305 as: 'ActorFollowing',
308 attributes: [ 'host' ],
309 model: ServerModel.unscoped(),
317 return ActorFollowModel.findAll(query)
320 static listFollowingForApi (options: {
326 actorType?: ActivityPubActorType
329 const { id, start, count, sort, search, state, actorType } = options
331 const followWhere = state ? { state } : {}
332 const followingWhere: WhereOptions = {}
333 const followingServerWhere: WhereOptions = {}
336 Object.assign(followingServerWhere, {
338 [Op.iLike]: '%' + search + '%'
344 Object.assign(followingWhere, { type: actorType })
351 order: getFollowsSort(sort),
364 as: 'ActorFollowing',
366 where: followingWhere,
371 where: followingServerWhere
378 return ActorFollowModel.findAndCountAll<MActorFollowActorsDefault>(query)
379 .then(({ rows, count }) => {
387 static listFollowersForApi (options: {
393 actorType?: ActivityPubActorType
396 const { actorId, start, count, sort, search, state, actorType } = options
398 const followWhere = state ? { state } : {}
399 const followerWhere: WhereOptions = {}
400 const followerServerWhere: WhereOptions = {}
403 Object.assign(followerServerWhere, {
405 [Op.iLike]: '%' + search + '%'
411 Object.assign(followerWhere, { type: actorType })
418 order: getFollowsSort(sort),
425 where: followerWhere,
430 where: followerServerWhere
436 as: 'ActorFollowing',
445 return ActorFollowModel.findAndCountAll<MActorFollowActorsDefault>(query)
446 .then(({ rows, count }) => {
454 static listSubscriptionsForApi (options: {
461 const { actorId, start, count, sort } = options
466 if (options.search) {
467 Object.assign(where, {
469 searchAttribute(options.search, '$ActorFollowing.preferredUsername$'),
470 searchAttribute(options.search, '$ActorFollowing.VideoChannel.name$')
480 order: getSort(sort),
484 attributes: [ 'id' ],
485 model: ActorModel.unscoped(),
486 as: 'ActorFollowing',
490 model: VideoChannelModel.unscoped(),
495 exclude: unusedActorAttributesForAPI
501 model: AccountModel.unscoped(),
506 exclude: unusedActorAttributesForAPI
520 return ActorFollowModel.findAndCountAll<MActorFollowSubscriptions>(query)
521 .then(({ rows, count }) => {
523 data: rows.map(r => r.ActorFollowing.VideoChannel),
529 static async keepUnfollowedInstance (hosts: string[]) {
530 const followerId = (await getServerActor()).id
533 attributes: [ 'id' ],
539 attributes: [ 'id' ],
540 model: ActorModel.unscoped(),
542 as: 'ActorFollowing',
544 preferredUsername: SERVER_ACTOR_NAME
548 attributes: [ 'host' ],
549 model: ServerModel.unscoped(),
562 const res = await ActorFollowModel.findAll(query)
563 const followedHosts = res.map(row => row.ActorFollowing.Server.host)
565 return difference(hosts, followedHosts)
568 static listAcceptedFollowerUrlsForAP (actorIds: number[], t: Transaction, start?: number, count?: number) {
569 return ActorFollowModel.createListAcceptedFollowForApiQuery('followers', actorIds, t, start, count)
572 static listAcceptedFollowerSharedInboxUrls (actorIds: number[], t: Transaction) {
573 return ActorFollowModel.createListAcceptedFollowForApiQuery(
584 static listAcceptedFollowingUrlsForApi (actorIds: number[], t: Transaction, start?: number, count?: number) {
585 return ActorFollowModel.createListAcceptedFollowForApiQuery('following', actorIds, t, start, count)
588 static async getStats () {
589 const serverActor = await getServerActor()
591 const totalInstanceFollowing = await ActorFollowModel.count({
593 actorId: serverActor.id
597 const totalInstanceFollowers = await ActorFollowModel.count({
599 targetActorId: serverActor.id
604 totalInstanceFollowing,
605 totalInstanceFollowers
609 static updateScore (inboxUrl: string, value: number, t?: Transaction) {
610 const query = `UPDATE "actorFollow" SET "score" = LEAST("score" + ${value}, ${ACTOR_FOLLOW_SCORE.MAX}) ` +
612 'SELECT "actorFollow"."id" FROM "actorFollow" ' +
613 'INNER JOIN "actor" ON "actor"."id" = "actorFollow"."actorId" ' +
614 `WHERE "actor"."inboxUrl" = '${inboxUrl}' OR "actor"."sharedInboxUrl" = '${inboxUrl}'` +
618 type: QueryTypes.BULKUPDATE,
622 return ActorFollowModel.sequelize.query(query, options)
625 static async updateScoreByFollowingServers (serverIds: number[], value: number, t?: Transaction) {
626 if (serverIds.length === 0) return
628 const me = await getServerActor()
629 const serverIdsString = createSafeIn(ActorFollowModel, serverIds)
631 const query = `UPDATE "actorFollow" SET "score" = LEAST("score" + ${value}, ${ACTOR_FOLLOW_SCORE.MAX}) ` +
633 'SELECT "actorFollow"."id" FROM "actorFollow" ' +
634 'INNER JOIN "actor" ON "actor"."id" = "actorFollow"."targetActorId" ' +
635 `WHERE "actorFollow"."actorId" = ${me.Account.actorId} ` + // I'm the follower
636 `AND "actor"."serverId" IN (${serverIdsString})` + // Criteria on followings
640 type: QueryTypes.BULKUPDATE,
644 return ActorFollowModel.sequelize.query(query, options)
647 private static async createListAcceptedFollowForApiQuery (
648 type: 'followers' | 'following',
656 let firstJoin: string
657 let secondJoin: string
659 if (type === 'followers') {
660 firstJoin = 'targetActorId'
661 secondJoin = 'actorId'
663 firstJoin = 'actorId'
664 secondJoin = 'targetActorId'
667 const selections: string[] = []
668 if (distinct === true) selections.push(`DISTINCT("Follows"."${columnUrl}") AS "selectionUrl"`)
669 else selections.push(`"Follows"."${columnUrl}" AS "selectionUrl"`)
671 selections.push('COUNT(*) AS "total"')
673 const tasks: Promise<any>[] = []
675 for (const selection of selections) {
676 let query = 'SELECT ' + selection + ' FROM "actor" ' +
677 'INNER JOIN "actorFollow" ON "actorFollow"."' + firstJoin + '" = "actor"."id" ' +
678 'INNER JOIN "actor" AS "Follows" ON "actorFollow"."' + secondJoin + '" = "Follows"."id" ' +
679 `WHERE "actor"."id" = ANY ($actorIds) AND "actorFollow"."state" = 'accepted' AND "Follows"."${columnUrl}" IS NOT NULL `
681 if (count !== undefined) query += 'LIMIT ' + count
682 if (start !== undefined) query += ' OFFSET ' + start
686 type: QueryTypes.SELECT,
689 tasks.push(ActorFollowModel.sequelize.query(query, options))
692 const [ followers, [ dataTotal ] ] = await Promise.all(tasks)
693 const urls: string[] = followers.map(f => f.selectionUrl)
697 total: dataTotal ? parseInt(dataTotal.total, 10) : 0
701 private static listBadActorFollows () {
711 return ActorFollowModel.findAll(query)
714 toFormattedJSON (this: MActorFollowFormattable): ActorFollow {
715 const follower = this.ActorFollower.toFormattedJSON()
716 const following = this.ActorFollowing.toFormattedJSON()
724 createdAt: this.createdAt,
725 updatedAt: this.updatedAt