about summary refs log tree commit diff
path: root/app/lib
diff options
context:
space:
mode:
Diffstat (limited to 'app/lib')
-rw-r--r--app/lib/activitypub/adapter.rb13
-rw-r--r--app/lib/activitypub/tag_manager.rb69
-rw-r--r--app/lib/feed_manager.rb7
-rw-r--r--app/lib/provider_discovery.rb4
-rw-r--r--app/lib/request.rb70
-rw-r--r--app/lib/tag_manager.rb2
-rw-r--r--app/lib/user_settings_decorator.rb5
7 files changed, 163 insertions, 7 deletions
diff --git a/app/lib/activitypub/adapter.rb b/app/lib/activitypub/adapter.rb
new file mode 100644
index 000000000..0a70207bc
--- /dev/null
+++ b/app/lib/activitypub/adapter.rb
@@ -0,0 +1,13 @@
+# frozen_string_literal: true
+
+class ActivityPub::Adapter < ActiveModelSerializers::Adapter::Base
+  def self.default_key_transform
+    :camel_lower
+  end
+
+  def serializable_hash(options = nil)
+    options = serialization_options(options)
+    serialized_hash = { '@context': 'https://www.w3.org/ns/activitystreams' }.merge(ActiveModelSerializers::Adapter::Attributes.new(serializer, instance_options).serializable_hash(options))
+    self.class.transform_key_casing!(serialized_hash, instance_options)
+  end
+end
diff --git a/app/lib/activitypub/tag_manager.rb b/app/lib/activitypub/tag_manager.rb
new file mode 100644
index 000000000..ec42bcad3
--- /dev/null
+++ b/app/lib/activitypub/tag_manager.rb
@@ -0,0 +1,69 @@
+# frozen_string_literal: true
+
+require 'singleton'
+
+class ActivityPub::TagManager
+  include Singleton
+  include RoutingHelper
+
+  COLLECTIONS = {
+    public: 'https://www.w3.org/ns/activitystreams#Public',
+  }.freeze
+
+  def url_for(target)
+    return target.url if target.respond_to?(:local?) && !target.local?
+
+    case target.object_type
+    when :person
+      short_account_url(target)
+    when :note, :comment, :activity
+      short_account_status_url(target.account, target)
+    end
+  end
+
+  def uri_for(target)
+    return target.uri if target.respond_to?(:local?) && !target.local?
+
+    case target.object_type
+    when :person
+      account_url(target)
+    when :note, :comment, :activity
+      account_status_url(target.account, target)
+    end
+  end
+
+  # Primary audience of a status
+  # Public statuses go out to primarily the public collection
+  # Unlisted and private statuses go out primarily to the followers collection
+  # Others go out only to the people they mention
+  def to(status)
+    case status.visibility
+    when 'public'
+      [COLLECTIONS[:public]]
+    when 'unlisted', 'private'
+      [account_followers_url(status.account)]
+    when 'direct'
+      status.mentions.map { |mention| uri_for(mention.account) }
+    end
+  end
+
+  # Secondary audience of a status
+  # Public statuses go out to followers as well
+  # Unlisted statuses go to the public as well
+  # Both of those and private statuses also go to the people mentioned in them
+  # Direct ones don't have a secondary audience
+  def cc(status)
+    cc = []
+
+    case status.visibility
+    when 'public'
+      cc << account_followers_url(status.account)
+    when 'unlisted'
+      cc << COLLECTIONS[:public]
+    end
+
+    cc.concat(status.mentions.map { |mention| uri_for(mention.account) }) unless status.direct_visibility?
+
+    cc
+  end
+end
diff --git a/app/lib/feed_manager.rb b/app/lib/feed_manager.rb
index 35b18fa1b..3b6796142 100644
--- a/app/lib/feed_manager.rb
+++ b/app/lib/feed_manager.rb
@@ -99,7 +99,7 @@ class FeedManager
     #return true if reggie === status.content || reggie === status.spoiler_text
     # extremely violent filtering code END
 
-    return true if status.reply? && status.in_reply_to_id.nil?
+    return true if status.reply? && (status.in_reply_to_id.nil? || status.in_reply_to_account_id.nil?)
 
     check_for_mutes = [status.account_id]
     check_for_mutes.concat([status.reblog.account_id]) if status.reblog?
@@ -126,12 +126,13 @@ class FeedManager
   end
 
   def filter_from_mentions?(status, receiver_id)
+    return true if receiver_id == status.account_id
+
     check_for_blocks = [status.account_id]
     check_for_blocks.concat(status.mentions.pluck(:account_id))
     check_for_blocks.concat([status.in_reply_to_account]) if status.reply? && !status.in_reply_to_account_id.nil?
 
