2021-08-27 07:32:44 -05:00
|
|
|
import { Job } from 'bull'
|
2021-05-11 04:15:29 -05:00
|
|
|
import { getLocalActorFollowActivityPubUrl } from '@server/lib/activitypub/url'
|
|
|
|
import { ActivitypubFollowPayload } from '@shared/models'
|
2018-04-18 08:32:40 -05:00
|
|
|
import { sanitizeHost } from '../../../helpers/core-utils'
|
|
|
|
import { retryTransactionWrapper } from '../../../helpers/database-utils'
|
2021-05-11 04:15:29 -05:00
|
|
|
import { logger } from '../../../helpers/logger'
|
|
|
|
import { REMOTE_SCHEME, WEBSERVER } from '../../../initializers/constants'
|
2019-04-11 07:26:41 -05:00
|
|
|
import { sequelizeTypescript } from '../../../initializers/database'
|
2021-05-11 04:15:29 -05:00
|
|
|
import { ActorModel } from '../../../models/actor/actor'
|
|
|
|
import { ActorFollowModel } from '../../../models/actor/actor-follow'
|
2020-06-18 03:45:25 -05:00
|
|
|
import { MActor, MActorFollowActors, MActorFull } from '../../../types/models'
|
2021-06-03 10:33:44 -05:00
|
|
|
import { getOrCreateAPActor, loadActorUrlOrGetFromWebfinger } from '../../activitypub/actors'
|
2021-05-11 04:15:29 -05:00
|
|
|
import { sendFollow } from '../../activitypub/send'
|
|
|
|
import { Notifier } from '../../notifier'
|
2018-04-18 08:32:40 -05:00
|
|
|
|
2021-08-27 07:32:44 -05:00
|
|
|
async function processActivityPubFollow (job: Job) {
|
2018-04-18 08:32:40 -05:00
|
|
|
const payload = job.data as ActivitypubFollowPayload
|
|
|
|
const host = payload.host
|
|
|
|
|
|
|
|
logger.info('Processing ActivityPub follow in job %d.', job.id)
|
|
|
|
|
2019-08-15 04:53:26 -05:00
|
|
|
let targetActor: MActorFull
|
2019-04-11 04:33:44 -05:00
|
|
|
if (!host || host === WEBSERVER.HOST) {
|
2018-08-21 09:18:59 -05:00
|
|
|
targetActor = await ActorModel.loadLocalByName(payload.name)
|
|
|
|
} else {
|
|
|
|
const sanitizedHost = sanitizeHost(host, REMOTE_SCHEME.HTTP)
|
|
|
|
const actorUrl = await loadActorUrlOrGetFromWebfinger(payload.name + '@' + sanitizedHost)
|
2021-06-03 09:02:29 -05:00
|
|
|
targetActor = await getOrCreateAPActor(actorUrl, 'all')
|
2018-08-21 09:18:59 -05:00
|
|
|
}
|
2018-04-18 08:32:40 -05:00
|
|
|
|
2020-04-16 09:36:43 -05:00
|
|
|
if (payload.assertIsChannel && !targetActor.VideoChannel) {
|
2020-04-20 02:04:38 -05:00
|
|
|
logger.warn('Do not follow %s@%s because it is not a channel.', payload.name, host)
|
2020-04-16 09:36:43 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-08-16 08:25:20 -05:00
|
|
|
const fromActor = await ActorModel.load(payload.followerActorId)
|
2018-04-18 08:32:40 -05:00
|
|
|
|
2019-08-30 09:50:12 -05:00
|
|
|
return retryTransactionWrapper(follow, fromActor, targetActor, payload.isAutoFollow)
|
2018-04-18 08:32:40 -05:00
|
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
export {
|
|
|
|
processActivityPubFollow
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2019-08-30 09:50:12 -05:00
|
|
|
async function follow (fromActor: MActor, targetActor: MActorFull, isAutoFollow = false) {
|
2018-04-18 08:32:40 -05:00
|
|
|
if (fromActor.id === targetActor.id) {
|
2019-09-23 01:17:42 -05:00
|
|
|
throw new Error('Follower is the same as target actor.')
|
2018-04-18 08:32:40 -05:00
|
|
|
}
|
|
|
|
|
2018-08-16 08:25:20 -05:00
|
|
|
// Same server, direct accept
|
|
|
|
const state = !fromActor.serverId && !targetActor.serverId ? 'accepted' : 'pending'
|
|
|
|
|
2019-01-04 01:56:20 -06:00
|
|
|
const actorFollow = await sequelizeTypescript.transaction(async t => {
|
2021-10-14 03:52:15 -05:00
|
|
|
const [ actorFollow ] = await ActorFollowModel.findOrCreateCustom({
|
|
|
|
byActor: fromActor,
|
|
|
|
state,
|
|
|
|
targetActor,
|
|
|
|
activityId: getLocalActorFollowActivityPubUrl(fromActor, targetActor),
|
2018-04-18 08:32:40 -05:00
|
|
|
transaction: t
|
|
|
|
})
|
|
|
|
|
2018-07-31 04:15:35 -05:00
|
|
|
// Send a notification to remote server if our follow is not already accepted
|
2019-07-29 04:59:29 -05:00
|
|
|
if (actorFollow.state !== 'accepted') sendFollow(actorFollow, t)
|
2019-01-04 01:56:20 -06:00
|
|
|
|
|
|
|
return actorFollow
|
2018-04-18 08:32:40 -05:00
|
|
|
})
|
2019-01-04 01:56:20 -06:00
|
|
|
|
2019-08-30 09:50:12 -05:00
|
|
|
const followerFull = await ActorModel.loadFull(fromActor.id)
|
2019-08-20 06:52:49 -05:00
|
|
|
|
2019-08-30 09:50:12 -05:00
|
|
|
const actorFollowFull = Object.assign(actorFollow, {
|
|
|
|
ActorFollowing: targetActor,
|
|
|
|
ActorFollower: followerFull
|
|
|
|
})
|
2019-08-20 06:52:49 -05:00
|
|
|
|
2019-08-30 09:50:12 -05:00
|
|
|
if (actorFollow.state === 'accepted') Notifier.Instance.notifyOfNewUserFollow(actorFollowFull)
|
|
|
|
if (isAutoFollow === true) Notifier.Instance.notifyOfAutoInstanceFollowing(actorFollowFull)
|
|
|
|
|
|
|
|
return actorFollow
|
2018-04-18 08:32:40 -05:00
|
|
|
}
|