strapi-plugin-comments 2.0.5 → 2.0.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -84,7 +84,7 @@ In our minimum support we're following [official Node.js releases timelines](htt
84
84
 
85
85
  **Supported Strapi versions**:
86
86
 
87
- - Strapi v4.0.8 (recently tested)
87
+ - Strapi v4.1.0 (recently tested)
88
88
  - Strapi v4.x
89
89
 
90
90
  > This plugin is designed for **Strapi v4** and is not working with v3.x. To get version for **Strapi v3** install version [v1.x](https://github.com/VirtusLab-Open-Source/strapi-plugin-comments/tree/strapi-v3).
@@ -109,6 +109,8 @@ To setup amend default plugin configuration we recommend to put following snippe
109
109
  module.exports = ({ env }) => ({
110
110
  //...
111
111
  comments: {
112
+ enabled: true,
113
+ config: {
112
114
  badWords: false,
113
115
  moderatorRoles: ["Authenticated"],
114
116
  approvalFlow: ['api::page.page'],
@@ -122,6 +124,7 @@ To setup amend default plugin configuration we recommend to put following snippe
122
124
  gql: {
123
125
  // ...
124
126
  },
127
+ },
125
128
  },
126
129
  //...
127
130
  });
@@ -144,8 +147,8 @@ All you need to do is to install and enable `@strapi/plugin-graphql` for you ins
144
147
  ```json
145
148
  {
146
149
  // ...
147
- gql: {
148
- auth: true, // Default: false
150
+ "gql": {
151
+ "auth": true, // Default: false
149
152
  },
150
153
  // ...
151
154
  }
@@ -226,10 +229,10 @@ Return a hierarchical tree structure of comments for specified instance of Conte
226
229
  [
227
230
  {
228
231
  // -- Comment Model fields ---,
229
- children: [
232
+ "children": [
230
233
  {
231
234
  // -- Comment Model fields ---,
232
- children: [
235
+ "children": [
233
236
  // ...
234
237
  ]
235
238
  },
@@ -255,7 +258,8 @@ Return a flat structure of comments for specified instance of Content Type like
255
258
  **Example response body**
256
259
 
257
260
  ```json
258
- [
261
+ {
262
+ "data": [
259
263
  {
260
264
  // -- Comment Model fields ---
261
265
  },
@@ -263,7 +267,13 @@ Return a flat structure of comments for specified instance of Content Type like
263
267
  // -- Comment Model fields ---
264
268
  },
265
269
  // ...
266
- ]
270
+ ],
271
+ "meta": {
272
+ "pagination": {
273
+ // payload based on Strapi REST Pagination specification
274
+ }
275
+ }
276
+ }
267
277
  ```
268
278
 
269
279
  **Possible response codes**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "strapi-plugin-comments",
3
- "version": "2.0.5",
3
+ "version": "2.0.6",
4
4
  "description": "Strapi - Comments plugin",
5
5
  "strapi": {
6
6
  "name": "comments",
@@ -18,8 +18,8 @@
18
18
  "url": "https://github.com/VirtusLab-Open-Source/strapi-plugin-comments"
19
19
  },
20
20
  "dependencies": {
21
- "@strapi/utils": "^4.0.8",
22
- "@strapi/plugin-graphql": "^4.0.8",
21
+ "@strapi/utils": "^4.1.0",
22
+ "@strapi/plugin-graphql": "^4.1.0",
23
23
  "bad-words": "^3.0.4"
24
24
  },
25
25
  "devDependencies": {
@@ -10,7 +10,7 @@ module.exports = ({ strapi, nexus }) => {
10
10
  const { args } = getService('internals');
11
11
 
12
12
  return {
13
- type: nonNull(list('CommentSingle')),
13
+ type: 'ResponseFindAll',
14
14
  args: {
15
15
  relation: nonNull(stringArg()),
16
16
  filters: getPluginService('gql').buildContentTypeFilters(contentType),
@@ -11,6 +11,9 @@ const typesFactories = [
11
11
  require('./report'),
12
12
  require('./report-reason'),
13
13
  require('./create-report'),
14
+ require('./response-pagination'),
15
+ require('./response-meta'),
16
+ require('./response-find-all'),
14
17
  ];
15
18
 
16
19
  module.exports = context => typesFactories.map(factory => factory(context));
@@ -0,0 +1,7 @@
1
+ module.exports = ({ nexus }) => nexus.objectType({
2
+ name: "ResponseFindAll",
3
+ definition(t) {
4
+ t.list.field("data", { type: 'CommentSingle' })
5
+ t.field("meta", { type: 'ResponseMeta' })
6
+ }
7
+ })
@@ -0,0 +1,6 @@
1
+ module.exports = ({ nexus }) => nexus.objectType({
2
+ name: "ResponseMeta",
3
+ definition(t) {
4
+ t.field("pagination", { type: 'ResponsePagination' })
5
+ }
6
+ })
@@ -0,0 +1,11 @@
1
+ module.exports = ({ nexus }) => nexus.objectType({
2
+ name: "ResponsePagination",
3
+ definition(t) {
4
+ t.int('page')
5
+ t.int('pageSize')
6
+ t.int('pageCount')
7
+ t.int('total')
8
+ t.int('start')
9
+ t.int('limit')
10
+ }
11
+ })
@@ -68,12 +68,35 @@ module.exports = ({ strapi }) => ({
68
68
  };
69
69
  }
70
70
 
71
+ let meta = {};
71
72
  if (pagination && isObject(pagination)) {
72
- const { page = 1, pageSize = 10 } = pagination;
73
+ const parsedPagination = Object.keys(pagination).reduce((prev, curr) => ({
74
+ ...prev,
75
+ [curr]: parseInt(pagination[curr]),
76
+ }), {});
77
+ const { page, pageSize = 10, limit, start = 0 } = parsedPagination
78
+ const paginationByPage = !isNil(page);
79
+
73
80
  queryExtension = {
74
81
  ...queryExtension,
75
- offset: (page - 1) * pageSize,
76
- limit: pageSize,
82
+ offset: paginationByPage ? (page - 1) * pageSize : start,
83
+ limit: paginationByPage ? pageSize : limit,
84
+ };
85
+
86
+ const metaPagination = paginationByPage ? {
87
+ pagination: {
88
+ page,
89
+ pageSize,
90
+ },
91
+ } : {
92
+ pagination: {
93
+ start,
94
+ limit,
95
+ },
96
+ };
97
+
98
+ meta = {
99
+ ...metaPagination,
77
100
  };
78
101
  }
79
102
 
@@ -89,6 +112,26 @@ module.exports = ({ strapi }) => ({
89
112
  ...queryExtension,
90
113
  });
91
114
 
115
+ if (pagination?.withCount && (pagination.withCount === 'true' || pagination.withCount === true)) {
116
+ const total = await strapi.db.query(getModelUid('comment'))
117
+ .count({
118
+ where: {
119
+ ...query,
120
+ }
121
+ });
122
+ const pageCount = Math.floor(total / meta.pagination.pageSize);
123
+ meta = {
124
+ ...meta,
125
+ pagination: {
126
+ ...meta.pagination,
127
+ pageCount: !isNil(meta.pagination.page) ?
128
+ total % meta.pagination.pageSize === 0 ? pageCount : pageCount + 1 :
129
+ undefined,
130
+ total,
131
+ },
132
+ };
133
+ }
134
+
92
135
  const entriesWithThreads = await Promise.all(entries.map(async _ => {
93
136
  const [nestedEntries, count] = await strapi.db.query(getModelUid('comment'))
94
137
  .findWithCount({
@@ -112,9 +155,15 @@ module.exports = ({ strapi }) => ({
112
155
  });
113
156
 
114
157
  if (relatedEntities.filter(_ => _).length > 0) {
115
- return result.map(_ => this.mergeRelatedEntityTo(_, relatedEntities));
158
+ return {
159
+ data: result.map(_ => this.mergeRelatedEntityTo(_, relatedEntities)),
160
+ meta,
161
+ };
116
162
  }
117
- return result;
163
+ return {
164
+ data: result,
165
+ meta,
166
+ };
118
167
  },
119
168
 
120
169
  // Find comments and create relations tree structure
@@ -125,7 +174,7 @@ module.exports = ({ strapi }) => ({
125
174
  startingFromId = null,
126
175
  dropBlockedThreads = false,
127
176
  }, relatedEntity) {
128
- const entities = await this.findAllFlat({ query, populate, sort }, relatedEntity);
177
+ const entities = await this.findAllFlat({ query, populate, sort }, relatedEntity)?.data;
129
178
  return buildNestedStructure(entities, startingFromId, 'threadOf', dropBlockedThreads, false);
130
179
  },
131
180