reblog_service.rb 1.9 KB

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