-
Notifications
You must be signed in to change notification settings - Fork 163
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
Update sidebar users #24
Open
bemesa21
wants to merge
6
commits into
master
Choose a base branch
from
update-sidebar-users
base: master
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 all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
b440ec2
create user tracker
bemesa21 f23539c
send messages to user tracker from presence client
bemesa21 d93bb8e
update active users with a hook
bemesa21 b8608bc
fetch active users on mount
bemesa21 6c3eacf
set active_users as temporary assigns
bemesa21 979469d
send leaves and joines separated
bemesa21 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
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,109 @@ | ||
defmodule LiveBeats.UserTracker do | ||
@moduledoc """ | ||
Send active users updates using a polling interval. | ||
""" | ||
|
||
use GenServer | ||
@pubsub LiveBeats.PubSub | ||
@poll_interval :timer.seconds(30) | ||
|
||
def subscribe() do | ||
Phoenix.PubSub.subscribe(@pubsub, topic()) | ||
end | ||
|
||
def start_link(opts) do | ||
GenServer.start_link(__MODULE__, opts, name: __MODULE__) | ||
end | ||
|
||
def list_active_users() do | ||
GenServer.call(__MODULE__, :list_users) | ||
end | ||
|
||
def presence_joined(presence) do | ||
GenServer.call(__MODULE__, {:presence_joined, presence}) | ||
end | ||
|
||
def presence_left(presence) do | ||
GenServer.call(__MODULE__, {:presence_left, presence}) | ||
end | ||
|
||
@impl true | ||
def init(_opts) do | ||
{:ok, | ||
schedule_updates(%{ | ||
active_users: %{}, | ||
user_leaves: [], | ||
user_joins: [] | ||
})} | ||
end | ||
|
||
@impl true | ||
def handle_call(:list_users, _from, state) do | ||
{:reply, list_users(state), state} | ||
end | ||
|
||
@impl true | ||
def handle_call({:presence_joined, presence}, _from, state) do | ||
{:reply, :ok, handle_join(state, presence)} | ||
end | ||
|
||
@impl true | ||
def handle_call({:presence_left, presence}, _from, state) do | ||
{:reply, :ok, handle_leave(state, presence)} | ||
end | ||
|
||
@impl true | ||
def handle_info(:send_updates, state) do | ||
leaves = state.user_leaves -- state.user_joins | ||
joins = state.user_joins -- state.user_leaves | ||
|
||
broadcast_updates(leaves, joins) | ||
|
||
# cleaning joins and leaves for each interval | ||
new_state = %{state | user_leaves: [], user_joins: []} | ||
{:noreply, schedule_updates(new_state)} | ||
end | ||
|
||
defp schedule_updates(state) do | ||
Process.send_after(self(), :send_updates, @poll_interval) | ||
state | ||
end | ||
|
||
defp handle_join(state, %{user: user}) do | ||
if Map.has_key?(state.active_users, user.id) do | ||
state | ||
else | ||
updated_active_users = Map.put_new(state.active_users, user.id, user) | ||
updated_user_joins = [user | state.user_joins] | ||
|
||
%{state | active_users: updated_active_users, user_joins: updated_user_joins} | ||
end | ||
end | ||
|
||
defp handle_leave(state, %{user: user, metas: metas}) do | ||
if Map.has_key?(state.active_users, user.id) and metas == [] do | ||
updated_active_users = Map.delete(state.active_users, user.id) | ||
updated_user_leaves = [user | state.user_leaves] | ||
|
||
%{state | active_users: updated_active_users, user_leaves: updated_user_leaves} | ||
else | ||
state | ||
end | ||
end | ||
|
||
defp topic() do | ||
"active_users" | ||
end | ||
|
||
defp broadcast_updates(leaves, joins) do | ||
Phoenix.PubSub.local_broadcast( | ||
@pubsub, | ||
topic(), | ||
{LiveBeats.UserTracker, %{user_leaves: leaves, user_joins: joins}} | ||
) | ||
end | ||
|
||
defp list_users(state) do | ||
Enum.map(state.active_users, fn {_key, value} -> value 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
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
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.
It might be better if I only save the user IDs and not the whole user structure in the
user_leaves
anduser_joins
lists