Update for latest phoenix

This commit is contained in:
Chris McCord 2022-04-06 12:20:23 -04:00
parent 694daec4da
commit f1908151f9
6 changed files with 65 additions and 87 deletions

View file

@ -1,70 +0,0 @@
defmodule LiveBeats.PresenceClient do
@presence LiveBeatsWeb.Presence
@pubsub LiveBeats.PubSub
alias LiveBeats.MediaLibrary
def track(%MediaLibrary.Profile{} = profile, current_user_id) do
@presence.track(
self(),
"proxy:" <> topic(profile),
current_user_id,
%{}
)
end
def untrack(%MediaLibrary.Profile{} = profile, current_user_id) do
@presence.untrack(
self(),
"proxy:" <> topic(profile),
current_user_id
)
end
def subscribe(%MediaLibrary.Profile{} = profile) do
Phoenix.PubSub.subscribe(@pubsub, topic(profile))
end
def list(%MediaLibrary.Profile{} = profile) do
list("proxy:" <> topic(profile))
end
def list(topic) do
@presence.list(topic)
end
def init(_opts) do
# user-land state
{:ok, %{}}
end
def handle_metas(topic, %{joins: joins, leaves: leaves}, presences, state) do
for {user_id, presence} <- joins do
user_data = %{user: presence.user, metas: Map.fetch!(presences, user_id)}
local_broadcast(topic, {LiveBeats.PresenceClient, %{user_joined: user_data}})
end
for {user_id, presence} <- leaves do
metas =
case Map.fetch(presences, user_id) do
{:ok, presence_metas} -> presence_metas
:error -> []
end
user_data = %{user: presence.user, metas: metas}
local_broadcast(topic, {LiveBeats.PresenceClient, %{user_left: user_data}})
end
{:ok, state}
end
defp topic(%MediaLibrary.Profile{} = profile) do
"active_profiles:#{profile.user_id}"
end
defp local_broadcast("proxy:" <> topic, payload) do
Phoenix.PubSub.local_broadcast(@pubsub, topic, payload)
end
end

View file

