feat(chat): show username using preload
This commit is contained in:
parent
ca90b77bcb
commit
6581aa3ba5
@ -41,7 +41,12 @@ defmodule Slax.Chat do
|
||||
|
||||
def list_messages_in_room(%Room{id: room_id}) do
|
||||
query =
|
||||
from(m in Message, where: m.room_id == ^room_id, order_by: [asc: m.inserted_at, asc: m.id])
|
||||
from(m in Message,
|
||||
inner_join: u in assoc(m, :user),
|
||||
preload: [user: u],
|
||||
where: m.room_id == ^room_id,
|
||||
order_by: [asc: m.inserted_at, asc: m.id]
|
||||
)
|
||||
|
||||
Repo.all(query)
|
||||
end
|
||||
|
@ -47,7 +47,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">
|
||||
<%= @current_user.email %>
|
||||
<%= username(@current_user) %>
|
||||
</li>
|
||||
<li>
|
||||
<.link
|
||||
@ -157,7 +157,7 @@ defmodule SlaxWeb.ChatRoomLive do
|
||||
<div class="ml-2">
|
||||
<div class="-mt-1">
|
||||
<.link class="text-sm font-semibold hover:underline">
|
||||
<span>User</span>
|
||||
<span><%= username(@message.user) %></span>
|
||||
</.link>
|
||||
<p class="text-sm"><%= @message.body %></p>
|
||||
</div>
|
||||
@ -165,4 +165,8 @@ defmodule SlaxWeb.ChatRoomLive do
|
||||
</div>
|
||||
"""
|
||||
end
|
||||
|
||||
defp username(user) do
|
||||
user.email |> String.split("@") |> List.first() |> String.capitalize()
|
||||
end
|
||||
end
|
||||
|
Loading…
x
Reference in New Issue
Block a user