Skip to content

Commit

Permalink
Add PresenceChannel.my_user_id/1
Browse files Browse the repository at this point in the history
  • Loading branch information
edgurgel committed Jan 27, 2019
1 parent b22f174 commit e6b6ec4
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
11 changes: 11 additions & 0 deletions lib/poxa/presence_channel.ex
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,15 @@ defmodule Poxa.PresenceChannel do
"""
@spec user_count(binary) :: non_neg_integer
def user_count(channel), do: channel |> users |> Enum.count

@doc """
Returns user data for a presence channel
"""
@spec my_user_id(binary) :: {binary, map} | nil
def my_user_id(channel) do
case Poxa.registry.fetch(channel) do
{user_id, _} -> user_id
_ -> nil
end
end
end
15 changes: 15 additions & 0 deletions test/presence_channel_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,19 @@ defmodule Poxa.PresenceChannelTest do

assert user_count("presence-channel") == 2
end

test "return user_id if available" do
expect(Poxa.registry, :fetch, fn "presence-channel" ->
{:user_id, :user_info}
end)

assert my_user_id("presence-channel") == :user_id
end


test "return nil if user_id is not available" do
expect(Poxa.registry, :fetch, fn "presence-channel" -> nil end)

assert my_user_id("presence-channel") == nil
end
end

0 comments on commit e6b6ec4

Please sign in to comment.