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

Improve Phoenix.Presence.get_by_key/2 documentation, return value, and typespec. #6042

Open
wants to merge 1 commit into
base: main
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
18 changes: 12 additions & 6 deletions lib/phoenix/presence.ex
Original file line number Diff line number Diff line change
Expand Up @@ -189,8 +189,8 @@ defmodule Phoenix.Presence do

"""

@type presences :: %{String.t() => %{metas: [map()]}}
@type presence :: %{key: String.t(), meta: map()}
@type presence :: %{metas: [map()]}
@type presences :: %{String.t() => presence}
@type topic :: String.t()

@doc """
Expand Down Expand Up @@ -297,12 +297,19 @@ defmodule Phoenix.Presence do
devices, could return:

iex> MyPresence.get_by_key("room:1", "user1")
[%{name: "User 1", metas: [%{device: "Desktop"}, %{device: "Mobile"}]}]
%{
name: "User 1",
additional_info: %{},
metas: [%{device: "Desktop"}, %{device: "Mobile"}]
}

iex> MyPresence.get_by_key("room:1", "untracked_user")
nil

Like `c:list/1`, the presence metadata is passed to the `fetch`
callback of your presence module to fetch any additional information.
"""
@callback get_by_key(Phoenix.Socket.t() | topic, key :: String.t()) :: [presence]
@callback get_by_key(socket_or_topic :: Phoenix.Socket.t() | topic, key :: String.t()) :: presence | nil

@doc """
Extend presence information with additional data.
Expand Down Expand Up @@ -540,8 +547,7 @@ defmodule Phoenix.Presence do
string_key = to_string(key)

case Phoenix.Tracker.get_by_key(module, topic, key) do
[] ->
[]
[] -> nil

[_ | _] = pid_metas ->
metas = Enum.map(pid_metas, fn {_pid, meta} -> meta end)
Expand Down
6 changes: 3 additions & 3 deletions test/phoenix/presence_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -98,16 +98,16 @@ defmodule Phoenix.PresenceTest do
test "get_by_key/2 returns metadata for key", config do
pid2 = spawn(fn -> :timer.sleep(:infinity) end)
pid3 = spawn(fn -> :timer.sleep(:infinity) end)
assert MyPresence.get_by_key(config.topic, 1) == []
assert is_nil(MyPresence.get_by_key(config.topic, 1))
assert {:ok, _} = MyPresence.track(self(), config.topic, 1, %{name: "u1"})
assert {:ok, _} = MyPresence.track(pid2, config.topic, 1, %{name: "u1.2"})
assert {:ok, _} = MyPresence.track(pid3, config.topic, 2, %{name: "u1.2"})

assert %{extra: "extra", metas: [%{name: "u1", phx_ref: _}, %{name: "u1.2", phx_ref: _}]} =
MyPresence.get_by_key(config.topic, 1)

assert MyPresence.get_by_key(config.topic, "another_key") == []
assert MyPresence.get_by_key("another_topic", 2) == []
assert is_nil(MyPresence.get_by_key(config.topic, "another_key"))
assert is_nil(MyPresence.get_by_key("another_topic", 2))
end

test "handle_diff broadcasts events with default fetched data",
Expand Down