From 4c918392c61ca30f46d8e5f8e964993844b70ad8 Mon Sep 17 00:00:00 2001 From: lain Date: Sun, 18 Nov 2018 21:40:52 +0100 Subject: [PATCH] Fix most User tests. --- lib/pleroma/formatter.ex | 4 +-- lib/pleroma/user.ex | 43 +++++++++++++++++----------- lib/pleroma/user/info.ex | 21 ++++++++++++++ lib/pleroma/web/activity_pub/activity_pub.ex | 2 +- 4 files changed, 50 insertions(+), 20 deletions(-) diff --git a/lib/pleroma/formatter.ex b/lib/pleroma/formatter.ex index 26bb17377..1a5c07c8a 100644 --- a/lib/pleroma/formatter.ex +++ b/lib/pleroma/formatter.ex @@ -116,8 +116,8 @@ defmodule Pleroma.Formatter do subs ++ Enum.map(mentions, fn {match, %User{ap_id: ap_id, info: info}, uuid} -> ap_id = - if is_binary(info["source_data"]["url"]) do - info["source_data"]["url"] + if is_binary(info.source_data["url"]) do + info.source_data["url"] else ap_id end diff --git a/lib/pleroma/user.ex b/lib/pleroma/user.ex index af2a696a9..23e0346a5 100644 --- a/lib/pleroma/user.ex +++ b/lib/pleroma/user.ex @@ -36,13 +36,13 @@ defmodule Pleroma.User do end def banner_url(user) do - case user.info["banner"] do + case user.info.banner do %{"url" => [%{"href" => href} | _]} -> href _ -> "#{Web.base_url()}/images/banner.png" 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(_), do: nil @@ -80,18 +80,24 @@ defmodule Pleroma.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])?)*$/ 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 = - %User{} - |> cast(params, [:bio, :name, :ap_id, :nickname, :info, :avatar]) + %User{info: %{}} + |> cast(params, [:bio, :name, :ap_id, :nickname, :avatar]) |> validate_required([:name, :ap_id]) |> unique_constraint(:nickname) |> validate_format(:nickname, @email_regex) |> validate_length(:bio, max: 5000) |> validate_length(:name, max: 100) |> put_change(:local, false) + |> put_embed(:info, info_cng) if changes.valid? do - case changes.changes[:info]["source_data"] do + case info_cng.changes[:source_data] do %{"followers" => followers} -> changes |> put_change(:follower_address, followers) @@ -592,21 +598,23 @@ defmodule Pleroma.User do end def block_domain(user, domain) do - domain_blocks = user.info.domain_blocks - new_blocks = Enum.uniq([domain | domain_blocks]) - new_info = Map.put(user.info, "domain_blocks", new_blocks) + info_cng = user.info + |> User.Info.add_to_domain_block(domain) - cs = User.info_changeset(user, %{info: new_info}) - update_and_set_cache(cs) + cng = change(user) + |> put_embed(:info, info_cng) + + update_and_set_cache(cng) end def unblock_domain(user, domain) do - blocks = user.info["domain_blocks"] || [] - new_blocks = List.delete(blocks, domain) - new_info = Map.put(user.info, "domain_blocks", new_blocks) + info_cng = user.info + |> User.Info.remove_from_domain_block(domain) - cs = User.info_changeset(user, %{info: new_info}) - update_and_set_cache(cs) + cng = change(user) + |> put_embed(:info, info_cng) + + update_and_set_cache(cng) end def local_user_query() do @@ -700,7 +708,7 @@ defmodule Pleroma.User do user else changes = - %User{} + %User{info: %{}} |> cast(%{}, [:ap_id, :nickname, :local]) |> put_change(:ap_id, relay_uri) |> put_change(:nickname, nil) @@ -745,13 +753,14 @@ defmodule Pleroma.User do data = data |> Map.put(:name, blank?(data[:name]) || data[:nickname]) + |> Map.put(:info, data[:info] || %{}) cs = User.remote_user_creation(data) Repo.insert(cs, on_conflict: :replace_all, conflict_target: :nickname) end 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 get_or_fetch(uri_or_nickname) do diff --git a/lib/pleroma/user/info.ex b/lib/pleroma/user/info.ex index 39a1291dd..4a4c620ed 100644 --- a/lib/pleroma/user/info.ex +++ b/lib/pleroma/user/info.ex @@ -62,6 +62,22 @@ defmodule Pleroma.User.Info do set_blocks(info, List.delete(info.blocks, blocked)) 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 params = %{keys: keys} @@ -69,4 +85,9 @@ defmodule Pleroma.User.Info do |> cast(params, [:keys]) |> validate_required([:keys]) end + + def remote_user_creation(info, params) do + info + |> cast(params, [:source_data]) + end end diff --git a/lib/pleroma/web/activity_pub/activity_pub.ex b/lib/pleroma/web/activity_pub/activity_pub.ex index ed579e336..54ca891b9 100644 --- a/lib/pleroma/web/activity_pub/activity_pub.ex +++ b/lib/pleroma/web/activity_pub/activity_pub.ex @@ -42,7 +42,7 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do defp check_actor_is_active(actor) do if not is_nil(actor) do with user <- User.get_cached_by_ap_id(actor), - false <- !!user.info["deactivated"] do + false <- user.info.deactivated do :ok else _e -> :reject