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.

71 lines
2.0 KiB

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