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.

73 lines
2.0 KiB

  1. # frozen_string_literal: true
  2. class OStatus::Activity::Base
  3. def initialize(xml, account = nil, **options)
  4. @xml = xml
  5. @account = account
  6. @options = options
  7. end
  8. def status?
  9. [:activity, :note, :comment].include?(type)
  10. end
  11. def verb
  12. raw = @xml.at_xpath('./activity:verb', activity: OStatus::TagManager::AS_XMLNS).content
  13. OStatus::TagManager::VERBS.key(raw)
  14. rescue
  15. :post
  16. end
  17. def type
  18. raw = @xml.at_xpath('./activity:object-type', activity: OStatus::TagManager::AS_XMLNS).content
  19. OStatus::TagManager::TYPES.key(raw)
  20. rescue
  21. :activity
  22. end
  23. def id
  24. @xml.at_xpath('./xmlns:id', xmlns: OStatus::TagManager::XMLNS).content
  25. end
  26. def url
  27. link = @xml.xpath('./xmlns:link[@rel="alternate"]', xmlns: OStatus::TagManager::XMLNS).find { |link_candidate| link_candidate['type'] == 'text/html' }
  28. link.nil? ? nil : link['href']
  29. end
  30. def activitypub_uri
  31. link = @xml.xpath('./xmlns:link[@rel="alternate"]', xmlns: OStatus::TagManager::XMLNS).find { |link_candidate| ['application/activity+json', 'application/ld+json; profile="https://www.w3.org/ns/activitystreams"'].include?(link_candidate['type']) }
  32. link.nil? ? nil : link['href']
  33. end
  34. def activitypub_uri?
  35. activitypub_uri.present?
  36. end
  37. private
  38. def find_status(uri)
  39. if OStatus::TagManager.instance.local_id?(uri)
  40. local_id = OStatus::TagManager.instance.unique_tag_to_local_id(uri, 'Status')
  41. return Status.find_by(id: local_id)
  42. elsif ActivityPub::TagManager.instance.local_uri?(uri)
  43. local_id = ActivityPub::TagManager.instance.uri_to_local_id(uri)
  44. return Status.find_by(id: local_id)
  45. end
  46. Status.find_by(uri: uri)
  47. end
  48. def find_activitypub_status(uri, href)
  49. tag_matches = /tag:([^,:]+)[^:]*:objectId=([\d]+)/.match(uri)
  50. href_matches = %r{/users/([^/]+)}.match(href)
  51. unless tag_matches.nil? || href_matches.nil?
  52. uri = "https://#{tag_matches[1]}/users/#{href_matches[1]}/statuses/#{tag_matches[2]}"
  53. Status.find_by(uri: uri)
  54. end
  55. end
  56. def redis
  57. Redis.current
  58. end
  59. end