Skip to content

Commit

Permalink
feat(common): add users' name to online status
Browse files Browse the repository at this point in the history
  • Loading branch information
trueChazza committed Jan 27, 2022
1 parent 77cf0cd commit 4924e2b
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 8 deletions.
4 changes: 2 additions & 2 deletions assets/js/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import topbar from "../vendor/topbar"
let socket = new Socket("/socket", {})
socket.connect()

let channel = socket.channel("room:lobby", { user_id: window.userId, user_email: window.userEmail, page_title: document.title })
let channel = socket.channel("room:lobby", { user_id: window.userId, user_name: window.userName, page_title: document.title })
let presence = new Presence(channel)

let csrfToken = document.querySelector("meta[name='csrf-token']").getAttribute("content")
Expand Down Expand Up @@ -57,7 +57,7 @@ presence.onSync(() => {
<span class="bg-green-400 absolute top-0 right-0 block h-2.5 w-2.5 rounded-full ring-2 ring-white" aria-hidden="true"></span>
</span>
<div class="ml-4 truncate">
<p class="text-sm font-medium text-gray-900 truncate">${ item.user_email }</p>
<p class="text-sm font-medium text-gray-900 truncate">${ item.user_name }</p>
<p id="current-location-${ item.user_id }" class="text-sm text-gray-500 truncate">${ item.page_title }</p>
</div>
</div>
Expand Down
6 changes: 3 additions & 3 deletions lib/media_server_web/channels/room_channel.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ defmodule MediaServerWeb.RoomChannel do
alias MediaServerWeb.Presence

@impl true
def join("room:lobby", %{"user_id" => user_id, "user_email" => user_email, "page_title" => page_title}, socket) do
def join("room:lobby", %{"user_id" => user_id, "user_name" => user_name, "page_title" => page_title}, socket) do
send(self(), :after_join)

{:ok, assign(socket, %{user_id: user_id, user_email: user_email, page_title: page_title})}
{:ok, assign(socket, %{user_id: user_id, user_name: user_name, page_title: page_title})}
end

@impl true
Expand All @@ -15,7 +15,7 @@ defmodule MediaServerWeb.RoomChannel do
Presence.track(socket, socket.assigns.user_id, %{
online_at: inspect(System.system_time(:second)),
user_id: socket.assigns.user_id,
user_email: socket.assigns.user_email,
user_name: socket.assigns.user_name,
page_title: socket.assigns.page_title,
})

Expand Down
3 changes: 2 additions & 1 deletion lib/media_server_web/templates/layout/root.html.heex
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@
<script>
window.userId = "<%= assigns[:current_user].id %>";
window.userEmail = "<%= assigns[:current_user].email %>";
window.userName = "<%= assigns[:current_user].name %>";
</script>
<script defer src="https://unpkg.com/[email protected]/dist/cdn.min.js"></script>
<script defer phx-track-static type="text/javascript" src={Routes.static_path(@conn, "/assets/app.js?id=7")}></script>
<script defer phx-track-static type="text/javascript" src={Routes.static_path(@conn, "/assets/app.js?id=8")}></script>
<% end %>
</head>
<body>
Expand Down
3 changes: 2 additions & 1 deletion lib/media_server_web/templates/watch/watch.html.heex
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@
<script>
window.userId = "<%= assigns[:current_user].id %>";
window.userEmail = "<%= assigns[:current_user].email %>";
window.userName = "<%= assigns[:current_user].name %>";
</script>
<script defer phx-track-static type="text/javascript" src={Routes.static_path(@conn, "/assets/app.js?id=7")}></script>
<script defer phx-track-static type="text/javascript" src={Routes.static_path(@conn, "/assets/app.js?id=8")}></script>
<% end %>
</head>
<body>
Expand Down
2 changes: 1 addition & 1 deletion test/media_server_web/channels/room_channel_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ defmodule MediaServerWeb.RoomChannelTest do
{:ok, _, socket} =
MediaServerWeb.UserSocket
|> socket("user_id", %{some: :assign})
|> subscribe_and_join(MediaServerWeb.RoomChannel, "room:lobby", %{user_id: 1, user_email: :user_email, page_title: :page_title})
|> subscribe_and_join(MediaServerWeb.RoomChannel, "room:lobby", %{user_id: 1, user_name: :user_name, page_title: :page_title})

%{socket: socket}
end
Expand Down

0 comments on commit 4924e2b

Please sign in to comment.