strapi-plugin-comments 1.0.2-beta.1 → 2.0.0-beta.2

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.
Files changed (149) hide show
  1. package/.circleci/config.yml +2 -2
  2. package/README.md +71 -95
  3. package/admin/src/components/ConfirmationDialog/index.js +56 -0
  4. package/admin/src/components/DiscussionStatus/index.js +33 -0
  5. package/admin/src/components/DiscussionThread/index.js +58 -0
  6. package/admin/src/components/DiscussionThreadItem/index.js +87 -0
  7. package/admin/src/components/DiscussionThreadItem/styles.js +87 -0
  8. package/admin/src/components/DiscussionThreadItemActions/index.js +176 -0
  9. package/admin/src/components/DiscussionThreadItemActions/styles.js +22 -0
  10. package/admin/src/components/DiscussionThreadItemMeta/index.js +31 -0
  11. package/admin/src/components/DiscussionThreadItemWarnings/index.js +137 -0
  12. package/admin/src/components/DiscussionThreadItemWarnings/styles.js +7 -0
  13. package/admin/src/components/EntityDetails/index.js +53 -0
  14. package/admin/src/{containers → components}/Initializer/index.js +4 -4
  15. package/admin/src/components/LoadingIndicatorOverlay/index.js +15 -0
  16. package/admin/src/components/LoadingIndicatorOverlay/styles.js +20 -0
  17. package/admin/src/components/Nav/index.js +45 -0
  18. package/admin/src/components/PluginIcon/index.js +12 -0
  19. package/admin/src/components/ReportsReviewModal/index.js +45 -0
  20. package/admin/src/components/ReportsReviewTable/index.js +142 -0
  21. package/admin/src/components/TableFilters/index.js +53 -0
  22. package/admin/src/components/TablePagination/index.js +35 -0
  23. package/admin/src/components/icons/comments.js +9 -0
  24. package/admin/src/index.js +58 -42
  25. package/admin/src/pages/App/index.js +60 -0
  26. package/admin/src/pages/App/reducer/actions.js +8 -0
  27. package/admin/src/pages/App/reducer/constants.js +4 -0
  28. package/admin/src/pages/App/reducer/index.js +27 -0
  29. package/admin/src/pages/App/reducer/init.js +7 -0
  30. package/admin/src/pages/App/reducer/selectors.js +35 -0
  31. package/admin/src/pages/App/utils/api.js +20 -0
  32. package/admin/src/pages/ComingSoonPage/index.js +25 -0
  33. package/admin/src/pages/Details/index.js +109 -0
  34. package/admin/src/pages/Details/utils/api.js +41 -0
  35. package/admin/src/pages/Discover/index.js +203 -0
  36. package/admin/src/pages/Discover/utils/api.js +20 -0
  37. package/admin/src/pages/Discover/utils/filtersSchema.js +9 -0
  38. package/admin/src/permissions.js +13 -33
  39. package/admin/src/pluginId.js +2 -4
  40. package/admin/src/reducers.js +8 -0
  41. package/admin/src/translations/en.json +72 -33
  42. package/admin/src/translations/fr.json +1 -0
  43. package/admin/src/utils/axiosInstance.js +35 -0
  44. package/admin/src/utils/constants.js +28 -0
  45. package/admin/src/utils/getApiUrl.js +5 -0
  46. package/admin/src/utils/getMessage.js +14 -0
  47. package/admin/src/utils/getRelatedGroups.js +5 -0
  48. package/admin/src/utils/getUrl.js +5 -0
  49. package/admin/src/utils/handleAPIError.js +15 -0
  50. package/admin/src/utils/index.js +6 -0
  51. package/codecov.yml +2 -1
  52. package/content-types/comment.js +97 -0
  53. package/content-types/index.js +11 -0
  54. package/content-types/report.js +54 -0
  55. package/package.json +11 -25
  56. package/server/bootstrap.js +5 -0
  57. package/server/config/index.js +10 -0
  58. package/server/controllers/admin.js +134 -0
  59. package/server/controllers/client.js +109 -0
  60. package/server/controllers/index.js +9 -0
  61. package/{controllers → server/controllers}/utils/__tests__/functions.test.js +0 -0
  62. package/{controllers → server/controllers}/utils/functions.js +1 -1
  63. package/server/destroy.js +5 -0
  64. package/server/index.js +22 -0
  65. package/server/register.js +5 -0
  66. package/server/routes/admin.js +92 -0
  67. package/server/routes/client.js +52 -0
  68. package/server/routes/index.js +15 -0
  69. package/server/services/admin.js +186 -0
  70. package/server/services/client.js +227 -0
  71. package/server/services/common.js +191 -0
  72. package/server/services/index.js +11 -0
  73. package/{services → server/services}/utils/__tests__/functions.test.js +7 -9
  74. package/{services → server/services}/utils/functions.js +19 -8
  75. package/server/utils/constants.js +16 -0
  76. package/{services → server}/utils/error.js +0 -0
  77. package/server/utils/functions.js +19 -0
  78. package/strapi-admin.js +3 -0
  79. package/strapi-server.js +9 -0
  80. package/admin/src/assets/images/icon-cross-blue.svg +0 -1
  81. package/admin/src/assets/images/icon_remove.svg +0 -19
  82. package/admin/src/assets/images/logo.svg +0 -5
  83. package/admin/src/components/AbuseReportsPopUp/ItemPreviewContainer.js +0 -8
  84. package/admin/src/components/AbuseReportsPopUp/ModalFooter.js +0 -31
  85. package/admin/src/components/AbuseReportsPopUp/TableContainer.js +0 -63
  86. package/admin/src/components/AbuseReportsPopUp/index.js +0 -148
  87. package/admin/src/components/Container/index.js +0 -7
  88. package/admin/src/components/EmptyView/index.js +0 -25
  89. package/admin/src/components/Filters/index.js +0 -151
  90. package/admin/src/components/Filters/index.styled.js +0 -82
  91. package/admin/src/components/Filters/utils.js +0 -21
  92. package/admin/src/components/Item/CardItem.js +0 -30
  93. package/admin/src/components/Item/CardWrapper.js +0 -10
  94. package/admin/src/components/Item/index.js +0 -92
  95. package/admin/src/components/ItemDetails/CardItem.js +0 -33
  96. package/admin/src/components/ItemDetails/CardLevelCounter.js +0 -46
  97. package/admin/src/components/ItemDetails/CardLevelCounterLink.js +0 -24
  98. package/admin/src/components/ItemDetails/CardLevelWrapper.js +0 -22
  99. package/admin/src/components/ItemDetails/CardWrapper.js +0 -52
  100. package/admin/src/components/ItemDetails/index.js +0 -151
  101. package/admin/src/components/ItemFooter/CardItemAuthor.js +0 -10
  102. package/admin/src/components/ItemFooter/CardItemRelation.js +0 -18
  103. package/admin/src/components/ItemFooter/Wrapper.js +0 -20
  104. package/admin/src/components/ItemFooter/index.js +0 -87
  105. package/admin/src/components/ItemHeader/CardHeaderBlocked.js +0 -24
  106. package/admin/src/components/ItemHeader/CardHeaderIndicator.js +0 -30
  107. package/admin/src/components/ItemHeader/CardHeaderIndicatorBlue.js +0 -11
  108. package/admin/src/components/ItemHeader/CardHeaderIndicatorRed.js +0 -11
  109. package/admin/src/components/ItemHeader/CardHeaderIndicatorsContainer.js +0 -18
  110. package/admin/src/components/ItemHeader/CardHeaderReports.js +0 -40
  111. package/admin/src/components/ItemHeader/Wrapper.js +0 -16
  112. package/admin/src/components/ItemHeader/index.js +0 -80
  113. package/admin/src/components/ItemModeration/Wrapper.js +0 -19
  114. package/admin/src/components/ItemModeration/index.js +0 -47
  115. package/admin/src/components/List/Container.js +0 -20
  116. package/admin/src/components/List/index.js +0 -21
  117. package/admin/src/components/Search/index.js +0 -83
  118. package/admin/src/containers/App/Wrapper.js +0 -14
  119. package/admin/src/containers/App/index.js +0 -47
  120. package/admin/src/containers/DataManagerProvider/actions.js +0 -11
  121. package/admin/src/containers/DataManagerProvider/index.js +0 -280
  122. package/admin/src/containers/DataManagerProvider/init.js +0 -5
  123. package/admin/src/containers/DataManagerProvider/reducer.js +0 -59
  124. package/admin/src/containers/DetailsView/Wrapper.js +0 -18
  125. package/admin/src/containers/DetailsView/index.js +0 -100
  126. package/admin/src/containers/ListView/FadedWrapper.js +0 -42
  127. package/admin/src/containers/ListView/Footer.js +0 -53
  128. package/admin/src/containers/ListView/Wrapper.js +0 -17
  129. package/admin/src/containers/ListView/components.js +0 -138
  130. package/admin/src/containers/ListView/index.js +0 -45
  131. package/admin/src/containers/Panel/actions.js +0 -5
  132. package/admin/src/containers/Panel/index.js +0 -191
  133. package/admin/src/containers/Panel/index.styled.js +0 -6
  134. package/admin/src/containers/Panel/reducer.js +0 -44
  135. package/admin/src/contexts/DataManagerContext.js +0 -5
  136. package/admin/src/hooks/useDataManager.js +0 -6
  137. package/admin/src/lifecycles.js +0 -3
  138. package/admin/src/translations/index.js +0 -7
  139. package/admin/src/utils/getTrad.js +0 -5
  140. package/admin/src/utils/getTradId.js +0 -5
  141. package/config/functions/bootstrap.js +0 -48
  142. package/config/routes.json +0 -116
  143. package/config/schema.graphql.js +0 -270
  144. package/controllers/comments.js +0 -211
  145. package/models/comment.js +0 -8
  146. package/models/comment.settings.json +0 -98
  147. package/models/report.js +0 -8
  148. package/models/report.settings.json +0 -49
  149. package/services/comments.js +0 -486
