VirtusLab-Open-Source / strapi-plugin-comments

A plugin for Strapi Headless CMS that provides end to end comments feature with their moderation panel, bad words filtering, abuse reporting and more.
MIT License
413 stars 65 forks source link

How to filter only comments that are not in an thread #201

Closed OliveiraCleidson closed 1 year ago

OliveiraCleidson commented 1 year ago

How to filter only comments that are not in a thread

I'm working on one feature that gets comments with pagination. I won't get comments sorted by created date and only comments that are root comments, then I'm using the REST API in flat mode.

Bugs (?)

But when I try to filter using threadOf the server returns 5xx. image

When I use threadOf with id, the threadOf field does not return the correct content inner threadOf, but the filter works. image

When I try to use threadFirstItemId image

image

My dependencies:

{
"dependencies": {
      "@strapi-community/strapi-provider-upload-google-cloud-storage": "^4.3.6",
      "@strapi/plugin-documentation": "^4.4.1",
      "@strapi/plugin-i18n": "4.6.1",
      "@strapi/plugin-sentry": "^4.5.0",
      "@strapi/plugin-users-permissions": "4.6.1",
      "@strapi/strapi": "4.6.1",
      "elastic-apm-node": "^3.40.1",
      "pg": "8.6.0",
      "sharp": "^0.31.2",
      "strapi-plugin-comments": "^2.2.4",
      "strapi-plugin-import-export-entries": "^1.18.0"
  }
}

My plugin.ts

comments: {
    enabled: true,
    config: {
      // ##Documentation https://market.strapi.io/plugins/strapi-plugin-comments
    },
}

My node version Is 16.17.0

cyp3rius commented 1 year ago

Hello @OliveiraCleidson ,

So step by step :)

  1. filters[threadOf][$null]=true

It causes 500 because of following error and that's purely Strapi error. I assume the case here is with relation property.

{
  threadOf: { '$null': 'true' },
  '$or': [ { removed: [Object] }, { removed: false } ],
  related: 'api::page.page:1'
}
[2023-04-07 15:47:25.215] error: Only $and, $or and $not can only be used as root level operators. Found $null.
Error: Only $and, $or and $not can only be used as root level operators. Found $null.
    at processWhere (/Users/mziarko/Projects/open-source/strapi-v4/node_modules/@strapi/database/lib/query/helpers/where.js:140:13)
    at processNested (/Users/mziarko/Projects/open-source/strapi-v4/node_modules/@strapi/database/lib/query/helpers/where.js:116:12)
    at processWhere (/Users/mziarko/Projects/open-source/strapi-v4/node_modules/@strapi/database/lib/query/helpers/where.js:161:25)
  1. filters[threadOf][id][$null]=true

This returned value is our error in reality as we're getting 1:1 from query. I will fix that in next release, but such query is correct.

  1. The threadFirstItemId is not DB field but calculated property. You can't query by it.
OliveiraCleidson commented 1 year ago

Exists a method that gets only root comments in a request?

I can work on this fix.

cyp3rius commented 1 year ago

Nope, there is no such dedicated method - ideally it should be done by the way you described.

If you can fix point 2. we would be grateful :) Should be fairly quick one liner at the end of service method.

cyp3rius commented 1 year ago

Released as part of v2.2.10