Strapi Comments Plugin

repository·master·Indexed 19 days ago

https://github.com/virtuslab-open-source/strapi-plugin-comments

A Strapi-based comments moderation tool providing end-to-end commenting features, including a moderation panel, bad words filtering, and abuse reporting. It supports hierarchical and flat comment structures via REST and GraphQL, RBAC permissions, and configurable approval flows for specific Content Types.

Tokens
18.3K
Snippets
61
Records
73
Agent score
65%

What's inside strapi-plugin-comments

  1. Install the Strapi Comments plugin via command line

    master

    To install the plugin in your Strapi project, use yarn (recommended). After installation, you must rebuild your Strapi instance to make the plugin appear in the sidebar.

    1. Install the package:
      yarn add strapi-plugin-comments@latest
    2. Rebuild and restart your instance:
      yarn build
      yarn develop

    Once running, the Comments plugin will be visible in the Plugins section of the Strapi sidebar.

    yarn add strapi-plugin-comments@latest
    
    yarn build
    yarn develop
  2. Enable avatars for plugin::user-permissions.user authors

    master
    By default, the comment author object includes an avatar field. If you want to use the standard Strapi plugin::user-permissions.user as a comment author and display their profile picture, you must manually add an avatar field to the User model. This field must be of type Media.
  3. Understand the plugin route structure

    master

    The strapi-plugin-comments plugin exposes two distinct sets of routes categorized by their access type:

    1. content-api: These routes are intended for client-side consumption (e.g., your frontend application) to interact with comments. They are defined under the clientRoutes module.
    2. admin: These routes are intended for the Strapi Admin panel to manage comments. They are defined under the adminRoutes module.

    When integrating or extending the plugin, ensure you are targeting the correct route type based on whether the request originates from a user-facing frontend or the Strapi administrative interface.

  4. Configure GraphQL for the Comments plugin

    master

    To use GraphQL with the Comments plugin, you must have @strapi/plugin-graphql installed and enabled.

    Important: When using config/plugins.{js|ts}, place the comments property before the graphql property. This ensures that dynamic types added during the bootstrap stage are correctly added to the GraphQL Schema.

    Inside the gql configuration object, you can set:

    • auth: Determines if GraphQL queries require authentication. Default: false.

    If auth is set to true, you must provide an Authorization header (e.g., Bearer <token>) with your requests.

    {
      "comments": {
        "config": {
          "gql": {
            "auth": true
          }
        }
      },
      "graphql": {
        // ...
      }
    }
  5. Configure the Comments plugin via config/plugins.{js|ts}

    master

    You can configure the plugin using the dedicated Settings page in the Strapi admin panel (recommended for validation) or by modifying your config/plugins.{js|ts} file.

    If you use the file-based configuration, ensure you use the comments key within the exported object. If the file does not exist, create it manually.

    module.exports = ({ env }) => ({
      //...
      comments: {
        enabled: true,
        config: {
          badWords: false,
          moderatorRoles: ["Authenticated"],
          approvalFlow: ["api::page.page"],
          entryLabel: {
            "*": ["Title", "title", "Name", "name", "Subject", "subject"],
            "api::page.page": ["MyField"],
          },
          blockedAuthorProps: ["name", "email"],
          reportReasons: {
            MY_CUSTOM_REASON: "MY_CUSTOM_REASON",
          },
          gql: {
            // ...
          },
        },
      },
      //...
    });
  6. Understand the Admin Plugin Entrypoint

    master

    The admin/src/index.ts file serves as the entrypoint for the Strapi admin panel integration. It defines how the plugin registers itself within the Strapi admin UI, including adding menu links, creating settings sections, and handling internationalization (translations).

    Key lifecycle methods exported by the default object:

    • register(app): Used to inject the plugin into the Strapi admin application. It adds a menu link to the sidebar and creates a settings section for plugin configuration.
    • registerTrads({ locales }): An asynchronous function used to load and prefix plugin translations for the specified locales.
  7. Configure Comments plugin properties

    master

    The following configuration keys are available within the comments.config object:

    • enabledCollections: A list of Collection and Single Types for which the plugin should be enabled, using the format 'api::<collection name>.<content type name>'. Defaults to empty.
    • no-profanity: Enables/disables profanity filtering (uses no-profanity package). Default: true.
    • moderatorRoles: An optional list of role names. Users with these roles receive email notifications for new abuse reports. Requires the Strapi email plugin.
    • approvalFlow: A list of Content Types that require an approval flow before comments are visible. Format: 'api::<collection name>.<content type name>'.
    • entryLabel: An ordered list of property names per Content Type used to generate related entity labels. Keys use the format 'api::<collection name>.<content type name>'. Default formatting is *.
    • reportReasons: An object defining enums for abuse reports. Default values are 'BAD_LANGUAGE', 'DISCRIMINATION', and 'OTHER'.
    • gql: Configuration specific to GraphQL (see Additional GQL Configuration).
    • blockedAuthorProps: A list of author entity properties to be removed from responses on the client side.
  8. Fix GraphQL schema and type issues

    master

    If you are using GraphQL and encounter 403/500 errors or missing types, it is likely because strapi-plugin-graphql is initializing before the comments plugin, preventing types from being injected correctly.

    To fix this, ensure the comments plugin is initialized before the graphql plugin in your config/plugins.{js|ts} file.

    module.exports = {
      comments: { enabled: true },
      graphql: { enabled: true },
    };
  9. Resolve abuse reports

    master

    The plugin provides several mutations to handle abuse reports:

    • resolveAbuseReport: Resolves a single specific report by reportId.
    • resolveCommentMultipleAbuseReports: Resolves a list of reportIds for a specific comment.
    • resolveAllAbuseReportsForComment: Resolves all reports associated with a single comment.
    • resolveAllAbuseReportsForThread: Resolves all reports for a comment and its entire thread.
    • resolveMultipleAbuseReports: Resolves multiple reports by their IDs across the system (requires relation and reportIds).
    # Example: Resolve a single report
    mutation resolveAbuseReport {
      resolveAbuseReport(
        input: {
          id: "2"
          relation: "api::page.page:njx99iv4p4txuqp307ye8625"
          reportId: 15
        }
      ) {
        id
        content
        updatedAt
      }
    }
  10. Retrieve comments in a hierarchical structure

    master

    Use the findAllInHierarchy GraphQL query to fetch comments and their nested children for a specific content relation. This is useful for rendering threaded comment sections.

    Note on Authorship: If you are using Strapi's authentication/authorization (auth/authz), the user context provided by the request takes priority over the author property passed in the payload.

    query {
      findAllInHierarchy(relation: "api::page.page:njx99iv4p4txuqp307ye8625") {
        id
        content
        blocked
        children {
          id
          content
        }
        threadOf {
          id
        }
        author {
          id
          name
        }
      }
    }
  11. Block or unblock a comment

    master

    Use blockComment or unblockComment mutations to moderate individual comments. Both require the comment id and the relation string.

    # Block a comment
    mutation blockComment {
      blockComment(input: {
        id: "2"
        relation: "api::page.page:njx99iv4p4txuqp307ye8625"
      }) {
        id
        blocked
        content
      }
    }
    
    # Unblock a comment
    mutation unblockComment {
      unblockComment(input: {
        id: "2"
        relation: "api::page.page:njx99iv4p4txuqp307ye8625"
      }) {
        id
        blocked
      }
    }