View Source Lightning.Workflows.Presence (Lightning v2.10.15)

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.

Informs if there is someone editing a workflow.

Callback implementation for Phoenix.Presence.list/1.

Lists all presences for a given workflow.

Callback implementation for Phoenix.Presence.track/3.

Tracks the presence of a user on editing a workflow.

Callback implementation for Phoenix.Presence.untrack/2.

Untracks the presence of a user on editing a workflow.

Functions

Link to this function

build_presences_summary(presences, params)

View Source

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.

Link to this function

has_any_presence?(workflow)

View Source

Informs if there is someone editing a workflow.

Callback implementation for Phoenix.Presence.list/1.

Link to this function

list_presences_for(workflow)

View Source

Lists all presences for a given workflow.

Parameters

  • workflow: The workflow to list the presences for.

Examples

iex> Lightning.Workflows.Presence.list_presences_for(%Workflow{id: xpto})
[%Lightning.Workflows.Presence{user: %User{id: 1}, ...}, ...]
Link to this function

new_user_presence(user, joined_at, active_sessions \\ 0)

View Source

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
}
Link to this function

track(socket, key, meta)

View Source

Callback implementation for Phoenix.Presence.track/3.

Link to this function

track(pid, topic, key, meta)

View Source

Callback implementation for Phoenix.Presence.track/4.

Link to this function

track_user_presence(user, workflow, pid)

View Source

Tracks the presence of a user on editing a workflow.

Parameters

  • user: The user to be tracked.
  • workflow: The workflow to track the user on.
  • pid: The process identifier for the user's session.

Examples

iex> Lightning.Workflows.Presence.track_user_presence(%User{id: user_id}, %Workflow{id: workflow_id}, self())
:ok

Callback implementation for Phoenix.Presence.untrack/2.

Link to this function

untrack(pid, topic, key)

View Source

Callback implementation for Phoenix.Presence.untrack/3.

Link to this function

untrack_user_presence(user, workflow, pid)

View Source

Untracks the presence of a user on editing a workflow.

Parameters

  • user: The user to be tracked.
  • workflow: The workflow to track the user on.
  • pid: The process identifier for the user's session.

Examples

iex> Lightning.Workflows.Presence.untrack_user_presence(%User{id: user_id}, %Workflow{id: workflow_id}, self())
:ok
Link to this function

update(socket, key, meta)

View Source

Callback implementation for Phoenix.Presence.update/3.

Link to this function

update(pid, topic, key, meta)

View Source

Callback implementation for Phoenix.Presence.update/4.