Move checking for restrict_local to User.get_cached_by_id_or_nickname
authorrinpatch <rinpatch@sdf.org>
Thu, 5 Sep 2019 12:33:49 +0000 (15:33 +0300)
committerrinpatch <rinpatch@sdf.org>
Thu, 5 Sep 2019 12:39:10 +0000 (15:39 +0300)
lib/pleroma/user.ex
lib/pleroma/web/mastodon_api/controllers/mastodon_api_controller.ex

index d68015a8079179b6cb5361a14236b53c22d60111..3aa245f2aa43f60389fc8a8f43023de09a39268e 100644 (file)
@@ -570,10 +570,20 @@ defmodule Pleroma.User do
   end
 
   def get_cached_by_nickname_or_id(nickname_or_id, opts \\ []) do
-    if is_integer(nickname_or_id) or Pleroma.FlakeId.is_flake_id?(nickname_or_id) do
-      get_cached_by_id(nickname_or_id) || get_cached_by_nickname(nickname_or_id)
-    else
-      unless opts[:restrict_remote_nicknames], do: get_cached_by_nickname(nickname_or_id)
+    restrict_to_local = Pleroma.Config.get([:instance, :limit_to_local_content])
+
+    cond do
+      is_integer(nickname_or_id) or Pleroma.FlakeId.is_flake_id?(nickname_or_id) ->
+        get_cached_by_id(nickname_or_id) || get_cached_by_nickname(nickname_or_id)
+
+      restrict_to_local == false ->
+        get_cached_by_nickname(nickname_or_id)
+
+      restrict_to_local == :unauthenticated and match?(%User{}, opts[:for]) ->
+        get_cached_by_nickname(nickname_or_id)
+
+      true ->
+        nil
     end
   end
 
index c5f2819764e1498f67224362bc3c46800f87e399..8dfad7a54e8d695c664508c6398e269b201e76e6 100644 (file)
@@ -290,7 +290,7 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do
   end
 
   def user(%{assigns: %{user: for_user}} = conn, %{"id" => nickname_or_id}) do
-    with %User{} = user <- get_user_by_nickname_or_id(for_user, nickname_or_id),
+    with %User{} = user <- User.get_cached_by_nickname_or_id(nickname_or_id, for: for_user),
          true <- User.auth_active?(user) || user.id == for_user.id || User.superuser?(for_user) do
       account = AccountView.render("account.json", %{user: user, for: for_user})
       json(conn, account)
@@ -390,7 +390,7 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do
   end
 
   def user_statuses(%{assigns: %{user: reading_user}} = conn, params) do
-    with %User{} = user <- get_user_by_nickname_or_id(reading_user, params["id"]) do
+    with %User{} = user <- User.get_cached_by_nickname_or_id(params["id"], for: reading_user) do
       params =
         params
         |> Map.put("tag", params["tagged"])
@@ -1697,25 +1697,4 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do
   defp present?(nil), do: false
   defp present?(false), do: false
   defp present?(_), do: true
-
-  defp get_user_by_nickname_or_id(for_user, nickname_or_id) do
-    restrict_to_local = Pleroma.Config.get([:instance, :limit_to_local_content])
-
-    opts =
-      cond do
-        restrict_to_local == :all ->
-          [restrict_remote_nicknames: true]
-
-        restrict_to_local == false ->
-          []
-
-        restrict_to_local == :unauthenticated and match?(%User{}, for_user) ->
-          []
-
-        true ->
-          [restrict_remote_nicknames: true]
-      end
-
-    User.get_cached_by_nickname_or_id(nickname_or_id, opts)
-  end
 end