unoffical wafrn mirror
wafrn.net
atproto
social-network
activitypub
1import { Follows, ServerBlock, User, UserOptions } from '../../../models/index.js'
2import { activityPubObject } from '../../../interfaces/fediverse/activityPubObject.js'
3import { createNotification } from '../../pushNotifications.js'
4import { acceptRemoteFollow } from '../acceptRemoteFollow.js'
5import { getRemoteActor } from '../getRemoteActor.js'
6import { } from '../signAndAccept.js'
7import { rejectremoteFollow } from '../rejectRemoteFollow.js'
8import { } from '../../logger.js'
9
10async function FollowActivity(body: activityPubObject, remoteUser: User, user: User) {
11 const apObject: activityPubObject = body
12 // Follow user
13 const userToBeFollowed = await getRemoteActor(apObject.object, user)
14 if (userToBeFollowed) {
15 const dbOptionAutoAcceptFollowsFromFollowing = await UserOptions.findOne({
16 where: {
17 userId: userToBeFollowed.id,
18 optionName: 'wafrn.autoAcceptFollowsFromFollowing'
19 }
20 })
21 const dbOptionAutoRejectFollowsFromUsersYouDoNotFollow = await UserOptions.findOne({
22 where: {
23 userId: userToBeFollowed.id,
24 optionName: 'wafrn.autoRejectFollowsFromUsersYouDoNotFollow'
25 }
26 })
27 let autoAcceptFollow = !user.manuallyAcceptsFollows
28
29 const userIsFollowingNewFollower = await Follows.findOne({
30 where: {
31 followerId: userToBeFollowed.id,
32 followedId: remoteUser.id
33 }
34 })
35
36 if (dbOptionAutoAcceptFollowsFromFollowing?.optionValue === 'true' && userIsFollowingNewFollower) {
37 autoAcceptFollow = true;
38 }
39
40 const blockExisting = await ServerBlock.findAll({
41 where: {
42 userBlockerId: userToBeFollowed.id,
43 blockedServerId: remoteUser.federatedHostId || '00000000-0000-0000-0000-000000000000'
44 }
45 })
46
47 if (
48 (!autoAcceptFollow &&
49 dbOptionAutoAcceptFollowsFromFollowing?.optionValue === 'true' &&
50 dbOptionAutoRejectFollowsFromUsersYouDoNotFollow?.optionValue === 'true') || blockExisting.length > 0
51 ) {
52 await rejectremoteFollow(userToBeFollowed.id, remoteUser.id)
53 return
54 }
55
56 let [remoteFollow, created] = await Follows.findOrCreate({
57 where: {
58 followerId: remoteUser.id,
59 followedId: userToBeFollowed.id
60 },
61 defaults: {
62 followerId: remoteUser.id,
63 followedId: userToBeFollowed.id,
64 remoteFollowId: apObject.id,
65 accepted: userToBeFollowed.isRemoteUser ? true : autoAcceptFollow,
66 muteQuotes: false,
67 muteRewoots: false
68 }
69 })
70 remoteFollow.remoteFollowId = apObject.id
71 await remoteFollow.save()
72 // we accept it if user accepts follows automaticaly
73 if (remoteFollow.accepted || autoAcceptFollow) {
74 if (created) {
75 createNotification(
76 {
77 notificationType: 'FOLLOW',
78 userId: remoteUser.id,
79 notifiedUserId: userToBeFollowed.id,
80 detached: false
81 },
82 {
83 userUrl: remoteUser.url
84 }
85 )
86 }
87 await acceptRemoteFollow(userToBeFollowed.id, remoteUser.id)
88 }
89 }
90}
91
92export { FollowActivity }