permalink_redirector.rb 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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. end
  18. end
  19. end
  20. private
  21. def path_segments
  22. @path_segments ||= @path.gsub(/\A\//, '').split('/')
  23. end
  24. def find_status_url_by_id(id)
  25. status = Status.find_by(id: id)
  26. return unless status&.distributable?
  27. ActivityPub::TagManager.instance.url_for(status)
  28. end
  29. def find_account_url_by_id(id)
  30. account = Account.find_by(id: id)
  31. return unless account
  32. ActivityPub::TagManager.instance.url_for(account)
  33. end
  34. def find_account_url_by_name(name)
  35. username, domain = name.gsub(/\A@/, '').split('@')
  36. domain = nil if TagManager.instance.local_domain?(domain)
  37. account = Account.find_remote(username, domain)
  38. return unless account
  39. ActivityPub::TagManager.instance.url_for(account)
  40. end
  41. def find_tag_url_by_name(name)
  42. tag_path(CGI.unescape(name))
  43. end
  44. end