about summary refs log tree commit diff
diff options
context:
space:
mode:
authorEugen Rochko <eugen@zeonfederated.com>2016-02-24 17:23:59 +0100
committerEugen Rochko <eugen@zeonfederated.com>2016-02-24 17:23:59 +0100
commit8da8387afee66e17b3ec864ba84a7a065187818b (patch)
tree756b66daa33fd6f53b3c0dea7b6ba8c3ee61c56c
parentfa7868675d0952f8e4e1aa2f6b77586bb56de2c1 (diff)
Add shared statuses to the database
-rw-r--r--app/helpers/application_helper.rb4
-rw-r--r--app/services/fetch_entry_service.rb16
-rw-r--r--app/services/process_feed_service.rb42
3 files changed, 31 insertions, 31 deletions
diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb
index 36e31929e..7a74a4d96 100644
--- a/app/helpers/application_helper.rb
+++ b/app/helpers/application_helper.rb
@@ -10,6 +10,10 @@ module ApplicationHelper
     return match[1] unless match.nil?
   end
 
+  def local_id?(id)
+    id.start_with?("tag:#{LOCAL_DOMAIN}")
+  end
+
   def subscription_url(account)
     add_base_url_prefix subscriptions_path(id: account.id, format: '')
   end
diff --git a/app/services/fetch_entry_service.rb b/app/services/fetch_entry_service.rb
deleted file mode 100644
index c4a5460e9..000000000
--- a/app/services/fetch_entry_service.rb
+++ /dev/null
@@ -1,16 +0,0 @@
-class FetchEntryService < BaseService
-  # Knowing nothing but the URL of a remote status, create a local representation of it and return it
-  # @param [String] url Atom URL
-  # @return [Status]
-  def call(url)
-    body = http_client.get(url)
-    xml  = Nokogiri::XML(body)
-    # todo
-  end
-
-  private
-
-  def http_client
-    HTTP
-  end
-end
diff --git a/app/services/process_feed_service.rb b/app/services/process_feed_service.rb
index 1aaf85d94..703938b46 100644
--- a/app/services/process_feed_service.rb
+++ b/app/services/process_feed_service.rb
@@ -32,12 +32,17 @@ class ProcessFeedService < BaseService
 
   def add_reblog!(entry, status)
     status.reblog = find_original_status(entry, target_id(entry))
+
+    if status.reblog.nil?
+      status.reblog = fetch_remote_status(entry)
+    end
+
     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?
+    status.save!
   end
 
   def find_original_status(xml, id)
@@ -46,23 +51,22 @@ class ProcessFeedService < BaseService
     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
+      Status.find_by(uri: id)
     end
   end
 
-  def fetch_remote_status(xml, id)
-    url = xml.at_xpath('./link[@rel="self"]').attribute('href').value
-    nil
-  end
+  def fetch_remote_status(xml)
+    username = xml.at_xpath('./activity:object/xmlns:author/xmlns:name').content
+    url      = xml.at_xpath('./activity:object/xmlns:author/xmlns:uri').content
+    domain   = Addressable::URI.parse(url).host
+    account  = Account.find_by(username: username, domain: domain)
+
+    if account.nil?
+      account = follow_remote_account_service.("acct:#{username}@#{domain}", false)
+      return nil if account.nil?
+    end
 
-  def local_id?(id)
-    id.start_with?("tag:#{LOCAL_DOMAIN}")
+    Status.new(account: account, uri: target_id(xml), text: target_content(xml), url: target_url(xml))
   end
 
   def published(xml)
@@ -84,7 +88,7 @@ class ProcessFeedService < BaseService
   end
 
   def target_id(xml)
-    xml.at_xpath('./activity:object/xmlns:id').content
+    xml.at_xpath('.//activity:object/xmlns:id').content
   rescue
     nil
   end
@@ -93,6 +97,14 @@ class ProcessFeedService < BaseService
     entry.at_xpath('./xmlns:id').content
   end
 
+  def target_content(xml)
+    xml.at_xpath('.//activity:object/xmlns:content').content
+  end
+
+  def target_url(xml)
+    xml.at_xpath('.//activity:object/xmlns:link[@rel=alternate]').attribute('href').value
+  end
+
   def object_type(xml)
     xml.at_xpath('./activity:object-type').content.gsub('http://activitystrea.ms/schema/1.0/', '').to_sym
   rescue