about summary refs log tree commit diff
path: root/app/lib/ostatus/activity/base.rb
blob: 1dc7abee3f39b853b5f5abbaaff26150c6639b5e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# frozen_string_literal: true

class OStatus::Activity::Base
  def initialize(xml, account = nil)
    @xml = xml
    @account = account
  end

  def status?
    [:activity, :note, :comment].include?(type)
  end

  def verb
    raw = @xml.at_xpath('./activity:verb', activity: TagManager::AS_XMLNS).content
    TagManager::VERBS.key(raw)
  rescue
    :post
  end

  def type
    raw = @xml.at_xpath('./activity:object-type', activity: TagManager::AS_XMLNS).content
    TagManager::TYPES.key(raw)
  rescue
    :activity
  end

  def id
    @xml.at_xpath('./xmlns:id', xmlns: TagManager::XMLNS).content
  end

  def url
    link = @xml.xpath('./xmlns:link[@rel="alternate"]', xmlns: TagManager::XMLNS).find { |link_candidate| link_candidate['type'] == 'text/html' }
    link.nil? ? nil : link['href']
  end

  def activitypub_uri
    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']) }
    link.nil? ? nil : link['href']
  end

  def activitypub_uri?
    activitypub_uri.present?
  end

  private

  def find_status(uri)
    if TagManager.instance.local_id?(uri)
      local_id = TagManager.instance.unique_tag_to_local_id(uri, 'Status')
      return Status.find_by(id: local_id)
    elsif ActivityPub::TagManager.instance.local_uri?(uri)
      local_id = ActivityPub::TagManager.instance.uri_to_local_id(uri)
      return Status.find_by(id: local_id)
    end

    Status.find_by(uri: uri)
  end

  def find_activitypub_status(uri, href)
    tag_matches = /tag:([^,:]+)[^:]*:objectId=([\d]+)/.match(uri)
    href_matches = %r{/users/([^/]+)}.match(href)

    unless tag_matches.nil? || href_matches.nil?
      uri = "https://#{tag_matches[1]}/users/#{href_matches[1]}/statuses/#{tag_matches[2]}"
      Status.find_by(uri: uri)
    end
  end

  def redis
    Redis.current
  end
end