First pass at having a chat count in the entity pages
This commit is contained in:
@@ -25,6 +25,14 @@ defmodule MusicLibrary.Chats do
|
|||||||
|> Repo.all()
|
|> Repo.all()
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@spec count_chats(atom(), String.t()) :: non_neg_integer()
|
||||||
|
def count_chats(entity, musicbrainz_id) do
|
||||||
|
from(c in Chat,
|
||||||
|
where: c.entity == ^entity and c.musicbrainz_id == ^musicbrainz_id
|
||||||
|
)
|
||||||
|
|> Repo.aggregate(:count)
|
||||||
|
end
|
||||||
|
|
||||||
@spec has_any_chats?(atom(), String.t()) :: boolean()
|
@spec has_any_chats?(atom(), String.t()) :: boolean()
|
||||||
def has_any_chats?(entity, musicbrainz_id) do
|
def has_any_chats?(entity, musicbrainz_id) do
|
||||||
from(c in Chat,
|
from(c in Chat,
|
||||||
|
|||||||
@@ -336,6 +336,7 @@ defmodule MusicLibraryWeb.Components.Chat do
|
|||||||
def handle_event("delete_chat", %{"id" => id}, socket) do
|
def handle_event("delete_chat", %{"id" => id}, socket) do
|
||||||
chat = Chats.get_chat!(id)
|
chat = Chats.get_chat!(id)
|
||||||
{:ok, _} = Chats.delete_chat(chat)
|
{:ok, _} = Chats.delete_chat(chat)
|
||||||
|
send(self(), {__MODULE__, :chats_changed})
|
||||||
|
|
||||||
chats = Chats.list_chats(socket.assigns.entity, socket.assigns.musicbrainz_id)
|
chats = Chats.list_chats(socket.assigns.entity, socket.assigns.musicbrainz_id)
|
||||||
|
|
||||||
@@ -418,6 +419,7 @@ defmodule MusicLibraryWeb.Components.Chat do
|
|||||||
%{role: user_message.role, content: user_message.content}
|
%{role: user_message.role, content: user_message.content}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
send(self(), {__MODULE__, :chats_changed})
|
||||||
chat
|
chat
|
||||||
|
|
||||||
chat ->
|
chat ->
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ defmodule MusicLibraryWeb.ArtistLive.Show do
|
|||||||
import MusicLibraryWeb.RecordComponents,
|
import MusicLibraryWeb.RecordComponents,
|
||||||
only: [record_grid: 1, country_label: 1, artist_image: 1]
|
only: [record_grid: 1, country_label: 1, artist_image: 1]
|
||||||
|
|
||||||
alias MusicLibrary.{Artists, Records}
|
alias MusicLibrary.{Artists, Chats, Records}
|
||||||
alias MusicLibrary.Artists.ArtistInfo
|
alias MusicLibrary.Artists.ArtistInfo
|
||||||
alias MusicLibraryWeb.ErrorMessages
|
alias MusicLibraryWeb.ErrorMessages
|
||||||
|
|
||||||
@@ -120,6 +120,7 @@ defmodule MusicLibraryWeb.ArtistLive.Show do
|
|||||||
aria-hidden="true"
|
aria-hidden="true"
|
||||||
data-slot="icon"
|
data-slot="icon"
|
||||||
/>
|
/>
|
||||||
|
<span :if={@chat_count > 0} class="text-xs font-medium">{@chat_count}</span>
|
||||||
</.button>
|
</.button>
|
||||||
<.button
|
<.button
|
||||||
variant="soft"
|
variant="soft"
|
||||||
@@ -646,6 +647,16 @@ defmodule MusicLibraryWeb.ArtistLive.Show do
|
|||||||
|> put_toast(:info, gettext("Record deleted"))}
|
|> put_toast(:info, gettext("Record deleted"))}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@impl true
|
||||||
|
def handle_info({MusicLibraryWeb.Components.Chat, :chats_changed}, socket) do
|
||||||
|
{:noreply,
|
||||||
|
assign(
|
||||||
|
socket,
|
||||||
|
:chat_count,
|
||||||
|
Chats.count_chats(:artist, socket.assigns.artist.musicbrainz_id)
|
||||||
|
)}
|
||||||
|
end
|
||||||
|
|
||||||
defp apply_action(socket, :show, %{"musicbrainz_id" => musicbrainz_id}) do
|
defp apply_action(socket, :show, %{"musicbrainz_id" => musicbrainz_id}) do
|
||||||
artist = Artists.get_artist!(musicbrainz_id)
|
artist = Artists.get_artist!(musicbrainz_id)
|
||||||
artist_info = Artists.get_artist_info!(musicbrainz_id)
|
artist_info = Artists.get_artist_info!(musicbrainz_id)
|
||||||
@@ -655,6 +666,7 @@ defmodule MusicLibraryWeb.ArtistLive.Show do
|
|||||||
|> assign(:current_section, :artists)
|
|> assign(:current_section, :artists)
|
||||||
|> assign(:artist, artist)
|
|> assign(:artist, artist)
|
||||||
|> assign(:artist_info, artist_info)
|
|> assign(:artist_info, artist_info)
|
||||||
|
|> assign(:chat_count, Chats.count_chats(:artist, musicbrainz_id))
|
||||||
|> assign(:biography, build_biography(artist_info))
|
|> assign(:biography, build_biography(artist_info))
|
||||||
|> assign(:external_links, ArtistInfo.external_links(artist_info))
|
|> assign(:external_links, ArtistInfo.external_links(artist_info))
|
||||||
|> assign(:country, ArtistInfo.country(artist_info))
|
|> assign(:country, ArtistInfo.country(artist_info))
|
||||||
|
|||||||
@@ -20,7 +20,7 @@ defmodule MusicLibraryWeb.CollectionLive.Show do
|
|||||||
release_summary: 1
|
release_summary: 1
|
||||||
]
|
]
|
||||||
|
|
||||||
alias MusicLibrary.{ListeningStats, Records, RecordSets, ScrobbleActivity}
|
alias MusicLibrary.{Chats, ListeningStats, Records, RecordSets, ScrobbleActivity}
|
||||||
alias MusicLibrary.Records.Similarity
|
alias MusicLibrary.Records.Similarity
|
||||||
alias MusicLibraryWeb.ErrorMessages
|
alias MusicLibraryWeb.ErrorMessages
|
||||||
alias Phoenix.LiveView.JS
|
alias Phoenix.LiveView.JS
|
||||||
@@ -70,6 +70,7 @@ defmodule MusicLibraryWeb.CollectionLive.Show do
|
|||||||
aria-hidden="true"
|
aria-hidden="true"
|
||||||
data-slot="icon"
|
data-slot="icon"
|
||||||
/>
|
/>
|
||||||
|
<span :if={@chat_count > 0} class="text-xs font-medium">{@chat_count}</span>
|
||||||
</.button>
|
</.button>
|
||||||
<.button
|
<.button
|
||||||
:if={@record.selected_release_id}
|
:if={@record.selected_release_id}
|
||||||
@@ -354,6 +355,7 @@ defmodule MusicLibraryWeb.CollectionLive.Show do
|
|||||||
|> assign(:last_listened_track, last_listened_track)
|
|> assign(:last_listened_track, last_listened_track)
|
||||||
|> assign(:play_count, play_count)
|
|> assign(:play_count, play_count)
|
||||||
|> assign(:record_sets, record_sets)
|
|> assign(:record_sets, record_sets)
|
||||||
|
|> assign(:chat_count, Chats.count_chats(:record, record.musicbrainz_id))
|
||||||
|> assign_embedding_text()
|
|> assign_embedding_text()
|
||||||
|> assign_similar_records()}
|
|> assign_similar_records()}
|
||||||
end
|
end
|
||||||
@@ -511,6 +513,11 @@ defmodule MusicLibraryWeb.CollectionLive.Show do
|
|||||||
|> assign_embedding_text()}
|
|> assign_embedding_text()}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def handle_info({MusicLibraryWeb.Components.Chat, :chats_changed}, socket) do
|
||||||
|
{:noreply,
|
||||||
|
assign(socket, :chat_count, Chats.count_chats(:record, socket.assigns.record.musicbrainz_id))}
|
||||||
|
end
|
||||||
|
|
||||||
@impl true
|
@impl true
|
||||||
def handle_info({:update, record}, socket) do
|
def handle_info({:update, record}, socket) do
|
||||||
{:noreply,
|
{:noreply,
|
||||||
|
|||||||
@@ -16,6 +16,7 @@ defmodule MusicLibraryWeb.WishlistLive.Show do
|
|||||||
release_summary: 1
|
release_summary: 1
|
||||||
]
|
]
|
||||||
|
|
||||||
|
alias MusicLibrary.Chats
|
||||||
alias MusicLibrary.OnlineStoreTemplates
|
alias MusicLibrary.OnlineStoreTemplates
|
||||||
alias MusicLibrary.{Records, RecordSets}
|
alias MusicLibrary.{Records, RecordSets}
|
||||||
alias MusicLibrary.Records.Similarity
|
alias MusicLibrary.Records.Similarity
|
||||||
@@ -51,6 +52,7 @@ defmodule MusicLibraryWeb.WishlistLive.Show do
|
|||||||
aria-hidden="true"
|
aria-hidden="true"
|
||||||
data-slot="icon"
|
data-slot="icon"
|
||||||
/>
|
/>
|
||||||
|
<span :if={@chat_count > 0} class="text-xs font-medium">{@chat_count}</span>
|
||||||
</.button>
|
</.button>
|
||||||
<.dropdown id={"actions-#{@record.id}"} placement="bottom-end">
|
<.dropdown id={"actions-#{@record.id}"} placement="bottom-end">
|
||||||
<:toggle>
|
<:toggle>
|
||||||
@@ -304,6 +306,7 @@ defmodule MusicLibraryWeb.WishlistLive.Show do
|
|||||||
|> assign(:record, record)
|
|> assign(:record, record)
|
||||||
|> assign(:online_store_templates, online_store_templates)
|
|> assign(:online_store_templates, online_store_templates)
|
||||||
|> assign(:record_sets, record_sets)
|
|> assign(:record_sets, record_sets)
|
||||||
|
|> assign(:chat_count, Chats.count_chats(:record, record.musicbrainz_id))
|
||||||
|> assign_embedding_text()}
|
|> assign_embedding_text()}
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -418,6 +421,11 @@ defmodule MusicLibraryWeb.WishlistLive.Show do
|
|||||||
|> assign_embedding_text()}
|
|> assign_embedding_text()}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def handle_info({MusicLibraryWeb.Components.Chat, :chats_changed}, socket) do
|
||||||
|
{:noreply,
|
||||||
|
assign(socket, :chat_count, Chats.count_chats(:record, socket.assigns.record.musicbrainz_id))}
|
||||||
|
end
|
||||||
|
|
||||||
@impl true
|
@impl true
|
||||||
def handle_info({:update, record}, socket) do
|
def handle_info({:update, record}, socket) do
|
||||||
{:noreply,
|
{:noreply,
|
||||||
|
|||||||
@@ -16,6 +16,35 @@ defmodule MusicLibrary.ChatsTest do
|
|||||||
%{chat: chat}
|
%{chat: chat}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
describe "count_chats/2" do
|
||||||
|
test "returns 0 when no chats exist" do
|
||||||
|
assert Chats.count_chats(:record, Ecto.UUID.generate()) == 0
|
||||||
|
end
|
||||||
|
|
||||||
|
test "returns the number of chats for an entity" do
|
||||||
|
Chats.create_chat_with_message(
|
||||||
|
%{entity: :record, musicbrainz_id: @musicbrainz_id},
|
||||||
|
%{role: "user", content: "First chat"}
|
||||||
|
)
|
||||||
|
|
||||||
|
Chats.create_chat_with_message(
|
||||||
|
%{entity: :record, musicbrainz_id: @musicbrainz_id},
|
||||||
|
%{role: "user", content: "Second chat"}
|
||||||
|
)
|
||||||
|
|
||||||
|
assert Chats.count_chats(:record, @musicbrainz_id) == 2
|
||||||
|
end
|
||||||
|
|
||||||
|
test "does not count chats for different entity" do
|
||||||
|
Chats.create_chat_with_message(
|
||||||
|
%{entity: :record, musicbrainz_id: @musicbrainz_id},
|
||||||
|
%{role: "user", content: "A chat"}
|
||||||
|
)
|
||||||
|
|
||||||
|
assert Chats.count_chats(:artist, @musicbrainz_id) == 0
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
describe "list_chats/2" do
|
describe "list_chats/2" do
|
||||||
setup [:create_chat]
|
setup [:create_chat]
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user