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