feat(chat): add username to users

And use it instead of hacky email transformation.
This commit is contained in:
João Paulo Dubas 2025-02-13 23:13:36 +00:00
parent 489be57db7
commit 24b609ec3a
Signed by: joao.dubas
SSH Key Fingerprint: SHA256:V1mixgOGRc/YMhGx/DNkOSmJxgA2vHNrDZEk3wt/kOA
3 changed files with 29 additions and 7 deletions

View File

@ -7,6 +7,7 @@ defmodule Slax.Accounts.User do
schema "users" do
field :email, :string
field :username, :string
field :password, :string, virtual: true, redact: true
field :hashed_password, :string, redact: true
field :current_password, :string, virtual: true, redact: true

View File

@ -98,7 +98,7 @@ defmodule SlaxWeb.ChatRoomLive do
<ul class="relative z-10 flex items-center gap-4 px-4 sm:px-6 lg:px-8 justify-end">
<%= if @current_user do %>
<li class="text-[0.8125rem] leading-6 text-zinc-900">
{username(@current_user)}
{@current_user.username}
</li>
<li>
<.link
@ -469,7 +469,7 @@ defmodule SlaxWeb.ChatRoomLive do
<span class="w-2 h-2 rounded-full border-2 border-gray-500"></span>
<% end %>
</div>
<span class="ml-2 leading-none">{username(@user)}</span>
<span class="ml-2 leading-none">{@user.username}</span>
</.link>
"""
end
@ -526,7 +526,7 @@ defmodule SlaxWeb.ChatRoomLive do
<div class="ml-2">
<div class="-mt-1">
<.link class="text-sm font-semibold hover:underline">
<span>{username(@message.user)}</span>
<span>{@message.user.username}</span>
</.link>
<span :if={@timezone} class="ml-1 text-xs text-gray-500">
{message_timestamp(@message, @timezone)}
@ -538,10 +538,6 @@ defmodule SlaxWeb.ChatRoomLive do
"""
end
defp username(user) do
user.email |> String.split("@") |> List.first() |> String.capitalize()
end
defp message_timestamp(message, timezone) do
message.inserted_at
|> Timex.Timezone.convert(timezone)

View File

@ -0,0 +1,25 @@
defmodule Slax.Repo.Migrations.AlterUsersAddUsername do
use Ecto.Migration
def change do
alter table(:users) do
add :username, :citext
end
query = """
update
users
set
username = initcap(substring(email from '^[^@]+'))
;
"""
execute query, ""
alter table(:users) do
modify :username, :citext, null: false, from: {:citext, null: true}
end
create unique_index(:users, :username)
end
end