2021-06-15 03:17:19 -04:00
|
|
|
import { Transaction } from 'sequelize'
|
|
|
|
import { getServerActor } from '@server/models/application/application'
|
|
|
|
import { logger } from '../../helpers/logger'
|
2019-08-30 10:50:12 -04:00
|
|
|
import { CONFIG } from '../../initializers/config'
|
|
|
|
import { SERVER_ACTOR_NAME } from '../../initializers/constants'
|
2019-09-04 09:50:40 -04:00
|
|
|
import { ServerModel } from '../../models/server/server'
|
2021-06-15 03:17:19 -04:00
|
|
|
import { MActorFollowActors } from '../../types/models'
|
|
|
|
import { JobQueue } from '../job-queue'
|
2019-08-30 10:50:12 -04:00
|
|
|
|
2021-06-15 03:17:19 -04:00
|
|
|
async function autoFollowBackIfNeeded (actorFollow: MActorFollowActors, transaction?: Transaction) {
|
2019-08-30 10:50:12 -04:00
|
|
|
if (!CONFIG.FOLLOWINGS.INSTANCE.AUTO_FOLLOW_BACK.ENABLED) return
|
|
|
|
|
|
|
|
const follower = actorFollow.ActorFollower
|
|
|
|
|
|
|
|
if (follower.type === 'Application' && follower.preferredUsername === SERVER_ACTOR_NAME) {
|
|
|
|
logger.info('Auto follow back %s.', follower.url)
|
|
|
|
|
|
|
|
const me = await getServerActor()
|
|
|
|
|
2021-06-15 03:17:19 -04:00
|
|
|
const server = await ServerModel.load(follower.serverId, transaction)
|
2019-08-30 10:50:12 -04:00
|
|
|
const host = server.host
|
|
|
|
|
|
|
|
const payload = {
|
|
|
|
host,
|
|
|
|
name: SERVER_ACTOR_NAME,
|
|
|
|
followerActorId: me.id,
|
|
|
|
isAutoFollow: true
|
|
|
|
}
|
|
|
|
|
2022-08-08 09:48:17 -04:00
|
|
|
JobQueue.Instance.createJobAsync({ type: 'activitypub-follow', payload })
|
2019-08-30 10:50:12 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-20 08:15:15 -04:00
|
|
|
// If we only have an host, use a default account handle
|
|
|
|
function getRemoteNameAndHost (handleOrHost: string) {
|
|
|
|
let name = SERVER_ACTOR_NAME
|
|
|
|
let host = handleOrHost
|
|
|
|
|
|
|
|
const splitted = handleOrHost.split('@')
|
|
|
|
if (splitted.length === 2) {
|
|
|
|
name = splitted[0]
|
|
|
|
host = splitted[1]
|
|
|
|
}
|
|
|
|
|
|
|
|
return { name, host }
|
|
|
|
}
|
|
|
|
|
2019-08-30 10:50:12 -04:00
|
|
|
export {
|
2021-07-20 08:15:15 -04:00
|
|
|
autoFollowBackIfNeeded,
|
|
|
|
getRemoteNameAndHost
|
2019-08-30 10:50:12 -04:00
|
|
|
}
|