Use changeset for remote user creation.

This commit is contained in:
Roger Braun 2017-05-10 10:16:20 +02:00
parent 373753e595
commit 118c572006
3 changed files with 11 additions and 9 deletions

View File

@ -63,13 +63,14 @@ def user_info(%User{} = user) do
@email_regex ~r/^[a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/ @email_regex ~r/^[a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/
def remote_user_creation(params) do def remote_user_creation(params) do
changeset = %User{} %User{}
|> cast(params, [:bio, :name, :ap_id, :nickname, :info]) |> cast(params, [:bio, :name, :ap_id, :nickname, :info, :avatar])
|> validate_required([:bio, :name, :ap_id, :nickname]) |> validate_required([:name, :ap_id, :nickname])
|> unique_constraint(:nickname) |> unique_constraint(:nickname)
|> validate_format(:nickname, @email_regex) |> validate_format(:nickname, @email_regex)
|> validate_length(:bio, max: 1000) |> validate_length(:bio, max: 1000)
|> validate_length(:name, max: 100) |> validate_length(:name, max: 100)
|> put_change(:local, false)
end end
def register_changeset(struct, params \\ %{}) do def register_changeset(struct, params \\ %{}) do

View File

@ -211,16 +211,14 @@ def find_or_make_user(uri) do
def make_user(uri) do def make_user(uri) do
with {:ok, info} <- gather_user_info(uri) do with {:ok, info} <- gather_user_info(uri) do
data = %{ data = %{
local: false,
name: info["name"], name: info["name"],
nickname: info["nickname"] <> "@" <> info["host"], nickname: info["nickname"] <> "@" <> info["host"],
ap_id: info["uri"], ap_id: info["uri"],
info: info, info: info,
avatar: info["avatar"] avatar: info["avatar"]
} }
# TODO: Make remote user changeset cs = User.remote_user_creation(data)
# SHould enforce fqn nickname Repo.insert(cs)
Repo.insert(Ecto.Changeset.change(%User{}, data))
end end
end end

View File

@ -146,7 +146,8 @@ test "returns an ap_followers link for a user" do
name: "Someone", name: "Someone",
nickname: "a@b.de", nickname: "a@b.de",
ap_id: "http...", ap_id: "http...",
info: %{ some: "info" } info: %{some: "info"},
avatar: %{some: "avatar"}
} }
test "it confirms validity" do test "it confirms validity" do
@ -156,11 +157,13 @@ test "it confirms validity" do
test "it enforces the fqn format for nicknames" do test "it enforces the fqn format for nicknames" do
cs = User.remote_user_creation(%{@valid_remote | nickname: "bla"}) cs = User.remote_user_creation(%{@valid_remote | nickname: "bla"})
assert cs.changes.local == false
assert cs.changes.avatar
refute cs.valid? refute cs.valid?
end end
test "it has required fields" do test "it has required fields" do
[:bio, :name, :nickname, :ap_id] [:name, :nickname, :ap_id]
|> Enum.each(fn (field) -> |> Enum.each(fn (field) ->
cs = User.remote_user_creation(Map.delete(@valid_remote, field)) cs = User.remote_user_creation(Map.delete(@valid_remote, field))
refute cs.valid? refute cs.valid?