User table: ensure bio is always a string

Gets rid of '|| ""' in multiple places and fixes #2067
This commit is contained in:
rinpatch 2020-09-01 09:25:32 +03:00
parent dc3a418c27
commit 126461942b
11 changed files with 27 additions and 10 deletions

View File

@ -83,7 +83,7 @@ defmodule Pleroma.User do
] ]
schema "users" do schema "users" do
field(:bio, :string) field(:bio, :string, default: "")
field(:raw_bio, :string) field(:raw_bio, :string)
field(:email, :string) field(:email, :string)
field(:name, :string) field(:name, :string)
@ -1587,7 +1587,7 @@ def purge_user_changeset(user) do
# "Right to be forgotten" # "Right to be forgotten"
# https://gdpr.eu/right-to-be-forgotten/ # https://gdpr.eu/right-to-be-forgotten/
change(user, %{ change(user, %{
bio: nil, bio: "",
raw_bio: nil, raw_bio: nil,
email: nil, email: nil,
name: nil, name: nil,

View File

@ -1224,7 +1224,7 @@ defp object_to_user_data(data) do
name: data["name"], name: data["name"],
follower_address: data["followers"], follower_address: data["followers"],
following_address: data["following"], following_address: data["following"],
bio: data["summary"], bio: data["summary"] || "",
actor_type: actor_type, actor_type: actor_type,
also_known_as: Map.get(data, "alsoKnownAs", []), also_known_as: Map.get(data, "alsoKnownAs", []),
public_key: public_key, public_key: public_key,

View File

@ -68,7 +68,7 @@ def create_from_registration(
nickname = value([registration_attrs["nickname"], Registration.nickname(registration)]) nickname = value([registration_attrs["nickname"], Registration.nickname(registration)])
email = value([registration_attrs["email"], Registration.email(registration)]) email = value([registration_attrs["email"], Registration.email(registration)])
name = value([registration_attrs["name"], Registration.name(registration)]) || nickname 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() random_password = :crypto.strong_rand_bytes(64) |> Base.encode64()

View File

@ -245,7 +245,7 @@ defp do_render("show.json", %{user: user} = opts) do
followers_count: followers_count, followers_count: followers_count,
following_count: following_count, following_count: following_count,
statuses_count: user.note_count, statuses_count: user.note_count,
note: user.bio || "", note: user.bio,
url: user.uri || user.ap_id, url: user.uri || user.ap_id,
avatar: image, avatar: image,
avatar_static: image, avatar_static: image,

View File

@ -61,7 +61,7 @@ def build_tags(%{
@impl Provider @impl Provider
def build_tags(%{user: user}) do 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, {:meta,
[ [

View File

@ -40,7 +40,7 @@ def build_tags(%{activity_id: id, object: object, user: user}) do
@impl Provider @impl Provider
def build_tags(%{user: user}) do 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), title_tag(user),
{:meta, [property: "twitter:description", content: truncated_bio], []}, {:meta, [property: "twitter:description", content: truncated_bio], []},

View File

@ -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

View File

@ -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

View File

@ -1466,7 +1466,7 @@ test "delete/1 purges a user when they wouldn't be fully deleted" do
user = User.get_by_id(user.id) user = User.get_by_id(user.id)
assert %User{ assert %User{
bio: nil, bio: "",
raw_bio: nil, raw_bio: nil,
email: nil, email: nil,
name: nil, name: nil,

View File

@ -203,7 +203,7 @@ test "single user", %{admin: admin, conn: conn} do
assert user.note_count == 0 assert user.note_count == 0
assert user.follower_count == 0 assert user.follower_count == 0
assert user.following_count == 0 assert user.following_count == 0
assert user.bio == nil assert user.bio == ""
assert user.name == nil assert user.name == nil
assert called(Pleroma.Web.Federator.publish(:_)) assert called(Pleroma.Web.Federator.publish(:_))

View File

@ -594,7 +594,7 @@ test "with proper permissions and valid password", %{conn: conn, user: user} do
user = User.get_by_id(user.id) user = User.get_by_id(user.id)
assert user.deactivated == true assert user.deactivated == true
assert user.name == nil assert user.name == nil
assert user.bio == nil assert user.bio == ""
assert user.password_hash == nil assert user.password_hash == nil
end end
end end