@ -10,6 +10,7 @@ defmodule LiveBeatsWeb.Presence do
pubsub_server: LiveBeats.PubSub,
presence: __MODULE__
@pubsub LiveBeats.PubSub
import Phoenix.LiveView.Helpers
import LiveBeatsWeb.LiveHelpers
@ -17,16 +18,54 @@ defmodule LiveBeatsWeb.Presence do
alias LiveBeats.{Accounts, MediaLibrary}
alias LiveBeatsWeb.Presence.BadgeComponent
def init(state) do
LiveBeats.PresenceClient.init(state)
def track_profile_user(%MediaLibrary.Profile{} = profile, current_user_id) do
track(
self(),
"proxy:" <> topic(profile),
current_user_id,
%{}
)
end
def handle_metas(topic, presences_diff, presences, state) do
LiveBeats.PresenceClient.handle_metas(topic, presences_diff, presences, state)
def untrack_profile_user(%MediaLibrary.Profile{} = profile, current_user_id) do
untrack(
self(),
"proxy:" <> topic(profile),
current_user_id
)
end
def init(_opts) do
{:ok, %{}}
end
def handle_metas(topic, %{joins: joins, leaves: leaves}, presences, state) do
for {user_id, presence} <- joins do
user_data = %{user: presence.user, metas: Map.fetch!(presences, user_id)}
local_broadcast(topic, {__MODULE__, %{user_joined: user_data}})
end
for {user_id, presence} <- leaves do
metas =
case Map.fetch(presences, user_id) do
{:ok, presence_metas} -> presence_metas
:error -> []
end
user_data = %{user: presence.user, metas: metas}
local_broadcast(topic, {__MODULE__, %{user_left: user_data}})
end
{:ok, state}
end
def list_profile_users(%MediaLibrary.Profile{} = profile) do
list("proxy:" <> topic(profile))
end
def subscribe(%MediaLibrary.Profile{} = profile) do
LiveBeats.PresenceClient.subscribe(profile)
Phoenix.PubSub.subscribe(@pubsub, topic(profile))
end
def fetch(_topic, presences) do
@ -73,6 +112,14 @@ defmodule LiveBeatsWeb.Presence do
</div>
"""
end
defp local_broadcast("proxy:" <> topic, payload) do
Phoenix.PubSub.local_broadcast(@pubsub, topic, payload)
end
defp topic(%MediaLibrary.Profile{} = profile) do
"active_profiles:#{profile.user_id}"
end
end
defmodule LiveBeatsWeb.Presence.BadgeComponent do

View file

@ -3,6 +3,7 @@ defmodule LiveBeatsWeb.PlayerLive do
alias LiveBeats.{Accounts, MediaLibrary}
alias LiveBeats.MediaLibrary.Song
alias LiveBeatsWeb.Presence
on_mount {LiveBeatsWeb.UserAuth, :current_user}
@ -141,7 +142,7 @@ defmodule LiveBeatsWeb.PlayerLive do
current_user = Accounts.update_active_profile(socket.assigns.current_user, nil)
if profile = connected?(socket) and socket.assigns.profile do
LiveBeats.PresenceClient.untrack(profile, current_user.id)
Presence.untrack_profile_user(profile, current_user.id)
end
socket
@ -157,9 +158,9 @@ defmodule LiveBeatsWeb.PlayerLive do
current_user = Accounts.update_active_profile(current_user, profile.user_id)
#untrack last profile the user was listening
if socket.assigns.profile do
LiveBeats.PresenceClient.untrack(socket.assigns.profile, current_user.id)
Presence.untrack_profile_user(socket.assigns.profile, current_user.id)
end
LiveBeats.PresenceClient.track(profile, current_user.id)
Presence.track_profile_user(profile, current_user.id)
send(self(), :play_current)
socket
@ -256,8 +257,8 @@ defmodule LiveBeatsWeb.PlayerLive do
%{current_user: current_user} = socket.assigns
if update.profile.user_id == socket.assigns.current_user.id do
LiveBeats.PresenceClient.untrack(socket.assigns.profile, current_user.id)
LiveBeats.PresenceClient.track(update.profile, current_user.id)
Presence.untrack_profile_user(socket.assigns.profile, current_user.id)
Presence.track_profile_user(update.profile, current_user.id)
end
{:noreply, assign_profile(socket, update.profile)}

View file

@ -101,7 +101,7 @@ defmodule LiveBeatsWeb.ProfileLive do
if connected?(socket) do
MediaLibrary.subscribe_to_profile(profile)
Accounts.subscribe(current_user.id)
LiveBeatsWeb.Presence.subscribe(profile)
Presence.subscribe(profile)
end
active_song_id =
@ -157,11 +157,11 @@ defmodule LiveBeatsWeb.ProfileLive do
{:noreply, socket}
end
def handle_info({LiveBeats.PresenceClient, %{user_joined: presence}}, socket) do
def handle_info({LiveBeatsWeb.Presence, %{user_joined: presence}}, socket) do
{:noreply, assign_presence(socket, presence)}
end
def handle_info({LiveBeats.PresenceClient, %{user_left: presence}}, socket) do
def handle_info({LiveBeatsWeb.Presence, %{user_left: presence}}, socket) do
%{user: user} = presence
if presence.metas == [] do
@ -286,7 +286,7 @@ defmodule LiveBeatsWeb.ProfileLive do
if profile = connected?(socket) && socket.assigns.profile do
profile
|> LiveBeats.PresenceClient.list()
|> LiveBeatsWeb.Presence.list_profile_users()
|> Enum.reduce(socket, fn {_, presence}, acc -> assign_presence(acc, presence) end)
else
socket

View file

@ -33,7 +33,7 @@ defmodule LiveBeats.MixProject do
# Type `mix help deps` for examples and options.
defp deps do
[
{:phoenix, github: "bemesa21/phoenix", branch: "presence_client", override: true},
{:phoenix, github: "phoenixframework/phoenix", override: true},
{:phoenix_ecto, "~> 4.4"},
{:ecto_sql, "~> 3.6"},
{:ecto_network, "~> 1.3.0"},

View file

@ -22,13 +22,13 @@
"libcluster": {:hex, :libcluster, "3.3.1", "e7a4875cd1290cee7a693d6bd46076863e9e433708b01339783de6eff5b7f0aa", [:mix], [{:jason, "~> 1.1", [hex: :jason, repo: "hexpm", optional: false]}], "hexpm", "b575ca63c1cd84e01f3fa0fc45e6eb945c1ee7ae8d441d33def999075e9e5398"},
"mime": {:hex, :mime, "2.0.2", "0b9e1a4c840eafb68d820b0e2158ef5c49385d17fb36855ac6e7e087d4b1dcc5", [:mix], [], "hexpm", "e6a3f76b4c277739e36c2e21a2c640778ba4c3846189d5ab19f97f126df5f9b7"},
"mint": {:hex, :mint, "1.3.0", "396b3301102f7b775e103da5a20494b25753aed818d6d6f0ad222a3a018c3600", [:mix], [{:castore, "~> 0.1.0", [hex: :castore, repo: "hexpm", optional: true]}], "hexpm", "a9aac960562e43ca69a77e5176576abfa78b8398cec5543dd4fb4ab0131d5c1e"},
"phoenix": {:git, "https://github.com/bemesa21/phoenix.git", "0d34e030ca29cc4eae621ece1f3417e7c04eda76", [branch: "presence_client"]},
"phoenix": {:git, "https://github.com/phoenixframework/phoenix.git", "ae2fb6bddd64fc7e3908ee6dd632f608254f532e", []},
"phoenix_ecto": {:hex, :phoenix_ecto, "4.4.0", "0672ed4e4808b3fbed494dded89958e22fb882de47a97634c0b13e7b0b5f7720", [:mix], [{:ecto, "~> 3.3", [hex: :ecto, repo: "hexpm", optional: false]}, {:phoenix_html, "~> 2.14.2 or ~> 3.0", [hex: :phoenix_html, repo: "hexpm", optional: true]}, {:plug, "~> 1.9", [hex: :plug, repo: "hexpm", optional: false]}], "hexpm", "09864e558ed31ee00bd48fcc1d4fc58ae9678c9e81649075431e69dbabb43cc1"},
"phoenix_html": {:hex, :phoenix_html, "3.2.0", "1c1219d4b6cb22ac72f12f73dc5fad6c7563104d083f711c3fcd8551a1f4ae11", [:mix], [{:plug, "~> 1.5", [hex: :plug, repo: "hexpm", optional: true]}], "hexpm", "36ec97ba56d25c0136ef1992c37957e4246b649d620958a1f9fa86165f8bc54f"},
"phoenix_live_dashboard": {:hex, :phoenix_live_dashboard, "0.6.2", "0769470265eb13af01b5001b29cb935f4710d6adaa1ffc18417a570a337a2f0f", [:mix], [{:ecto, "~> 3.6.2 or ~> 3.7", [hex: :ecto, repo: "hexpm", optional: true]}, {:ecto_mysql_extras, "~> 0.3", [hex: :ecto_mysql_extras, repo: "hexpm", optional: true]}, {:ecto_psql_extras, "~> 0.7", [hex: :ecto_psql_extras, repo: "hexpm", optional: true]}, {:mime, "~> 1.6 or ~> 2.0", [hex: :mime, repo: "hexpm", optional: false]}, {:phoenix_live_view, "~> 0.17.1", [hex: :phoenix_live_view, repo: "hexpm", optional: false]}, {:telemetry_metrics, "~> 0.6.0", [hex: :telemetry_metrics, repo: "hexpm", optional: false]}], "hexpm", "5bc6c6b38a2ca8b5020b442322fcee6afd5e641637a0b1fb059d4bd89bc58e7b"},
"phoenix_live_reload": {:hex, :phoenix_live_reload, "1.3.3", "3a53772a6118d5679bf50fc1670505a290e32a1d195df9e069d8c53ab040c054", [:mix], [{:file_system, "~> 0.2.1 or ~> 0.3", [hex: :file_system, repo: "hexpm", optional: false]}, {:phoenix, "~> 1.4", [hex: :phoenix, repo: "hexpm", optional: false]}], "hexpm", "766796676e5f558dbae5d1bdb066849673e956005e3730dfd5affd7a6da4abac"},
"phoenix_live_view": {:git, "https://github.com/phoenixframework/phoenix_live_view.git", "1ae64fb69b0951f2f67f01d369ec9a4006060001", []},
"phoenix_pubsub": {:git, "https://github.com/phoenixframework/phoenix_pubsub.git", "3a286c69bdd6df5a5854cb0ad12ee5a1aa6087bd", []},
"phoenix_pubsub": {:hex, :phoenix_pubsub, "2.1.1", "ba04e489ef03763bf28a17eb2eaddc2c20c6d217e2150a61e3298b0f4c2012b5", [:mix], [], "hexpm", "81367c6d1eea5878ad726be80808eb5a787a23dee699f96e72b1109c57cdd8d9"},
"phoenix_view": {:hex, :phoenix_view, "1.0.0", "fea71ecaaed71178b26dd65c401607de5ec22e2e9ef141389c721b3f3d4d8011", [:mix], [{:phoenix_html, "~> 2.14.2 or ~> 3.0", [hex: :phoenix_html, repo: "hexpm", optional: true]}], "hexpm", "82be3e2516f5633220246e2e58181282c71640dab7afc04f70ad94253025db0c"},
"plug": {:hex, :plug, "1.13.4", "addb6e125347226e3b11489e23d22a60f7ab74786befb86c14f94fb5f23ca9a4", [:mix], [{:mime, "~> 1.0 or ~> 2.0", [hex: :mime, repo: "hexpm", optional: false]}, {:plug_crypto, "~> 1.1.1 or ~> 1.2", [hex: :plug_crypto, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4.3 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "06114c1f2a334212fe3ae567dbb3b1d29fd492c1a09783d52f3d489c1a6f4cf2"},
"plug_cowboy": {:hex, :plug_cowboy, "2.5.2", "62894ccd601cf9597e2c23911ff12798a8a18d237e9739f58a6b04e4988899fe", [:mix], [{:cowboy, "~> 2.7", [hex: :cowboy, repo: "hexpm", optional: false]}, {:cowboy_telemetry, "~> 0.3", [hex: :cowboy_telemetry, repo: "hexpm", optional: false]}, {:plug, "~> 1.7", [hex: :plug, repo: "hexpm", optional: false]}], "hexpm", "ea6e87f774c8608d60c8d34022a7d073bd7680a0a013f049fc62bf35efea1044"},