1 import { difference, values } from 'lodash'
2 import { Includeable, IncludeOptions, literal, 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'
27 MActorFollowActorsDefault,
28 MActorFollowActorsDefaultSubscription,
29 MActorFollowFollowingHost,
30 MActorFollowFormattable,
31 MActorFollowSubscriptions
32 } from '@server/types/models'
33 import { ActivityPubActorType } from '@shared/models'
34 import { AttributesOnly } from '@shared/typescript-utils'
35 import { FollowState } from '../../../shared/models/actors'
36 import { ActorFollow } from '../../../shared/models/actors/follow.model'
37 import { logger } from '../../helpers/logger'
38 import { ACTOR_FOLLOW_SCORE, CONSTRAINTS_FIELDS, FOLLOW_STATES, SERVER_ACTOR_NAME, SORTABLE_COLUMNS } from '../../initializers/constants'
39 import { AccountModel } from '../account/account'
40 import { ServerModel } from '../server/server'
41 import { doesExist } from '../shared/query'
42 import { createSafeIn, getFollowsSort, getSort, searchAttribute, throwIfNotValid } from '../utils'
43 import { VideoChannelModel } from '../video/video-channel'
44 import { ActorModel, unusedActorAttributesForAPI } from './actor'
47 tableName: 'actorFollow',
53 fields: [ 'targetActorId' ]
56 fields: [ 'actorId', 'targetActorId' ],
68 export class ActorFollowModel extends Model<Partial<AttributesOnly<ActorFollowModel>>> {
71 @Column(DataType.ENUM(...values(FOLLOW_STATES)))
75 @Default(ACTOR_FOLLOW_SCORE.BASE)
77 @Max(ACTOR_FOLLOW_SCORE.MAX)
81 // Allow null because we added this column in PeerTube v3, and don't want to generate fake URLs of remote follows
83 @Is('ActorFollowUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'url'))
84 @Column(DataType.STRING(CONSTRAINTS_FIELDS.COMMONS.URL.max))
93 @ForeignKey(() => ActorModel)
97 @BelongsTo(() => ActorModel, {
105 ActorFollower: ActorModel
107 @ForeignKey(() => ActorModel)
109 targetActorId: number
111 @BelongsTo(() => ActorModel, {
113 name: 'targetActorId',
116 as: 'ActorFollowing',
119 ActorFollowing: ActorModel
123 static incrementFollowerAndFollowingCount (instance: ActorFollowModel, options: any) {
124 return afterCommitIfTransaction(options.transaction, () => {
126 ActorModel.rebuildFollowsCount(instance.actorId, 'following'),
127 ActorModel.rebuildFollowsCount(instance.targetActorId, 'followers')
133 static decrementFollowerAndFollowingCount (instance: ActorFollowModel, options: any) {
134 return afterCommitIfTransaction(options.transaction, () => {
136 ActorModel.rebuildFollowsCount(instance.actorId, 'following'),
137 ActorModel.rebuildFollowsCount(instance.targetActorId, 'followers')
143 * @deprecated Use `findOrCreateCustom` instead
145 static findOrCreate (): any {
146 throw new Error('Must not be called')
149 // findOrCreate has issues with actor follow hooks
150 static async findOrCreateCustom (options: {
155 transaction: Transaction
156 }): Promise<[ MActorFollowActors, boolean ]> {
157 const { byActor, targetActor, activityId, state, transaction } = options
160 let actorFollow: MActorFollowActors = await ActorFollowModel.loadByActorAndTarget(byActor.id, targetActor.id, transaction)
165 actorFollow = await ActorFollowModel.create({
167 targetActorId: targetActor.id,
173 actorFollow.ActorFollowing = targetActor
174 actorFollow.ActorFollower = byActor
177 return [ actorFollow, created ]
180 static removeFollowsOf (actorId: number, t?: Transaction) {
188 targetActorId: actorId
195 return ActorFollowModel.destroy(query)
198 // Remove actor follows with a score of 0 (too many requests where they were unreachable)
199 static async removeBadActorFollows () {
200 const actorFollows = await ActorFollowModel.listBadActorFollows()
202 const actorFollowsRemovePromises = actorFollows.map(actorFollow => actorFollow.destroy())
203 await Promise.all(actorFollowsRemovePromises)
205 const numberOfActorFollowsRemoved = actorFollows.length
207 if (numberOfActorFollowsRemoved) logger.info('Removed bad %d actor follows.', numberOfActorFollowsRemoved)
210 static isFollowedBy (actorId: number, followerActorId: number) {
211 const query = 'SELECT 1 FROM "actorFollow" WHERE "actorId" = $followerActorId AND "targetActorId" = $actorId LIMIT 1'
213 return doesExist(query, { actorId, followerActorId })
216 static loadByActorAndTarget (actorId: number, targetActorId: number, t?: Transaction): Promise<MActorFollowActorsDefault> {
220 targetActorId: targetActorId
237 return ActorFollowModel.findOne(query)
240 static loadByActorAndTargetNameAndHostForAPI (
245 ): Promise<MActorFollowActorsDefaultSubscription> {
246 const actorFollowingPartInclude: IncludeOptions = {
249 as: 'ActorFollowing',
251 preferredUsername: targetName
255 model: VideoChannelModel.unscoped(),
261 if (targetHost === null) {
262 actorFollowingPartInclude.where['serverId'] = null
264 actorFollowingPartInclude.include.push({
278 actorFollowingPartInclude,
288 return ActorFollowModel.findOne(query)
291 static listSubscriptionsOf (actorId: number, targets: { name: string, host?: string }[]): Promise<MActorFollowFollowingHost[]> {
292 const whereTab = targets
298 $preferredUsername$: t.name
310 $preferredUsername$: t.name
320 attributes: [ 'id' ],
333 attributes: [ 'preferredUsername' ],
334 model: ActorModel.unscoped(),
336 as: 'ActorFollowing',
339 attributes: [ 'host' ],
340 model: ServerModel.unscoped(),
348 return ActorFollowModel.findAll(query)
351 static listInstanceFollowingForApi (options: {
357 actorType?: ActivityPubActorType
360 const { id, start, count, sort, search, state, actorType } = options
362 const followWhere = state ? { state } : {}
363 const followingWhere: WhereOptions = {}
366 Object.assign(followWhere, {
368 searchAttribute(options.search, '$ActorFollowing.preferredUsername$'),
369 searchAttribute(options.search, '$ActorFollowing.Server.host$')
375 Object.assign(followingWhere, { type: actorType })
378 const getQuery = (forCount: boolean) => {
379 const actorModel = forCount
380 ? ActorModel.unscoped()
387 order: getFollowsSort(sort),
400 as: 'ActorFollowing',
402 where: followingWhere,
415 ActorFollowModel.count(getQuery(true)),
416 ActorFollowModel.findAll<MActorFollowActorsDefault>(getQuery(false))
417 ]).then(([ total, data ]) => ({ total, data }))
420 static listFollowersForApi (options: {
426 actorType?: ActivityPubActorType
429 const { actorIds, start, count, sort, search, state, actorType } = options
431 const followWhere = state ? { state } : {}
432 const followerWhere: WhereOptions = {}
435 const escapedSearch = ActorFollowModel.sequelize.escape('%' + search + '%')
437 Object.assign(followerWhere, {
441 `SELECT "actor".id FROM actor LEFT JOIN server on server.id = actor."serverId" ` +
442 `WHERE "preferredUsername" ILIKE ${escapedSearch} OR "host" ILIKE ${escapedSearch}` +
450 Object.assign(followerWhere, { type: actorType })
453 const getQuery = (forCount: boolean) => {
454 const actorModel = forCount
455 ? ActorModel.unscoped()
463 order: getFollowsSort(sort),
474 as: 'ActorFollowing',
487 ActorFollowModel.count(getQuery(true)),
488 ActorFollowModel.findAll<MActorFollowActorsDefault>(getQuery(false))
489 ]).then(([ total, data ]) => ({ total, data }))
492 static listSubscriptionsForApi (options: {
499 const { actorId, start, count, sort } = options
504 if (options.search) {
505 Object.assign(where, {
507 searchAttribute(options.search, '$ActorFollowing.preferredUsername$'),
508 searchAttribute(options.search, '$ActorFollowing.VideoChannel.name$')
513 const getQuery = (forCount: boolean) => {
514 let channelInclude: Includeable[] = []
516 if (forCount !== true) {
520 exclude: unusedActorAttributesForAPI
526 model: AccountModel.unscoped(),
531 exclude: unusedActorAttributesForAPI
542 attributes: forCount === true
544 : SORTABLE_COLUMNS.USER_SUBSCRIPTIONS,
548 order: getSort(sort),
552 attributes: [ 'id' ],
553 model: ActorModel.unscoped(),
554 as: 'ActorFollowing',
558 model: VideoChannelModel.unscoped(),
560 include: channelInclude
569 ActorFollowModel.count(getQuery(true)),
570 ActorFollowModel.findAll<MActorFollowSubscriptions>(getQuery(false))
571 ]).then(([ total, rows ]) => ({
573 data: rows.map(r => r.ActorFollowing.VideoChannel)
577 static async keepUnfollowedInstance (hosts: string[]) {
578 const followerId = (await getServerActor()).id
581 attributes: [ 'id' ],
587 attributes: [ 'id' ],
588 model: ActorModel.unscoped(),
590 as: 'ActorFollowing',
592 preferredUsername: SERVER_ACTOR_NAME
596 attributes: [ 'host' ],
597 model: ServerModel.unscoped(),
610 const res = await ActorFollowModel.findAll(query)
611 const followedHosts = res.map(row => row.ActorFollowing.Server.host)
613 return difference(hosts, followedHosts)
616 static listAcceptedFollowerUrlsForAP (actorIds: number[], t: Transaction, start?: number, count?: number) {
617 return ActorFollowModel.createListAcceptedFollowForApiQuery('followers', actorIds, t, start, count)
620 static listAcceptedFollowerSharedInboxUrls (actorIds: number[], t: Transaction) {
621 return ActorFollowModel.createListAcceptedFollowForApiQuery(
632 static listAcceptedFollowingUrlsForApi (actorIds: number[], t: Transaction, start?: number, count?: number) {
633 return ActorFollowModel.createListAcceptedFollowForApiQuery('following', actorIds, t, start, count)
636 static async getStats () {
637 const serverActor = await getServerActor()
639 const totalInstanceFollowing = await ActorFollowModel.count({
641 actorId: serverActor.id
645 const totalInstanceFollowers = await ActorFollowModel.count({
647 targetActorId: serverActor.id
652 totalInstanceFollowing,
653 totalInstanceFollowers
657 static updateScore (inboxUrl: string, value: number, t?: Transaction) {
658 const query = `UPDATE "actorFollow" SET "score" = LEAST("score" + ${value}, ${ACTOR_FOLLOW_SCORE.MAX}) ` +
660 'SELECT "actorFollow"."id" FROM "actorFollow" ' +
661 'INNER JOIN "actor" ON "actor"."id" = "actorFollow"."actorId" ' +
662 `WHERE "actor"."inboxUrl" = '${inboxUrl}' OR "actor"."sharedInboxUrl" = '${inboxUrl}'` +
666 type: QueryTypes.BULKUPDATE,
670 return ActorFollowModel.sequelize.query(query, options)
673 static async updateScoreByFollowingServers (serverIds: number[], value: number, t?: Transaction) {
674 if (serverIds.length === 0) return
676 const me = await getServerActor()
677 const serverIdsString = createSafeIn(ActorFollowModel.sequelize, serverIds)
679 const query = `UPDATE "actorFollow" SET "score" = LEAST("score" + ${value}, ${ACTOR_FOLLOW_SCORE.MAX}) ` +
681 'SELECT "actorFollow"."id" FROM "actorFollow" ' +
682 'INNER JOIN "actor" ON "actor"."id" = "actorFollow"."targetActorId" ' +
683 `WHERE "actorFollow"."actorId" = ${me.Account.actorId} ` + // I'm the follower
684 `AND "actor"."serverId" IN (${serverIdsString})` + // Criteria on followings
688 type: QueryTypes.BULKUPDATE,
692 return ActorFollowModel.sequelize.query(query, options)
695 private static async createListAcceptedFollowForApiQuery (
696 type: 'followers' | 'following',
704 let firstJoin: string
705 let secondJoin: string
707 if (type === 'followers') {
708 firstJoin = 'targetActorId'
709 secondJoin = 'actorId'
711 firstJoin = 'actorId'
712 secondJoin = 'targetActorId'
715 const selections: string[] = []
716 if (distinct === true) selections.push(`DISTINCT("Follows"."${columnUrl}") AS "selectionUrl"`)
717 else selections.push(`"Follows"."${columnUrl}" AS "selectionUrl"`)
719 selections.push('COUNT(*) AS "total"')
721 const tasks: Promise<any>[] = []
723 for (const selection of selections) {
724 let query = 'SELECT ' + selection + ' FROM "actor" ' +
725 'INNER JOIN "actorFollow" ON "actorFollow"."' + firstJoin + '" = "actor"."id" ' +
726 'INNER JOIN "actor" AS "Follows" ON "actorFollow"."' + secondJoin + '" = "Follows"."id" ' +
727 `WHERE "actor"."id" = ANY ($actorIds) AND "actorFollow"."state" = 'accepted' AND "Follows"."${columnUrl}" IS NOT NULL `
729 if (count !== undefined) query += 'LIMIT ' + count
730 if (start !== undefined) query += ' OFFSET ' + start
734 type: QueryTypes.SELECT,
737 tasks.push(ActorFollowModel.sequelize.query(query, options))
740 const [ followers, [ dataTotal ] ] = await Promise.all(tasks)
741 const urls: string[] = followers.map(f => f.selectionUrl)
745 total: dataTotal ? parseInt(dataTotal.total, 10) : 0
749 private static listBadActorFollows () {
759 return ActorFollowModel.findAll(query)
762 toFormattedJSON (this: MActorFollowFormattable): ActorFollow {
763 const follower = this.ActorFollower.toFormattedJSON()
764 const following = this.ActorFollowing.toFormattedJSON()
772 createdAt: this.createdAt,
773 updatedAt: this.updatedAt