User: Add raw_bio, storing unformatted bio
authorHaelwenn (lanodan) Monnier <contact@hacktivis.me>
Mon, 23 Mar 2020 21:52:25 +0000 (22:52 +0100)
committerHaelwenn (lanodan) Monnier <contact@hacktivis.me>
Sat, 6 Jun 2020 14:23:16 +0000 (16:23 +0200)
Related: https://git.pleroma.social/pleroma/pleroma/issues/1643

lib/pleroma/user.ex
lib/pleroma/web/mastodon_api/controllers/account_controller.ex
lib/pleroma/web/mastodon_api/views/account_view.ex
priv/repo/migrations/20200322174133_user_raw_bio.exs [new file with mode: 0644]
priv/repo/migrations/20200328193433_populate_user_raw_bio.exs [new file with mode: 0644]
test/support/factory.ex
test/web/mastodon_api/controllers/account_controller/update_credentials_test.exs
test/web/mastodon_api/views/account_view_test.exs

index 72ee2d58e2951a615e247e574faf480c8e1180e2..23ca8c9f3e2b2eb0e3c4f8edf4d28872b7ad53a3 100644 (file)
@@ -79,6 +79,7 @@ defmodule Pleroma.User do
 
   schema "users" do
     field(:bio, :string)
+    field(:raw_bio, :string)
     field(:email, :string)
     field(:name, :string)
     field(:nickname, :string)
@@ -432,6 +433,7 @@ defmodule Pleroma.User do
       params,
       [
         :bio,
+        :raw_bio,
         :name,
         :emoji,
         :avatar,
@@ -607,7 +609,16 @@ defmodule Pleroma.User do
 
     struct
     |> confirmation_changeset(need_confirmation: need_confirmation?)
-    |> cast(params, [:bio, :email, :name, :nickname, :password, :password_confirmation, :emoji])
+    |> cast(params, [
+      :bio,
+      :raw_bio,
+      :email,
+      :name,
+      :nickname,
+      :password,
+      :password_confirmation,
+      :emoji
+    ])
     |> validate_required([:name, :nickname, :password, :password_confirmation])
     |> validate_confirmation(:password)
     |> unique_constraint(:email)
index 5734bb854841be1399394d24e1decef47c8412b7..ebfa533dd58b547f47a42c60f5449baef446c680 100644 (file)
@@ -165,6 +165,7 @@ defmodule Pleroma.Web.MastodonAPI.AccountController do
       end)
       |> Maps.put_if_present(:name, params[:display_name])
       |> Maps.put_if_present(:bio, params[:note])
+      |> Maps.put_if_present(:raw_bio, params[:note])
       |> Maps.put_if_present(:avatar, params[:avatar])
       |> Maps.put_if_present(:banner, params[:header])
       |> Maps.put_if_present(:background, params[:pleroma_background_image])
