1 import { difference } from 'lodash'
2 import { Attributes, FindOptions, Includeable, IncludeOptions, Op, QueryTypes, Transaction, WhereAttributeHash } 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'
27 MActorFollowActorsDefault,
28 MActorFollowActorsDefaultSubscription,
29 MActorFollowFollowingHost,
30 MActorFollowFormattable,
31 MActorFollowSubscriptions
32 } from '@server/types/models'
33 import { AttributesOnly } from '@shared/typescript-utils'
34 import { FollowState } from '../../../shared/models/actors'
35 import { ActorFollow } from '../../../shared/models/actors/follow.model'
36 import { logger } from '../../helpers/logger'
37 import { ACTOR_FOLLOW_SCORE, CONSTRAINTS_FIELDS, FOLLOW_STATES, SERVER_ACTOR_NAME, SORTABLE_COLUMNS } from '../../initializers/constants'
38 import { AccountModel } from '../account/account'
39 import { ServerModel } from '../server/server'
40 import { buildSQLAttributes, createSafeIn, getSort, searchAttribute, throwIfNotValid } from '../shared'
41 import { doesExist } from '../shared/query'
42 import { VideoChannelModel } from '../video/video-channel'
43 import { ActorModel, unusedActorAttributesForAPI } from './actor'
44 import { InstanceListFollowersQueryBuilder, ListFollowersOptions } from './sql/instance-list-followers-query-builder'
45 import { InstanceListFollowingQueryBuilder, ListFollowingOptions } from './sql/instance-list-following-query-builder'
48 tableName: 'actorFollow',
54 fields: [ 'targetActorId' ]
57 fields: [ 'actorId', 'targetActorId' ],
69 export class ActorFollowModel extends Model<Partial<AttributesOnly<ActorFollowModel>>> {
72 @Column(DataType.ENUM(...Object.values(FOLLOW_STATES)))
76 @Default(ACTOR_FOLLOW_SCORE.BASE)
78 @Max(ACTOR_FOLLOW_SCORE.MAX)
82 // Allow null because we added this column in PeerTube v3, and don't want to generate fake URLs of remote follows
84 @Is('ActorFollowUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'url'))
85 @Column(DataType.STRING(CONSTRAINTS_FIELDS.COMMONS.URL.max))
94 @ForeignKey(() => ActorModel)
98 @BelongsTo(() => ActorModel, {
106 ActorFollower: ActorModel
108 @ForeignKey(() => ActorModel)
110 targetActorId: number
112 @BelongsTo(() => ActorModel, {
114 name: 'targetActorId',
117 as: 'ActorFollowing',
120 ActorFollowing: ActorModel
124 static incrementFollowerAndFollowingCount (instance: ActorFollowModel, options: any) {
125 return afterCommitIfTransaction(options.transaction, () => {
127 ActorModel.rebuildFollowsCount(instance.actorId, 'following'),
128 ActorModel.rebuildFollowsCount(instance.targetActorId, 'followers')
134 static decrementFollowerAndFollowingCount (instance: ActorFollowModel, options: any) {
135 return afterCommitIfTransaction(options.transaction, () => {
137 ActorModel.rebuildFollowsCount(instance.actorId, 'following'),
138 ActorModel.rebuildFollowsCount(instance.targetActorId, 'followers')
143 // ---------------------------------------------------------------------------
145 static getSQLAttributes (tableName: string, aliasPrefix = '') {
146 return buildSQLAttributes({
153 // ---------------------------------------------------------------------------
156 * @deprecated Use `findOrCreateCustom` instead
158 static findOrCreate (): any {
159 throw new Error('Must not be called')
162 // findOrCreate has issues with actor follow hooks
163 static async findOrCreateCustom (options: {
168 transaction: Transaction
169 }): Promise<[ MActorFollowActors, boolean ]> {
170 const { byActor, targetActor, activityId, state, transaction } = options
173 let actorFollow: MActorFollowActors = await ActorFollowModel.loadByActorAndTarget(byActor.id, targetActor.id, transaction)
178 actorFollow = await ActorFollowModel.create({
180 targetActorId: targetActor.id,
186 actorFollow.ActorFollowing = targetActor
187 actorFollow.ActorFollower = byActor
190 return [ actorFollow, created ]
193 static removeFollowsOf (actorId: number, t?: Transaction) {
201 targetActorId: actorId
208 return ActorFollowModel.destroy(query)
211 // Remove actor follows with a score of 0 (too many requests where they were unreachable)
212 static async removeBadActorFollows () {
213 const actorFollows = await ActorFollowModel.listBadActorFollows()
215 const actorFollowsRemovePromises = actorFollows.map(actorFollow => actorFollow.destroy())
216 await Promise.all(actorFollowsRemovePromises)
218 const numberOfActorFollowsRemoved = actorFollows.length
220 if (numberOfActorFollowsRemoved) logger.info('Removed bad %d actor follows.', numberOfActorFollowsRemoved)
223 static isFollowedBy (actorId: number, followerActorId: number) {
224 const query = `SELECT 1 FROM "actorFollow" ` +
225 `WHERE "actorId" = $followerActorId AND "targetActorId" = $actorId AND "state" = 'accepted' ` +
228 return doesExist(this.sequelize, query, { actorId, followerActorId })
231 static loadByActorAndTarget (actorId: number, targetActorId: number, t?: Transaction): Promise<MActorFollowActorsDefault> {
252 return ActorFollowModel.findOne(query)
255 static loadByActorAndTargetNameAndHostForAPI (options: {
260 transaction?: Transaction
261 }): Promise<MActorFollowActorsDefaultSubscription> {
262 const { actorId, targetHost, targetName, state, transaction } = options
264 const actorFollowingPartInclude: IncludeOptions = {
267 as: 'ActorFollowing',
268 where: ActorModel.wherePreferredUsername(targetName),
271 model: VideoChannelModel.unscoped(),
277 if (targetHost === null) {
278 actorFollowingPartInclude.where['serverId'] = null
280 actorFollowingPartInclude.include.push({
289 const where: WhereAttributeHash<Attributes<ActorFollowModel>> = { actorId }
290 if (state) where.state = state
292 const query: FindOptions<Attributes<ActorFollowModel>> = {
295 actorFollowingPartInclude,
305 return ActorFollowModel.findOne(query)
308 static listSubscriptionsOf (actorId: number, targets: { name: string, host?: string }[]): Promise<MActorFollowFollowingHost[]> {
309 const whereTab = targets
314 ActorModel.wherePreferredUsername(t.name),
322 ActorModel.wherePreferredUsername(t.name),
329 attributes: [ 'id' ],
343 attributes: [ 'preferredUsername' ],
344 model: ActorModel.unscoped(),
346 as: 'ActorFollowing',
349 attributes: [ 'host' ],
350 model: ServerModel.unscoped(),
358 return ActorFollowModel.findAll(query)
361 static listInstanceFollowingForApi (options: ListFollowingOptions) {
363 new InstanceListFollowingQueryBuilder(this.sequelize, options).countFollowing(),
364 new InstanceListFollowingQueryBuilder(this.sequelize, options).listFollowing()
365 ]).then(([ total, data ]) => ({ total, data }))
368 static listFollowersForApi (options: ListFollowersOptions) {
370 new InstanceListFollowersQueryBuilder(this.sequelize, options).countFollowers(),
371 new InstanceListFollowersQueryBuilder(this.sequelize, options).listFollowers()
372 ]).then(([ total, data ]) => ({ total, data }))
375 static listSubscriptionsForApi (options: {
382 const { actorId, start, count, sort } = options
388 if (options.search) {
389 Object.assign(where, {
391 searchAttribute(options.search, '$ActorFollowing.preferredUsername$'),
392 searchAttribute(options.search, '$ActorFollowing.VideoChannel.name$')
397 const getQuery = (forCount: boolean) => {
398 let channelInclude: Includeable[] = []
400 if (forCount !== true) {
404 exclude: unusedActorAttributesForAPI
410 model: AccountModel.unscoped(),
415 exclude: unusedActorAttributesForAPI
426 attributes: forCount === true
428 : SORTABLE_COLUMNS.USER_SUBSCRIPTIONS,
432 order: getSort(sort),
436 attributes: [ 'id' ],
437 model: ActorModel.unscoped(),
438 as: 'ActorFollowing',
442 model: VideoChannelModel.unscoped(),
444 include: channelInclude
453 ActorFollowModel.count(getQuery(true)),
454 ActorFollowModel.findAll<MActorFollowSubscriptions>(getQuery(false))
455 ]).then(([ total, rows ]) => ({
457 data: rows.map(r => r.ActorFollowing.VideoChannel)
461 static async keepUnfollowedInstance (hosts: string[]) {
462 const followerId = (await getServerActor()).id
465 attributes: [ 'id' ],
471 attributes: [ 'id' ],
472 model: ActorModel.unscoped(),
474 as: 'ActorFollowing',
476 preferredUsername: SERVER_ACTOR_NAME
480 attributes: [ 'host' ],
481 model: ServerModel.unscoped(),
494 const res = await ActorFollowModel.findAll(query)
495 const followedHosts = res.map(row => row.ActorFollowing.Server.host)
497 return difference(hosts, followedHosts)
500 static listAcceptedFollowerUrlsForAP (actorIds: number[], t: Transaction, start?: number, count?: number) {
501 return ActorFollowModel.createListAcceptedFollowForApiQuery('followers', actorIds, t, start, count)
504 static listAcceptedFollowerSharedInboxUrls (actorIds: number[], t: Transaction) {
505 return ActorFollowModel.createListAcceptedFollowForApiQuery(
516 static listAcceptedFollowingUrlsForApi (actorIds: number[], t: Transaction, start?: number, count?: number) {
517 return ActorFollowModel.createListAcceptedFollowForApiQuery('following', actorIds, t, start, count)
520 static async getStats () {
521 const serverActor = await getServerActor()
523 const totalInstanceFollowing = await ActorFollowModel.count({
525 actorId: serverActor.id,
530 const totalInstanceFollowers = await ActorFollowModel.count({
532 targetActorId: serverActor.id,
538 totalInstanceFollowing,
539 totalInstanceFollowers
543 static updateScore (inboxUrl: string, value: number, t?: Transaction) {
544 const query = `UPDATE "actorFollow" SET "score" = LEAST("score" + ${value}, ${ACTOR_FOLLOW_SCORE.MAX}) ` +
546 'SELECT "actorFollow"."id" FROM "actorFollow" ' +
547 'INNER JOIN "actor" ON "actor"."id" = "actorFollow"."actorId" ' +
548 `WHERE "actor"."inboxUrl" = '${inboxUrl}' OR "actor"."sharedInboxUrl" = '${inboxUrl}'` +
552 type: QueryTypes.BULKUPDATE,
556 return ActorFollowModel.sequelize.query(query, options)
559 static async updateScoreByFollowingServers (serverIds: number[], value: number, t?: Transaction) {
560 if (serverIds.length === 0) return
562 const me = await getServerActor()
563 const serverIdsString = createSafeIn(ActorFollowModel.sequelize, serverIds)
565 const query = `UPDATE "actorFollow" SET "score" = LEAST("score" + ${value}, ${ACTOR_FOLLOW_SCORE.MAX}) ` +
567 'SELECT "actorFollow"."id" FROM "actorFollow" ' +
568 'INNER JOIN "actor" ON "actor"."id" = "actorFollow"."targetActorId" ' +
569 `WHERE "actorFollow"."actorId" = ${me.Account.actorId} ` + // I'm the follower
570 `AND "actor"."serverId" IN (${serverIdsString})` + // Criteria on followings
574 type: QueryTypes.BULKUPDATE,
578 return ActorFollowModel.sequelize.query(query, options)
581 private static async createListAcceptedFollowForApiQuery (
582 type: 'followers' | 'following',
590 let firstJoin: string
591 let secondJoin: string
593 if (type === 'followers') {
594 firstJoin = 'targetActorId'
595 secondJoin = 'actorId'
597 firstJoin = 'actorId'
598 secondJoin = 'targetActorId'
601 const selections: string[] = []
602 if (distinct === true) selections.push(`DISTINCT("Follows"."${columnUrl}") AS "selectionUrl"`)
603 else selections.push(`"Follows"."${columnUrl}" AS "selectionUrl"`)
605 selections.push('COUNT(*) AS "total"')
607 const tasks: Promise<any>[] = []
609 for (const selection of selections) {
610 let query = 'SELECT ' + selection + ' FROM "actor" ' +
611 'INNER JOIN "actorFollow" ON "actorFollow"."' + firstJoin + '" = "actor"."id" ' +
612 'INNER JOIN "actor" AS "Follows" ON "actorFollow"."' + secondJoin + '" = "Follows"."id" ' +
613 `WHERE "actor"."id" = ANY ($actorIds) AND "actorFollow"."state" = 'accepted' AND "Follows"."${columnUrl}" IS NOT NULL `
615 if (count !== undefined) query += 'LIMIT ' + count
616 if (start !== undefined) query += ' OFFSET ' + start
620 type: QueryTypes.SELECT,
623 tasks.push(ActorFollowModel.sequelize.query(query, options))
626 const [ followers, [ dataTotal ] ] = await Promise.all(tasks)
627 const urls: string[] = followers.map(f => f.selectionUrl)
631 total: dataTotal ? parseInt(dataTotal.total, 10) : 0
635 private static listBadActorFollows () {
645 return ActorFollowModel.findAll(query)
648 toFormattedJSON (this: MActorFollowFormattable): ActorFollow {
649 const follower = this.ActorFollower.toFormattedJSON()
650 const following = this.ActorFollowing.toFormattedJSON()
658 createdAt: this.createdAt,
659 updatedAt: this.updatedAt