diff --git a/apps/chat_server/lib/chat_server/contexts/rooms/track_support_rooms.ex b/apps/chat_server/lib/chat_server/contexts/rooms/track_support_rooms.ex index d5550eb..09589e6 100644 --- a/apps/chat_server/lib/chat_server/contexts/rooms/track_support_rooms.ex +++ b/apps/chat_server/lib/chat_server/contexts/rooms/track_support_rooms.ex @@ -23,7 +23,8 @@ defmodule ChatServer.TrackSupportRooms do nil -> Presence.track(pid, @presence_key, key(room), %{ slug: room.slug, - type: room.type + type: room.type, + state: room.state }) _data -> Logger.warn "Support room already tracked #{room.slug}" <> inspect(pid) diff --git a/apps/chat_server/lib/chat_server/contexts/rooms/update_state.ex b/apps/chat_server/lib/chat_server/contexts/rooms/update_room.ex similarity index 58% rename from apps/chat_server/lib/chat_server/contexts/rooms/update_state.ex rename to apps/chat_server/lib/chat_server/contexts/rooms/update_room.ex index 99d98c1..e168055 100644 --- a/apps/chat_server/lib/chat_server/contexts/rooms/update_state.ex +++ b/apps/chat_server/lib/chat_server/contexts/rooms/update_room.ex @@ -1,7 +1,9 @@ -defmodule ChatServer.UpdateState do +defmodule ChatServer.UpdateRoom do require Logger + alias ChatServer.BroadcastEvent alias ChatServer.Schema + alias ChatServer.GetRoom defmodule State do @derive {Poison.Encoder, only: [room: [:id, :slug, :name]]} @@ -9,12 +11,13 @@ defmodule ChatServer.UpdateState do defstruct [:room] end - def call(params \\ %{}, _user, state) do - Logger.info "Updating room " <> inspect(params) + def call(room_id, _user, state) do + Logger.info "Updating room " <> inspect(room_id) # TODO verify user is allowed to update room of that type - with {:ok, record} <- update(params, state), + with {:ok, record} <- GetRoom.call(room_id), + {:ok, record} <- update(record, state), :ok <- broadcast_update(record) do {:ok, record} else @@ -25,14 +28,12 @@ defmodule ChatServer.UpdateState do defp update(params, state) do changeset = params |> Map.take([:id, :name]) - |> Map.put(:status, state) + |> Map.put(:state, state) Schema.Room.update(params, changeset) end defp broadcast_update(room) do - event = %State{room: room} - - ChatPubSub.broadcast! "events", "room:updated", event + BroadcastEvent.call("room:updated", room) end end diff --git a/apps/chat_server/lib/chat_server/schemas/room.ex b/apps/chat_server/lib/chat_server/schemas/room.ex index 7a69d30..3f30a2c 100644 --- a/apps/chat_server/lib/chat_server/schemas/room.ex +++ b/apps/chat_server/lib/chat_server/schemas/room.ex @@ -64,7 +64,7 @@ defmodule ChatServer.Schema.Room do def archived do Room - |> where(status: "archived") + |> where(state: "archived") |> Repo.all end diff --git a/apps/chat_websocket/lib/chat_websocket/channels/room_channel.ex b/apps/chat_websocket/lib/chat_websocket/channels/room_channel.ex index cbc7558..953d740 100644 --- a/apps/chat_websocket/lib/chat_websocket/channels/room_channel.ex +++ b/apps/chat_websocket/lib/chat_websocket/channels/room_channel.ex @@ -11,7 +11,9 @@ defmodule ChatWebsocket.RoomChannel do alias ChatServer.GetSubscription alias ChatServer.ListSubscriptions alias ChatServer.Room + alias ChatServer.TrackSupportRooms alias ChatServer.UpdateMessage + alias ChatServer.UpdateRoom alias ChatServer.UpdateSubscription alias ChatServer.ViewSubscription @@ -130,6 +132,34 @@ defmodule ChatWebsocket.RoomChannel do end end + def handle_in("room:archive", _params, socket) do + %{room: room, user: user} = socket.assigns + + with {:ok, record} <- UpdateRoom.call(room.id, user, "archived"), + :ok <- broadcast!(socket, "room:archived", record), + {:ok, _} <- maybe_update_support_room_presence(record), + subscriptions <- ListSubscriptions.call(record), + :ok <- broadcast_user_subscriptions!(subscriptions) do + reply(:ok, %{room: record}, socket) + else + _ -> reply(:error, "Error archiving room", socket) + end + end + + def handle_in("room:reactivate", _params, socket) do + %{room: room, user: user} = socket.assigns + + with {:ok, record} <- UpdateRoom.call(room.id, user, "active"), + :ok <- broadcast!(socket, "room:active", record), + {:ok, _} <- maybe_update_support_room_presence(record), + subscriptions <- ListSubscriptions.call(record), + :ok <- broadcast_user_subscriptions!(subscriptions) do + reply(:ok, %{room: record}, socket) + else + _ -> reply(:error, "Error reactivating room", socket) + end + end + defp broadcast_user_subscriptions!(subscriptions) do Enum.map(subscriptions, fn (subscription) -> broadcast_user_event!(subscription.user, "user:current:subscription:updated", subscription) @@ -138,6 +168,13 @@ defmodule ChatWebsocket.RoomChannel do :ok end + defp maybe_update_support_room_presence(room) do + case room.type do + "support" -> TrackSupportRooms.update(room, %{state: room.state}) + _ -> {:ok, true} + end + end + # Filters def handle_out(event, payload, socket) do