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.

76 lines
2.3 KiB

  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. # @option [String] :visibility
  10. # @option [Boolean] :with_rate_limit
  11. # @return [Status]
  12. def call(account, reblogged_status, options = {})
  13. reblogged_status = reblogged_status.reblog if reblogged_status.reblog?
  14. authorize_with account, reblogged_status, :reblog?
  15. reblog = account.statuses.find_by(reblog: reblogged_status)
  16. return reblog unless reblog.nil?
  17. visibility = begin
  18. if reblogged_status.hidden?
  19. reblogged_status.visibility
  20. else
  21. options[:visibility] || account.user&.setting_default_privacy
  22. end
  23. end
  24. reblog = account.statuses.create!(reblog: reblogged_status, text: '', visibility: visibility, rate_limit: options[:with_rate_limit])
  25. DistributionWorker.perform_async(reblog.id)
  26. ActivityPub::DistributionWorker.perform_async(reblog.id)
  27. create_notification(reblog)
  28. bump_potential_friendship(account, reblog)
  29. record_use(account, reblog)
  30. reblog
  31. end
  32. private
  33. def create_notification(reblog)
  34. reblogged_status = reblog.reblog
  35. if reblogged_status.account.local?
  36. LocalNotificationWorker.perform_async(reblogged_status.account_id, reblog.id, reblog.class.name, :reblog)
  37. elsif reblogged_status.account.activitypub? && !reblogged_status.account.following?(reblog.account)
  38. ActivityPub::DeliveryWorker.perform_async(build_json(reblog), reblog.account_id, reblogged_status.account.inbox_url)
  39. end
  40. end
  41. def bump_potential_friendship(account, reblog)
  42. ActivityTracker.increment('activity:interactions')
  43. return if account.following?(reblog.reblog.account_id)
  44. PotentialFriendshipTracker.record(account.id, reblog.reblog.account_id, :reblog)
  45. end
  46. def record_use(account, reblog)
  47. return unless reblog.public_visibility?
  48. original_status = reblog.reblog
  49. original_status.tags.each do |tag|
  50. tag.use!(account)
  51. end
  52. end
  53. def build_json(reblog)
  54. Oj.dump(serialize_payload(ActivityPub::ActivityPresenter.from_status(reblog), ActivityPub::ActivitySerializer, signer: reblog.account))
  55. end
  56. end