unoffical wafrn mirror
wafrn.net
atproto
social-network
activitypub
1import { Follows, Notification, User } 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 { signAndAccept } from '../signAndAccept.js'
7
8async function FollowActivity(body: activityPubObject, remoteUser: User, user: User) {
9 const apObject: activityPubObject = body
10 // Follow user
11 const userToBeFollowed = await getRemoteActor(apObject.object, user)
12 if (userToBeFollowed) {
13 let [remoteFollow, created] = await Follows.findOrCreate({
14 where: {
15 followerId: remoteUser.id,
16 followedId: userToBeFollowed.id
17 },
18 defaults: {
19 followerId: remoteUser.id,
20 followedId: userToBeFollowed.id,
21 remoteFollowId: apObject.id,
22 accepted: userToBeFollowed.isRemoteUser ? true : !userToBeFollowed.manuallyAcceptsFollows,
23 muteQuotes: false,
24 muteRewoots: false
25 }
26 })
27 remoteFollow.remoteFollowId = apObject.id
28 await remoteFollow.save()
29 // we accept it if user accepts follows automaticaly
30 if (remoteFollow.accepted) {
31 if (created) {
32 createNotification(
33 {
34 notificationType: 'FOLLOW',
35 userId: remoteUser.id,
36 notifiedUserId: userToBeFollowed.id
37 },
38 {
39 userUrl: remoteUser.url
40 }
41 )
42 }
43 await acceptRemoteFollow(userToBeFollowed.id, remoteUser.id)
44 }
45 }
46}
47
48export { FollowActivity }