live_beats/lib/live_beats_web/live/profile_live.ex

347 lines
9.9 KiB
Elixir
Raw Normal View History

2021-11-22 14:57:24 +00:00
defmodule LiveBeatsWeb.ProfileLive do
2021-10-29 16:12:23 +00:00
use LiveBeatsWeb, :live_view
2021-11-12 03:42:10 +00:00
alias LiveBeats.{Accounts, MediaLibrary, MP3Stat}
2021-11-16 16:58:22 +00:00
alias LiveBeatsWeb.{LayoutComponent, Presence}
2021-11-22 14:57:24 +00:00
alias LiveBeatsWeb.ProfileLive.{SongRowComponent, UploadFormComponent}
2021-10-29 16:12:23 +00:00
2022-01-31 19:27:06 +00:00
@max_presences 20
2021-10-29 16:12:23 +00:00
def render(assigns) do
~H"""
<.title_bar>
2021-11-16 16:58:22 +00:00
<div>
<div class="block">
2022-08-03 13:40:11 +00:00
<%= @profile.tagline %>
<%= if @owns_profile? do %>
(you)
<% end %>
2021-11-16 16:58:22 +00:00
</div>
<.link href={@profile.external_homepage_url} target="_blank" class="text-sm text-gray-600">
2022-08-03 13:40:11 +00:00
<.icon name={:code} /> <span class=""><%= url_text(@profile.external_homepage_url) %></span>
2021-11-16 16:58:22 +00:00
</.link>
</div>
2021-10-29 16:12:23 +00:00
<:actions>
2021-11-12 03:42:10 +00:00
<%= if @active_profile_id == @profile.user_id do %>
2022-08-03 13:40:11 +00:00
<.button
primary
phx-click={
JS.push("switch_profile", value: %{user_id: nil}, target: "#player", loading: "#player")
}
2021-11-12 03:42:10 +00:00
>
2022-08-03 13:40:11 +00:00
<.icon name={:stop} /><span class="ml-2">Stop Listening</span>
2021-11-12 03:42:10 +00:00
</.button>
<% else %>
2022-08-03 13:40:11 +00:00
<.button
primary
phx-click={
JS.push("switch_profile",
value: %{user_id: @profile.user_id},
target: "#player",
loading: "#player"
)
}
2021-11-12 03:42:10 +00:00
>
2022-08-03 13:40:11 +00:00
<.icon name={:play} /><span class="ml-2">Listen</span>
2021-11-12 03:42:10 +00:00
</.button>
<% end %>
<%= if @owns_profile? do %>
2021-12-14 20:19:03 +00:00
<.button id="upload-btn" primary patch={profile_path(@current_user, :new)}>
2022-08-03 13:40:11 +00:00
<.icon name={:upload} /><span class="ml-2">Upload Songs</span>
2021-11-12 03:42:10 +00:00
</.button>
<% end %>
2021-10-29 16:12:23 +00:00
</:actions>
</.title_bar>
2022-01-31 19:27:06 +00:00
<Presence.listening_now
2022-01-31 13:21:27 +00:00
presences={@presences}
2022-01-31 19:27:06 +00:00
presence_ids={@presence_ids}
total_count={@presences_count}
/>
2021-11-16 16:58:22 +00:00
2021-12-16 02:51:09 +00:00
<div id="dialogs" phx-update="append">
<%= for song <- if(@owns_profile?, do: @songs, else: []), id = "delete-modal-#{song.id}" do %>
<.modal
id={id}
on_confirm={
JS.push("delete", value: %{id: song.id})
|> hide_modal(id)
|> focus_closest("#song-#{song.id}")
2022-02-07 17:03:35 +00:00
|> hide("#song-#{song.id}")
}
on_cancel={focus("##{id}", "#delete-song-#{song.id}")}
2021-12-16 02:51:09 +00:00
>
Are you sure you want to delete "<%= song.title %>"?
<:cancel>Cancel</:cancel>
<:confirm>Delete</:confirm>
</.modal>
<% end %>
</div>
2021-10-29 16:12:23 +00:00
2021-11-05 00:49:19 +00:00
<.live_table
2021-12-16 02:51:09 +00:00
id="songs"
2021-11-05 19:57:33 +00:00
module={SongRowComponent}
2021-11-05 00:49:19 +00:00
rows={@songs}
row_id={fn song -> "song-#{song.id}" end}
2021-11-30 15:03:08 +00:00
owns_profile?={@owns_profile?}
2021-11-05 00:49:19 +00:00
>
2022-08-03 13:40:11 +00:00
<:col :let={%{song: song}} label="Title"><%= song.title %></:col>
<:col :let={%{song: song}} label="Artist"><%= song.artist %></:col>
<:col
:let={%{song: song}}
label="Attribution"
class="max-w-5xl break-words text-gray-600 font-light"
>
<%= song.attribution %>
</:col>
<:col :let={%{song: song}} label="Duration"><%= MP3Stat.to_mmss(song.duration) %></:col>
2022-11-17 15:01:20 +00:00
<:col :let={%{song: song}} label="" :if={@owns_profile?}>
<.link
id={"delete-song-#{song.id}"}
phx-click={show_modal("delete-modal-#{song.id}")}
class="inline-flex items-center px-3 py-2 text-sm leading-4 font-medium"
>
2022-08-03 13:40:11 +00:00
<.icon name={:trash} class="-ml-0.5 mr-2 h-4 w-4" /> Delete
2021-11-05 00:49:19 +00:00
</.link>
2021-10-29 16:12:23 +00:00
</:col>
2021-11-05 00:49:19 +00:00
</.live_table>
2021-10-29 16:12:23 +00:00
"""
end
2021-11-12 03:42:10 +00:00
def mount(%{"profile_username" => profile_username}, _session, socket) do
2022-11-17 15:01:20 +00:00
IO.inspect(get_connect_params(socket))
2021-11-05 19:57:33 +00:00
%{current_user: current_user} = socket.assigns
2021-11-12 03:42:10 +00:00
profile =
Accounts.get_user_by!(username: profile_username)
|> MediaLibrary.get_profile!()
2021-11-05 00:49:19 +00:00
if connected?(socket) do
MediaLibrary.subscribe_to_profile(profile)
2021-11-12 03:42:10 +00:00
Accounts.subscribe(current_user.id)
2022-04-06 16:20:23 +00:00
Presence.subscribe(profile)
2021-11-05 00:49:19 +00:00
end
2021-11-05 12:57:48 +00:00
2021-11-12 03:42:10 +00:00
active_song_id =
if song = MediaLibrary.get_current_active_song(profile) do
2021-12-16 03:03:18 +00:00
SongRowComponent.send_status(song.id, song.status)
2021-11-05 19:57:33 +00:00
song.id
end
2021-11-12 03:42:10 +00:00
socket =
socket
|> assign(
active_song_id: active_song_id,
active_profile_id: current_user.active_profile_user_id,
profile: profile,
owns_profile?: MediaLibrary.owns_profile?(current_user, profile)
)
|> list_songs()
2021-11-16 16:58:22 +00:00
|> assign_presences()
2021-11-12 03:42:10 +00:00
2022-01-31 19:27:06 +00:00
{:ok, socket, temporary_assigns: [songs: [], presences: %{}]}
2021-10-29 16:12:23 +00:00
end
def handle_params(params, _url, socket) do
2021-11-18 16:50:15 +00:00
LayoutComponent.hide_modal()
{:noreply, socket |> apply_action(socket.assigns.live_action, params)}
2021-11-05 00:49:19 +00:00
end
2021-11-05 19:57:33 +00:00
def handle_event("play_or_pause", %{"id" => id}, socket) do
song = MediaLibrary.get_song!(id)
2021-11-12 03:42:10 +00:00
can_playback? = MediaLibrary.can_control_playback?(socket.assigns.current_user, song)
cond do
can_playback? and socket.assigns.active_song_id == id and MediaLibrary.playing?(song) ->
MediaLibrary.pause_song(song)
can_playback? ->
MediaLibrary.play_song(id)
true ->
:noop
2021-11-05 19:57:33 +00:00
end
2021-11-05 00:49:19 +00:00
{:noreply, socket}
end
def handle_event("delete", %{"id" => id}, socket) do
song = MediaLibrary.get_song!(id)
2021-11-12 11:42:07 +00:00
2021-11-12 03:42:10 +00:00
if song.user_id == socket.assigns.current_user.id do
2021-12-14 20:29:07 +00:00
:ok = MediaLibrary.delete_song(song)
2021-11-12 03:42:10 +00:00
end
2021-11-12 11:42:07 +00:00
2021-11-05 00:49:19 +00:00
{:noreply, socket}
end
2022-04-06 16:20:23 +00:00
def handle_info({LiveBeatsWeb.Presence, %{user_joined: presence}}, socket) do
2022-01-31 19:27:06 +00:00
{:noreply, assign_presence(socket, presence)}
end
2022-04-06 16:20:23 +00:00
def handle_info({LiveBeatsWeb.Presence, %{user_left: presence}}, socket) do
%{user: user} = presence
2022-01-31 13:21:27 +00:00
if presence.metas == [] do
2022-01-31 19:27:06 +00:00
{:noreply, remove_presence(socket, user)}
else
{:noreply, socket}
end
end
def handle_info({Accounts, %Accounts.Events.ActiveProfileChanged{} = event}, socket) do
{:noreply, assign(socket, active_profile_id: event.new_profile_user_id)}
2021-11-12 03:42:10 +00:00
end
def handle_info({MediaLibrary, %MediaLibrary.Events.PublicProfileUpdated{} = update}, socket) do
{:noreply,
socket
|> assign(profile: update.profile)
|> push_patch(to: profile_path(update.profile))}
end
def handle_info({MediaLibrary, %MediaLibrary.Events.Play{song: song}}, socket) do
2021-11-05 00:49:19 +00:00
{:noreply, play_song(socket, song)}
end
def handle_info({MediaLibrary, %MediaLibrary.Events.Pause{song: song}}, socket) do
2021-11-05 00:49:19 +00:00
{:noreply, pause_song(socket, song.id)}
end
2021-12-16 02:51:09 +00:00
def handle_info({MediaLibrary, %MediaLibrary.Events.SongsImported{songs: songs}}, socket) do
{:noreply, update(socket, :songs, &(&1 ++ songs))}
end
2022-01-31 13:21:27 +00:00
def handle_info({MediaLibrary, {:ping, ping}}, socket) do
%{user: user, rtt: rtt, region: region} = ping
2022-01-31 19:27:06 +00:00
send_update(Presence.BadgeComponent,
id: user.id,
2022-01-31 13:21:27 +00:00
action: {:ping, %{user: user, ping: rtt, region: region}}
)
2022-01-29 01:40:48 +00:00
{:noreply, socket}
end
def handle_info({MediaLibrary, _}, socket), do: {:noreply, socket}
def handle_info({Accounts, _}, socket), do: {:noreply, socket}
2021-11-05 19:57:33 +00:00
defp stop_song(socket, song_id) do
2021-12-16 03:03:18 +00:00
SongRowComponent.send_status(song_id, :stopped)
2021-11-05 19:57:33 +00:00
2021-11-12 03:42:10 +00:00
if socket.assigns.active_song_id == song_id do
assign(socket, :active_song_id, nil)
2021-11-05 19:57:33 +00:00
else
socket
end
end
2021-11-05 00:49:19 +00:00
defp pause_song(socket, song_id) do
2021-12-16 03:03:18 +00:00
SongRowComponent.send_status(song_id, :paused)
2021-11-05 00:49:19 +00:00
socket
end
2021-11-05 19:57:33 +00:00
defp play_song(socket, %MediaLibrary.Song{} = song) do
2021-11-12 03:42:10 +00:00
%{active_song_id: active_song_id} = socket.assigns
2021-11-05 00:49:19 +00:00
2021-11-05 19:57:33 +00:00
cond do
2021-11-12 03:42:10 +00:00
active_song_id == song.id ->
2021-12-16 03:03:18 +00:00
SongRowComponent.send_status(song.id, :playing)
2021-11-05 19:57:33 +00:00
socket
2021-11-12 03:42:10 +00:00
active_song_id ->
2021-12-16 03:03:18 +00:00
SongRowComponent.send_status(song.id, :playing)
2021-11-05 19:57:33 +00:00
socket
2021-11-12 03:42:10 +00:00
|> stop_song(active_song_id)
|> assign(active_song_id: song.id)
2021-11-05 19:57:33 +00:00
true ->
2021-12-16 03:03:18 +00:00
SongRowComponent.send_status(song.id, :playing)
2021-11-12 03:42:10 +00:00
assign(socket, active_song_id: song.id)
2021-11-05 00:49:19 +00:00
end
end
2021-11-18 16:50:15 +00:00
defp apply_action(socket, :new, _params) do
if socket.assigns.owns_profile? do
socket
|> assign(:page_title, "Add Music")
2021-11-18 16:50:15 +00:00
|> assign(:song, %MediaLibrary.Song{})
|> show_upload_modal()
2021-11-05 00:49:19 +00:00
else
2021-11-18 16:50:15 +00:00
socket
|> put_flash(:error, "You can't do that")
|> redirect(to: profile_path(socket.assigns.current_user))
2021-11-05 00:49:19 +00:00
end
2021-10-29 16:12:23 +00:00
end
2021-12-14 15:35:51 +00:00
defp apply_action(socket, :show, _params) do
2021-10-29 16:12:23 +00:00
socket
|> assign(:page_title, "Listing Songs")
|> assign(:song, nil)
end
2021-11-18 16:50:15 +00:00
defp show_upload_modal(socket) do
LayoutComponent.show_modal(UploadFormComponent, %{
id: :new,
confirm: {"Save", type: "submit", form: "song-form"},
2021-11-23 14:34:34 +00:00
patch: profile_path(socket.assigns.current_user),
2021-11-18 16:50:15 +00:00
song: socket.assigns.song,
title: socket.assigns.page_title,
current_user: socket.assigns.current_user
})
socket
end
2021-11-12 03:42:10 +00:00
defp list_songs(socket) do
assign(socket, songs: MediaLibrary.list_profile_songs(socket.assigns.profile, 50))
2021-10-29 16:12:23 +00:00
end
2021-11-16 16:58:22 +00:00
defp assign_presences(socket) do
2022-01-31 19:27:06 +00:00
socket = assign(socket, presences_count: 0, presences: %{}, presence_ids: %{})
2022-01-31 19:27:06 +00:00
if profile = connected?(socket) && socket.assigns.profile do
profile
2022-04-06 16:20:23 +00:00
|> LiveBeatsWeb.Presence.list_profile_users()
2022-01-31 19:27:06 +00:00
|> Enum.reduce(socket, fn {_, presence}, acc -> assign_presence(acc, presence) end)
2022-01-12 17:27:30 +00:00
else
2022-01-31 19:27:06 +00:00
socket
end
end
defp assign_presence(socket, presence) do
%{user: user} = presence
%{presence_ids: presence_ids} = socket.assigns
cond do
Map.has_key?(presence_ids, user.id) ->
socket
Enum.count(presence_ids) < @max_presences ->
socket
|> update(:presences, &Map.put(&1, user.id, user))
|> update(:presence_ids, &Map.put(&1, user.id, System.system_time()))
|> update(:presences_count, &(&1 + 1))
true ->
update(socket, :presences_count, &(&1 + 1))
2022-01-12 17:27:30 +00:00
end
2021-11-16 16:58:22 +00:00
end
2022-01-31 19:27:06 +00:00
defp remove_presence(socket, user) do
socket
|> update(:presences, &Map.delete(&1, user.id))
|> update(:presence_ids, &Map.delete(&1, user.id))
|> update(:presences_count, &(&1 - 1))
end
2021-11-16 16:58:22 +00:00
defp url_text(nil), do: ""
2021-11-18 16:50:15 +00:00
2021-11-16 16:58:22 +00:00
defp url_text(url_str) do
uri = URI.parse(url_str)
uri.host <> uri.path
end
2021-10-29 16:12:23 +00:00
end