Add function to fetch users from fqn.

This commit is contained in:
Roger Braun 2017-04-30 18:48:48 +02:00
parent eb12a89d22
commit bed0b39813
2 changed files with 32 additions and 0 deletions

View File

@ -3,6 +3,7 @@ defmodule Pleroma.User do
import Ecto.Changeset
import Ecto.Query
alias Pleroma.{Repo, User, Activity, Object}
alias Pleroma.Web.OStatus
schema "users" do
field :bio, :string
@ -131,4 +132,15 @@ def get_cached_user_info(user) do
key = "user_info:#{user.id}"
Cachex.get!(:user_cache, key, fallback: fn(_) -> user_info(user) end)
end
def get_or_fetch_by_nickname(nickname) do
with %User{} = user <- get_by_nickname(nickname) do
user
else _e ->
with {:ok, user} <- OStatus.make_user(nickname) do
user
else _e -> nil
end
end
end
end

View File

@ -86,4 +86,24 @@ test "it sets the password_hash, ap_id and following fields" do
assert changeset.changes[:following] == [User.ap_followers(%User{nickname: @full_user_data.nickname})]
end
end
describe "fetching a user from nickname or trying to build one" do
test "gets an existing user" do
user = insert(:user)
fetched_user = User.get_or_fetch_by_nickname(user.nickname)
assert user == fetched_user
end
test "fetches an external user via ostatus if no user exists" do
fetched_user = User.get_or_fetch_by_nickname("shp@social.heldscal.la")
assert fetched_user.nickname == "shp@social.heldscal.la"
end
test "returns nil if no user could be fetched" do
fetched_user = User.get_or_fetch_by_nickname("nonexistant@social.heldscal.la")
assert fetched_user == nil
end
end
end