permalink_redirector.rb 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. # frozen_string_literal: true
  2. class PermalinkRedirector
  3. include RoutingHelper
  4. def initialize(path)
  5. @path = path
  6. end
  7. def redirect_path
  8. if path_segments[0].present? && path_segments[0].start_with?('@') && path_segments[1] =~ /\d/
  9. find_status_url_by_id(path_segments[1])
  10. elsif path_segments[0].present? && path_segments[0].start_with?('@')
  11. find_account_url_by_name(path_segments[0])
  12. elsif path_segments[0] == 'statuses' && path_segments[1] =~ /\d/
  13. find_status_url_by_id(path_segments[1])
  14. elsif path_segments[0] == 'accounts' && path_segments[1] =~ /\d/
  15. find_account_url_by_id(path_segments[1])
  16. end
  17. end
  18. private
  19. def path_segments
  20. @path_segments ||= @path.gsub(/\A\//, '').split('/')
  21. end
  22. def find_status_url_by_id(id)
  23. status = Status.find_by(id: id)
  24. ActivityPub::TagManager.instance.url_for(status) if status&.distributable? && !status.account.local?
  25. end
  26. def find_account_url_by_id(id)
  27. account = Account.find_by(id: id)
  28. ActivityPub::TagManager.instance.url_for(account) if account.present? && !account.local?
  29. end
  30. def find_account_url_by_name(name)
  31. username, domain = name.gsub(/\A@/, '').split('@')
  32. domain = nil if TagManager.instance.local_domain?(domain)
  33. account = Account.find_remote(username, domain)
  34. ActivityPub::TagManager.instance.url_for(account) if account.present? && !account.local?
  35. end
  36. end