From 88d3cb44c3adc234ee828a8b50bc0c3857eb85a9 Mon Sep 17 00:00:00 2001 From: Egor Kislitsyn Date: Tue, 2 Apr 2019 17:47:02 +0700 Subject: [PATCH 1/3] replace `Repo.get_by(User, nickname: nickname)` with `User.get_by_nickname(nickname)` --- lib/pleroma/plugs/user_fetcher_plug.ex | 22 ++----------------- .../mastodon_api/mastodon_api_controller.ex | 2 +- lib/pleroma/web/twitter_api/twitter_api.ex | 9 +++----- 3 files changed, 6 insertions(+), 27 deletions(-) diff --git a/lib/pleroma/plugs/user_fetcher_plug.ex b/lib/pleroma/plugs/user_fetcher_plug.ex index 5a77f6833..4089aa958 100644 --- a/lib/pleroma/plugs/user_fetcher_plug.ex +++ b/lib/pleroma/plugs/user_fetcher_plug.ex @@ -3,9 +3,7 @@ # SPDX-License-Identifier: AGPL-3.0-only defmodule Pleroma.Plugs.UserFetcherPlug do - alias Pleroma.Repo alias Pleroma.User - import Plug.Conn def init(options) do @@ -14,26 +12,10 @@ def init(options) do def call(conn, _options) do with %{auth_credentials: %{username: username}} <- conn.assigns, - {:ok, %User{} = user} <- user_fetcher(username) do - conn - |> assign(:auth_user, user) + %User{} = user <- User.get_by_nickname_or_email(username) do + assign(conn, :auth_user, user) else _ -> conn end end - - defp user_fetcher(username_or_email) do - { - :ok, - cond do - # First, try logging in as if it was a name - user = Repo.get_by(User, %{nickname: username_or_email}) -> - user - - # If we get nil, we try using it as an email - user = Repo.get_by(User, %{email: username_or_email}) -> - user - end - } - end end diff --git a/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex b/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex index da96d1674..ffd544644 100644 --- a/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex +++ b/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex @@ -755,7 +755,7 @@ def follow(%{assigns: %{user: follower}} = conn, %{"id" => id}) do end def follow(%{assigns: %{user: follower}} = conn, %{"uri" => uri}) do - with %User{} = followed <- Repo.get_by(User, nickname: uri), + with %User{} = followed <- User.get_by_nickname(uri), {:ok, follower, followed, _} <- CommonAPI.follow(follower, followed) do conn |> put_view(AccountView) diff --git a/lib/pleroma/web/twitter_api/twitter_api.ex b/lib/pleroma/web/twitter_api/twitter_api.ex index d0e58e71b..9b081a316 100644 --- a/lib/pleroma/web/twitter_api/twitter_api.ex +++ b/lib/pleroma/web/twitter_api/twitter_api.ex @@ -227,12 +227,9 @@ def get_user(user \\ nil, params) do end %{"screen_name" => nickname} -> - case target = Repo.get_by(User, nickname: nickname) do - nil -> - {:error, "No user with such screen_name"} - - _ -> - {:ok, target} + case User.get_by_nickname(nickname) do + nil -> {:error, "No user with such screen_name"} + target -> {:ok, target} end _ -> From 9a59c26619bada93e238f52e9432a93e54c04b5e Mon Sep 17 00:00:00 2001 From: Egor Kislitsyn Date: Tue, 2 Apr 2019 17:47:52 +0700 Subject: [PATCH 2/3] replace `Repo.get_by(User, ap_id: ap_id)` with `User.get_by_ap_id(ap_id)` --- test/web/activity_pub/activity_pub_test.exs | 4 ++-- test/web/mastodon_api/status_view_test.exs | 2 +- test/web/salmon/salmon_test.exs | 2 +- test/web/twitter_api/twitter_api_controller_test.exs | 4 ++-- 4 files changed, 6 insertions(+), 6 deletions(-) diff --git a/test/web/activity_pub/activity_pub_test.exs b/test/web/activity_pub/activity_pub_test.exs index 7969c8035..46b4cf7b6 100644 --- a/test/web/activity_pub/activity_pub_test.exs +++ b/test/web/activity_pub/activity_pub_test.exs @@ -638,8 +638,8 @@ test "works with base64 encoded images" do describe "fetch the latest Follow" do test "fetches the latest Follow activity" do %Activity{data: %{"type" => "Follow"}} = activity = insert(:follow_activity) - follower = Repo.get_by(User, ap_id: activity.data["actor"]) - followed = Repo.get_by(User, ap_id: activity.data["object"]) + follower = User.get_by_ap_id(activity.data["actor"]) + followed = User.get_by_ap_id(activity.data["object"]) assert activity == Utils.fetch_latest_follow(follower, followed) end diff --git a/test/web/mastodon_api/status_view_test.exs b/test/web/mastodon_api/status_view_test.exs index e1c9b2c8f..8db92ac16 100644 --- a/test/web/mastodon_api/status_view_test.exs +++ b/test/web/mastodon_api/status_view_test.exs @@ -175,7 +175,7 @@ test "contains mentions" do status = StatusView.render("status.json", %{activity: activity}) - actor = Repo.get_by(User, ap_id: activity.actor) + actor = User.get_by_ap_id(activity.actor) assert status.mentions == Enum.map([user, actor], fn u -> AccountView.render("mention.json", %{user: u}) end) diff --git a/test/web/salmon/salmon_test.exs b/test/web/salmon/salmon_test.exs index 265e1abbd..35503259b 100644 --- a/test/web/salmon/salmon_test.exs +++ b/test/web/salmon/salmon_test.exs @@ -99,7 +99,7 @@ test "it pushes an activity to remote accounts it's addressed to" do } {:ok, activity} = Repo.insert(%Activity{data: activity_data, recipients: activity_data["to"]}) - user = Repo.get_by(User, ap_id: activity.data["actor"]) + user = User.get_by_ap_id(activity.data["actor"]) {:ok, user} = Pleroma.Web.WebFinger.ensure_keys_present(user) poster = fn url, _data, _headers -> diff --git a/test/web/twitter_api/twitter_api_controller_test.exs b/test/web/twitter_api/twitter_api_controller_test.exs index dffd401f7..405f0cfae 100644 --- a/test/web/twitter_api/twitter_api_controller_test.exs +++ b/test/web/twitter_api/twitter_api_controller_test.exs @@ -955,7 +955,7 @@ test "with credentials", %{conn: conn, user: current_user} do |> post(request_path) activity = Activity.get_by_id(note_activity.id) - activity_user = Repo.get_by(User, ap_id: note_activity.data["actor"]) + activity_user = User.get_by_ap_id(note_activity.data["actor"]) assert json_response(response, 200) == ActivityView.render("activity.json", %{ @@ -993,7 +993,7 @@ test "with credentials", %{conn: conn, user: current_user} do |> post(request_path) activity = Activity.get_by_id(note_activity.id) - activity_user = Repo.get_by(User, ap_id: note_activity.data["actor"]) + activity_user = User.get_by_ap_id(note_activity.data["actor"]) assert json_response(response, 200) == ActivityView.render("activity.json", %{ From 20c619f85f7ccd69469eb9977b6f2805edd18525 Mon Sep 17 00:00:00 2001 From: Egor Kislitsyn Date: Tue, 2 Apr 2019 17:58:32 +0700 Subject: [PATCH 3/3] replace `Repo.get_by(User, nickname: nickname)` with `User.get_by_nickname(nickname)` in tests --- test/web/twitter_api/twitter_api_controller_test.exs | 2 +- test/web/twitter_api/twitter_api_test.exs | 12 ++++++------ 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/test/web/twitter_api/twitter_api_controller_test.exs b/test/web/twitter_api/twitter_api_controller_test.exs index 405f0cfae..72b7ea85e 100644 --- a/test/web/twitter_api/twitter_api_controller_test.exs +++ b/test/web/twitter_api/twitter_api_controller_test.exs @@ -1021,7 +1021,7 @@ test "it creates a new user", %{conn: conn} do user = json_response(conn, 200) - fetched_user = Repo.get_by(User, nickname: "lain") + fetched_user = User.get_by_nickname("lain") assert user == UserView.render("show.json", %{user: fetched_user}) end diff --git a/test/web/twitter_api/twitter_api_test.exs b/test/web/twitter_api/twitter_api_test.exs index b823bfd68..6c00244de 100644 --- a/test/web/twitter_api/twitter_api_test.exs +++ b/test/web/twitter_api/twitter_api_test.exs @@ -275,7 +275,7 @@ test "it registers a new user and returns the user." do {:ok, user} = TwitterAPI.register_user(data) - fetched_user = Repo.get_by(User, nickname: "lain") + fetched_user = User.get_by_nickname("lain") assert UserView.render("show.json", %{user: user}) == UserView.render("show.json", %{user: fetched_user}) @@ -293,7 +293,7 @@ test "it registers a new user with empty string in bio and returns the user." do {:ok, user} = TwitterAPI.register_user(data) - fetched_user = Repo.get_by(User, nickname: "lain") + fetched_user = User.get_by_nickname("lain") assert UserView.render("show.json", %{user: user}) == UserView.render("show.json", %{user: fetched_user}) @@ -369,7 +369,7 @@ test "it registers a new user via invite token and returns the user." do {:ok, user} = TwitterAPI.register_user(data) - fetched_user = Repo.get_by(User, nickname: "vinny") + fetched_user = User.get_by_nickname("vinny") token = Repo.get_by(UserInviteToken, token: token.token) assert token.used == true @@ -393,7 +393,7 @@ test "it returns an error if invalid token submitted" do {:error, msg} = TwitterAPI.register_user(data) assert msg == "Invalid token" - refute Repo.get_by(User, nickname: "GrimReaper") + refute User.get_by_nickname("GrimReaper") end @moduletag skip: "needs 'registrations_open: false' in config" @@ -414,7 +414,7 @@ test "it returns an error if expired token submitted" do {:error, msg} = TwitterAPI.register_user(data) assert msg == "Expired token" - refute Repo.get_by(User, nickname: "GrimReaper") + refute User.get_by_nickname("GrimReaper") end test "it returns the error on registration problems" do @@ -429,7 +429,7 @@ test "it returns the error on registration problems" do {:error, error_object} = TwitterAPI.register_user(data) assert is_binary(error_object[:error]) - refute Repo.get_by(User, nickname: "lain") + refute User.get_by_nickname("lain") end test "it assigns an integer conversation_id" do