Skip to content

Commit

Permalink
Add function for getting client's user info (#412)
Browse files Browse the repository at this point in the history
  • Loading branch information
jonatanklosko authored Apr 3, 2024
1 parent f840ebb commit c2b6646
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 4 deletions.
19 changes: 15 additions & 4 deletions lib/kino/bridge.ex
Original file line number Diff line number Diff line change
Expand Up @@ -219,15 +219,26 @@ defmodule Kino.Bridge do
* `{:client_leave, client_id}`
When the monitor starts and there are already clients that joined,
the join message is going to be delivered for each of them right
away.
Returns a list of client ids that are already joined.
"""
@spec monitor_clients(pid()) :: :ok | {:error, request_error()}
@spec monitor_clients(pid()) :: {:ok, list(String.t())} | {:error, request_error()}
def monitor_clients(pid) do
with {:ok, reply} <- io_request({:livebook_monitor_clients, pid}), do: reply
end

@doc """
Returns user information for the given connected client id.
Errors with `:not_available`, unless the notebook uses a Livebook
Teams hub.
"""
@spec get_user_info(String.t()) ::
{:ok, Kino.Hub.user_info()}
| {:error, :not_available | :not_found | request_error()}
def get_user_info(client_id) do
with {:ok, reply} <- io_request({:livebook_get_user_info, client_id}), do: reply
end

@doc """
Checks if the caller is running within Livebook context (group leader).
"""
Expand Down
22 changes: 22 additions & 0 deletions lib/kino/hub.ex
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,26 @@ defmodule Kino.Hub do
{:error, _} -> %{type: :none}
end
end

@doc """
Returns user information for the given connected client id.
Note that this information is only available when the session uses
Livebook Teams hub, otherwise `:not_available` error is returned.
If there is no such connected client, `:not_found` error is returned.
"""
@spec user_info(String.t()) :: {:ok, user_info()} | {:error, :not_found | :not_available}
def user_info(client_id) do
case Kino.Bridge.get_user_info(client_id) do
{:ok, user_info} ->
{:ok, user_info}

{:error, reason} when reason in [:not_found, :not_available] ->
{:error, reason}

{:error, reason} ->
raise "failed to access user info, reason: #{inspect(reason)}"
end
end
end

0 comments on commit c2b6646

Please sign in to comment.