diff --git a/web/channels/system_channel.ex b/web/channels/system_channel.ex index 06700a1..1d076b2 100644 --- a/web/channels/system_channel.ex +++ b/web/channels/system_channel.ex @@ -1,57 +1,61 @@ defmodule KolabChat.SystemChannel do use KolabChat.Web, :channel alias KolabChat.Presence @status [ # user is available for chat :online, :away, # user is connected and visible, but not available :busy, :unavailable, # user is shown as offline :invisible, :offline ] def join("system", %{"context" => context}, socket) do socket = assign(socket, :context, context) send self(), :after_join {:ok, socket} end def handle_info(:after_join, socket) do push socket, "presence_state", Presence.list(socket) push socket, "info", %{user: socket.assigns.user.username} Presence.track(socket, socket.assigns.user.username, %{ status: :online, context: socket.assigns.context }) {:noreply, socket} end def handle_in("set-status", %{"status" => status}, socket) do + update_presence_status(check_status(status), socket) + {:noreply, socket} + end + + defp update_presence_status(:invalid, _socket), do: :ok + defp update_presence_status(status, socket) do {:ok, _} = Presence.update(socket, socket.assigns.user.username, %{ - status: check_status(status), + status: status, context: socket.assigns.context }) - - {:noreply, socket} end # Makes sure the provided status name is supported # Returns status name as an atom - def check_status(status) do + defp check_status(status) do status = String.to_atom(status) if Enum.member?(@status, status) do status else - :online + :invalid end end end