]>
Commit | Line | Data |
---|---|---|
7d9ba5c0 | 1 | import { getServerActor } from '@server/models/application/application' |
3fd3ab2d | 2 | import { ActivityFollow } from '../../../../shared/models/activitypub' |
7d9ba5c0 | 3 | import { getAPId } from '../../../helpers/activitypub' |
da854ddd C |
4 | import { retryTransactionWrapper } from '../../../helpers/database-utils' |
5 | import { logger } from '../../../helpers/logger' | |
6dd9de95 | 6 | import { CONFIG } from '../../../initializers/config' |
7d9ba5c0 C |
7 | import { sequelizeTypescript } from '../../../initializers/database' |
8 | import { ActorModel } from '../../../models/actor/actor' | |
9 | import { ActorFollowModel } from '../../../models/actor/actor-follow' | |
26d6bf65 C |
10 | import { APProcessorOptions } from '../../../types/activitypub-processor.model' |
11 | import { MActorFollowActors, MActorSignature } from '../../../types/models' | |
7d9ba5c0 | 12 | import { Notifier } from '../../notifier' |
8424c402 | 13 | import { autoFollowBackIfNeeded } from '../follow' |
7d9ba5c0 | 14 | import { sendAccept, sendReject } from '../send' |
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 | ||
eae0365b | 46 | 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 | 71 | actorFollow.state = 'accepted' |
4cbdcf44 C |
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 | |
50d6de9c | 79 | await actorFollow.save({ transaction: t }) |
40ff5707 C |
80 | } |
81 | ||
5224c394 | 82 | actorFollow.ActorFollower = byActor |
50d6de9c | 83 | actorFollow.ActorFollowing = targetActor |
ce548a10 | 84 | |
50d6de9c | 85 | // Target sends to actor he accepted the follow request |
8424c402 | 86 | if (actorFollow.state === 'accepted') { |
eae0365b C |
87 | sendAccept(actorFollow) |
88 | ||
89 | await autoFollowBackIfNeeded(actorFollow, t) | |
8424c402 | 90 | } |
f7cc67b4 | 91 | |
8424c402 | 92 | return { actorFollow, created, isFollowingInstance, targetActor } |
7a7724e6 | 93 | }) |
ce548a10 | 94 | |
883993c8 C |
95 | // Rejected |
96 | if (!actorFollow) return | |
97 | ||
98 | if (created) { | |
8424c402 C |
99 | const follower = await ActorModel.loadFull(byActor.id) |
100 | const actorFollowFull = Object.assign(actorFollow, { ActorFollowing: targetActor, ActorFollower: follower }) | |
101 | ||
453e83ea | 102 | if (isFollowingInstance) { |
8424c402 | 103 | Notifier.Instance.notifyOfNewInstanceFollow(actorFollowFull) |
453e83ea | 104 | } else { |
453e83ea C |
105 | Notifier.Instance.notifyOfNewUserFollow(actorFollowFull) |
106 | } | |
883993c8 | 107 | } |
f7cc67b4 | 108 | |
5224c394 | 109 | logger.info('Actor %s is followed by actor %s.', targetActorURL, byActor.url) |
7a7724e6 | 110 | } |