闭社主体 forked from https://github.com/tootsuite/mastodon
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.

72 lines
1.9 KiB

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