permalink_redirector.rb 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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] == 'web'
  9. if path_segments[1].present? && path_segments[1].start_with?('@') && path_segments[2] =~ /\d/
  10. find_status_url_by_id(path_segments[2])
  11. elsif path_segments[1].present? && path_segments[1].start_with?('@')
  12. find_account_url_by_name(path_segments[1])
  13. elsif path_segments[1] == 'statuses' && path_segments[2] =~ /\d/
  14. find_status_url_by_id(path_segments[2])
  15. elsif path_segments[1] == 'accounts' && path_segments[2] =~ /\d/
  16. find_account_url_by_id(path_segments[2])
  17. elsif path_segments[1] == 'timelines' && path_segments[2] == 'tag' && path_segments[3].present?
  18. find_tag_url_by_name(path_segments[3])
  19. elsif path_segments[1] == 'tags' && path_segments[2].present?
  20. find_tag_url_by_name(path_segments[2])
  21. end
  22. end
  23. end
  24. private
  25. def path_segments
  26. @path_segments ||= @path.gsub(/\A\//, '').split('/')
  27. end
  28. def find_status_url_by_id(id)
  29. status = Status.find_by(id: id)
  30. return unless status&.distributable?
  31. ActivityPub::TagManager.instance.url_for(status)
  32. end
  33. def find_account_url_by_id(id)
  34. account = Account.find_by(id: id)
  35. return unless account
  36. ActivityPub::TagManager.instance.url_for(account)
  37. end
  38. def find_account_url_by_name(name)
  39. username, domain = name.gsub(/\A@/, '').split('@')
  40. domain = nil if TagManager.instance.local_domain?(domain)
  41. account = Account.find_remote(username, domain)
  42. return unless account
  43. ActivityPub::TagManager.instance.url_for(account)
  44. end
  45. def find_tag_url_by_name(name)
  46. tag_path(CGI.unescape(name))
  47. end
  48. end