]>
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, CONFIG } from '../../../initializers' | |
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 { getServerActor } from '../../../helpers/utils' | |
11 | ||
12 | async function processFollowActivity (activity: ActivityFollow, byActor: ActorModel) { | |
13 | const activityObject = getAPId(activity.object) | |
14 | ||
15 | return retryTransactionWrapper(processFollow, byActor, activityObject) | |
16 | } | |
17 | ||
18 | // --------------------------------------------------------------------------- | |
19 | ||
20 | export { | |
21 | processFollowActivity | |
22 | } | |
23 | ||
24 | // --------------------------------------------------------------------------- | |
25 | ||
26 | async function processFollow (actor: ActorModel, targetActorURL: string) { | |
27 | const { actorFollow, created } = await sequelizeTypescript.transaction(async t => { | |
28 | const targetActor = await ActorModel.loadByUrlAndPopulateAccountAndChannel(targetActorURL, t) | |
29 | ||
30 | if (!targetActor) throw new Error('Unknown actor') | |
31 | if (targetActor.isOwned() === false) throw new Error('This is not a local actor.') | |
32 | ||
33 | const serverActor = await getServerActor() | |
34 | if (targetActor.id === serverActor.id && CONFIG.FOLLOWERS.INSTANCE.ENABLED === false) { | |
35 | return sendReject(actor, targetActor) | |
36 | } | |
37 | ||
38 | const [ actorFollow, created ] = await ActorFollowModel.findOrCreate({ | |
39 | where: { | |
40 | actorId: actor.id, | |
41 | targetActorId: targetActor.id | |
42 | }, | |
43 | defaults: { | |
44 | actorId: actor.id, | |
45 | targetActorId: targetActor.id, | |
46 | state: 'accepted' | |
47 | }, | |
48 | transaction: t | |
49 | }) | |
50 | ||
51 | actorFollow.ActorFollower = actor | |
52 | actorFollow.ActorFollowing = targetActor | |
53 | ||
54 | if (actorFollow.state !== 'accepted') { | |
55 | actorFollow.state = 'accepted' | |
56 | await actorFollow.save({ transaction: t }) | |
57 | } | |
58 | ||
59 | actorFollow.ActorFollower = actor | |
60 | actorFollow.ActorFollowing = targetActor | |
61 | ||
62 | // Target sends to actor he accepted the follow request | |
63 | await sendAccept(actorFollow) | |
64 | ||
65 | return { actorFollow, created } | |
66 | }) | |
67 | ||
68 | if (created) Notifier.Instance.notifyOfNewFollow(actorFollow) | |
69 | ||
70 | logger.info('Actor %s is followed by actor %s.', targetActorURL, actor.url) | |
71 | } |