aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/lib/activitypub
diff options
context:
space:
mode:
Diffstat (limited to 'server/lib/activitypub')
-rw-r--r--server/lib/activitypub/follow.ts36
-rw-r--r--server/lib/activitypub/process/process-accept.ts2
-rw-r--r--server/lib/activitypub/process/process-follow.ts20
-rw-r--r--server/lib/activitypub/send/send-follow.ts1
4 files changed, 49 insertions, 10 deletions
diff --git a/server/lib/activitypub/follow.ts b/server/lib/activitypub/follow.ts
new file mode 100644
index 000000000..c57e43c91
--- /dev/null
+++ b/server/lib/activitypub/follow.ts
@@ -0,0 +1,36 @@
1import { MActorFollowActors } from '../../typings/models'
2import { CONFIG } from '../../initializers/config'
3import { SERVER_ACTOR_NAME } from '../../initializers/constants'
4import { JobQueue } from '../job-queue'
5import { logger } from '../../helpers/logger'
6import { getServerActor } from '../../helpers/utils'
7import { ServerModel } from '@server/models/server/server'
8
9async function autoFollowBackIfNeeded (actorFollow: MActorFollowActors) {
10 if (!CONFIG.FOLLOWINGS.INSTANCE.AUTO_FOLLOW_BACK.ENABLED) return
11
12 const follower = actorFollow.ActorFollower
13
14 if (follower.type === 'Application' && follower.preferredUsername === SERVER_ACTOR_NAME) {
15 logger.info('Auto follow back %s.', follower.url)
16
17 const me = await getServerActor()
18
19 const server = await ServerModel.load(follower.serverId)
20 const host = server.host
21
22 const payload = {
23 host,
24 name: SERVER_ACTOR_NAME,
25 followerActorId: me.id,
26 isAutoFollow: true
27 }
28
29 JobQueue.Instance.createJob({ type: 'activitypub-follow', payload })
30 .catch(err => logger.error('Cannot create auto follow back job for %s.', host, err))
31 }
32}
33
34export {
35 autoFollowBackIfNeeded
36}
diff --git a/server/lib/activitypub/process/process-accept.ts b/server/lib/activitypub/process/process-accept.ts
index 86f7c764d..dcfbb2c84 100644
--- a/server/lib/activitypub/process/process-accept.ts
+++ b/server/lib/activitypub/process/process-accept.ts
@@ -24,7 +24,7 @@ async function processAccept (actor: MActorDefault, targetActor: MActorSignature
24 if (!follow) throw new Error('Cannot find associated follow.') 24 if (!follow) throw new Error('Cannot find associated follow.')
25 25
26 if (follow.state !== 'accepted') { 26 if (follow.state !== 'accepted') {
27 follow.set('state', 'accepted') 27 follow.state = 'accepted'
28 await follow.save() 28 await follow.save()
29 29
30 await addFetchOutboxJob(targetActor) 30 await addFetchOutboxJob(targetActor)
diff --git a/server/lib/activitypub/process/process-follow.ts b/server/lib/activitypub/process/process-follow.ts
index bc5660395..85f22d654 100644
--- a/server/lib/activitypub/process/process-follow.ts
+++ b/server/lib/activitypub/process/process-follow.ts
@@ -10,7 +10,8 @@ import { getAPId } from '../../../helpers/activitypub'
10import { getServerActor } from '../../../helpers/utils' 10import { getServerActor } from '../../../helpers/utils'
11import { CONFIG } from '../../../initializers/config' 11import { CONFIG } from '../../../initializers/config'
12import { APProcessorOptions } from '../../../typings/activitypub-processor.model' 12import { APProcessorOptions } from '../../../typings/activitypub-processor.model'
13import { MAccount, MActorFollowActors, MActorFollowFull, MActorSignature } from '../../../typings/models' 13import { MActorFollowActors, MActorSignature } from '../../../typings/models'
14import { autoFollowBackIfNeeded } from '../follow'
14 15
15async function processFollowActivity (options: APProcessorOptions<ActivityFollow>) { 16async function processFollowActivity (options: APProcessorOptions<ActivityFollow>) {
16 const { activity, byActor } = options 17 const { activity, byActor } = options
@@ -28,7 +29,7 @@ export {
28// --------------------------------------------------------------------------- 29// ---------------------------------------------------------------------------
29 30
30async function processFollow (byActor: MActorSignature, targetActorURL: string) { 31async function processFollow (byActor: MActorSignature, targetActorURL: string) {
31 const { actorFollow, created, isFollowingInstance } = await sequelizeTypescript.transaction(async t => { 32 const { actorFollow, created, isFollowingInstance, targetActor } = await sequelizeTypescript.transaction(async t => {
32 const targetActor = await ActorModel.loadByUrlAndPopulateAccountAndChannel(targetActorURL, t) 33 const targetActor = await ActorModel.loadByUrlAndPopulateAccountAndChannel(targetActorURL, t)
33 34
34 if (!targetActor) throw new Error('Unknown actor') 35 if (!targetActor) throw new Error('Unknown actor')
@@ -67,21 +68,24 @@ async function processFollow (byActor: MActorSignature, targetActorURL: string)
67 actorFollow.ActorFollowing = targetActor 68 actorFollow.ActorFollowing = targetActor
68 69
69 // Target sends to actor he accepted the follow request 70 // Target sends to actor he accepted the follow request
70 if (actorFollow.state === 'accepted') await sendAccept(actorFollow) 71 if (actorFollow.state === 'accepted') {
72 await sendAccept(actorFollow)
73 await autoFollowBackIfNeeded(actorFollow)
74 }
71 75
72 return { actorFollow, created, isFollowingInstance } 76 return { actorFollow, created, isFollowingInstance, targetActor }
73 }) 77 })
74 78
75 // Rejected 79 // Rejected
76 if (!actorFollow) return 80 if (!actorFollow) return
77 81
78 if (created) { 82 if (created) {
83 const follower = await ActorModel.loadFull(byActor.id)
84 const actorFollowFull = Object.assign(actorFollow, { ActorFollowing: targetActor, ActorFollower: follower })
85
79 if (isFollowingInstance) { 86 if (isFollowingInstance) {
80 Notifier.Instance.notifyOfNewInstanceFollow(actorFollow) 87 Notifier.Instance.notifyOfNewInstanceFollow(actorFollowFull)
81 } else { 88 } else {
82 const actorFollowFull = actorFollow as MActorFollowFull
83 actorFollowFull.ActorFollower.Account = await actorFollow.ActorFollower.$get('Account') as MAccount
84
85 Notifier.Instance.notifyOfNewUserFollow(actorFollowFull) 89 Notifier.Instance.notifyOfNewUserFollow(actorFollowFull)
86 } 90 }
87 } 91 }
diff --git a/server/lib/activitypub/send/send-follow.ts b/server/lib/activitypub/send/send-follow.ts
index 6b17b25da..ce400d8ff 100644
--- a/server/lib/activitypub/send/send-follow.ts
+++ b/server/lib/activitypub/send/send-follow.ts
@@ -1,5 +1,4 @@
1import { ActivityFollow } from '../../../../shared/models/activitypub' 1import { ActivityFollow } from '../../../../shared/models/activitypub'
2import { ActorFollowModel } from '../../../models/activitypub/actor-follow'
3import { getActorFollowActivityPubUrl } from '../url' 2import { getActorFollowActivityPubUrl } from '../url'
4import { unicastTo } from './utils' 3import { unicastTo } from './utils'
5import { logger } from '../../../helpers/logger' 4import { logger } from '../../../helpers/logger'