strapi-plugin-comments 2.0.6 → 2.0.7
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/__mocks__/initSetup.js
CHANGED
|
@@ -1,14 +1,32 @@
|
|
|
1
|
-
const { get, set } = require('lodash');
|
|
1
|
+
const { get, set, isEmpty } = require('lodash');
|
|
2
2
|
|
|
3
|
-
module.exports = (config = {}, toStore = false) => {
|
|
4
|
-
const
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
config: { ...config },
|
|
9
|
-
},
|
|
3
|
+
module.exports = (config = {}, toStore = false, database = {}) => {
|
|
4
|
+
const dbConfig = toStore ? {
|
|
5
|
+
plugin: {
|
|
6
|
+
comments: {
|
|
7
|
+
config: { ...config },
|
|
10
8
|
},
|
|
11
|
-
}
|
|
9
|
+
},
|
|
10
|
+
}: {};
|
|
11
|
+
|
|
12
|
+
let mock = {
|
|
13
|
+
db: {
|
|
14
|
+
query: (uid) => {
|
|
15
|
+
const [handler, rest] = uid.split('::');
|
|
16
|
+
const [collection] = rest.split('.');
|
|
17
|
+
const values = get(mock.db, `${handler}.${collection}.records`, []);
|
|
18
|
+
return {
|
|
19
|
+
findOne: async () => new Promise(resolve => resolve(values[0])),
|
|
20
|
+
findMany: async () => new Promise(resolve => resolve(values)),
|
|
21
|
+
findWithCount: async () => new Promise(resolve => resolve([values, values.length])),
|
|
22
|
+
count: async () => new Promise(resolve => resolve(values.length)),
|
|
23
|
+
create: async (value) => new Promise(resolve => resolve(value)),
|
|
24
|
+
update: async (value) => new Promise(resolve => resolve(value)),
|
|
25
|
+
delete: async (value) => new Promise(resolve => resolve(value)),
|
|
26
|
+
};
|
|
27
|
+
},
|
|
28
|
+
...dbConfig,
|
|
29
|
+
},
|
|
12
30
|
getRef: function() {
|
|
13
31
|
return this;
|
|
14
32
|
},
|
|
@@ -78,5 +96,13 @@ module.exports = (config = {}, toStore = false) => {
|
|
|
78
96
|
}
|
|
79
97
|
}
|
|
80
98
|
};
|
|
99
|
+
|
|
100
|
+
if (!isEmpty(database)) {
|
|
101
|
+
Object.keys(database).forEach((uid) => {
|
|
102
|
+
const [handler, collection] = uid.split('::');
|
|
103
|
+
set(mock.db, `${handler}.${collection}.records`, database[uid]);
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
|
|
81
107
|
return mock;
|
|
82
108
|
};
|
package/package.json
CHANGED
|
@@ -5,9 +5,9 @@ const { getPluginService } = require('../../utils/functions');
|
|
|
5
5
|
jest.mock
|
|
6
6
|
|
|
7
7
|
|
|
8
|
-
const setup = function(config) {
|
|
8
|
+
const setup = function(config, toStore, database) {
|
|
9
9
|
Object.defineProperty(global, 'strapi', {
|
|
10
|
-
value: require('../../../__mocks__/initSetup')(config),
|
|
10
|
+
value: require('../../../__mocks__/initSetup')(config, toStore, database),
|
|
11
11
|
writable: true,
|
|
12
12
|
});
|
|
13
13
|
}
|
|
@@ -242,5 +242,134 @@ describe('Test Comments service functions utils', () => {
|
|
|
242
242
|
}
|
|
243
243
|
});
|
|
244
244
|
});
|
|
245
|
+
|
|
246
|
+
describe('Merge related entity', () => {
|
|
247
|
+
beforeEach(() => setup({ enabledCollections: [uid] }));
|
|
248
|
+
const collection = 'api::collection.test';
|
|
249
|
+
const related = `${collection}:1`;
|
|
250
|
+
const comment = {
|
|
251
|
+
id: 1,
|
|
252
|
+
content: 'ABC',
|
|
253
|
+
threadOf: null,
|
|
254
|
+
related,
|
|
255
|
+
authorId: 1,
|
|
256
|
+
authorName: 'Joe Doe',
|
|
257
|
+
authorEmail: 'joe@example.com'
|
|
258
|
+
};
|
|
259
|
+
const relatedEntity = { id: 1, title: 'Test', uid: collection};
|
|
260
|
+
|
|
261
|
+
|
|
262
|
+
test('Should merge related entity', async () => {
|
|
263
|
+
const result = await getPluginService('common').mergeRelatedEntityTo(comment, [relatedEntity]);
|
|
264
|
+
expect(result).toHaveProperty('id', 1);
|
|
265
|
+
expect(result).not.toHaveProperty('related', related);
|
|
266
|
+
expect(result).toHaveProperty(['related', 'title'], relatedEntity.title);
|
|
267
|
+
});
|
|
268
|
+
});
|
|
269
|
+
|
|
270
|
+
describe('Client API', () => {
|
|
271
|
+
const collection = 'api::collection.test';
|
|
272
|
+
const related = `${collection}:1`;
|
|
273
|
+
const db = [{
|
|
274
|
+
id: 1,
|
|
275
|
+
content: 'ABC',
|
|
276
|
+
threadOf: null,
|
|
277
|
+
related,
|
|
278
|
+
authorId: 1,
|
|
279
|
+
authorName: 'Joe Doe',
|
|
280
|
+
authorEmail: 'joe@example.com'
|
|
281
|
+
}, {
|
|
282
|
+
id: 2,
|
|
283
|
+
content: 'DEF',
|
|
284
|
+
threadOf: 1,
|
|
285
|
+
related,
|
|
286
|
+
authorId: 1,
|
|
287
|
+
authorName: 'Joe Doe',
|
|
288
|
+
authorEmail: 'joe@example.com',
|
|
289
|
+
blockedThread: true,
|
|
290
|
+
}, {
|
|
291
|
+
id: 3,
|
|
292
|
+
content: 'GHJ',
|
|
293
|
+
threadOf: null,
|
|
294
|
+
related,
|
|
295
|
+
authorId: 1,
|
|
296
|
+
authorName: 'Joe Doe',
|
|
297
|
+
authorEmail: 'joe@example.com'
|
|
298
|
+
}, {
|
|
299
|
+
id: 4,
|
|
300
|
+
content: 'IKL',
|
|
301
|
+
threadOf: 2,
|
|
302
|
+
related,
|
|
303
|
+
authorId: 1,
|
|
304
|
+
authorName: 'Joe Doe',
|
|
305
|
+
authorEmail: 'joe@example.com',
|
|
306
|
+
}];
|
|
307
|
+
const relatedEntity = { id: 1, title: 'Test', uid: collection};
|
|
308
|
+
|
|
309
|
+
beforeEach(() => setup({ enabledCollections: [collection] }, true, {
|
|
310
|
+
'plugins::comments': db,
|
|
311
|
+
'api::collection': [relatedEntity],
|
|
312
|
+
}));
|
|
313
|
+
|
|
314
|
+
describe('findAllFlat', () => {
|
|
315
|
+
test('Should return proper structure', async () => {
|
|
316
|
+
const result = await getPluginService('common').findAllFlat({ query: { related }}, relatedEntity);
|
|
317
|
+
expect(result).toHaveProperty('data');
|
|
318
|
+
expect(result).not.toHaveProperty('meta');
|
|
319
|
+
expect(result.data.length).toBe(4);
|
|
320
|
+
expect(result).toHaveProperty(['data', 0, 'content'], db[0].content);
|
|
321
|
+
expect(result).toHaveProperty(['data', 3, 'content'], db[3].content);
|
|
322
|
+
});
|
|
323
|
+
|
|
324
|
+
test('Should return structure with pagination', async () => {
|
|
325
|
+
const result = await getPluginService('common').findAllFlat({ query: { related }, pagination: { page: 1, pageSize: 5 }}, relatedEntity);
|
|
326
|
+
expect(result).toHaveProperty('data');
|
|
327
|
+
expect(result).toHaveProperty('meta');
|
|
328
|
+
expect(result.data.length).toBe(4);
|
|
329
|
+
expect(result).toHaveProperty(['data', 0, 'content'], db[0].content);
|
|
330
|
+
expect(result).toHaveProperty(['data', 3, 'content'], db[3].content);
|
|
331
|
+
expect(result).toHaveProperty(['meta', 'pagination', 'page'], 1);
|
|
332
|
+
expect(result).toHaveProperty(['meta', 'pagination', 'pageSize'], 5);
|
|
333
|
+
});
|
|
334
|
+
});
|
|
335
|
+
|
|
336
|
+
describe('findAllInHierarchy', () => {
|
|
337
|
+
test('Should return nested structure starting for root', async () => {
|
|
338
|
+
const result = await getPluginService('common').findAllInHierarchy({ query: { related }}, relatedEntity);
|
|
339
|
+
expect(result).not.toHaveProperty('data');
|
|
340
|
+
expect(result).not.toHaveProperty('meta');
|
|
341
|
+
expect(result.length).toBe(2);
|
|
342
|
+
expect(result).toHaveProperty([0, 'content'], db[0].content);
|
|
343
|
+
expect(result).toHaveProperty([0, 'children']);
|
|
344
|
+
expect(result[0].children.length).toBe(1);
|
|
345
|
+
expect(result).toHaveProperty([0, 'children', 0, 'content'], 'DEF');
|
|
346
|
+
expect(result).toHaveProperty([0, 'children', 0, 'children']);
|
|
347
|
+
expect(result[0].children[0].children.length).toBe(1);
|
|
348
|
+
expect(result).toHaveProperty([0, 'children', 0, 'children', 0, 'content'], 'IKL');
|
|
349
|
+
expect(result).toHaveProperty([1, 'content'], db[2].content);
|
|
350
|
+
});
|
|
351
|
+
|
|
352
|
+
test('Should return nested structure starting for comment id: 1', async () => {
|
|
353
|
+
const result = await getPluginService('common').findAllInHierarchy({ query: { related }, startingFromId: 1}, relatedEntity);
|
|
354
|
+
expect(result).not.toHaveProperty('data');
|
|
355
|
+
expect(result).not.toHaveProperty('meta');
|
|
356
|
+
expect(result.length).toBe(1);
|
|
357
|
+
expect(result).toHaveProperty([0, 'content'], db[1].content);
|
|
358
|
+
expect(result).toHaveProperty([0, 'children']);
|
|
359
|
+
expect(result[0].children.length).toBe(1);
|
|
360
|
+
expect(result).toHaveProperty([0, 'children', 0, 'content'], 'IKL');
|
|
361
|
+
});
|
|
362
|
+
|
|
363
|
+
test('Should return nested structure starting for comment id: 1 without blocked comments', async () => {
|
|
364
|
+
const result = await getPluginService('common').findAllInHierarchy({ query: { related }, startingFromId: 1, dropBlockedThreads: true}, relatedEntity);
|
|
365
|
+
expect(result).not.toHaveProperty('data');
|
|
366
|
+
expect(result).not.toHaveProperty('meta');
|
|
367
|
+
expect(result.length).toBe(1);
|
|
368
|
+
expect(result).toHaveProperty([0, 'content'], db[1].content);
|
|
369
|
+
expect(result).toHaveProperty([0, 'children']);
|
|
370
|
+
expect(result[0].children.length).toBe(0);
|
|
371
|
+
});
|
|
372
|
+
});
|
|
373
|
+
});
|
|
245
374
|
});
|
|
246
375
|
});
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
3
|
const BadWordsFilter = require('bad-words');
|
|
4
|
-
const { isArray, isNumber, isObject, isNil, isString, first, parseInt, set, get } = require('lodash');
|
|
4
|
+
const { isArray, isNumber, isObject, isNil, isString, isEmpty, first, parseInt, set, get } = require('lodash');
|
|
5
5
|
const { REGEX, CONFIG_PARAMS } = require('../utils/constants');
|
|
6
6
|
const PluginError = require('./../utils/error');
|
|
7
7
|
const {
|
|
@@ -143,6 +143,8 @@ module.exports = ({ strapi }) => ({
|
|
|
143
143
|
}));
|
|
144
144
|
|
|
145
145
|
const relatedEntities = relatedEntity !== null ? [relatedEntity] : await this.findRelatedEntitiesFor([...entries]);
|
|
146
|
+
const hasRelatedEntitiesToMap = relatedEntities.filter(_ => _).length > 0;
|
|
147
|
+
|
|
146
148
|
const result = entries
|
|
147
149
|
.map(_ => {
|
|
148
150
|
const threadedItem = entriesWithThreads.find(item => item.id === _.id);
|
|
@@ -154,16 +156,12 @@ module.exports = ({ strapi }) => ({
|
|
|
154
156
|
});
|
|
155
157
|
});
|
|
156
158
|
|
|
157
|
-
if (relatedEntities.filter(_ => _).length > 0) {
|
|
158
|
-
return {
|
|
159
|
-
data: result.map(_ => this.mergeRelatedEntityTo(_, relatedEntities)),
|
|
160
|
-
meta,
|
|
161
|
-
};
|
|
162
|
-
}
|
|
163
159
|
return {
|
|
164
|
-
data:
|
|
165
|
-
|
|
166
|
-
|
|
160
|
+
data: hasRelatedEntitiesToMap ?
|
|
161
|
+
result.map(_ => this.mergeRelatedEntityTo(_, relatedEntities)) :
|
|
162
|
+
result,
|
|
163
|
+
...(isEmpty(meta) ? {} : { meta }),
|
|
164
|
+
};
|
|
167
165
|
},
|
|
168
166
|
|
|
169
167
|
// Find comments and create relations tree structure
|
|
@@ -174,8 +172,8 @@ module.exports = ({ strapi }) => ({
|
|
|
174
172
|
startingFromId = null,
|
|
175
173
|
dropBlockedThreads = false,
|
|
176
174
|
}, relatedEntity) {
|
|
177
|
-
const entities = await this.findAllFlat({ query, populate, sort }, relatedEntity)
|
|
178
|
-
return buildNestedStructure(entities, startingFromId, 'threadOf', dropBlockedThreads, false);
|
|
175
|
+
const entities = await this.findAllFlat({ query, populate, sort }, relatedEntity);
|
|
176
|
+
return buildNestedStructure(entities?.data, startingFromId, 'threadOf', dropBlockedThreads, false);
|
|
179
177
|
},
|
|
180
178
|
|
|
181
179
|
// Find single comment
|