]>
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' | |
3fd3ab2d | 4 | import { sequelizeTypescript } from '../../../initializers' |
50d6de9c C |
5 | import { ActorModel } from '../../../models/activitypub/actor' |
6 | import { ActorFollowModel } from '../../../models/activitypub/actor-follow' | |
3fd3ab2d | 7 | import { sendAccept } from '../send' |
7a7724e6 | 8 | |
e587e0ec | 9 | async function processFollowActivity (activity: ActivityFollow, byActor: ActorModel) { |
7a7724e6 | 10 | const activityObject = activity.object |
7a7724e6 | 11 | |
e587e0ec | 12 | return retryTransactionWrapper(processFollow, byActor, activityObject) |
7a7724e6 C |
13 | } |
14 | ||
15 | // --------------------------------------------------------------------------- | |
16 | ||
17 | export { | |
18 | processFollowActivity | |
19 | } | |
20 | ||
21 | // --------------------------------------------------------------------------- | |
22 | ||
90d4bb81 | 23 | async function processFollow (actor: ActorModel, targetActorURL: string) { |
3fd3ab2d | 24 | await sequelizeTypescript.transaction(async t => { |
e587e0ec | 25 | const targetActor = await ActorModel.loadByUrlAndPopulateAccountAndChannel(targetActorURL, t) |
ce548a10 | 26 | |
50d6de9c C |
27 | if (!targetActor) throw new Error('Unknown actor') |
28 | if (targetActor.isOwned() === false) throw new Error('This is not a local actor.') | |
7a7724e6 | 29 | |
50d6de9c | 30 | const [ actorFollow ] = await ActorFollowModel.findOrCreate({ |
350e31d6 | 31 | where: { |
50d6de9c C |
32 | actorId: actor.id, |
33 | targetActorId: targetActor.id | |
350e31d6 C |
34 | }, |
35 | defaults: { | |
50d6de9c C |
36 | actorId: actor.id, |
37 | targetActorId: targetActor.id, | |
350e31d6 C |
38 | state: 'accepted' |
39 | }, | |
ce548a10 | 40 | transaction: t |
350e31d6 | 41 | }) |
40ff5707 | 42 | |
32b2b43c C |
43 | actorFollow.ActorFollower = actor |
44 | actorFollow.ActorFollowing = targetActor | |
45 | ||
50d6de9c C |
46 | if (actorFollow.state !== 'accepted') { |
47 | actorFollow.state = 'accepted' | |
48 | await actorFollow.save({ transaction: t }) | |
40ff5707 C |
49 | } |
50 | ||
50d6de9c C |
51 | actorFollow.ActorFollower = actor |
52 | actorFollow.ActorFollowing = targetActor | |
ce548a10 | 53 | |
50d6de9c | 54 | // Target sends to actor he accepted the follow request |
94a5ff8a | 55 | return sendAccept(actorFollow) |
7a7724e6 | 56 | }) |
ce548a10 | 57 | |
85414add | 58 | logger.info('Actor %s is followed by actor %s.', targetActorURL, actor.url) |
7a7724e6 | 59 | } |