GamendWeb.LiveHelpers (gamend_web v1.0.1216)

Copy Markdown View Source

Shared helpers for LiveViews.

Summary

Functions

Verify the captcha token carried by a phx-submit's params.

Check a rate limit bucket for the given IP.

Extract the client IP from a LiveView socket's connect_info.

Format a common Failed: reason message for LiveViews.

Return the public @username handle for a user, or nil when the user has none (renders as empty in HEEx, so callers can interpolate it directly).

Return a public user label without exposing email addresses.

Put a standard error flash on a LiveView socket.

Put a standard success flash on a LiveView socket.

Functions

check_captcha(socket, params)

@spec check_captcha(Phoenix.LiveView.Socket.t(), map()) ::
  :ok | {:error, Phoenix.LiveView.Socket.t()}

Verify the captcha token carried by a phx-submit's params.

Returns :ok — including whenever the captcha is disabled, so a call site needs no enabled?/0 branch of its own — or {:error, socket} with the failure already handled: a flash explaining which way it failed, and a reset pushed to the widget. The reset is not optional. A token Cloudflare rejected is spent, so without it the form would resubmit the same dead token forever and the player could never recover without a reload.

Pair it with <.captcha> in the form (see GamendWeb.CoreComponents), and read :client_ip off the socket, which both auth LiveViews assign at mount.

check_rate_limit(ip, bucket_type \\ :general)

Check a rate limit bucket for the given IP.

Bucket types:

  • :auth — 30 requests per 60 seconds (matches the HTTP auth bucket)
  • :general — 1200 requests per 60 seconds

Returns :ok or {:error, retry_after_ms}.

client_ip(socket)

Extract the client IP from a LiveView socket's connect_info.

Falls back to "unknown" when the socket has no peer data (e.g. during the initial static render or in tests).

failure_message(prefix, reason)

Format a common Failed: reason message for LiveViews.

public_user_handle(arg1)

Return the public @username handle for a user, or nil when the user has none (renders as empty in HEEx, so callers can interpolate it directly).

public_user_name(id)

Return a public user label without exposing email addresses.

put_failure(socket, message)

Put a standard error flash on a LiveView socket.

put_success(socket, message)

Put a standard success flash on a LiveView socket.