PhoenixKit.Modules.Comments (phoenix_kit v1.7.71)

Copy Markdown View Source

Standalone, resource-agnostic comments module.

Provides polymorphic commenting for any resource type (posts, entities, tickets, etc.) with unlimited threading, likes/dislikes, and moderation support.

Architecture

Comments are linked to resources via resource_type (string) + resource_uuid (UUID). No foreign key constraints on the resource side — any module can use comments.

Resource Handler Callbacks

Modules that consume comments can register handlers to receive notifications when comments are created or deleted. Configure in your app:

config :phoenix_kit, :comment_resource_handlers, %{
  "post" => PhoenixKit.Modules.Posts
}

Handler modules should implement on_comment_created/3 and on_comment_deleted/3.

Core Functions

System Management

Comment CRUD

Moderation

Like/Dislike

Summary

Functions

Sets a comment's status to published.

Bulk-updates status for multiple comment UUIDs.

Checks if a user has disliked a comment.

Checks if a user has liked a comment.

Returns aggregate statistics for all comments.

Deletes a comment.

Disables the Comments module.

User dislikes a comment. Creates dislike record and increments counter.

Enables the Comments module.

Checks if the Comments module is enabled.

Gets a single comment by ID with optional preloads.

Gets a single comment by ID with optional preloads.

Gets nested comment tree for a resource.

Gets the Comments module configuration with statistics.

Returns the configured maximum comment depth.

Returns the configured maximum comment length.

Sets a comment's status to hidden.

User likes a comment. Creates like record and increments counter.

Lists all comments across all resource types with filters.

Lists all dislikes for a comment.

Lists all likes for a comment.

Lists comments for a resource (flat list).

Resolves resource context (title and admin path) for a list of comments.

User removes dislike from a comment. Deletes dislike record and decrements counter.

User unlikes a comment. Deletes like record and decrements counter.

Updates a comment.

Functions

approve_comment(comment)

Sets a comment's status to published.

bulk_update_status(comment_uuids, status)

Bulk-updates status for multiple comment UUIDs.

comment_disliked_by?(comment_uuid, user_uuid)

Checks if a user has disliked a comment.

comment_liked_by?(comment_uuid, user_uuid)

Checks if a user has liked a comment.

comment_stats()

Returns aggregate statistics for all comments.

count_comments(resource_type, resource_uuid, opts \\ [])

Counts comments for a resource.

create_comment(resource_type, resource_uuid, user_uuid, attrs)

Creates a comment on a resource.

Automatically calculates depth from parent. Invokes resource handler callback if configured.

Parameters

  • resource_type - Type of resource (e.g., "post")
  • resource_uuid - UUID of the resource
  • user_uuid - UUID of commenter
  • attrs - Comment attributes (content, parent_uuid, etc.)

delete_comment(comment)

Deletes a comment.

Cascades to child comments. Invokes resource handler callback if configured.

disable_system()

Disables the Comments module.

dislike_comment(comment_uuid, user_uuid)

User dislikes a comment. Creates dislike record and increments counter.

enable_system()

Enables the Comments module.

enabled?()

Checks if the Comments module is enabled.

get_comment(id, opts \\ [])

Gets a single comment by ID with optional preloads.

Returns nil if not found.

get_comment!(id, opts \\ [])

Gets a single comment by ID with optional preloads.

Raises Ecto.NoResultsError if not found.

get_comment_tree(resource_type, resource_uuid)

Gets nested comment tree for a resource.

Returns all published comments organized in a tree structure.

get_config()

Gets the Comments module configuration with statistics.

get_max_depth()

Returns the configured maximum comment depth.

get_max_length()

Returns the configured maximum comment length.

hide_comment(comment)

Sets a comment's status to hidden.

like_comment(comment_uuid, user_uuid)

User likes a comment. Creates like record and increments counter.

list_all_comments(opts \\ [])

Lists all comments across all resource types with filters.

Options

  • :resource_type - Filter by resource type
  • :status - Filter by status
  • :user_uuid - Filter by user
  • :search - Search in content
  • :page - Page number (default: 1)
  • :per_page - Items per page (default: 20)

list_comment_dislikes(comment_uuid, opts \\ [])

Lists all dislikes for a comment.

list_comment_likes(comment_uuid, opts \\ [])

Lists all likes for a comment.

list_comments(resource_type, resource_uuid, opts \\ [])

Lists comments for a resource (flat list).

Options

  • :preload - Associations to preload
  • :status - Filter by status

resolve_resource_context(comments)

Resolves resource context (title and admin path) for a list of comments.

Returns a map of {resource_type, resource_uuid} => %{title: ..., path: ...} by delegating to registered comment_resource_handlers that implement resolve_comment_resources/1.

undislike_comment(comment_uuid, user_uuid)

User removes dislike from a comment. Deletes dislike record and decrements counter.

unlike_comment(comment_uuid, user_uuid)

User unlikes a comment. Deletes like record and decrements counter.

update_comment(comment, attrs)

Updates a comment.

Parameters

  • comment - Comment to update
  • attrs - Attributes to update (content, status)