Merge branch 'develop' into fix/csp-for-captcha
[akkoma] / lib / pleroma / web / mastodon_api / controllers / timeline_controller.ex
index 4bdd46d7e97f99a5d0ad6f5c52938ff7a2ca7eca..ab7b1d6aa5859025cd66630ff27eb89d3041ad9c 100644 (file)
@@ -88,21 +88,20 @@ defmodule Pleroma.Web.MastodonAPI.TimelineController do
     )
   end
 
+  defp restrict_unauthenticated?(true = _local_only) do
+    Pleroma.Config.get([:restrict_unauthenticated, :timelines, :local])
+  end
+
+  defp restrict_unauthenticated?(_) do
+    Pleroma.Config.get([:restrict_unauthenticated, :timelines, :federated])
+  end
+
   # GET /api/v1/timelines/public
   def public(%{assigns: %{user: user}} = conn, params) do
     local_only = params[:local]
 
-    cfg_key =
-      if local_only do
-        :local
-      else
-        :federated
-      end
-
-    restrict? = Pleroma.Config.get([:restrict_unauthenticated, :timelines, cfg_key])
-
-    if restrict? and is_nil(user) do
-      render_error(conn, :unauthorized, "authorization required for timeline view")
+    if is_nil(user) and restrict_unauthenticated?(local_only) do
+      fail_on_bad_auth(conn)
     else
       activities =
         params
@@ -123,6 +122,10 @@ defmodule Pleroma.Web.MastodonAPI.TimelineController do
     end
   end
 
+  defp fail_on_bad_auth(conn) do
+    render_error(conn, :unauthorized, "authorization required for timeline view")
+  end
+
   defp hashtag_fetching(params, user, local_only) do
     tags =
       [params[:tag], params[:any]]
@@ -157,15 +160,20 @@ defmodule Pleroma.Web.MastodonAPI.TimelineController do
   # GET /api/v1/timelines/tag/:tag
   def hashtag(%{assigns: %{user: user}} = conn, params) do
     local_only = params[:local]
-    activities = hashtag_fetching(params, user, local_only)
 
-    conn
-    |> add_link_headers(activities, %{"local" => local_only})
-    |> render("index.json",
-      activities: activities,
-      for: user,
-      as: :activity
-    )
+    if is_nil(user) and restrict_unauthenticated?(local_only) do
+      fail_on_bad_auth(conn)
+    else
+      activities = hashtag_fetching(params, user, local_only)
+
+      conn
+      |> add_link_headers(activities, %{"local" => local_only})
+      |> render("index.json",
+        activities: activities,
+        for: user,
+        as: :activity
+      )
+    end
   end
 
   # GET /api/v1/timelines/list/:list_id