Skip to content

Commit

Permalink
Archive room update (#20)
Browse files Browse the repository at this point in the history
* add handle in for archive room

* Correct Typo and change call arg to room_id

* Fix bug calling typo'd attribute

* Use room.id as arg to UpdateState.call

* use get_by instead of get, use more general var names for flexibility

* revert back to get(id) instead of get_by(id)

* handle_in for reactivation of room

* Renaming to UpdateRoom as it only functions to update rooms currently

* refactor broadcast to match new pattern

* Notify subscribers when room has changed (#2)

* Track archived support rooms (#3)

* Add room state to support room presence

* Update support presence if support  room state changes

* Update support state after reactivate room

* Update support state after reactivate room (#4)

* Change function name and handle_in call key name

* Use GetRoom so tuple is returned

* update getroom to include call

* fix syntax to remove single additional space

* Fix indentation
  • Loading branch information
jmhthethird authored and chrislaskey committed Nov 30, 2017
1 parent 9db3194 commit 3e93eb7
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
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]]}

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
Expand All @@ -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
2 changes: 1 addition & 1 deletion apps/chat_server/lib/chat_server/schemas/room.ex
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ defmodule ChatServer.Schema.Room do

def archived do
Room
|> where(status: "archived")
|> where(state: "archived")
|> Repo.all
end

Expand Down
37 changes: 37 additions & 0 deletions apps/chat_websocket/lib/chat_websocket/channels/room_channel.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand Down

0 comments on commit 3e93eb7

Please sign in to comment.