]>
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' | |
80fdaf06 | 4 | import { sequelizeTypescript } from '../../../initializers/database' |
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' |
6dd9de95 | 10 | import { CONFIG } from '../../../initializers/config' |
26d6bf65 C |
11 | import { APProcessorOptions } from '../../../types/activitypub-processor.model' |
12 | import { MActorFollowActors, MActorSignature } from '../../../types/models' | |
8424c402 | 13 | import { autoFollowBackIfNeeded } from '../follow' |
8dc8a34e | 14 | import { getServerActor } from '@server/models/application/application' |
7a7724e6 | 15 | |
1198edf4 C |
16 | async function processFollowActivity (options: APProcessorOptions<ActivityFollow>) { |
17 | const { activity, byActor } = options | |
7a7724e6 | 18 | |
de94ac86 C |
19 | const activityId = activity.id |
20 | const objectId = getAPId(activity.object) | |
21 | ||
22 | return retryTransactionWrapper(processFollow, byActor, activityId, objectId) | |
7a7724e6 C |
23 | } |
24 | ||
25 | // --------------------------------------------------------------------------- | |
26 | ||
27 | export { | |
28 | processFollowActivity | |
29 | } | |
30 | ||
31 | // --------------------------------------------------------------------------- | |
32 | ||
de94ac86 | 33 | async function processFollow (byActor: MActorSignature, activityId: string, targetActorURL: string) { |
8424c402 | 34 | const { actorFollow, created, isFollowingInstance, targetActor } = await sequelizeTypescript.transaction(async t => { |
e587e0ec | 35 | const targetActor = await ActorModel.loadByUrlAndPopulateAccountAndChannel(targetActorURL, t) |
ce548a10 | 36 | |
50d6de9c C |
37 | if (!targetActor) throw new Error('Unknown actor') |
38 | if (targetActor.isOwned() === false) throw new Error('This is not a local actor.') | |
7a7724e6 | 39 | |
5b9c965d | 40 | const serverActor = await getServerActor() |
883993c8 C |
41 | const isFollowingInstance = targetActor.id === serverActor.id |
42 | ||
43 | if (isFollowingInstance && CONFIG.FOLLOWERS.INSTANCE.ENABLED === false) { | |
14893eb7 C |
44 | logger.info('Rejecting %s because instance followers are disabled.', targetActor.url) |
45 | ||
de94ac86 | 46 | await sendReject(activityId, byActor, targetActor) |
1735c825 | 47 | |
453e83ea | 48 | return { actorFollow: undefined as MActorFollowActors } |
5b9c965d C |
49 | } |
50 | ||
453e83ea | 51 | const [ actorFollow, created ] = await ActorFollowModel.findOrCreate<MActorFollowActors>({ |
350e31d6 | 52 | where: { |
5224c394 | 53 | actorId: byActor.id, |
50d6de9c | 54 | targetActorId: targetActor.id |
350e31d6 C |
55 | }, |
56 | defaults: { | |
5224c394 | 57 | actorId: byActor.id, |
50d6de9c | 58 | targetActorId: targetActor.id, |
de94ac86 C |
59 | url: activityId, |
60 | ||
61 | state: CONFIG.FOLLOWERS.INSTANCE.MANUAL_APPROVAL | |
62 | ? 'pending' | |
63 | : 'accepted' | |
350e31d6 | 64 | }, |
ce548a10 | 65 | transaction: t |
453e83ea | 66 | }) |
40ff5707 | 67 | |
2db48acc C |
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)) { | |
50d6de9c C |
71 | actorFollow.state = 'accepted' |
72 | await actorFollow.save({ transaction: t }) | |
40ff5707 C |
73 | } |
74 | ||
5224c394 | 75 | actorFollow.ActorFollower = byActor |
50d6de9c | 76 | actorFollow.ActorFollowing = targetActor |
ce548a10 | 77 | |
50d6de9c | 78 | // Target sends to actor he accepted the follow request |
8424c402 C |
79 | if (actorFollow.state === 'accepted') { |
80 | await sendAccept(actorFollow) | |
81 | await autoFollowBackIfNeeded(actorFollow) | |
82 | } | |
f7cc67b4 | 83 | |
8424c402 | 84 | return { actorFollow, created, isFollowingInstance, targetActor } |
7a7724e6 | 85 | }) |
ce548a10 | 86 | |
883993c8 C |
87 | // Rejected |
88 | if (!actorFollow) return | |
89 | ||
90 | if (created) { | |
8424c402 C |
91 | const follower = await ActorModel.loadFull(byActor.id) |
92 | const actorFollowFull = Object.assign(actorFollow, { ActorFollowing: targetActor, ActorFollower: follower }) | |
93 | ||
453e83ea | 94 | if (isFollowingInstance) { |
8424c402 | 95 | Notifier.Instance.notifyOfNewInstanceFollow(actorFollowFull) |
453e83ea | 96 | } else { |
453e83ea C |
97 | Notifier.Instance.notifyOfNewUserFollow(actorFollowFull) |
98 | } | |
883993c8 | 99 | } |
f7cc67b4 | 100 | |
5224c394 | 101 | logger.info('Actor %s is followed by actor %s.', targetActorURL, byActor.url) |
7a7724e6 | 102 | } |