about summary refs log tree commit diff
path: root/app/services/verify_link_service.rb
diff options
context:
space:
mode:
authorThibaut Girka <thib@sitedethib.com>2018-09-19 21:46:01 +0200
committerThibaut Girka <thib@sitedethib.com>2018-09-19 21:46:01 +0200
commit91bef4759f40422b64d7990f9b90db0b493773b1 (patch)
tree3211b1014a6d0a88737db16c44d210ef96a070e4 /app/services/verify_link_service.rb
parent382cdd7f959480d59fee5646be320d6076cb18d8 (diff)
parent554f659f2aa1eb9c0ca64ec1c9c177538434826c (diff)
Merge branch 'master' into glitch-soc/merge-upstream
Conflicts:
	Vagrantfile
	app/javascript/packs/public.js
	app/views/admin/settings/edit.html.haml
	app/views/settings/preferences/show.html.haml
	app/views/settings/profiles/show.html.haml
	config/locales/es.yml
	config/locales/simple_form.en.yml
	config/webpack/configuration.js
	config/webpack/loaders/babel.js
	package.json
	yarn.lock

Split new additions to app/javascript/packs/public.js to
app/javascript/core/settings.js
Diffstat (limited to 'app/services/verify_link_service.rb')
-rw-r--r--app/services/verify_link_service.rb32
1 files changed, 32 insertions, 0 deletions
diff --git a/app/services/verify_link_service.rb b/app/services/verify_link_service.rb
new file mode 100644
index 000000000..846751cd5
--- /dev/null
+++ b/app/services/verify_link_service.rb
@@ -0,0 +1,32 @@
+# frozen_string_literal: true
+
+class VerifyLinkService < BaseService
+  def call(field)
+    @link_back = ActivityPub::TagManager.instance.url_for(field.account)
+    @url       = field.value
+
+    perform_request!
+
+    return unless link_back_present?
+
+    field.mark_verified!
+    field.account.save!
+  rescue HTTP::Error, Addressable::URI::InvalidURIError, Mastodon::HostValidationError, Mastodon::LengthValidationError => e
+    Rails.logger.debug "Error fetching link #{@url}: #{e}"
+    nil
+  end
+
+  private
+
+  def perform_request!
+    @body = Request.new(:get, @url).add_headers('Accept' => 'text/html').perform do |res|
+      res.code != 200 ? nil : res.body_with_limit
+    end
+  end
+
+  def link_back_present?
+    return false if @body.empty?
+
+    Nokogiri::HTML(@body).xpath('//a[contains(concat(" ", normalize-space(@rel), " "), " me ")]|//link[contains(concat(" ", normalize-space(@rel), " "), " me ")]').any? { |link| link['href'] == @link_back }
+  end
+end