闭社主体 forked from https://github.com/tootsuite/mastodon
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.

60 lines
2.2 KiB

  1. # frozen_string_literal: true
  2. class ReblogService < BaseService
  3. include Authorization
  4. include StreamEntryRenderer
  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. reblog = account.statuses.create!(reblog: reblogged_status, text: '', visibility: options[:visibility] || account.user&.setting_default_privacy)
  16. DistributionWorker.perform_async(reblog.id)
  17. Pubsubhubbub::DistributionWorker.perform_async(reblog.stream_entry.id)
  18. ActivityPub::DistributionWorker.perform_async(reblog.id)
  19. create_notification(reblog)
  20. bump_potential_friendship(account, reblog)
  21. reblog
  22. end
  23. private
  24. def create_notification(reblog)
  25. reblogged_status = reblog.reblog
  26. if reblogged_status.account.local?
  27. LocalNotificationWorker.perform_async(reblogged_status.account_id, reblog.id, reblog.class.name)
  28. elsif reblogged_status.account.ostatus?
  29. NotificationWorker.perform_async(stream_entry_to_xml(reblog.stream_entry), reblog.account_id, reblogged_status.account_id)
  30. elsif reblogged_status.account.activitypub? && !reblogged_status.account.following?(reblog.account)
  31. ActivityPub::DeliveryWorker.perform_async(build_json(reblog), reblog.account_id, reblogged_status.account.inbox_url)
  32. end
  33. end
  34. def bump_potential_friendship(account, reblog)
  35. ActivityTracker.increment('activity:interactions')
  36. return if account.following?(reblog.reblog.account_id)
  37. PotentialFriendshipTracker.record(account.id, reblog.reblog.account_id, :reblog)
  38. end
  39. def build_json(reblog)
  40. Oj.dump(ActivityPub::LinkedDataSignature.new(ActiveModelSerializers::SerializableResource.new(
  41. reblog,
  42. serializer: ActivityPub::ActivitySerializer,
  43. adapter: ActivityPub::Adapter
  44. ).as_json).sign!(reblog.account))
  45. end
  46. end