Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

API endpoints to add and remove room owners #644

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions lib/ret/api/rooms.ex
Original file line number Diff line number Diff line change
Expand Up @@ -104,4 +104,20 @@ defmodule Ret.Api.Rooms do

RetWeb.Endpoint.broadcast("hub:" <> hub.hub_sid, "hub_refresh", payload)
end

def authed_update_owner(event, hub_sid, %Credentials{} = credentials, params) when event in ["add_owner", "remove_owner"] do
hub = Hub |> Repo.get_by(hub_sid: hub_sid) |> Repo.preload([:hub_role_memberships, :hub_bindings])
if is_nil(hub) do
{:error, "Cannot find room with id: " <> hub_sid}
else
if can?(credentials, update_room(hub)) do
case RetWeb.Endpoint.broadcast("hub:" <> hub.hub_sid, event, %{ "session_id" => params.session_id }) do
{:error, reason} -> {:error, reason}
:ok -> {:ok, hub}
end
else
{:error, :invalid_credentials}
end
end
end
end
25 changes: 25 additions & 0 deletions lib/ret_web/resolvers/room_resolver.ex
Original file line number Diff line number Diff line change
Expand Up @@ -146,4 +146,29 @@ defmodule RetWeb.Resolvers.RoomResolver do
def update_room(_parent, _args, _resolutions) do
resolver_error(:unauthorized, "Unauthorized access")
end

def add_owner(_parent, %{id: hub_sid} = args, %{
context: %{
credentials: %Credentials{} = credentials
}
}) do
Ret.Api.Rooms.authed_update_owner("add_owner", hub_sid, credentials, args)
end

def add_owner(_parent, _args, _resolutions) do
resolver_error(:unauthorized, "Unauthorized access")
end

def remove_owner(_parent, %{id: hub_sid} = args, %{
context: %{
credentials: %Credentials{} = credentials
}
}) do
Ret.Api.Rooms.authed_update_owner("remove_owner", hub_sid, credentials, args)
end

def remove_owner(_parent, _args, _resolutions) do
resolver_error(:unauthorized, "Unauthorized access")
end

end
23 changes: 21 additions & 2 deletions lib/ret_web/schema/room_types.ex
Original file line number Diff line number Diff line change
Expand Up @@ -237,10 +237,29 @@ defmodule RetWeb.Schema.RoomTypes do
@desc "Arbitrary json data associated with this room"
arg(:user_data, :json)

# TODO: add/remove owner

resolve(&Resolvers.RoomResolver.update_room/3)
end

@desc "Add an owner to the room specified by the given id"
field :add_owner, :room do
@desc "The id of the room"
arg(:id, non_null(:string))
@desc "The session id of the user to promote"
arg(:session_id, non_null(:string))

resolve(&Resolvers.RoomResolver.add_owner/3)
end

@desc "Remove an owner to the room specified by the given id"
field :remove_owner, :room do
@desc "The id of the room"
arg(:id, non_null(:string))
@desc "The session id of the user to demote"
arg(:session_id, non_null(:string))

resolve(&Resolvers.RoomResolver.remove_owner/3)
end

end

object :room_subscriptions do
Expand Down