@@ -1,486 +0,0 @@
1
- 'use strict';
2
-
3
- const { first, isEmpty, isNil, map, uniq, get } = require('lodash');
4
- const { sanitizeEntity } = require('strapi-utils');
5
- const PluginError = require('./utils/error');
6
- const {
7
- isEqualEntity,
8
- extractMeta,
9
- buildNestedStructure,
10
- checkBadWords,
11
- filterOurResolvedReports,
12
- convertContentTypeNameToSlug,
13
- isValidUserContext,
14
- resolveUserContextError,
15
- } = require('./utils/functions');
16
-
17
- /**
18
- * comments.js service
19
- *
20
- * @description: A set of functions similar to controller's actions to avoid code duplication.
21
- */
22
-
23
- module.exports = {
24
- getAssociationModel(ormModel, related) {
25
- const relatedAssociation = ormModel.associations.find(_ => _.alias === 'related');
26
- if (relatedAssociation) {
27
- return relatedAssociation.related.find(rel => rel.collectionName === related);
28
- }
29
- return null;
30
- },
31
-
32
- getMorphModel() {
33
- const { pluginName, model } = extractMeta(strapi.plugins);
34
- const ormModel = strapi.query(model.modelName, pluginName);
35
- return ormModel.model.morph;
36
- },
37
-
38
- isMongoDB(){
39
- const { pluginName, model } = extractMeta(strapi.plugins);
40
- const orm = strapi.query(model.modelName, pluginName).model.orm;
41
- return orm === 'mongoose';
42
- },
43
-
44
- getMorphData(query) {
45
- return this
46
- .getMorphModel()
47
- .query(function () {
48
- this.where(query);
49
- })
50
- .fetchAll()
51
- .then((entities) => entities.map(entity => entity.toJSON()));
52
- },
53
-
54
- // Find all comments
55
- async findAll({ related, entity, ...query }) {
56
- const { pluginName, model } = extractMeta(strapi.plugins);
57
- const paginationEnabled = !isNil(query._start);
58
- const ormModel = strapi.query(model.modelName, pluginName);
59
- let relatedCommentIds = [];
60
- if (related) {
61
- const associationModel = this.getAssociationModel(ormModel, related);
62
- if (associationModel) {
63
- if (this.isMongoDB()) {
64
- const [fieldName] = Object.entries(associationModel.allAttributes).find(([, value]) => value.plugin === 'comments' && value.collection === 'comment') || [];
65
- if (!fieldName){
66
- throw strapi.errors.badRequest();
67
- }
68
- const query = { [fieldName]: { $exists: true, $not: { $size: 0 } } };
69
- if (entity){
70
- query._id = entity;
71
- }
72
- relatedCommentIds = await associationModel
73
- .find(query)
74
- .then((entry) => entry.map(_ => _.toJSON()[fieldName]))
75
- .then(entry => entry.flatMap(comments => comments.map(comment => comment.id)));
76
- } else {
77
- const morphQuery = entity
78
- ? { related_type: related, related_id: entity }
79
- : { related_type: related };
80
- relatedCommentIds = await this
81
- .getMorphData(morphQuery)
82
- .then(_ => _.map(entity => entity.comments_id));
83
- }
84
- } else {
85
- throw strapi.errors.badRequest();
86
- }
87
- }
88
- // we not find any content to related data
89
- if (related && relatedCommentIds.length === 0) {
90
- return {
91
- items: [],
92
- total: 0,
93
- page: 1,
94
- };
95
- }
96
-
97
- const params = {
98
- ...query,
99
- _sort: paginationEnabled ? query._sort || 'created_at:desc' : 'created_at:desc',
100
- };
101
- if (relatedCommentIds.length) {
102
- params.id = relatedCommentIds;
103
- }
104
- const entities = query && query._q ?
105
- await ormModel.search(params, ['authorUser', 'related', 'reports']) :
106
- await ormModel.find(params, ['authorUser', 'related', 'reports']);
107
- const items = entities.map(_ => filterOurResolvedReports(this.sanitizeCommentEntity(_)));
108
- const total = paginationEnabled ?
109
- query && query._q ?
110
- await ormModel.countSearch(params) :
111
- await ormModel.count(params) :
112
- items.length;
113
- return {
114
- items,
115
- total,
116
- page: paginationEnabled ? query._start / query._limit : undefined,
117
- };
118
- },
119
-
120
- // Find comments in the flat structure
121
- async findAllFlat(relation, query, isAdmin = false) {
122
- const { pluginName, model } = extractMeta(strapi.plugins);
123
- let baseCriteria = {};
124
- if (relation) {
125
- baseCriteria = {
126
- ...baseCriteria,
127
- _or: [{ removed_null: true }, { removed: false }],
128
- relatedSlug: relation,
129
- };
130
- }
131
- let criteria = {
132
- ...baseCriteria,
133
- _or: [{ removed_null: true }, { removed: false }],
134
- threadOf_null: true,
135
- };
136
-
137
- if (isAdmin){
138
- delete baseCriteria._or;
139
- delete criteria._or;
140
- }
141
-
142
- if (query) {
143
- criteria = {
144
- ...criteria,
145
- ...query,
146
- };
147
- }
148
-
149
- const entitiesRoot = query && query._q ?
150
- await strapi.query( model.modelName, pluginName)
151
- .search(criteria, ['authorUser', 'related', 'reports']) :
152
- await strapi.query( model.modelName, pluginName)
153
- .find(criteria, ['authorUser', 'related', 'reports']);
154
- const entitiesNested = await strapi.query( model.modelName, pluginName)
155
- .find({
156
- ...baseCriteria,
157
- threadOf_null: false,
158
- }, ['authorUser', 'related', 'reports']);
159
- return [...entitiesRoot, ...entitiesNested].map(_ => filterOurResolvedReports(this.sanitizeCommentEntity(_)));
160
- },
161
-
162
- // Find comments and create relations tree structure
163
- async findAllInHierarchy ({
164
- relation,
165
- query,
166
- startingFromId = null,
167
- dropBlockedThreads = false,
168
- isAdmin = false,
169
- }) {
170
- const entities = await this.findAllFlat(relation, query, isAdmin);
171
- return buildNestedStructure(entities, startingFromId, 'threadOf', dropBlockedThreads, false, this.isMongoDB());
172
- },
173
-
174
- // Find single comment
175
- async findOne(id, relation) {
176
- const { pluginName, model } = extractMeta(strapi.plugins);
177
- let criteria = { id };
178
- if (relation) {
179
- criteria = {
180
- ...criteria,
181
- relatedSlug: relation,
182
- };
183
- }
184
- const entity = await strapi.query( model.modelName, pluginName)
185
- .findOne(criteria, ['related', 'reports']);
186
- if (!entity){
187
- throw strapi.errors.notFound();
188
- }
189
- return filterOurResolvedReports(this.sanitizeCommentEntity(entity));
190
- },
191
-
192
- checkEntityRelation(parsedRelation) {
193
- const result = parsedRelation.some(ref => {
194
- const model = strapi.query(ref);
195
- return model && model.associations.length &&
196
- model.associations.some(_ => _.plugin === 'comments' && _.collection === 'comment');
197
- });
198
- if (!result) {
199
- throw new PluginError(400, 'Comment not have relation to this type content type.');
200
- }
201
- },
202
-
203
- // Create a comment
204
- async create(data, user = undefined) {
205
- const { content, related } = data;
206
- const parsedRelation = related && related instanceof Array ? related : [related];
207
- const singleRelationFulfilled = related && (parsedRelation.length === 1);
208
- if(!singleRelationFulfilled){
209
- throw new PluginError(400, 'Can relate only one content type');
210
- }
211
- const relation = parsedRelation.map(({refId, ref})=> `${ref}:${refId}`).toString();
212
- this.checkEntityRelation(map(parsedRelation, 'ref'));
213
-
214
- const linkToThread = !isNil(data.threadOf) ? !!await this.findOne(data.threadOf,relation) : true;
215
- const validContext = isValidUserContext(user);
216
-
217
- if (!linkToThread) {
218
- throw new PluginError(400, 'Thread does not exist');
219
- }
220
-
221
- if (!validContext) {
222
- throw resolveUserContextError(user);
223
- }
224
-
225
- if (checkBadWords(content) && singleRelationFulfilled) {
226
- const { pluginName, model } = extractMeta(strapi.plugins);
227
- const { authorId, authorEmail, authorName, authorUser, ...rest } = data;
228
- let authorData = {};
229
- if (validContext && user) {
230
- authorData = {
231
- authorUser: user.id ? user.id : authorUser,
232
- };
233
- } else if (authorUser) {
234
- authorData = {
235
- authorUser,
236
- };
237
- } else {
238
- const emailValidation = new RegExp(/\S+@\S+\.\S+/);
239
- if (authorEmail && !emailValidation.test(authorEmail)) {
240
- throw new PluginError(400, 'Author e-mail is not valid value. Key: \'authorEmail\'');
241
- }
242
-
243
- authorData = {
244
- authorId,
245
- authorName,
246
- authorEmail,
247
- };
248
- }
249
-
250
- const relationEntity = await Promise
251
- .all(parsedRelation.map(rel => strapi.query(rel.ref).findOne({ id: rel.refId })));
252
-
253
-
254
- relationEntity.forEach((rel) => {
255
- if (!rel) {
256
- throw new PluginError(404, 'Related entity not found');
257
- }
258
- });
259
-
260
- const entity = await strapi.query( model.modelName, pluginName).create({
261
- ...rest,
262
- ...authorData,
263
- relatedSlug: relation,
264
- related: parsedRelation
265
- });
266
- return this.sanitizeCommentEntity(entity);
267
- }
268
- throw new PluginError(400, 'No content received');
269
- },
270
-
271
- // Update a comment
272
- async update(id, data, user) {
273
- const { content, related } = data;
274
- const { pluginName, model } = extractMeta(strapi.plugins);
275
- const parsedRelation = related && related instanceof Array ? related : [related];
276
- const singleRelationFulfilled = related && (parsedRelation.length === 1);
277
- if(!singleRelationFulfilled){
278
- throw new PluginError(400, 'Can relate only one content type');
279
- }
280
- const relation = parsedRelation.map(({refId, ref})=> `${ref}:${refId}`).toString();
281
- const existingEntity = await this.findOne(id, relation);
282
- const validContext = isValidUserContext(user);
283
-
284
- if (!validContext) {
285
- throw resolveUserContextError(user);
286
- }
287
-
288
- if (isEqualEntity(existingEntity, data, user) && content) {
289
- if (checkBadWords(content)) {
290
- const entity = await strapi.query( model.modelName, pluginName).update(
291
- { id },
292
- { content }
293
- );
294
- return this.sanitizeCommentEntity(entity);
295
- }
296
- }
297
- throw new PluginError(409, 'Action on that entity is not allowed');
298
- },
299
-
300
- // Points up for comment
301
- async pointsUp(id, relation, user) {
302
- if (!isValidUserContext(user)) {
303
- throw resolveUserContextError(user);
304
- }
305
-
306
- const { pluginName, model } = extractMeta(strapi.plugins);
307
- const existingEntity = await this.findOne(id, relation);
308
- if (existingEntity) {
309
- const entity = await strapi.query( model.modelName, pluginName).update(
310
- { id },
311
- {
312
- points: (existingEntity.points || 0) + 1,
313
- }
314
- );
315
- return this.sanitizeCommentEntity(entity);
316
- }
317
- throw new PluginError(409, 'Action on that entity is not allowed');
318
- },
319
-
320
- // Report abuse in comment
321
- async reportAbuse(id, relation, payload, user) {
322
- if (!isValidUserContext(user)) {
323
- throw resolveUserContextError(user);
324
- }
325
- const { pluginName, plugin } = extractMeta(strapi.plugins);
326
- const { report: reportModel } = plugin.models;
327
- const existingEntity = await this.findOne(id, relation);
328
- if (existingEntity) {
329
- return strapi.query(reportModel.modelName, pluginName).create({
330
- ...payload,
331
- resolved: false,
332
- related: id,
333
- });
334
- }
335
- throw new PluginError(409, 'Action on that entity is not allowed');
336
- },
337
-
338
- async markAsRemoved(relationId, commentId, authorId){
339
- const { pluginName, model } = extractMeta(strapi.plugins);
340
- const entity = await this.findOne(
341
- commentId,
342
- relationId,
343
- { _or: [{ authorUser: authorId }, { authorId }] },
344
- );
345
- if (!entity){
346
- throw strapi.errors.notFound('Entity not exist');
347
- }
348
- return strapi
349
- .query(model.modelName, pluginName)
350
- .update({ id: commentId, relatedSlug: relationId }, { removed: true })
351
- .then(({ id }) => this.markAsRemovedNested(id, true))
352
- .then(() => ({ id: commentId }));
353
- },
354
-
355
- //
356
- // Moderation
357
- //
358
-
359
- // Find single comment
360
- async findOneAndThread(id) {
361
- const { pluginName, model } = extractMeta(strapi.plugins);
362
- const entity = await strapi.query( model.modelName, pluginName).findOne({ id }, ['threadOf', 'threadOf.reports', 'authorUser', 'related', 'reports']);
363
- if (!entity){
364
- throw new PluginError(404, 'Not found');
365
- }
366
- const relatedEntity = !isEmpty(entity.related) ? first(entity.related) : null;
367
- const relation = relatedEntity ? `${convertContentTypeNameToSlug(relatedEntity.__contentType).toLowerCase()}:${relatedEntity.id}` : null;
368
- const entitiesOnSameLevel = await this.findAllInHierarchy({
369
- relation,
370
- startingFromId: entity.threadOf ? entity.threadOf.id : null,
371
- isAdmin: true
372
- })
373
- const selectedEntity = filterOurResolvedReports(this.sanitizeCommentEntity(entity));
374
- return {
375
- selected: {
376
- ...selectedEntity,
377
- threadOf: selectedEntity.threadOf ? filterOurResolvedReports(selectedEntity.threadOf) : null,
378
- },
379
- level: entitiesOnSameLevel.map(_ => filterOurResolvedReports(_))
380
- };
381
- },
382
-
383
- // Block / Unblock a comment
384
- async blockComment(id) {
385
- const { pluginName, model } = extractMeta(strapi.plugins);
386
- const existingEntity = await this.findOne(id);
387
- const changedEntity = await strapi.query( model.modelName, pluginName).update(
388
- { id },
389
- { blocked: !existingEntity.blocked }
390
- );
391
- return this.sanitizeCommentEntity(changedEntity);
392
- },
393
-
394
- // Block / Unblock a comment thread
395
- async blockCommentThread(id) {
396
- const { pluginName, model } = extractMeta(strapi.plugins);
397
- const existingEntity = await this.findOne(id);
398
- const changedEntity = await strapi.query( model.modelName, pluginName).update(
399
- { id },
400
- { blockedThread: !existingEntity.blockedThread }
401
- );
402
- await this.blockNestedThreads(id, changedEntity.blockedThread)
403
- return this.sanitizeCommentEntity(changedEntity);
404
- },
405
-
406
- markAsRemovedNested(id, status){
407
- return this.modifiedNestedNestedComments(id, 'removed', status);
408
- },
409
-
410
- blockNestedThreads(id, blockStatus){
411
- return this.modifiedNestedNestedComments(id, 'blockedThread', blockStatus)
412
- },
413
-
414
- async modifiedNestedNestedComments(id, fieldName, value) {
415
- const { pluginName, model } = extractMeta(strapi.plugins);
416
- try {
417
- const entitiesToChange = await strapi.query(model.modelName, pluginName).find({ threadOf: id });
418
- const changedEntities = await Promise.all(entitiesToChange.map(item => strapi.query(model.modelName, pluginName).update(
419
- { id: item.id },
420
- { [fieldName]: value }
421
- )));
422
- if (changedEntities.length) {
423
- const nestedTransactions = await Promise.all(
424
- changedEntities.map(item => this.modifiedNestedNestedComments(item.id, fieldName, value))
425
- );
426
- return nestedTransactions.length === changedEntities.length;
427
- }
428
- return true;
429
- } catch (e) {
430
- return false;
431
- }
432
- },
433
-
434
- async contentTypeName(contentTypeName) {
435
- const { pluginName, model } = extractMeta(strapi.plugins);
436
- const ormModel = strapi.query(model.modelName, pluginName);
437
- const associationModel = this.getAssociationModel(ormModel, contentTypeName);
438
- const config = get(strapi.config, ['plugins', 'comments', 'relatedContentTypes', contentTypeName], []);
439
- if (isEmpty(config)) {
440
- return [];
441
- }
442
- if (associationModel) {
443
- let relatedIds = [];
444
- let result = [];
445
- if (this.isMongoDB()) {
446
- const [fieldName] = Object.entries(associationModel.allAttributes).find(([, value]) => value.plugin === 'comments' && value.collection === 'comment') || [];
447
- if (!fieldName){
448
- throw strapi.errors.badRequest();
449
- }
450
- result = await associationModel
451
- .find({ [fieldName]: { $exists: true, $not: { $size: 0 } } })
452
- .then((entities) => entities.map(_ => _.toJSON()));
453
- } else {
454
- relatedIds = await this
455
- .getMorphData({ related_type: contentTypeName })
456
- .then(_ => uniq(_.map(entity => entity.related_id)));
457
- result = await strapi.query(associationModel.uid).find({ id: relatedIds });
458
- }
459
- const entries = Object.entries(config);
460
- return result
461
- .map(_ =>
462
- entries
463
- .reduce((acc, [key, val]) => ({ ...acc, [key]: _[val] }), {}),
464
- );
465
-
466
- }
467
- throw strapi.errors.badRequest();
468
- },
469
-
470
- // Resolve reported abuse for comment
471
- async resolveAbuseReport(id, commentId) {
472
- const { pluginName, plugin } = extractMeta(strapi.plugins);
473
- const { report: reportModel } = plugin.models;
474
- return strapi.query(reportModel.modelName, pluginName).update({
475
- id,
476
- related: commentId,
477
- }, {
478
- resolved: true,
479
- });
480
- },
481
-
482
- sanitizeCommentEntity: (entity) => ({
483
- ...entity,
484
- authorUser: sanitizeEntity(entity.authorUser, { model: strapi.plugins['users-permissions'].models.user }),
485
- }),
486
- };