gearheads
/
mastodon
Archived
2
0
Fork 0
This repository has been archived on 2024-06-09. You can view files and clone it, but cannot push or open issues/pull-requests.
mastodon/app/services/process_interaction_service.rb

72 lines
2.0 KiB
Ruby
Raw Normal View History

2016-02-22 16:00:20 +01:00
class ProcessInteractionService
include ApplicationHelper
2016-02-22 16:00:20 +01:00
def call(envelope, target_account)
body = salmon.unpack(envelope)
xml = Nokogiri::XML(body)
return unless involves_target_account?(xml, target_account) && contains_author?(xml)
2016-02-22 16:00:20 +01:00
username = xml.at_xpath('/xmlns:entry/xmlns:author/xmlns:name').content
url = xml.at_xpath('/xmlns:entry/xmlns:author/xmlns:uri').content
2016-02-22 16:00:20 +01:00
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?
2016-02-22 16:00:20 +01:00
end
if salmon.verify(envelope, account.keypair)
case get_verb(xml)
when :follow
2016-02-22 16:00:20 +01:00
account.follow!(target_account)
when :unfollow
2016-02-22 16:00:20 +01:00
account.unfollow!(target_account)
when :favorite
# todo: a favourite
when :post
# todo: a reply
when :share
# todo: a reblog
2016-02-22 16:00:20 +01:00
end
end
end
private
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
2016-02-22 19:11:07 +01:00
end
2016-02-22 16:00:20 +01:00
def salmon
OStatus2::Salmon.new
end
def follow_remote_account_service
FollowRemoteAccountService.new
end
end