You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

62 lines
2.2 KiB

  1. # frozen_string_literal: true
  2. class ReblogService < BaseService
  3. include Authorization
  4. include StreamEntryRenderer
  5. include Payloadable
  6. # Reblog a status and notify its remote author
  7. # @param [Account] account Account to reblog from
  8. # @param [Status] reblogged_status Status to be reblogged
  9. # @param [Hash] options
  10. # @return [Status]
  11. def call(account, reblogged_status, options = {})
  12. reblogged_status = reblogged_status.reblog if reblogged_status.reblog?
  13. authorize_with account, reblogged_status, :reblog?
  14. reblog = account.statuses.find_by(reblog: reblogged_status)
  15. return reblog unless reblog.nil?
  16. visibility = options[:visibility] || account.user&.setting_default_privacy
  17. visibility = reblogged_status.visibility if reblogged_status.hidden?
  18. reblog = account.statuses.create!(reblog: reblogged_status, text: '', visibility: visibility)
  19. DistributionWorker.perform_async(reblog.id)
  20. unless reblogged_status.local_only?
  21. Pubsubhubbub::DistributionWorker.perform_async(reblog.stream_entry.id)
  22. ActivityPub::DistributionWorker.perform_async(reblog.id)
  23. end
  24. create_notification(reblog)
  25. bump_potential_friendship(account, reblog)
  26. reblog
  27. end
  28. private
  29. def create_notification(reblog)
  30. reblogged_status = reblog.reblog
  31. if reblogged_status.account.local?
  32. LocalNotificationWorker.perform_async(reblogged_status.account_id, reblog.id, reblog.class.name)
  33. elsif reblogged_status.account.ostatus?
  34. NotificationWorker.perform_async(stream_entry_to_xml(reblog.stream_entry), reblog.account_id, reblogged_status.account_id)
  35. elsif reblogged_status.account.activitypub? && !reblogged_status.account.following?(reblog.account)
  36. ActivityPub::DeliveryWorker.perform_async(build_json(reblog), reblog.account_id, reblogged_status.account.inbox_url)
  37. end
  38. end
  39. def bump_potential_friendship(account, reblog)
  40. ActivityTracker.increment('activity:interactions')
  41. return if account.following?(reblog.reblog.account_id)
  42. PotentialFriendshipTracker.record(account.id, reblog.reblog.account_id, :reblog)
  43. end
  44. def build_json(reblog)
  45. Oj.dump(serialize_payload(reblog, ActivityPub::ActivitySerializer, signer: reblog.account))
  46. end
  47. end