Lightning.Workflows.Presence (Lightning v2.13.5)

View Source

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

build_presences_summary(presences, params)

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
}

child_spec(opts)

fetch(topic, presences)

Callback implementation for Phoenix.Presence.fetch/2.

fetchers_pids()

get_by_key(topic, key)

Callback implementation for Phoenix.Presence.get_by_key/2.

has_any_presence?(workflow)

Informs if there is someone editing a workflow.

list(topic)

Callback implementation for Phoenix.Presence.list/1.

list_presences_for(workflow)

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}, ...}, ...]

new_user_presence(user, joined_at, active_sessions \\ 0)

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
}

track(socket, key, meta)

Callback implementation for Phoenix.Presence.track/3.

track(pid, topic, key, meta)

Callback implementation for Phoenix.Presence.track/4.

track_user_presence(user, workflow, pid)

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

untrack(socket, key)

Callback implementation for Phoenix.Presence.untrack/2.

untrack(pid, topic, key)

Callback implementation for Phoenix.Presence.untrack/3.

untrack_user_presence(user, workflow, pid)

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

update(socket, key, meta)

Callback implementation for Phoenix.Presence.update/3.

update(pid, topic, key, meta)

Callback implementation for Phoenix.Presence.update/4.