Merge branch 'get_by_id' into 'develop'
Replace `Repo.get_by` with existing functions See merge request pleroma/pleroma!1010
This commit is contained in:
commit
b95cf3d490
@ -3,9 +3,7 @@
|
|||||||
# SPDX-License-Identifier: AGPL-3.0-only
|
# SPDX-License-Identifier: AGPL-3.0-only
|
||||||
|
|
||||||
defmodule Pleroma.Plugs.UserFetcherPlug do
|
defmodule Pleroma.Plugs.UserFetcherPlug do
|
||||||
alias Pleroma.Repo
|
|
||||||
alias Pleroma.User
|
alias Pleroma.User
|
||||||
|
|
||||||
import Plug.Conn
|
import Plug.Conn
|
||||||
|
|
||||||
def init(options) do
|
def init(options) do
|
||||||
@ -14,26 +12,10 @@ def init(options) do
|
|||||||
|
|
||||||
def call(conn, _options) do
|
def call(conn, _options) do
|
||||||
with %{auth_credentials: %{username: username}} <- conn.assigns,
|
with %{auth_credentials: %{username: username}} <- conn.assigns,
|
||||||
{:ok, %User{} = user} <- user_fetcher(username) do
|
%User{} = user <- User.get_by_nickname_or_email(username) do
|
||||||
conn
|
assign(conn, :auth_user, user)
|
||||||
|> assign(:auth_user, user)
|
|
||||||
else
|
else
|
||||||
_ -> conn
|
_ -> conn
|
||||||
end
|
end
|
||||||
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
|
end
|
||||||
|
@ -755,7 +755,7 @@ def follow(%{assigns: %{user: follower}} = conn, %{"id" => id}) do
|
|||||||
end
|
end
|
||||||
|
|
||||||
def follow(%{assigns: %{user: follower}} = conn, %{"uri" => uri}) do
|
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
|
{:ok, follower, followed, _} <- CommonAPI.follow(follower, followed) do
|
||||||
conn
|
conn
|
||||||
|> put_view(AccountView)
|
|> put_view(AccountView)
|
||||||
|
@ -227,12 +227,9 @@ def get_user(user \\ nil, params) do
|
|||||||
end
|
end
|
||||||
|
|
||||||
%{"screen_name" => nickname} ->
|
%{"screen_name" => nickname} ->
|
||||||
case target = Repo.get_by(User, nickname: nickname) do
|
case User.get_by_nickname(nickname) do
|
||||||
nil ->
|
nil -> {:error, "No user with such screen_name"}
|
||||||
{:error, "No user with such screen_name"}
|
target -> {:ok, target}
|
||||||
|
|
||||||
_ ->
|
|
||||||
{:ok, target}
|
|
||||||
end
|
end
|
||||||
|
|
||||||
_ ->
|
_ ->
|
||||||
|
@ -638,8 +638,8 @@ test "works with base64 encoded images" do
|
|||||||
describe "fetch the latest Follow" do
|
describe "fetch the latest Follow" do
|
||||||
test "fetches the latest Follow activity" do
|
test "fetches the latest Follow activity" do
|
||||||
%Activity{data: %{"type" => "Follow"}} = activity = insert(:follow_activity)
|
%Activity{data: %{"type" => "Follow"}} = activity = insert(:follow_activity)
|
||||||
follower = Repo.get_by(User, ap_id: activity.data["actor"])
|
follower = User.get_by_ap_id(activity.data["actor"])
|
||||||
followed = Repo.get_by(User, ap_id: activity.data["object"])
|
followed = User.get_by_ap_id(activity.data["object"])
|
||||||
|
|
||||||
assert activity == Utils.fetch_latest_follow(follower, followed)
|
assert activity == Utils.fetch_latest_follow(follower, followed)
|
||||||
end
|
end
|
||||||
|
@ -175,7 +175,7 @@ test "contains mentions" do
|
|||||||
|
|
||||||
status = StatusView.render("status.json", %{activity: activity})
|
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 ==
|
assert status.mentions ==
|
||||||
Enum.map([user, actor], fn u -> AccountView.render("mention.json", %{user: u}) end)
|
Enum.map([user, actor], fn u -> AccountView.render("mention.json", %{user: u}) end)
|
||||||
|
@ -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"]})
|
{: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)
|
{:ok, user} = Pleroma.Web.WebFinger.ensure_keys_present(user)
|
||||||
|
|
||||||
poster = fn url, _data, _headers ->
|
poster = fn url, _data, _headers ->
|
||||||
|
@ -955,7 +955,7 @@ test "with credentials", %{conn: conn, user: current_user} do
|
|||||||
|> post(request_path)
|
|> post(request_path)
|
||||||
|
|
||||||
activity = Activity.get_by_id(note_activity.id)
|
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) ==
|
assert json_response(response, 200) ==
|
||||||
ActivityView.render("activity.json", %{
|
ActivityView.render("activity.json", %{
|
||||||
@ -993,7 +993,7 @@ test "with credentials", %{conn: conn, user: current_user} do
|
|||||||
|> post(request_path)
|
|> post(request_path)
|
||||||
|
|
||||||
activity = Activity.get_by_id(note_activity.id)
|
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) ==
|
assert json_response(response, 200) ==
|
||||||
ActivityView.render("activity.json", %{
|
ActivityView.render("activity.json", %{
|
||||||
@ -1021,7 +1021,7 @@ test "it creates a new user", %{conn: conn} do
|
|||||||
|
|
||||||
user = json_response(conn, 200)
|
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})
|
assert user == UserView.render("show.json", %{user: fetched_user})
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -275,7 +275,7 @@ test "it registers a new user and returns the user." do
|
|||||||
|
|
||||||
{:ok, user} = TwitterAPI.register_user(data)
|
{: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}) ==
|
assert UserView.render("show.json", %{user: user}) ==
|
||||||
UserView.render("show.json", %{user: fetched_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)
|
{: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}) ==
|
assert UserView.render("show.json", %{user: user}) ==
|
||||||
UserView.render("show.json", %{user: fetched_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)
|
{: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)
|
token = Repo.get_by(UserInviteToken, token: token.token)
|
||||||
|
|
||||||
assert token.used == true
|
assert token.used == true
|
||||||
@ -393,7 +393,7 @@ test "it returns an error if invalid token submitted" do
|
|||||||
{:error, msg} = TwitterAPI.register_user(data)
|
{:error, msg} = TwitterAPI.register_user(data)
|
||||||
|
|
||||||
assert msg == "Invalid token"
|
assert msg == "Invalid token"
|
||||||
refute Repo.get_by(User, nickname: "GrimReaper")
|
refute User.get_by_nickname("GrimReaper")
|
||||||
end
|
end
|
||||||
|
|
||||||
@moduletag skip: "needs 'registrations_open: false' in config"
|
@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)
|
{:error, msg} = TwitterAPI.register_user(data)
|
||||||
|
|
||||||
assert msg == "Expired token"
|
assert msg == "Expired token"
|
||||||
refute Repo.get_by(User, nickname: "GrimReaper")
|
refute User.get_by_nickname("GrimReaper")
|
||||||
end
|
end
|
||||||
|
|
||||||
test "it returns the error on registration problems" do
|
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)
|
{:error, error_object} = TwitterAPI.register_user(data)
|
||||||
|
|
||||||
assert is_binary(error_object[:error])
|
assert is_binary(error_object[:error])
|
||||||
refute Repo.get_by(User, nickname: "lain")
|
refute User.get_by_nickname("lain")
|
||||||
end
|
end
|
||||||
|
|
||||||
test "it assigns an integer conversation_id" do
|
test "it assigns an integer conversation_id" do
|
||||||
|
Loading…
Reference in New Issue
Block a user