about summary refs log tree commit diff
path: root/app/services/process_feed_service.rb
blob: b7952035bb3d9231d156186cfb5f71a28324d7c0 (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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
class ProcessFeedService
  include ApplicationHelper

  def call(body, account)
    xml = Nokogiri::XML(body)

    xml.xpath('//xmlns:entry').each do |entry|
      next unless [:note, :comment, :activity].includes? object_type(entry)

      status = Status.find_by(uri: activity_id(entry))

      next unless status.nil?

      status = Status.new(uri: activity_id(entry), account: account, text: content(entry), created_at: published(entry), updated_at: updated(entry))

      if object_type(entry) == :comment
        add_reply!(entry, status)
      elsif verb(entry) == :share
        add_reblog!(entry, status)
      else
        add_post!(entry, status)
      end
    end
  end

  private

  def add_post!(entry, status)
    status.save!
  end

  def add_reblog!(entry, status)
    status.reblog = find_original_status(entry, target_id(entry))
    status.save! unless status.reblog.nil?
  end

  def add_reply!(entry, status)
    status.thread = find_original_status(entry, thread_id(entry))
    status.save! unless status.thread.nil?
  end

  def find_original_status(xml, id)
    return nil if id.nil?

    if local_id?(id)
      Status.find(unique_tag_to_local_id(id, 'Status'))
    else
      status = Status.find_by(uri: id)

      if status.nil?
        status = fetch_remote_status(xml, id)
      end

      status
    end
  end

  def fetch_remote_status(xml, id)
    url = xml.at_xpath('./link[@rel="self"]').attribute('href').value
    nil
  end

  def local_id?(id)
    id.start_with?("tag:#{LOCAL_DOMAIN}")
  end

  def published(xml)
    xml.at_xpath('./xmlns:published').content
  end

  def updated(xml)
    xml.at_xpath('./xmlns:updated').content
  end

  def content(xml)
    xml.at_xpath('./xmlns:content').content
  end

  def thread_id(xml)
    xml.at_xpath('./thr:in-reply-to-id').attribute('ref').value
  rescue
    nil
  end

  def target_id(xml)
    xml.at_xpath('./activity:object/xmlns:id').content
  rescue
    nil
  end

  def activity_id(xml)
    entry.at_xpath('./xmlns:id').content
  end

  def object_type(xml)
    xml.at_xpath('./activity:object-type').content.gsub('http://activitystrea.ms/schema/1.0/', '').to_sym
  rescue
    :note
  end

  def verb(xml)
    xml.at_xpath('./activity:verb').content.gsub('http://activitystrea.ms/schema/1.0/', '').to_sym
  rescue
    :post
  end

  def follow_remote_account_service
    FollowRemoteAccountService.new
  end
end