about summary refs log tree commit diff
path: root/app
diff options
context:
space:
mode:
authorEugen Rochko <eugen@zeonfederated.com>2016-02-23 22:17:07 +0100
committerEugen Rochko <eugen@zeonfederated.com>2016-02-23 22:17:07 +0100
commitee73d35eea024894e851d807132c3f21a133152d (patch)
tree9368486314b3665376d55e2a44ccf1f1de12fd48 /app
parentfa33750105389110a3395ca19167f789d21a149e (diff)
Incoming Salmon requests can be turned into follows and unfollows
Diffstat (limited to 'app')
-rw-r--r--app/services/process_interaction_service.rb49
1 files changed, 39 insertions, 10 deletions
diff --git a/app/services/process_interaction_service.rb b/app/services/process_interaction_service.rb
index dd9e76956..b91cfcf66 100644
--- a/app/services/process_interaction_service.rb
+++ b/app/services/process_interaction_service.rb
@@ -1,35 +1,64 @@
 class ProcessInteractionService
+  include ApplicationHelper
+
   def call(envelope, target_account)
     body = salmon.unpack(envelope)
     xml  = Nokogiri::XML(body)
 
-    return if !involves_target_account(xml, target_account) || xml.at_xpath('//xmlns:author/xmlns:name').nil? || xml.at_xpath('//xmlns:author/xmlns:uri').nil?
+    return unless involves_target_account?(xml, target_account) && contains_author?(xml)
 
-    username = xml.at_xpath('//xmlns:author/xmlns:name').content
-    url      = xml.at_xpath('//xmlns:author/xmlns:uri').content
+    username = xml.at_xpath('/xmlns:entry/xmlns:author/xmlns:name').content
+    url      = xml.at_xpath('/xmlns:entry/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}")
+      return if account.nil?
     end
 
     if salmon.verify(envelope, account.keypair)
-      verb = xml.at_path('//activity:verb').content
-
-      case verb
-      when 'http://activitystrea.ms/schema/1.0/follow', 'follow'
+      case get_verb(xml)
+      when :follow
         account.follow!(target_account)
-      when 'http://activitystrea.ms/schema/1.0/unfollow', 'unfollow'
+      when :unfollow
         account.unfollow!(target_account)
+      when :favorite
+        # todo: a favourite
+      when :post
+        # todo: a reply
+      when :share
+        # todo: a reblog
       end
     end
   end
 
   private
 
-  def involves_target_account(target_account)
-    # todo
+  def contains_author?(xml)
+    !(xml.at_xpath('/xmlns:entry/xmlns:author/xmlns:name').nil? || xml.at_xpath('/xmlns:entry/xmlns:author/xmlns:uri').nil?)
+  end
+
+  def involves_target_account?(xml, account)
+    targeted_at_account?(xml, account) || mentions_account?(xml, account)
+  end
+
+  def targeted_at_account?(xml, account)
+    target_id = xml.at_xpath('/xmlns:entry/activity:object/xmlns:id')
+    !target_id.nil? && target_id.content == profile_url(name: account.username)
+  end
+
+  def mentions_account?(xml, account)
+    xml.xpath('/xmlns:entry/xmlns:link[@rel="mentioned"]').each do |mention_link|
+      return true if mention_link.attribute('ref') == profile_url(name: account.username)
+    end
+
+    false
+  end
+
+  def get_verb(xml)
+    verb = xml.at_xpath('//activity:verb').content.gsub 'http://activitystrea.ms/schema/1.0/', ''
+    verb.to_sym
   end
 
   def salmon