about summary refs log tree commit diff
path: root/app/services
diff options
context:
space:
mode:
Diffstat (limited to 'app/services')
-rw-r--r--app/services/fetch_feed_service.rb12
-rw-r--r--app/services/follow_remote_account_service.rb (renamed from app/services/follow_remote_user_service.rb)19
-rw-r--r--app/services/follow_service.rb12
-rw-r--r--app/services/process_feed_service.rb (renamed from app/services/process_feed_update_service.rb)2
-rw-r--r--app/services/process_interaction_service.rb38
-rw-r--r--app/services/setup_local_account_service.rb14
6 files changed, 84 insertions, 13 deletions
diff --git a/app/services/fetch_feed_service.rb b/app/services/fetch_feed_service.rb
index 3b8efbe3b..059d65925 100644
--- a/app/services/fetch_feed_service.rb
+++ b/app/services/fetch_feed_service.rb
@@ -1,5 +1,15 @@
 class FetchFeedService
   def call(account)
-    # todo
+    process_service.(http_client.get(account.remote_url), account)
+  end
+
+  private
+
+  def process_service
+    ProcessFeedService.new
+  end
+
+  def http_client
+    HTTP
   end
 end
diff --git a/app/services/follow_remote_user_service.rb b/app/services/follow_remote_account_service.rb
index f3c0e68df..41f8fa4a0 100644
--- a/app/services/follow_remote_user_service.rb
+++ b/app/services/follow_remote_account_service.rb
@@ -1,14 +1,14 @@
-class FollowRemoteUserService
-  include GrapeRouteHelpers::NamedRouteMatcher
+class FollowRemoteAccountService
+  include ApplicationHelper
 
-  def call(user)
-    username, domain = user.split('@')
+  def call(uri)
+    username, domain = uri.split('@')
     account = Account.where(username: username, domain: domain).first
 
     return account unless account.nil?
 
     account = Account.new(username: username, domain: domain)
-    data    = Goldfinger.finger("acct:#{user}")
+    data    = Goldfinger.finger("acct:#{uri}")
 
     account.remote_url  = data.link('http://schemas.google.com/g/2010#updates-from').href
     account.salmon_url  = data.link('salmon').href
@@ -21,8 +21,9 @@ class FollowRemoteUserService
     feed = get_feed(account.remote_url)
     hubs = feed.xpath('//xmlns:link[@rel="hub"]')
 
-    return false if hubs.empty? || hubs.first.attribute('href').nil?
+    return false if hubs.empty? || hubs.first.attribute('href').nil? || feed.at_xpath('/xmlns:author/xmlns:uri').nil?
 
+    account.uri     = feed.at_xpath('/xmlns:author/xmlns:uri').content
     account.hub_url = hubs.first.attribute('href').value
     account.save!
 
@@ -45,7 +46,7 @@ class FollowRemoteUserService
 
     key   = OpenSSL::PKey::RSA.new
     key.n = modulus
-    key.d = exponent
+    key.e = exponent
 
     key.to_pem
   end
@@ -53,8 +54,4 @@ class FollowRemoteUserService
   def http_client
     HTTP
   end
-
-  def subscription_url(account)
-    "https://649841dc.ngrok.io/api#{subscriptions_path(id: account.id)}"
-  end
 end
diff --git a/app/services/follow_service.rb b/app/services/follow_service.rb
new file mode 100644
index 000000000..fc606730b
--- /dev/null
+++ b/app/services/follow_service.rb
@@ -0,0 +1,12 @@
+class FollowService
+  def call(source_account, uri)
+    target_account = follow_remote_account_service.(uri)
+    source_account.follow!(target_account)
+  end
+
+  private
+
+  def follow_remote_account_service
+    FollowRemoteAccountService.new
+  end
+end
diff --git a/app/services/process_feed_update_service.rb b/app/services/process_feed_service.rb
index 0585fad7a..f2523a313 100644
--- a/app/services/process_feed_update_service.rb
+++ b/app/services/process_feed_service.rb
@@ -1,4 +1,4 @@
-class ProcessFeedUpdateService
+class ProcessFeedService
   def call(body, account)
     xml = Nokogiri::XML(body)
 
diff --git a/app/services/process_interaction_service.rb b/app/services/process_interaction_service.rb
new file mode 100644
index 000000000..8262ead8f
--- /dev/null
+++ b/app/services/process_interaction_service.rb
@@ -0,0 +1,38 @@
+class ProcessInteractionService
+  def call(envelope, target_account)
+    body = salmon.unpack(envelope)
+    xml  = Nokogiri::XML(body)
+
+    return if xml.at_xpath('//author/name').nil? || xml.at_xpath('//author/uri').nil?
+
+    username = xml.at_xpath('//author/name').content
+    url      = xml.at_xpath('//author/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}")
+    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'
+        account.follow!(target_account)
+      when 'http://activitystrea.ms/schema/1.0/unfollow', 'unfollow'
+        account.unfollow!(target_account)
+      end
+    end
+  end
+
+  private
+
+  def salmon
+    OStatus2::Salmon.new
+  end
+
+  def follow_remote_account_service
+    FollowRemoteAccountService.new
+  end
+end
diff --git a/app/services/setup_local_account_service.rb b/app/services/setup_local_account_service.rb
new file mode 100644
index 000000000..c40e51855
--- /dev/null
+++ b/app/services/setup_local_account_service.rb
@@ -0,0 +1,14 @@
+class SetupLocalAccountService
+  def call(user, username)
+    user.build_account
+
+    user.account.username = username
+    user.account.domain   = nil
+
+    keypair = OpenSSL::PKey::RSA.new(2048)
+    user.account.private_key = keypair.to_pem
+    user.account.public_key  = keypair.public_key.to_pem
+
+    user.save!
+  end
+end