[#2046] Defaulted pleroma/restrict_unauthenticated basing on instance privacy setting...
authorIvan Tashkinov <ivantashkinov@gmail.com>
Fri, 14 Aug 2020 17:55:45 +0000 (20:55 +0300)
committerIvan Tashkinov <ivantashkinov@gmail.com>
Fri, 14 Aug 2020 17:55:45 +0000 (20:55 +0300)
config/config.exs
lib/pleroma/config.ex
lib/pleroma/user.ex
lib/pleroma/web/activity_pub/visibility.ex
lib/pleroma/web/mastodon_api/controllers/timeline_controller.ex
lib/pleroma/web/preload/timelines.ex
test/web/preload/timeline_test.exs

index eb85a6ed41e5dbcd64a5584270c38c5e289dc904..a7c9e54b163b57bf3d9a03700016927ae743caf6 100644 (file)
@@ -725,10 +725,12 @@ config :pleroma, :hackney_pools,
     timeout: 300_000
   ]
 
+private_instance? = :if_instance_is_private
+
 config :pleroma, :restrict_unauthenticated,
-  timelines: %{local: false, federated: false},
-  profiles: %{local: false, remote: false},
-  activities: %{local: false, remote: false}
+  timelines: %{local: private_instance?, federated: private_instance?},
+  profiles: %{local: private_instance?, remote: private_instance?},
+  activities: %{local: private_instance?, remote: private_instance?}
 
 config :pleroma, Pleroma.Web.ApiSpec.CastAndValidate, strict: false
 
index a8329cc1efbda910a5a0de6dda0068996d5b7d27..97f87759554f325192b315222444a5f8fc6840bb 100644 (file)
@@ -81,6 +81,16 @@ defmodule Pleroma.Config do
     Application.delete_env(:pleroma, key)
   end
 
+  def restrict_unauthenticated_access?(resource, kind) do
+    setting = get([:restrict_unauthenticated, resource, kind])
+
+    if setting in [nil, :if_instance_is_private] do
+      !get!([:instance, :public])
+    else
+      setting
+    end
+  end
+
   def oauth_consumer_strategies, do: get([:auth, :oauth_consumer_strategies], [])
 
   def oauth_consumer_enabled?, do: oauth_consumer_strategies() != []
index d1436a688455b2eeab07c883d612a56eeb1103c3..ac065e9dcc6b86e1574d54d2eaaea36b426e36da 100644 (file)
@@ -311,10 +311,12 @@ defmodule Pleroma.User do
 
   def visible_for(_, _), do: :invisible
 
-  defp restrict_unauthenticated?(%User{local: local}) do
-    config_key = if local, do: :local, else: :remote
+  defp restrict_unauthenticated?(%User{local: true}) do
+    Config.restrict_unauthenticated_access?(:profiles, :local)
+  end
 
-    Config.get([:restrict_unauthenticated, :profiles, config_key], false)
+  defp restrict_unauthenticated?(%User{local: _}) do
+    Config.restrict_unauthenticated_access?(:profiles, :remote)
   end
 
   defp visible_account_status(user) do
index 343f41caa6a6399bd05b4b6261542fac1843595b..5c349bb7a2f7c9023b4a5f8f4e6b901e8c05d781 100644 (file)
@@ -59,12 +59,9 @@ defmodule Pleroma.Web.ActivityPub.Visibility do
   end
 
   def visible_for_user?(%{local: local} = activity, nil) do
-    cfg_key =
-      if local,
-        do: :local,
-        else: :remote
+    cfg_key = if local, do: :local, else: :remote
 
-    if Pleroma.Config.get([:restrict_unauthenticated, :activities, cfg_key]),
+    if Pleroma.Config.restrict_unauthenticated_access?(:activities, cfg_key),
       do: false,
       else: is_public?(activity)
   end
index ab7b1d6aa5859025cd66630ff27eb89d3041ad9c..9244316ed2406d9e98c8e948e71cd8ae78ccaf64 100644 (file)
@@ -8,6 +8,7 @@ defmodule Pleroma.Web.MastodonAPI.TimelineController do
   import Pleroma.Web.ControllerHelper,
     only: [add_link_headers: 2, add_link_headers: 3]
 
+  alias Pleroma.Config
   alias Pleroma.Pagination
   alias Pleroma.Plugs.EnsurePublicOrAuthenticatedPlug
   alias Pleroma.Plugs.OAuthScopesPlug
@@ -89,11 +90,11 @@ defmodule Pleroma.Web.MastodonAPI.TimelineController do
   end
 
   defp restrict_unauthenticated?(true = _local_only) do
-    Pleroma.Config.get([:restrict_unauthenticated, :timelines, :local])
+    Config.restrict_unauthenticated_access?(:timelines, :local)
   end
 
   defp restrict_unauthenticated?(_) do
-    Pleroma.Config.get([:restrict_unauthenticated, :timelines, :federated])
+    Config.restrict_unauthenticated_access?(:timelines, :federated)
   end
 
   # GET /api/v1/timelines/public
index 57de0405104bcf7599260e9278b44aff7e36abff..b279a865db3f5097afa44dc8abe3cd4ea77425bf 100644 (file)
@@ -16,7 +16,7 @@ defmodule Pleroma.Web.Preload.Providers.Timelines do
   end
 
   def build_public_tag(acc, params) do
-    if Pleroma.Config.get([:restrict_unauthenticated, :timelines, :federated], true) do
+    if Pleroma.Config.restrict_unauthenticated_access?(:timelines, :federated) do
       acc
     else
       Map.put(acc, @public_url, public_timeline(params))
index fea95a6a465b44e4931dce899dde8a2a560e68d8..3b1f2f1aa17b357e807c8e8c3dee5895d102c724 100644 (file)
@@ -12,16 +12,8 @@ defmodule Pleroma.Web.Preload.Providers.TimelineTest do
   @public_url "/api/v1/timelines/public"
 
   describe "unauthenticated timeliness when restricted" do
-    setup do
-      svd_config = Pleroma.Config.get([:restrict_unauthenticated, :timelines])
-      Pleroma.Config.put([:restrict_unauthenticated, :timelines], %{local: true, federated: true})
-
-      on_exit(fn ->
-        Pleroma.Config.put([:restrict_unauthenticated, :timelines], svd_config)
-      end)
-
-      :ok
-    end
+    setup do: clear_config([:restrict_unauthenticated, :timelines, :local], true)
+    setup do: clear_config([:restrict_unauthenticated, :timelines, :federated], true)
 
     test "return nothing" do
       tl_data = Timelines.generate_terms(%{})
@@ -31,20 +23,10 @@ defmodule Pleroma.Web.Preload.Providers.TimelineTest do
   end
 
   describe "unauthenticated timeliness when unrestricted" do
-    setup do
-      svd_config = Pleroma.Config.get([:restrict_unauthenticated, :timelines])
+    setup do: clear_config([:restrict_unauthenticated, :timelines, :local], false)
+    setup do: clear_config([:restrict_unauthenticated, :timelines, :federated], false)
 
-      Pleroma.Config.put([:restrict_unauthenticated, :timelines], %{
-        local: false,
-        federated: false
-      })
-
-      on_exit(fn ->
-        Pleroma.Config.put([:restrict_unauthenticated, :timelines], svd_config)
-      end)
-
-      {:ok, user: insert(:user)}
-    end
+    setup do: {:ok, user: insert(:user)}
 
     test "returns the timeline when not restricted" do
       assert Timelines.generate_terms(%{})