index 04c419d2fc3c912b2ef449937044df31ca0f3f21..5326b02c637c312cb8197e3b7ccaf7ccf58633b4 100644 (file)
@@ -224,7 +224,7 @@ defmodule Pleroma.Web.MastodonAPI.AccountView do
       fields: user.fields,
       bot: bot,
       source: %{
-        note: prepare_user_bio(user),
+        note: user.raw_bio || "",
         sensitive: false,
         fields: user.raw_fields,
         pleroma: %{
@@ -259,17 +259,6 @@ defmodule Pleroma.Web.MastodonAPI.AccountView do
     |> maybe_put_unread_notification_count(user, opts[:for])
   end
 
-  defp prepare_user_bio(%User{bio: ""}), do: ""
-
-  defp prepare_user_bio(%User{bio: bio}) when is_binary(bio) do
-    bio
-    |> String.replace(~r(<br */?>), "\n")
-    |> Pleroma.HTML.strip_tags()
-    |> HtmlEntities.decode()
-  end
-
-  defp prepare_user_bio(_), do: ""
-
   defp username_from_nickname(string) when is_binary(string) do
     hd(String.split(string, "@"))
   end
diff --git a/priv/repo/migrations/20200322174133_user_raw_bio.exs b/priv/repo/migrations/20200322174133_user_raw_bio.exs
new file mode 100644 (file)
index 0000000..ddf9be4
--- /dev/null
@@ -0,0 +1,9 @@
+defmodule Pleroma.Repo.Migrations.UserRawBio do
+  use Ecto.Migration
+
+  def change do
+    alter table(:users) do
+      add_if_not_exists(:raw_bio, :text)
+    end
+  end
+end
diff --git a/priv/repo/migrations/20200328193433_populate_user_raw_bio.exs b/priv/repo/migrations/20200328193433_populate_user_raw_bio.exs
new file mode 100644 (file)
index 0000000..cb35db3
--- /dev/null
@@ -0,0 +1,25 @@
+defmodule Pleroma.Repo.Migrations.PopulateUserRawBio do
+  use Ecto.Migration
+  import Ecto.Query
+  alias Pleroma.User
+  alias Pleroma.Repo
+
+  def change do
+    {:ok, _} = Application.ensure_all_started(:fast_sanitize)
+
+    User.Query.build(%{local: true})
+    |> select([u], struct(u, [:id, :ap_id, :bio]))
+    |> Repo.stream()
+    |> Enum.each(fn %{bio: bio} = user ->
+      if bio do
+        raw_bio =
+          bio
+          |> String.replace(~r(<br */?>), "\n")
+          |> Pleroma.HTML.strip_tags()
+
+        Ecto.Changeset.cast(user, %{raw_bio: raw_bio}, [:raw_bio])
+        |> Repo.update()
+      end
+    end)
+  end
+end
index 6e3676aca302bd1a6542810e4782aac48d2e2215..1a9b96180ec4e30719386373894fd97e05267c9a 100644 (file)
@@ -42,7 +42,8 @@ defmodule Pleroma.Factory do
       user
       | ap_id: User.ap_id(user),
         follower_address: User.ap_followers(user),
-        following_address: User.ap_following(user)
+        following_address: User.ap_following(user),
+        raw_bio: user.bio
     }
   end
 
index 7c420985d927992014316f43f5af380f543f4336..76e6d603ab21af07da3255012f4f1b3470e576af 100644 (file)
@@ -83,10 +83,9 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController.UpdateCredentialsTest do
     test "updates the user's bio", %{conn: conn} do
       user2 = insert(:user)
 
-      conn =
-        patch(conn, "/api/v1/accounts/update_credentials", %{
-          "note" => "I drink #cofe with @#{user2.nickname}\n\nsuya.."
-        })
+      raw_bio = "I drink #cofe with @#{user2.nickname}\n\nsuya.."
+
+      conn = patch(conn, "/api/v1/accounts/update_credentials", %{"note" => raw_bio})
 
       assert user_data = json_response_and_validate_schema(conn, 200)
 
@@ -94,6 +93,12 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController.UpdateCredentialsTest do
                ~s(I drink <a class="hashtag" data-tag="cofe" href="http://localhost:4001/tag/cofe">#cofe</a> with <span class="h-card"><a class="u-url mention" data-user="#{
                  user2.id
                }" href="#{user2.ap_id}" rel="ugc">@<span>#{user2.nickname}</span></a></span><br/><br/>suya..)
+
+      assert user_data["source"]["note"] == raw_bio
+
+      user = Repo.get(User, user_data["id"])
+
+      assert user.raw_bio == raw_bio
     end
 
     test "updates the user's locking status", %{conn: conn} do
index f91333e5c3a3033a860410d76ee3abead9a07c8e..7ac70dc582b105827d33ba62d474ebac7f744da5 100644 (file)
@@ -33,7 +33,8 @@ defmodule Pleroma.Web.MastodonAPI.AccountViewTest do
         bio:
           "<script src=\"invalid-html\"></script><span>valid html</span>. a<br>b<br/>c<br >d<br />f '&<>\"",
         inserted_at: ~N[2017-08-15 15:47:06.597036],
-        emoji: %{"karjalanpiirakka" => "/file.png"}
+        emoji: %{"karjalanpiirakka" => "/file.png"},
+        raw_bio: "valid html. a\nb\nc\nd\nf '&<>\""
       })
 
     expected = %{