Fix most User tests.

This commit is contained in:
lain 2018-11-18 21:40:52 +01:00
parent e7cd6e9739
commit 4c918392c6
4 changed files with 50 additions and 20 deletions

View File

@ -116,8 +116,8 @@ def add_user_links({subs, text}, mentions) do
subs ++ subs ++
Enum.map(mentions, fn {match, %User{ap_id: ap_id, info: info}, uuid} -> Enum.map(mentions, fn {match, %User{ap_id: ap_id, info: info}, uuid} ->
ap_id = ap_id =
if is_binary(info["source_data"]["url"]) do if is_binary(info.source_data["url"]) do
info["source_data"]["url"] info.source_data["url"]
else else
ap_id ap_id
end end

View File

@ -36,13 +36,13 @@ def avatar_url(user) do
end end
def banner_url(user) do def banner_url(user) do
case user.info["banner"] do case user.info.banner do
%{"url" => [%{"href" => href} | _]} -> href %{"url" => [%{"href" => href} | _]} -> href
_ -> "#{Web.base_url()}/images/banner.png" _ -> "#{Web.base_url()}/images/banner.png"
end end
end end
def profile_url(%User{info: %{"source_data" => %{"url" => url}}}), do: url def profile_url(%User{info: %{source_data: %{"url" => url}}}), do: url
def profile_url(%User{ap_id: ap_id}), do: ap_id def profile_url(%User{ap_id: ap_id}), do: ap_id
def profile_url(_), do: nil def profile_url(_), do: nil
@ -80,18 +80,24 @@ 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
params = params
|> Map.put(:info, params[:info] || %{})
info_cng = User.Info.remote_user_creation(%User.Info{}, params[:info])
changes = changes =
%User{} %User{info: %{}}
|> cast(params, [:bio, :name, :ap_id, :nickname, :info, :avatar]) |> cast(params, [:bio, :name, :ap_id, :nickname, :avatar])
|> validate_required([:name, :ap_id]) |> validate_required([:name, :ap_id])
|> unique_constraint(:nickname) |> unique_constraint(:nickname)
|> validate_format(:nickname, @email_regex) |> validate_format(:nickname, @email_regex)
|> validate_length(:bio, max: 5000) |> validate_length(:bio, max: 5000)
|> validate_length(:name, max: 100) |> validate_length(:name, max: 100)
|> put_change(:local, false) |> put_change(:local, false)
|> put_embed(:info, info_cng)
if changes.valid? do if changes.valid? do
case changes.changes[:info]["source_data"] do case info_cng.changes[:source_data] do
%{"followers" => followers} -> %{"followers" => followers} ->
changes changes
|> put_change(:follower_address, followers) |> put_change(:follower_address, followers)
@ -592,21 +598,23 @@ def blocks?(user, %{ap_id: ap_id}) do
end end
def block_domain(user, domain) do def block_domain(user, domain) do
domain_blocks = user.info.domain_blocks info_cng = user.info
new_blocks = Enum.uniq([domain | domain_blocks]) |> User.Info.add_to_domain_block(domain)
new_info = Map.put(user.info, "domain_blocks", new_blocks)
cs = User.info_changeset(user, %{info: new_info}) cng = change(user)
update_and_set_cache(cs) |> put_embed(:info, info_cng)
update_and_set_cache(cng)
end end
def unblock_domain(user, domain) do def unblock_domain(user, domain) do
blocks = user.info["domain_blocks"] || [] info_cng = user.info
new_blocks = List.delete(blocks, domain) |> User.Info.remove_from_domain_block(domain)
new_info = Map.put(user.info, "domain_blocks", new_blocks)
cs = User.info_changeset(user, %{info: new_info}) cng = change(user)
update_and_set_cache(cs) |> put_embed(:info, info_cng)
update_and_set_cache(cng)
end end
def local_user_query() do def local_user_query() do
@ -700,7 +708,7 @@ def get_or_create_instance_user do
user user
else else
changes = changes =
%User{} %User{info: %{}}
|> cast(%{}, [:ap_id, :nickname, :local]) |> cast(%{}, [:ap_id, :nickname, :local])
|> put_change(:ap_id, relay_uri) |> put_change(:ap_id, relay_uri)
|> put_change(:nickname, nil) |> put_change(:nickname, nil)
@ -745,13 +753,14 @@ def insert_or_update_user(data) do
data = data =
data data
|> Map.put(:name, blank?(data[:name]) || data[:nickname]) |> Map.put(:name, blank?(data[:name]) || data[:nickname])
|> Map.put(:info, data[:info] || %{})
cs = User.remote_user_creation(data) cs = User.remote_user_creation(data)
Repo.insert(cs, on_conflict: :replace_all, conflict_target: :nickname) Repo.insert(cs, on_conflict: :replace_all, conflict_target: :nickname)
end end
def ap_enabled?(%User{local: true}), do: true def ap_enabled?(%User{local: true}), do: true
def ap_enabled?(%User{info: info}), do: info["ap_enabled"] def ap_enabled?(%User{info: info}), do: info.ap_enabled
def ap_enabled?(_), do: false def ap_enabled?(_), do: false
def get_or_fetch(uri_or_nickname) do def get_or_fetch(uri_or_nickname) do

View File

@ -62,6 +62,22 @@ def remove_from_block(info, blocked) do
set_blocks(info, List.delete(info.blocks, blocked)) set_blocks(info, List.delete(info.blocks, blocked))
end end
def set_domain_blocks(info, domain_blocks) do
params = %{domain_blocks: domain_blocks}
info
|> cast(params, [:domain_blocks])
|> validate_required([:domain_blocks])
end
def add_to_domain_block(info, domain_blocked) do
set_domain_blocks(info, Enum.uniq([domain_blocked | info.domain_blocks]))
end
def remove_from_domain_block(info, domain_blocked) do
set_domain_blocks(info, List.delete(info.domain_blocks, domain_blocked))
end
def set_keys(info, keys) do def set_keys(info, keys) do
params = %{keys: keys} params = %{keys: keys}
@ -69,4 +85,9 @@ def set_keys(info, keys) do
|> cast(params, [:keys]) |> cast(params, [:keys])
|> validate_required([:keys]) |> validate_required([:keys])
end end
def remote_user_creation(info, params) do
info
|> cast(params, [:source_data])
end
end end

View File

@ -42,7 +42,7 @@ defp get_recipients(data) do
defp check_actor_is_active(actor) do defp check_actor_is_active(actor) do
if not is_nil(actor) do if not is_nil(actor) do
with user <- User.get_cached_by_ap_id(actor), with user <- User.get_cached_by_ap_id(actor),
false <- !!user.info["deactivated"] do false <- user.info.deactivated do
:ok :ok
else else
_e -> :reject _e -> :reject