Fix most User tests.
authorlain <lain@soykaf.club>
Sun, 18 Nov 2018 20:40:52 +0000 (21:40 +0100)
committerlain <lain@soykaf.club>
Sun, 18 Nov 2018 20:40:52 +0000 (21:40 +0100)
lib/pleroma/formatter.ex
lib/pleroma/user.ex
lib/pleroma/user/info.ex
lib/pleroma/web/activity_pub/activity_pub.ex

index 26bb17377094340d69a0280086cc146ee4e85f83..1a5c07c8a61e73edba5ac88297090fd69aa28cbe 100644 (file)
@@ -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
index af2a696a98ab7019ef520f903c7dfa16ac15af73..23e0346a5a7238acaddbb1580badf964990d77d4 100644 (file)
@@ -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
index 39a1291dd01096de089097c66c2abe73393503a9..4a4c620ed3facc3b80bc0522aafedbb34fd7647b 100644 (file)
@@ -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
index ed579e336fc8e890296c56f7d8cb238d71de25ae..54ca891b9d27a8a24f46cf159423b13f322887da 100644 (file)
@@ -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