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/account_search_service.rb2
-rw-r--r--app/services/fetch_link_card_service.rb58
-rw-r--r--app/services/fetch_remote_status_service.rb2
-rw-r--r--app/services/follow_service.rb2
-rw-r--r--app/services/resolve_remote_account_service.rb2
5 files changed, 34 insertions, 32 deletions
diff --git a/app/services/account_search_service.rb b/app/services/account_search_service.rb
index a289ceac4..3be110665 100644
--- a/app/services/account_search_service.rb
+++ b/app/services/account_search_service.rb
@@ -4,7 +4,7 @@ class AccountSearchService < BaseService
   attr_reader :query, :limit, :options, :account
 
   def call(query, limit, account = nil, options = {})
-    @query   = query
+    @query   = query.strip
     @limit   = limit
     @options = options
     @account = account
diff --git a/app/services/fetch_link_card_service.rb b/app/services/fetch_link_card_service.rb
index cec96d927..d0472a1d7 100644
--- a/app/services/fetch_link_card_service.rb
+++ b/app/services/fetch_link_card_service.rb
@@ -38,7 +38,13 @@ class FetchLinkCardService < BaseService
     @card ||= PreviewCard.new(url: @url)
     res     = Request.new(:head, @url).perform
 
-    return if res.code != 200 || res.mime_type != 'text/html'
+    return if res.code != 405 && (res.code != 200 || res.mime_type != 'text/html')
+
+    @response = Request.new(:get, @url).perform
+
+    return if @response.code != 200 || @response.mime_type != 'text/html'
+
+    @html = @response.to_s
 
     attempt_oembed || attempt_opengraph
   end
@@ -70,30 +76,32 @@ class FetchLinkCardService < BaseService
   end
 
   def attempt_oembed
-    response = OEmbed::Providers.get(@url)
+    embed = OEmbed::Providers.get(@url, html: @html)
 
-    return false unless response.respond_to?(:type)
+    return false unless embed.respond_to?(:type)
 
-    @card.type          = response.type
-    @card.title         = response.respond_to?(:title)         ? response.title         : ''
-    @card.author_name   = response.respond_to?(:author_name)   ? response.author_name   : ''
-    @card.author_url    = response.respond_to?(:author_url)    ? response.author_url    : ''
-    @card.provider_name = response.respond_to?(:provider_name) ? response.provider_name : ''
-    @card.provider_url  = response.respond_to?(:provider_url)  ? response.provider_url  : ''
+    @card.type          = embed.type
+    @card.title         = embed.respond_to?(:title)         ? embed.title         : ''
+    @card.author_name   = embed.respond_to?(:author_name)   ? embed.author_name   : ''
+    @card.author_url    = embed.respond_to?(:author_url)    ? embed.author_url    : ''
+    @card.provider_name = embed.respond_to?(:provider_name) ? embed.provider_name : ''
+    @card.provider_url  = embed.respond_to?(:provider_url)  ? embed.provider_url  : ''
     @card.width         = 0
     @card.height        = 0
 
     case @card.type
     when 'link'
-      @card.image = URI.parse(response.thumbnail_url) if response.respond_to?(:thumbnail_url)
+      @card.image = URI.parse(embed.thumbnail_url) if embed.respond_to?(:thumbnail_url)
     when 'photo'
-      @card.embed_url = response.url
-      @card.width     = response.width.presence  || 0
-      @card.height    = response.height.presence || 0
+      return false unless embed.respond_to?(:url)
+      @card.embed_url = embed.url
+      @card.image     = URI.parse(embed.url)
+      @card.width     = embed.width.presence  || 0
+      @card.height    = embed.height.presence || 0
     when 'video'
-      @card.width  = response.width.presence  || 0
-      @card.height = response.height.presence || 0
-      @card.html   = Formatter.instance.sanitize(response.html, Sanitize::Config::MASTODON_OEMBED)
+      @card.width  = embed.width.presence  || 0
+      @card.height = embed.height.presence || 0
+      @card.html   = Formatter.instance.sanitize(embed.html, Sanitize::Config::MASTODON_OEMBED)
     when 'rich'
       # Most providers rely on <script> tags, which is a no-no
       return false
