activitypub-follow.ts 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. import * as Bull from 'bull'
  2. import { logger } from '../../../helpers/logger'
  3. import { REMOTE_SCHEME, WEBSERVER } from '../../../initializers/constants'
  4. import { sendFollow } from '../../activitypub/send'
  5. import { sanitizeHost } from '../../../helpers/core-utils'
  6. import { loadActorUrlOrGetFromWebfinger } from '../../../helpers/webfinger'
  7. import { getOrCreateActorAndServerAndModel } from '../../activitypub/actor'
  8. import { retryTransactionWrapper } from '../../../helpers/database-utils'
  9. import { ActorFollowModel } from '../../../models/activitypub/actor-follow'
  10. import { ActorModel } from '../../../models/activitypub/actor'
  11. import { Notifier } from '../../notifier'
  12. import { sequelizeTypescript } from '../../../initializers/database'
  13. import { MActor, MActorFollowActors, MActorFull } from '../../../types/models'
  14. import { ActivitypubFollowPayload } from '@shared/models'
  15. import { getLocalActorFollowActivityPubUrl } from '@server/lib/activitypub/url'
  16. async function processActivityPubFollow (job: Bull.Job) {
  17. const payload = job.data as ActivitypubFollowPayload
  18. const host = payload.host
  19. logger.info('Processing ActivityPub follow in job %d.', job.id)
  20. let targetActor: MActorFull
  21. if (!host || host === WEBSERVER.HOST) {
  22. targetActor = await ActorModel.loadLocalByName(payload.name)
  23. } else {
  24. const sanitizedHost = sanitizeHost(host, REMOTE_SCHEME.HTTP)
  25. const actorUrl = await loadActorUrlOrGetFromWebfinger(payload.name + '@' + sanitizedHost)
  26. targetActor = await getOrCreateActorAndServerAndModel(actorUrl, 'all')
  27. }
  28. if (payload.assertIsChannel && !targetActor.VideoChannel) {
  29. logger.warn('Do not follow %s@%s because it is not a channel.', payload.name, host)
  30. return
  31. }
  32. const fromActor = await ActorModel.load(payload.followerActorId)
  33. return retryTransactionWrapper(follow, fromActor, targetActor, payload.isAutoFollow)
  34. }
  35. // ---------------------------------------------------------------------------
  36. export {
  37. processActivityPubFollow
  38. }
  39. // ---------------------------------------------------------------------------
  40. async function follow (fromActor: MActor, targetActor: MActorFull, isAutoFollow = false) {
  41. if (fromActor.id === targetActor.id) {
  42. throw new Error('Follower is the same as target actor.')
  43. }
  44. // Same server, direct accept
  45. const state = !fromActor.serverId && !targetActor.serverId ? 'accepted' : 'pending'
  46. const actorFollow = await sequelizeTypescript.transaction(async t => {
  47. const [ actorFollow ] = await ActorFollowModel.findOrCreate<MActorFollowActors>({
  48. where: {
  49. actorId: fromActor.id,
  50. targetActorId: targetActor.id
  51. },
  52. defaults: {
  53. state,
  54. url: getLocalActorFollowActivityPubUrl(fromActor, targetActor),
  55. actorId: fromActor.id,
  56. targetActorId: targetActor.id
  57. },
  58. transaction: t
  59. })
  60. actorFollow.ActorFollowing = targetActor
  61. actorFollow.ActorFollower = fromActor
  62. // Send a notification to remote server if our follow is not already accepted
  63. if (actorFollow.state !== 'accepted') sendFollow(actorFollow, t)
  64. return actorFollow
  65. })
  66. const followerFull = await ActorModel.loadFull(fromActor.id)
  67. const actorFollowFull = Object.assign(actorFollow, {
  68. ActorFollowing: targetActor,
  69. ActorFollower: followerFull
  70. })
  71. if (actorFollow.state === 'accepted') Notifier.Instance.notifyOfNewUserFollow(actorFollowFull)
  72. if (isAutoFollow === true) Notifier.Instance.notifyOfAutoInstanceFollowing(actorFollowFull)
  73. return actorFollow
  74. }