]>
Commit | Line | Data |
---|---|---|
1 | import { ActivityFollow } from '../../../../shared/models/activitypub' | |
2 | import { retryTransactionWrapper } from '../../../helpers/database-utils' | |
3 | import { logger } from '../../../helpers/logger' | |
4 | import { sequelizeTypescript } from '../../../initializers/database' | |
5 | import { ActorModel } from '../../../models/activitypub/actor' | |
6 | import { ActorFollowModel } from '../../../models/activitypub/actor-follow' | |
7 | import { sendAccept, sendReject } from '../send' | |
8 | import { Notifier } from '../../notifier' | |
9 | import { getAPId } from '../../../helpers/activitypub' | |
10 | import { CONFIG } from '../../../initializers/config' | |
11 | import { APProcessorOptions } from '../../../typings/activitypub-processor.model' | |
12 | import { MActorFollowActors, MActorSignature } from '../../../typings/models' | |
13 | import { autoFollowBackIfNeeded } from '../follow' | |
14 | import { getServerActor } from '@server/models/application/application' | |
15 | ||
16 | async function processFollowActivity (options: APProcessorOptions<ActivityFollow>) { | |
17 | const { activity, byActor } = options | |
18 | const activityObject = getAPId(activity.object) | |
19 | ||
20 | return retryTransactionWrapper(processFollow, byActor, activityObject) | |
21 | } | |
22 | ||
23 | // --------------------------------------------------------------------------- | |
24 | ||
25 | export { | |
26 | processFollowActivity | |
27 | } | |
28 | ||
29 | // --------------------------------------------------------------------------- | |
30 | ||
31 | async function processFollow (byActor: MActorSignature, targetActorURL: string) { | |
32 | const { actorFollow, created, isFollowingInstance, targetActor } = await sequelizeTypescript.transaction(async t => { | |
33 | const targetActor = await ActorModel.loadByUrlAndPopulateAccountAndChannel(targetActorURL, t) | |
34 | ||
35 | if (!targetActor) throw new Error('Unknown actor') | |
36 | if (targetActor.isOwned() === false) throw new Error('This is not a local actor.') | |
37 | ||
38 | const serverActor = await getServerActor() | |
39 | const isFollowingInstance = targetActor.id === serverActor.id | |
40 | ||
41 | if (isFollowingInstance && CONFIG.FOLLOWERS.INSTANCE.ENABLED === false) { | |
42 | logger.info('Rejecting %s because instance followers are disabled.', targetActor.url) | |
43 | ||
44 | await sendReject(byActor, targetActor) | |
45 | ||
46 | return { actorFollow: undefined as MActorFollowActors } | |
47 | } | |
48 | ||
49 | const [ actorFollow, created ] = await ActorFollowModel.findOrCreate<MActorFollowActors>({ | |
50 | where: { | |
51 | actorId: byActor.id, | |
52 | targetActorId: targetActor.id | |
53 | }, | |
54 | defaults: { | |
55 | actorId: byActor.id, | |
56 | targetActorId: targetActor.id, | |
57 | state: CONFIG.FOLLOWERS.INSTANCE.MANUAL_APPROVAL ? 'pending' : 'accepted' | |
58 | }, | |
59 | transaction: t | |
60 | }) | |
61 | ||
62 | // Set the follow as accepted if the remote actor follows a channel or account | |
63 | // Or if the instance automatically accepts followers | |
64 | if (actorFollow.state !== 'accepted' && (isFollowingInstance === false || CONFIG.FOLLOWERS.INSTANCE.MANUAL_APPROVAL === false)) { | |
65 | actorFollow.state = 'accepted' | |
66 | await actorFollow.save({ transaction: t }) | |
67 | } | |
68 | ||
69 | actorFollow.ActorFollower = byActor | |
70 | actorFollow.ActorFollowing = targetActor | |
71 | ||
72 | // Target sends to actor he accepted the follow request | |
73 | if (actorFollow.state === 'accepted') { | |
74 | await sendAccept(actorFollow) | |
75 | await autoFollowBackIfNeeded(actorFollow) | |
76 | } | |
77 | ||
78 | return { actorFollow, created, isFollowingInstance, targetActor } | |
79 | }) | |
80 | ||
81 | // Rejected | |
82 | if (!actorFollow) return | |
83 | ||
84 | if (created) { | |
85 | const follower = await ActorModel.loadFull(byActor.id) | |
86 | const actorFollowFull = Object.assign(actorFollow, { ActorFollowing: targetActor, ActorFollower: follower }) | |
87 | ||
88 | if (isFollowingInstance) { | |
89 | Notifier.Instance.notifyOfNewInstanceFollow(actorFollowFull) | |
90 | } else { | |
91 | Notifier.Instance.notifyOfNewUserFollow(actorFollowFull) | |
92 | } | |
93 | } | |
94 | ||
95 | logger.info('Actor %s is followed by actor %s.', targetActorURL, byActor.url) | |
96 | } |