]>
Commit | Line | Data |
---|---|---|
3fd3ab2d | 1 | import { ActivityFollow } from '../../../../shared/models/activitypub' |
da854ddd C |
2 | import { retryTransactionWrapper } from '../../../helpers/database-utils' |
3 | import { logger } from '../../../helpers/logger' | |
5b9c965d | 4 | import { sequelizeTypescript, CONFIG } from '../../../initializers' |
50d6de9c C |
5 | import { ActorModel } from '../../../models/activitypub/actor' |
6 | import { ActorFollowModel } from '../../../models/activitypub/actor-follow' | |
5b9c965d | 7 | import { sendAccept, sendReject } from '../send' |
f7cc67b4 | 8 | import { Notifier } from '../../notifier' |
848f499d | 9 | import { getAPId } from '../../../helpers/activitypub' |
5b9c965d | 10 | import { getServerActor } from '../../../helpers/utils' |
7a7724e6 | 11 | |
e587e0ec | 12 | async function processFollowActivity (activity: ActivityFollow, byActor: ActorModel) { |
848f499d | 13 | const activityObject = getAPId(activity.object) |
7a7724e6 | 14 | |
e587e0ec | 15 | return retryTransactionWrapper(processFollow, byActor, activityObject) |
7a7724e6 C |
16 | } |
17 | ||
18 | // --------------------------------------------------------------------------- | |
19 | ||
20 | export { | |
21 | processFollowActivity | |
22 | } | |
23 | ||
24 | // --------------------------------------------------------------------------- | |
25 | ||
90d4bb81 | 26 | async function processFollow (actor: ActorModel, targetActorURL: string) { |
f7cc67b4 | 27 | const { actorFollow, created } = await sequelizeTypescript.transaction(async t => { |
e587e0ec | 28 | const targetActor = await ActorModel.loadByUrlAndPopulateAccountAndChannel(targetActorURL, t) |
ce548a10 | 29 | |
50d6de9c C |
30 | if (!targetActor) throw new Error('Unknown actor') |
31 | if (targetActor.isOwned() === false) throw new Error('This is not a local actor.') | |
7a7724e6 | 32 | |
5b9c965d C |
33 | const serverActor = await getServerActor() |
34 | if (targetActor.id === serverActor.id && CONFIG.FOLLOWERS.INSTANCE.ENABLED === false) { | |
14893eb7 C |
35 | logger.info('Rejecting %s because instance followers are disabled.', targetActor.url) |
36 | ||
5b9c965d C |
37 | return sendReject(actor, targetActor) |
38 | } | |
39 | ||
f7cc67b4 | 40 | const [ actorFollow, created ] = await ActorFollowModel.findOrCreate({ |
350e31d6 | 41 | where: { |
50d6de9c C |
42 | actorId: actor.id, |
43 | targetActorId: targetActor.id | |
350e31d6 C |
44 | }, |
45 | defaults: { | |
50d6de9c C |
46 | actorId: actor.id, |
47 | targetActorId: targetActor.id, | |
14893eb7 | 48 | state: CONFIG.FOLLOWERS.INSTANCE.MANUAL_APPROVAL ? 'pending' : 'accepted' |
350e31d6 | 49 | }, |
ce548a10 | 50 | transaction: t |
350e31d6 | 51 | }) |
40ff5707 | 52 | |
32b2b43c C |
53 | actorFollow.ActorFollower = actor |
54 | actorFollow.ActorFollowing = targetActor | |
55 | ||
14893eb7 | 56 | if (actorFollow.state !== 'accepted' && CONFIG.FOLLOWERS.INSTANCE.MANUAL_APPROVAL === false) { |
50d6de9c C |
57 | actorFollow.state = 'accepted' |
58 | await actorFollow.save({ transaction: t }) | |
40ff5707 C |
59 | } |
60 | ||
50d6de9c C |
61 | actorFollow.ActorFollower = actor |
62 | actorFollow.ActorFollowing = targetActor | |
ce548a10 | 63 | |
50d6de9c | 64 | // Target sends to actor he accepted the follow request |
14893eb7 | 65 | if (actorFollow.state === 'accepted') await sendAccept(actorFollow) |
f7cc67b4 C |
66 | |
67 | return { actorFollow, created } | |
7a7724e6 | 68 | }) |
ce548a10 | 69 | |
f7cc67b4 C |
70 | if (created) Notifier.Instance.notifyOfNewFollow(actorFollow) |
71 | ||
85414add | 72 | logger.info('Actor %s is followed by actor %s.', targetActorURL, actor.url) |
7a7724e6 | 73 | } |