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