Skip to content

Commit 5b09965

Browse files
committed
Record when admin views a chatroom and send more events.
We expect "previousRoom" and "nextRoom" in the payload when a user joins a room topic. We use this to record a timestamp of when admin has viewed both rooms. This means we record when an admin both leaves a previous room and joins a new room. We then publish two more events every time an anonymous user sends a message. The "lobby_list" event for updating the user's last message sent and timestamp and the "notifications" event for triggering browser notifications. We'll get into more detail on that once we work on the frontend.
1 parent 1a2feca commit 5b09965

1 file changed

Lines changed: 47 additions & 8 deletions

File tree

web/channels/room_channel.ex

Lines changed: 47 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,19 @@ defmodule PhoenixChat.RoomChannel do
1111
|> Repo.all
1212
|> Enum.map(&message_payload/1)
1313
|> Enum.reverse
14-
send(self, :after_join)
14+
send(self, {:after_join, payload})
1515
{:ok, %{messages: messages}, socket}
1616
end)
1717
end
1818

19-
def handle_info(:after_join, socket) do
19+
def handle_info({:after_join, payload}, socket) do
2020
# We create the anonymous user in our DB if its `uuid` does not match
2121
# any existing record.
2222
get_or_create_anonymous_user!(socket)
23+
24+
# We record when admin views a room
25+
update_last_viewed_at(payload["previousRoom"])
26+
update_last_viewed_at(payload["nextRoom"])
2327
{:noreply, socket}
2428
end
2529

@@ -30,21 +34,56 @@ defmodule PhoenixChat.RoomChannel do
3034
changeset = Message.changeset(%Message{}, payload)
3135

3236
case Repo.insert(changeset) do
37+
# This branch gets triggered when a message is sent by an anonymous user
38+
{:ok, %{anonymous_user_id: uuid} = message} when not is_nil(uuid) ->
39+
user = Repo.preload(message, :anonymous_user).anonymous_user
40+
message_payload = message_payload(message, user)
41+
broadcast! socket, "message", message_payload
42+
43+
# Apart from sending the message, we want to update the lobby list
44+
# with the last message sent by the user and its timestamp
45+
Endpoint.broadcast_from! self, "admin:active_users",
46+
"lobby_list", user_payload({user, message})
47+
48+
# We also send the message via the "notifications" event. This event
49+
# will be listened to in the frontend and will publish an Notification
50+
# via the browser when admin is not viewing the sender's chatroom.
51+
Endpoint.broadcast_from! self, "admin:active_users",
52+
"notifications", message_payload
53+
54+
# This branch gets triggered when a message is sent by admin
3355
{:ok, message} ->
34-
payload = message_payload(message)
35-
broadcast! socket, "message", payload
36-
{:reply, :ok, socket}
56+
broadcast! socket, "message", message_payload(message)
3757
{:error, changeset} ->
3858
{:reply, {:error, %{errors: changeset}}, socket}
3959
end
4060
end
4161

42-
defp message_payload(message) do
43-
from = message.user_id || message.from
62+
defp update_last_viewed_at(nil), do: nil #noop
63+
64+
defp update_last_viewed_at(uuid) do
65+
user = Repo.get(AnonymousUser, uuid)
66+
changeset = AnonymousUser.last_viewed_changeset(user)
67+
user = Repo.update!(changeset)
68+
Endpoint.broadcast_from! self, "admin:active_users",
69+
"lobby_list", user_payload(user)
70+
end
71+
72+
defp message_payload(%{anonymous_user_id: nil} = message) do
73+
%{body: message.body,
74+
timestamp: message.timestamp,
75+
room: message.room,
76+
from: message.user_id,
77+
id: message.id}
78+
end
79+
80+
defp message_payload(message, user \\ nil) do
81+
user = user || Repo.preload(message, :anonymous_user).anonymous_user
4482
%{body: message.body,
4583
timestamp: message.timestamp,
4684
room: message.room,
47-
from: from,
85+
from: user.name,
86+
uuid: user.id,
4887
id: message.id}
4988
end
5089
end

0 commit comments

Comments
 (0)