User table: ensure bio is always a string
authorrinpatch <rinpatch@sdf.org>
Tue, 1 Sep 2020 06:25:32 +0000 (09:25 +0300)
committerrinpatch <rinpatch@sdf.org>
Tue, 1 Sep 2020 07:45:42 +0000 (10:45 +0300)
Gets rid of '|| ""' in multiple places and fixes #2067

lib/pleroma/user.ex
lib/pleroma/web/activity_pub/activity_pub.ex
lib/pleroma/web/auth/pleroma_authenticator.ex
lib/pleroma/web/mastodon_api/views/account_view.ex
lib/pleroma/web/metadata/opengraph.ex
lib/pleroma/web/metadata/twitter_card.ex
priv/repo/migrations/20200901061256_ensure_bio_is_string.exs [new file with mode: 0644]
priv/repo/migrations/20200901061637_bio_set_not_null.exs [new file with mode: 0644]
test/user_test.exs
test/web/admin_api/controllers/admin_api_controller_test.exs
test/web/twitter_api/util_controller_test.exs

index d2ad9516f400ae1dba88afaeac6316646c1e68b0..94c96de8db55dd7274c95aeb9af7e4edc4bc1297 100644 (file)
@@ -83,7 +83,7 @@ defmodule Pleroma.User do
   ]
 
   schema "users" do
-    field(:bio, :string)
+    field(:bio, :string, default: "")
     field(:raw_bio, :string)
     field(:email, :string)
     field(:name, :string)
@@ -1587,7 +1587,7 @@ defmodule Pleroma.User do
     # "Right to be forgotten"
     # https://gdpr.eu/right-to-be-forgotten/
     change(user, %{
-      bio: nil,
+      bio: "",
       raw_bio: nil,
       email: nil,
       name: nil,
index 624a508ae3d907438d0df2301cdb1ea458580257..3336214133a9e046ea8ad2a01d793e594602aff1 100644 (file)
@@ -1224,7 +1224,7 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do
       name: data["name"],
       follower_address: data["followers"],
       following_address: data["following"],
-      bio: data["summary"],
+      bio: data["summary"] || "",
       actor_type: actor_type,
       also_known_as: Map.get(data, "alsoKnownAs", []),
       public_key: public_key,
index 200ca03dcd7a83bfdc1b56c583df1bde8f36d059..c611b3e0916dd83485bb65c3a04cd789d5532c6c 100644 (file)
@@ -68,7 +68,7 @@ defmodule Pleroma.Web.Auth.PleromaAuthenticator do
     nickname = value([registration_attrs["nickname"], Registration.nickname(registration)])
     email = value([registration_attrs["email"], Registration.email(registration)])
     name = value([registration_attrs["name"], Registration.name(registration)]) || nickname
-    bio = value([registration_attrs["bio"], Registration.description(registration)])
+    bio = value([registration_attrs["bio"], Registration.description(registration)]) || ""
 
     random_password = :crypto.strong_rand_bytes(64) |> Base.encode64()
 
index 864c0417f14a2694ac9cb0583d118ceb4d4806c4..d2a30a5483022d426da265b9a43bf2a931c48d77 100644 (file)
@@ -245,7 +245,7 @@ defmodule Pleroma.Web.MastodonAPI.AccountView do
       followers_count: followers_count,
       following_count: following_count,
       statuses_count: user.note_count,
-      note: user.bio || "",
+      note: user.bio,
       url: user.uri || user.ap_id,
       avatar: image,
       avatar_static: image,
index 68c871e71b19aa750bdeb7e67c6105b9bfa47c59..bb1b23208f8a112e96d923aeb13d7dc4287622e3 100644 (file)
@@ -61,7 +61,7 @@ defmodule Pleroma.Web.Metadata.Providers.OpenGraph do
 
   @impl Provider
   def build_tags(%{user: user}) do
-    with truncated_bio = Utils.scrub_html_and_truncate(user.bio || "") do
+    with truncated_bio = Utils.scrub_html_and_truncate(user.bio) do
       [
         {:meta,
          [
index 5d08ce422ea11b8e59a83b162c2fd3af3eb09ffe..df34b033f6a3fbd527119967f719dd3b69e33f46 100644 (file)
@@ -40,7 +40,7 @@ defmodule Pleroma.Web.Metadata.Providers.TwitterCard do
 
   @impl Provider
   def build_tags(%{user: user}) do
-    with truncated_bio = Utils.scrub_html_and_truncate(user.bio || "") do
+    with truncated_bio = Utils.scrub_html_and_truncate(user.bio) do
       [
         title_tag(user),
         {:meta, [property: "twitter:description", content: truncated_bio], []},
diff --git a/priv/repo/migrations/20200901061256_ensure_bio_is_string.exs b/priv/repo/migrations/20200901061256_ensure_bio_is_string.exs
new file mode 100644 (file)
index 0000000..0e3bb3c
--- /dev/null
@@ -0,0 +1,7 @@
+defmodule Pleroma.Repo.Migrations.EnsureBioIsString do
+  use Ecto.Migration
+
+  def change do
+    execute("update users set bio = '' where bio is null", "")
+  end
+end
diff --git a/priv/repo/migrations/20200901061637_bio_set_not_null.exs b/priv/repo/migrations/20200901061637_bio_set_not_null.exs
new file mode 100644 (file)
index 0000000..e3a67d4
--- /dev/null
@@ -0,0 +1,10 @@
+defmodule Pleroma.Repo.Migrations.BioSetNotNull do
+  use Ecto.Migration
+
+  def change do
+    execute(
+      "alter table users alter column bio set not null",
+      "alter table users alter column bio drop not null"
+    )
+  end
+end
index 3cf248659c40f962342069df2014f8bbd460829b..50f72549eeab1a61a683d479b27f20f185f3ed3a 100644 (file)
@@ -1466,7 +1466,7 @@ defmodule Pleroma.UserTest do
     user = User.get_by_id(user.id)
 
     assert %User{
-             bio: nil,
+             bio: "",
              raw_bio: nil,
              email: nil,
              name: nil,
index dbf478edf0dd2c9fb87299fb7a5595774d87726a..3bc88c6a913bb7e26eaac1b576bd8a00e0c3613a 100644 (file)
@@ -203,7 +203,7 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do
         assert user.note_count == 0
         assert user.follower_count == 0
         assert user.following_count == 0
-        assert user.bio == nil
+        assert user.bio == ""
         assert user.name == nil
 
         assert called(Pleroma.Web.Federator.publish(:_))
index 354d77b562dff4478febf9f695ed1fb38e666191..d164127eec448451706e6d965c7d84e50f42731c 100644 (file)
@@ -594,7 +594,7 @@ defmodule Pleroma.Web.TwitterAPI.UtilControllerTest do
       user = User.get_by_id(user.id)
       assert user.deactivated == true
       assert user.name == nil
-      assert user.bio == nil
+      assert user.bio == ""
       assert user.password_hash == nil
     end
   end