@@ -105,17 +113,11 @@ class FetchLinkCardService < BaseService
   end
 
   def attempt_opengraph
-    response = Request.new(:get, @url).perform
-
-    return if response.code != 200 || response.mime_type != 'text/html'
-
-    html = response.to_s
-
     detector = CharlockHolmes::EncodingDetector.new
     detector.strip_tags = true
 
-    guess = detector.detect(html, response.charset)
-    page  = Nokogiri::HTML(html, nil, guess&.fetch(:encoding, nil))
+    guess = detector.detect(@html, @response.charset)
+    page  = Nokogiri::HTML(@html, nil, guess&.fetch(:encoding, nil))
 
     if meta_property(page, 'twitter:player')
       @card.type   = :video
@@ -132,16 +134,16 @@ class FetchLinkCardService < BaseService
       @card.image_remote_url = meta_property(page, 'og:image') if meta_property(page, 'og:image')
     end
 
-    @card.title            = meta_property(page, 'og:title').presence || page.at_xpath('//title')&.content || ''
-    @card.description      = meta_property(page, 'og:description').presence || meta_property(page, 'description') || ''
+    @card.title       = meta_property(page, 'og:title').presence || page.at_xpath('//title')&.content || ''
+    @card.description = meta_property(page, 'og:description').presence || meta_property(page, 'description') || ''
 
     return if @card.title.blank? && @card.html.blank?
 
     @card.save_with_optional_image!
   end
 
-  def meta_property(html, property)
-    html.at_xpath("//meta[@property=\"#{property}\"]")&.attribute('content')&.value || html.at_xpath("//meta[@name=\"#{property}\"]")&.attribute('content')&.value
+  def meta_property(page, property)
+    page.at_xpath("//meta[@property=\"#{property}\"]")&.attribute('content')&.value || page.at_xpath("//meta[@name=\"#{property}\"]")&.attribute('content')&.value
   end
 
   def lock_options
diff --git a/app/services/fetch_remote_status_service.rb b/app/services/fetch_remote_status_service.rb
index 9c009335b..9c3008035 100644
--- a/app/services/fetch_remote_status_service.rb
+++ b/app/services/fetch_remote_status_service.rb
@@ -40,6 +40,6 @@ class FetchRemoteStatusService < BaseService
   end
 
   def confirmed_domain?(domain, account)
-    account.domain.nil? || domain.casecmp(account.domain).zero? || domain.casecmp(Addressable::URI.parse(account.remote_url || account.uri).normalized_host).zero?
+    account.domain.nil? || domain.casecmp(account.domain).zero? || domain.casecmp(Addressable::URI.parse(account.remote_url.presence || account.uri).normalized_host).zero?
   end
 end
diff --git a/app/services/follow_service.rb b/app/services/follow_service.rb
index 20579ca63..ac0207a0a 100644
--- a/app/services/follow_service.rb
+++ b/app/services/follow_service.rb
@@ -22,7 +22,7 @@ class FollowService < BaseService
     elsif source_account.requested?(target_account)
       # This isn't managed by a method in AccountInteractions, so we modify it
       # ourselves if necessary.
-      req = follow_requests.find_by(target_account: other_account)
+      req = source_account.follow_requests.find_by(target_account: target_account)
       req.update!(show_reblogs: reblogs)
       return
     end
diff --git a/app/services/resolve_remote_account_service.rb b/app/services/resolve_remote_account_service.rb
index 3293fe40f..d7d0be210 100644
--- a/app/services/resolve_remote_account_service.rb
+++ b/app/services/resolve_remote_account_service.rb
@@ -44,7 +44,7 @@ class ResolveRemoteAccountService < BaseService
       if lock.acquired?
         @account = Account.find_remote(@username, @domain)
 
-        if activitypub_ready?
+        if activitypub_ready? || @account&.activitypub?
           handle_activitypub
         else
           handle_ostatus