-
Notifications
You must be signed in to change notification settings - Fork 83
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
Adding Phoenix.Presence functionality #87
Open
alanj853
wants to merge
11
commits into
absinthe-graphql:main
Choose a base branch
from
se-apc:master
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 8 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
f3a2a57
First pass at adding Phoenix.Presence
alanj853 b1b0386
tidy up
alanj853 96bb98a
follow previous function-passing structure
alanj853 5e0ac5f
Merge pull request #1 from se-apc/presence
alanj853 802741c
Add ready_for_data clause
alanj853 1f4d027
check for pid
alanj853 be9960a
make sure we send to the right pid
alanj853 408fc00
Merge pull request #2 from se-apc/ready_for_data
alanj853 9730ab7
update .tool-versions
alanj853 6074a32
Merge pull request #3 from absinthe-graphql/master
alanj853 575783b
Merge remote-tracking branch 'upstream/main'
alanj853 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,105 @@ | ||||||||||||
defmodule Absinthe.Phoenix.Presence do | ||||||||||||
@moduledoc """ | ||||||||||||
This module is to allow the use of the Phoenix.Presence behaviour (https://hexdocs.pm/phoenix/Phoenix.Presence.html) while using Absinthe.Phoenix. | ||||||||||||
|
||||||||||||
To use it, do the following: | ||||||||||||
1. Set up your implementation of Phoenix.Presence as per the official documentation: https://hexdocs.pm/phoenix/Phoenix.Presence.html | ||||||||||||
2. When 'using' Absinthe.Phoenix.Socket, simply add the presence_config option like so: | ||||||||||||
''' | ||||||||||||
defmodule MyAppWeb.SocketUserModule do | ||||||||||||
use Absinthe.Phoenix.Socket, | ||||||||||||
presence_config: %{ | ||||||||||||
module: MyAppWeb.Presence, | ||||||||||||
meta_fn: {MyAppWeb, :some_meta_logic_fn}, | ||||||||||||
key_fn: {MyAppWeb, :some_key_logic_fn} | ||||||||||||
} | ||||||||||||
|
||||||||||||
... | ||||||||||||
end | ||||||||||||
''' | ||||||||||||
* The :some_meta_logic atom is a function that returns the `meta` argument that Phoenix.Presence.track/3 expects (see https://hexdocs.pm/phoenix/Phoenix.Presence.html#c:track/3 for more info). | ||||||||||||
The single argument this function should handle is the socket itself, which is of type `Phoenix.Socket`. The reason it is a function and not a single data item is that it allows the devloper to be | ||||||||||||
flexible and apply/get whatever meta logic/data they want from the socket | ||||||||||||
* The :some_key_logic_fn atom is a function that returns the `key` argument that Phoenix.Presence.track/3 expects (see https://hexdocs.pm/phoenix/Phoenix.Presence.html#c:track/3 for more info). | ||||||||||||
The single argument this function should handle is the socket itself, which is of type `Phoenix.Socket`. The reason it is a function and not a single data item is that it allows the devloper to be | ||||||||||||
flexible and apply/get whatever key logic/data they want from the socket | ||||||||||||
""" | ||||||||||||
require Logger | ||||||||||||
@presence_topic "__absinthe__:control" | ||||||||||||
|
||||||||||||
def presence_topic() do | ||||||||||||
@presence_topic | ||||||||||||
end | ||||||||||||
|
||||||||||||
@doc """ | ||||||||||||
Function to call the Phoenix.Presence.track/3 callback from the module that the user has configured in __absinthe_presence_config__. | ||||||||||||
""" | ||||||||||||
def track(socket = %{assigns: %{__absinthe_presence_config__: presence_config}}) | ||||||||||||
when is_map(presence_config) do | ||||||||||||
module = Map.get(presence_config, :module, __MODULE__.Defaults) | ||||||||||||
meta_fn = Map.get(presence_config, :meta_fn, {__MODULE__.Defaults, :meta_fn}) | ||||||||||||
key_fn = Map.get(presence_config, :key_fn, {__MODULE__.Defaults, :key_fn}) | ||||||||||||
|
||||||||||||
meta = execute(meta_fn, socket) | ||||||||||||
key = execute(key_fn, socket) | ||||||||||||
|
||||||||||||
{:ok, _} = module.track(socket, key, meta) | ||||||||||||
end | ||||||||||||
|
||||||||||||
def track(_socket) do | ||||||||||||
Logger.debug( | ||||||||||||
"Cannot track as socket.assigns does not contain a valid :__abinthe_presence_config__ key!" | ||||||||||||
) | ||||||||||||
|
||||||||||||
nil | ||||||||||||
end | ||||||||||||
|
||||||||||||
@doc """ | ||||||||||||
Function to call the Phoenix.Presence.list/1 callback from the module that the user has configured in __absinthe_presence_config__. | ||||||||||||
""" | ||||||||||||
def list(socket = %{assigns: %{__absinthe_presence_config__: presence_config}}) | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Elixir can pattern match at multiple levels
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||||||||||||
when is_map(presence_config) do | ||||||||||||
module = Map.get(presence_config, :module) | ||||||||||||
|
||||||||||||
if module == nil do | ||||||||||||
Logger.debug( | ||||||||||||
"Cannot list as the :__abinthe_presence_config__ map does not contain a :module key!" | ||||||||||||
) | ||||||||||||
|
||||||||||||
nil | ||||||||||||
else | ||||||||||||
module.list(socket) | ||||||||||||
end | ||||||||||||
end | ||||||||||||
|
||||||||||||
def list(_socket) do | ||||||||||||
Logger.debug( | ||||||||||||
"Cannot list as socket.assigns does not contain a valid :__abinthe_presence_config__ key!" | ||||||||||||
) | ||||||||||||
|
||||||||||||
nil | ||||||||||||
end | ||||||||||||
|
||||||||||||
defp execute({module, function}, args) do | ||||||||||||
apply(module, function, [args]) | ||||||||||||
end | ||||||||||||
|
||||||||||||
defmodule Defaults do | ||||||||||||
@moduledoc """ | ||||||||||||
Module for housing the default functions if none are given | ||||||||||||
""" | ||||||||||||
def track(_socket, _key, _meta) do | ||||||||||||
{:ok, ""} | ||||||||||||
end | ||||||||||||
|
||||||||||||
def meta_fn(_socket) do | ||||||||||||
%{ | ||||||||||||
online_at: inspect(System.system_time(:second)) | ||||||||||||
} | ||||||||||||
end | ||||||||||||
|
||||||||||||
def key_fn(_socket) do | ||||||||||||
"" | ||||||||||||
end | ||||||||||||
end | ||||||||||||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why do we need
%{handler: nil}
here?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because you cannot do
Map.get(nil, :handler)
which will happen if thecontext
is missing fromopts
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This whole chain feels like it would be improved with use of Access since it is nil resilient: