Made some progress

This commit is contained in:
Eugen Rochko 2016-02-22 16:00:20 +01:00
parent 9c4856bdb1
commit 709c6685a9
61 changed files with 570 additions and 37 deletions

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -1,4 +1,4 @@
class ProcessFeedUpdateService
class ProcessFeedService
def call(body, account)
xml = Nokogiri::XML(body)

View file

@ -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

View file

@ -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