View Source Lightning.Workflows.Presence (Lightning v2.10.4)
Handles user presence tracking within the Workflow canvas page.
This module leverages Phoenix.Presence to track user sessions, manage user priorities, and list active presences on specified topics.
Summary
Functions
Builds a summary of presences with details about the current user's presence, promotable presences, and edit priority.
Callback implementation for Phoenix.Presence.fetch/2
.
Callback implementation for Phoenix.Presence.get_by_key/2
.
Callback implementation for Phoenix.Presence.list/1
.
Lists all presences for a given topic.
Creates a new UserPresence
struct.
Callback implementation for Phoenix.Presence.track/3
.
Callback implementation for Phoenix.Presence.track/4
.
Tracks the presence of a user on a given topic.
Callback implementation for Phoenix.Presence.untrack/2
.
Callback implementation for Phoenix.Presence.untrack/3
.
Callback implementation for Phoenix.Presence.update/3
.
Callback implementation for Phoenix.Presence.update/4
.
Functions
Builds a summary of presences with details about the current user's presence, promotable presences, and edit priority.
Parameters
presences
(list): A list of presence records, each containing user information and a joined_at timestamp.params
(map): A map containing the following keys::current_user_presence
- The presence record for the current user.:current_user
- The current user record.:view_only_users_ids
- A list of user IDs who have view-only permissions.
Returns
map
: A map containing the following keys::presences
- The sorted list of all presences.:prior_user_presence
- The presence record with edit priority.:current_user_presence
- The presence record for the current user.:has_presence_edit_priority
- A boolean indicating if the current user has edit priority.
Examples
iex> presences = [
...> %{user: %{id: 1}, joined_at: ~N[2024-07-03 12:00:00], active_sessions: 1},
...> %{user: %{id: 2}, joined_at: ~N[2024-07-03 12:05:00], active_sessions: 1},
...> %{user: %{id: 3}, joined_at: ~N[2024-07-03 12:10:00], active_sessions: 1}
...> ]
iex> params = %{
...> current_user_presence: %{user: %{id: 1}, joined_at: ~N[2024-07-03 12:00:00], active_sessions: 1},
...> current_user: %{id: 1},
...> view_only_users_ids: [2]
...> }
iex> build_presences_summary(presences, params)
%{
presences: [
%{user: %{id: 1}, joined_at: ~N[2024-07-03 12:00:00], active_sessions: 1},
%{user: %{id: 2}, joined_at: ~N[2024-07-03 12:05:00], active_sessions: 1},
%{user: %{id: 3}, joined_at: ~N[2024-07-03 12:10:00], active_sessions: 1}
],
prior_user_presence: %{user: %{id: 3}, joined_at: ~N[2024-07-03 12:10:00], active_sessions: 1},
current_user_presence: %{user: %{id: 1}, joined_at: ~N[2024-07-03 12:00:00], active_sessions: 1},
has_presence_edit_priority: true
}
Callback implementation for Phoenix.Presence.fetch/2
.
Callback implementation for Phoenix.Presence.get_by_key/2
.
Callback implementation for Phoenix.Presence.list/1
.
Lists all presences for a given topic.
Parameters
topic
: The topic to list the presences for.
Examples
iex> Lightning.Workflows.Presence.list_presences("workflow:canvas")
[%Lightning.Workflows.Presence{user: %User{id: 1}, ...}, ...]
Creates a new UserPresence
struct.
Parameters
user
: The user data to be included in the presence.joined_at
: The timestamp when the user joined, in microseconds.active_sessions
: The number of active sessions for the user (default is 0).
Examples
iex> Lightning.Workflows.Presence.new_user_presence(%User{id: 1}, 1625597762000000)
%Lightning.Workflows.Presence{
user: %User{id: 1},
joined_at: 1625597762000000,
active_sessions: 0
}
Callback implementation for Phoenix.Presence.track/3
.
Callback implementation for Phoenix.Presence.track/4
.
Tracks the presence of a user on a given topic.
Parameters
user
: The user to be tracked.topic
: The topic to track the user on.pid
: The process identifier for the user's session.
Examples
iex> Lightning.Workflows.Presence.track_user_presence(%User{id: 1}, "room:lobby", self())
:ok
Callback implementation for Phoenix.Presence.untrack/2
.
Callback implementation for Phoenix.Presence.untrack/3
.
Callback implementation for Phoenix.Presence.update/3
.
Callback implementation for Phoenix.Presence.update/4
.