-    should_filter   = receiver_id == status.account_id                                                                                   # Filter if I'm mentioning myself
-    should_filter ||= Block.where(account_id: receiver_id, target_account_id: check_for_blocks).any?                                     # or it's from someone I blocked, in reply to someone I blocked, or mentioning someone I blocked
+    should_filter   = Block.where(account_id: receiver_id, target_account_id: check_for_blocks).any?                                     # Filter if it's from someone I blocked, in reply to someone I blocked, or mentioning someone I blocked
     should_filter ||= (status.account.silenced? && !Follow.where(account_id: receiver_id, target_account_id: status.account_id).exists?) # of if the account is silenced and I'm not following them
 
     should_filter
diff --git a/app/lib/provider_discovery.rb b/app/lib/provider_discovery.rb
index 6d48cae2f..5e02e6806 100644
--- a/app/lib/provider_discovery.rb
+++ b/app/lib/provider_discovery.rb
@@ -1,11 +1,9 @@
 # frozen_string_literal: true
 
 class ProviderDiscovery < OEmbed::ProviderDiscovery
-  extend HttpHelper
-
   class << self
     def discover_provider(url, options = {})
-      res    = http_client.get(url)
+      res    = Request.new(:get, url).perform
       format = options[:format]
 
       raise OEmbed::NotFound, url if res.code != 200 || res.mime_type != 'text/html'
diff --git a/app/lib/request.rb b/app/lib/request.rb
new file mode 100644
index 000000000..e73c5ac20
--- /dev/null
+++ b/app/lib/request.rb
@@ -0,0 +1,70 @@
+# frozen_string_literal: true
+
+class Request
+  REQUEST_TARGET = '(request-target)'
+
+  include RoutingHelper
+
+  def initialize(verb, url, options = {})
+    @verb    = verb
+    @url     = Addressable::URI.parse(url).normalize
+    @options = options
+    @headers = {}
+
+    set_common_headers!
+  end
+
+  def on_behalf_of(account)
+    raise ArgumentError unless account.local?
+    @account = account
+  end
+
+  def add_headers(new_headers)
+    @headers.merge!(new_headers)
+  end
+
+  def perform
+    http_client.headers(headers).public_send(@verb, @url.to_s, @options)
+  end
+
+  def headers
+    (@account ? @headers.merge('Signature' => signature) : @headers).without(REQUEST_TARGET)
+  end
+
+  private
+
+  def set_common_headers!
+    @headers[REQUEST_TARGET] = "#{@verb} #{@url.path}"
+    @headers['User-Agent']   = user_agent
+    @headers['Host']         = @url.host
+    @headers['Date']         = Time.now.utc.httpdate
+  end
+
+  def signature
+    key_id    = @account.to_webfinger_s
+    algorithm = 'rsa-sha256'
+    signature = Base64.strict_encode64(@account.keypair.sign(OpenSSL::Digest::SHA256.new, signed_string))
+
+    "keyId=\"#{key_id}\",algorithm=\"#{algorithm}\",headers=\"#{signed_headers}\",signature=\"#{signature}\""
+  end
+
+  def signed_string
+    @headers.map { |key, value| "#{key.downcase}: #{value}" }.join("\n")
+  end
+
+  def signed_headers
+    @headers.keys.join(' ').downcase
+  end
+
+  def user_agent
+    @user_agent ||= "#{HTTP::Request::USER_AGENT} (Mastodon/#{Mastodon::Version}; +#{root_url})"
+  end
+
+  def timeout
+    { write: 10, connect: 10, read: 10 }
+  end
+
+  def http_client
+    HTTP.timeout(:per_operation, timeout).follow
+  end
+end
diff --git a/app/lib/tag_manager.rb b/app/lib/tag_manager.rb
index f1a2234dc..5f87a2a48 100644
--- a/app/lib/tag_manager.rb
+++ b/app/lib/tag_manager.rb
@@ -70,7 +70,7 @@ class TagManager
 
     uri = Addressable::URI.new
     uri.host = domain.gsub(/[\/]/, '')
-    uri.normalize.host
+    uri.normalized_host
   end
 
   def same_acct?(canonical, needle)
diff --git a/app/lib/user_settings_decorator.rb b/app/lib/user_settings_decorator.rb
index e0e92b19d..c5da18029 100644
--- a/app/lib/user_settings_decorator.rb
+++ b/app/lib/user_settings_decorator.rb
@@ -23,6 +23,7 @@ class UserSettingsDecorator
     user.settings['delete_modal'] = delete_modal_preference
     user.settings['auto_play_gif'] = auto_play_gif_preference
     user.settings['system_font_ui'] = system_font_ui_preference
+    user.settings['noindex'] = noindex_preference
   end
 
   def merged_notification_emails
@@ -57,6 +58,10 @@ class UserSettingsDecorator
     boolean_cast_setting 'setting_auto_play_gif'
   end
 
+  def noindex_preference
+    boolean_cast_setting 'setting_noindex'
+  end
+
   def boolean_cast_setting(key)
     settings[key] == '1'
   end