strapi-plugin-comments 2.0.0-beta.4 → 2.0.0-rc.1
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 +421 -72
- package/admin/src/components/DiscussionThread/index.js +9 -2
- package/admin/src/components/DiscussionThreadItem/index.js +27 -12
- package/admin/src/components/DiscussionThreadItemActions/index.js +61 -50
- package/admin/src/components/DiscussionThreadItemActions/styles.js +0 -4
- package/admin/src/components/DiscussionThreadItemApprovalFlowActions/index.js +81 -0
- package/admin/src/components/DiscussionThreadItemApprovalFlowActions/styles.js +6 -0
- package/admin/src/components/DiscussionThreadItemMeta/index.js +16 -5
- package/admin/src/components/DiscussionThreadItemWarnings/index.js +16 -10
- package/admin/src/components/Nav/index.js +5 -1
- package/admin/src/components/ReportsReviewModal/index.js +5 -0
- package/admin/src/components/ReportsReviewTable/index.js +60 -52
- package/admin/src/components/icons/review.js +2 -2
- package/admin/src/index.js +1 -1
- package/admin/src/pages/Details/components/DetailsEntity/index.js +66 -0
- package/admin/src/pages/Details/components/DetailsFilters/index.js +37 -0
- package/admin/src/pages/Details/components/DetailsFilters/styles.js +8 -0
- package/admin/src/pages/Details/index.js +75 -42
- package/admin/src/pages/Discover/components/DiscoverTableRow/index.js +69 -48
- package/admin/src/pages/Discover/components/DiscoverTableRow/styles.js +8 -0
- package/admin/src/pages/Discover/index.js +72 -56
- package/admin/src/pages/{Details/utils → utils}/api.js +15 -3
- package/admin/src/permissions.js +10 -8
- package/admin/src/translations/en.json +17 -4
- package/admin/src/utils/constants.js +3 -6
- package/admin/src/utils/getMessage.js +1 -1
- package/admin/src/utils/getRelatedGroups.js +1 -3
- package/admin/src/utils/index.js +5 -1
- package/admin/src/utils/renderEntryTitle.js +14 -0
- package/admin/src/utils/renderInitials.js +5 -0
- package/admin/src/utils/resolveCommentStatus.js +30 -0
- package/admin/src/utils/resolveCommentStatusColor.js +22 -0
- package/package.json +8 -3
- package/permissions.js +19 -0
- package/public/assets/logo.png +0 -0
- package/public/assets/preview.png +0 -0
- package/server/bootstrap.js +38 -2
- package/server/config/index.js +9 -3
- package/server/controllers/admin.js +3 -3
- package/server/controllers/client.js +4 -18
- package/server/controllers/utils/parsers.js +17 -0
- package/server/graphql/index.js +26 -0
- package/server/graphql/mutations/createAbuseReport.js +20 -0
- package/server/graphql/mutations/createComment.js +20 -0
- package/server/graphql/mutations/index.js +21 -0
- package/server/graphql/mutations/removeComment.js +20 -0
- package/server/graphql/mutations/updateComment.js +20 -0
- package/server/graphql/queries/findAllFlat.js +29 -0
- package/server/graphql/queries/findAllInHierarchy.js +31 -0
- package/server/graphql/queries/index.js +19 -0
- package/server/graphql/resolvers-config.js +10 -0
- package/server/graphql/types/comment-author-user.js +9 -0
- package/server/graphql/types/comment-author.js +9 -0
- package/server/graphql/types/comment-nested.js +16 -0
- package/server/graphql/types/comment.js +16 -0
- package/server/graphql/types/create-comment-author.js +9 -0
- package/server/graphql/types/create-comment.js +9 -0
- package/server/graphql/types/create-report.js +9 -0
- package/server/graphql/types/id.js +24 -0
- package/server/graphql/types/identify-comment-author.js +6 -0
- package/server/graphql/types/index.js +17 -0
- package/server/graphql/types/remove-comment.js +8 -0
- package/server/graphql/types/report-reason.js +10 -0
- package/server/graphql/types/report.js +11 -0
- package/server/graphql/types/update-comment.js +9 -0
- package/server/register.js +3 -1
- package/server/services/admin.js +42 -15
- package/server/services/client.js +54 -38
- package/server/services/common.js +38 -67
- package/server/services/graphql.js +190 -0
- package/server/services/index.js +2 -0
- package/server/services/utils/functions.js +24 -17
- package/server/utils/constants.js +3 -3
- package/strapi-server.js +33 -0
- package/admin/src/components/EntityDetails/index.js +0 -53
package/server/services/admin.js
CHANGED
|
@@ -1,14 +1,12 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
3
|
const { getPluginService, parseParams } = require('./../utils/functions');
|
|
4
|
-
const {
|
|
5
|
-
const { first, isEmpty, isNil, uniq, get } = require('lodash');
|
|
4
|
+
const { isEmpty, isNil, isNumber, parseInt } = require('lodash');
|
|
6
5
|
const PluginError = require('./../utils/error');
|
|
7
6
|
const {
|
|
8
7
|
getModelUid,
|
|
9
8
|
getRelatedGroups,
|
|
10
9
|
filterOurResolvedReports,
|
|
11
|
-
convertContentTypeNameToSlug
|
|
12
10
|
} = require('./utils/functions');
|
|
13
11
|
const { APPROVAL_STATUS, REGEX } = require('./../utils/constants')
|
|
14
12
|
|
|
@@ -25,8 +23,14 @@ const { APPROVAL_STATUS, REGEX } = require('./../utils/constants')
|
|
|
25
23
|
// Config
|
|
26
24
|
async config() {
|
|
27
25
|
const entryLabel = this.getCommonService().getConfig('entryLabel');
|
|
26
|
+
const approvalFlow = this.getCommonService().getConfig('approvalFlow');
|
|
28
27
|
return {
|
|
29
28
|
entryLabel,
|
|
29
|
+
approvalFlow,
|
|
30
|
+
regex: Object.keys(REGEX).reduce((prev, curr) => ({
|
|
31
|
+
...prev,
|
|
32
|
+
[curr]: REGEX[curr].toString(),
|
|
33
|
+
}), {}),
|
|
30
34
|
};
|
|
31
35
|
},
|
|
32
36
|
|
|
@@ -34,10 +38,18 @@ const { APPROVAL_STATUS, REGEX } = require('./../utils/constants')
|
|
|
34
38
|
async findAll({ related, entity, ...query }) {
|
|
35
39
|
const { _q, orderBy, pageSize = 10, page = 1, filters, ...rest } = parseParams(query);
|
|
36
40
|
|
|
41
|
+
const defaultWhere = {
|
|
42
|
+
$or: [
|
|
43
|
+
{ removed: false },
|
|
44
|
+
{ removed: null },
|
|
45
|
+
],
|
|
46
|
+
};
|
|
47
|
+
|
|
37
48
|
let params = {
|
|
38
49
|
where: !isEmpty(filters) ? {
|
|
50
|
+
...defaultWhere,
|
|
39
51
|
...filters,
|
|
40
|
-
} :
|
|
52
|
+
} : { ...defaultWhere },
|
|
41
53
|
offset: (page - 1)*pageSize,
|
|
42
54
|
limit: pageSize,
|
|
43
55
|
orderBy: orderBy || [{ createdAt: 'desc' }],
|
|
@@ -59,6 +71,7 @@ const { APPROVAL_STATUS, REGEX } = require('./../utils/constants')
|
|
|
59
71
|
...params,
|
|
60
72
|
populate: {
|
|
61
73
|
authorUser: true,
|
|
74
|
+
threadOf: true,
|
|
62
75
|
reports: {
|
|
63
76
|
where: {
|
|
64
77
|
resolved: false
|
|
@@ -75,19 +88,27 @@ const { APPROVAL_STATUS, REGEX } = require('./../utils/constants')
|
|
|
75
88
|
.map(_ => filterOurResolvedReports(this.getCommonService().sanitizeCommentEntity(_)))
|
|
76
89
|
.map(_ => this.getCommonService().mergeRelatedEntityTo(_, relatedEntities));
|
|
77
90
|
|
|
91
|
+
const pageCount = Math.floor(total / pageSize);
|
|
78
92
|
return {
|
|
79
93
|
result,
|
|
80
94
|
pagination: {
|
|
81
95
|
page: page,
|
|
82
96
|
pageSize: pageSize,
|
|
83
|
-
pageCount:
|
|
97
|
+
pageCount: total % pageSize === 0 ? pageCount : pageCount + 1,
|
|
84
98
|
total,
|
|
85
99
|
},
|
|
86
100
|
};
|
|
87
101
|
},
|
|
88
102
|
|
|
89
103
|
// Find single comment
|
|
90
|
-
async findOneAndThread(id) {
|
|
104
|
+
async findOneAndThread(id, { removed, ...query }) {
|
|
105
|
+
const defaultWhere = !removed ? {
|
|
106
|
+
$or: [
|
|
107
|
+
{ removed: false },
|
|
108
|
+
{ removed: null },
|
|
109
|
+
],
|
|
110
|
+
} : {};
|
|
111
|
+
|
|
91
112
|
const reportsPopulation = {
|
|
92
113
|
reports: {
|
|
93
114
|
where: {
|
|
@@ -98,7 +119,7 @@ const { APPROVAL_STATUS, REGEX } = require('./../utils/constants')
|
|
|
98
119
|
|
|
99
120
|
const entity = await strapi.db.query(getModelUid('comment')).findOne({
|
|
100
121
|
where: {
|
|
101
|
-
id
|
|
122
|
+
id,
|
|
102
123
|
},
|
|
103
124
|
populate: {
|
|
104
125
|
authorUser: true,
|
|
@@ -116,7 +137,8 @@ const { APPROVAL_STATUS, REGEX } = require('./../utils/constants')
|
|
|
116
137
|
}
|
|
117
138
|
|
|
118
139
|
const [relatedUid, relatedStringId] = getRelatedGroups(entity.related);
|
|
119
|
-
const
|
|
140
|
+
const parsedRelatedId = parseInt(relatedStringId);
|
|
141
|
+
const relatedId = isNumber(parsedRelatedId) ? parsedRelatedId : relatedStringId;
|
|
120
142
|
const relatedEntity = await strapi.db.query(relatedUid).findOne({
|
|
121
143
|
where: { id: relatedId }
|
|
122
144
|
}).then(_ => ({
|
|
@@ -132,6 +154,8 @@ const { APPROVAL_STATUS, REGEX } = require('./../utils/constants')
|
|
|
132
154
|
const entitiesOnSameLevel = await this.getCommonService()
|
|
133
155
|
.findAllInHierarchy({
|
|
134
156
|
query: {
|
|
157
|
+
...defaultWhere,
|
|
158
|
+
...query,
|
|
135
159
|
threadOf: levelThreadId,
|
|
136
160
|
related: entity.related,
|
|
137
161
|
},
|
|
@@ -141,15 +165,14 @@ const { APPROVAL_STATUS, REGEX } = require('./../utils/constants')
|
|
|
141
165
|
startingFromId: levelThreadId,
|
|
142
166
|
isAdmin: true
|
|
143
167
|
}, false);
|
|
144
|
-
|
|
145
|
-
|
|
168
|
+
const selectedEntity = this.getCommonService().sanitizeCommentEntity({
|
|
169
|
+
...entity,
|
|
170
|
+
threadOf: entity.threadOf || null,
|
|
171
|
+
});
|
|
146
172
|
|
|
147
173
|
return {
|
|
148
174
|
entity: relatedEntity,
|
|
149
|
-
selected:
|
|
150
|
-
...selectedEntity,
|
|
151
|
-
threadOf: selectedEntity.threadOf ? selectedEntity.threadOf : null,
|
|
152
|
-
},
|
|
175
|
+
selected: selectedEntity,
|
|
153
176
|
level: entitiesOnSameLevel,
|
|
154
177
|
};
|
|
155
178
|
},
|
|
@@ -167,9 +190,13 @@ const { APPROVAL_STATUS, REGEX } = require('./../utils/constants')
|
|
|
167
190
|
// Block / Unblock a comment thread
|
|
168
191
|
async blockCommentThread(id, forceStatus) {
|
|
169
192
|
const existingEntity = await this.getCommonService().findOne({ id });
|
|
193
|
+
const status = !isNil(forceStatus) ? forceStatus : !existingEntity.blockedThread;
|
|
170
194
|
const changedEntity = await strapi.db.query(getModelUid('comment')).update({
|
|
171
195
|
where: { id },
|
|
172
|
-
data: {
|
|
196
|
+
data: {
|
|
197
|
+
blocked: status,
|
|
198
|
+
blockedThread: status,
|
|
199
|
+
},
|
|
173
200
|
});
|
|
174
201
|
await this.blockNestedThreads(id, changedEntity.blockedThread);
|
|
175
202
|
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
const config = require('
|
|
3
|
+
const config = require('../config');
|
|
4
4
|
const { getPluginService } = require('./../utils/functions');
|
|
5
|
-
const { isNil, isEmpty } = require('lodash');
|
|
5
|
+
const { isNil, isEmpty, isNumber, parseInt } = require('lodash');
|
|
6
6
|
const PluginError = require('./../utils/error');
|
|
7
7
|
const {
|
|
8
8
|
isEqualEntity,
|
|
@@ -26,28 +26,28 @@ module.exports = ({ strapi }) => ({
|
|
|
26
26
|
|
|
27
27
|
// Create a comment
|
|
28
28
|
async create(relation, data, user = undefined) {
|
|
29
|
-
const { content, threadOf
|
|
30
|
-
const singleRelationFulfilled =
|
|
31
|
-
|
|
32
|
-
if (relation !== related) {
|
|
33
|
-
throw new PluginError(400, `Insonsistent relation uid`);
|
|
34
|
-
}
|
|
35
|
-
|
|
29
|
+
const { content, threadOf } = data;
|
|
30
|
+
const singleRelationFulfilled = relation && REGEX.relatedUid.test(relation);
|
|
31
|
+
|
|
36
32
|
if(!singleRelationFulfilled) {
|
|
37
33
|
throw new PluginError(400, `Field "related" got incorrect format, use format like "api::<collection name>.<content type name>:<entity id>"`);
|
|
38
34
|
}
|
|
39
35
|
|
|
40
|
-
const [ uid, relatedStringId ] = getRelatedGroups(
|
|
41
|
-
const
|
|
36
|
+
const [ uid, relatedStringId ] = getRelatedGroups(relation);
|
|
37
|
+
const parsedRelatedId = parseInt(relatedStringId);
|
|
38
|
+
const relatedId = isNumber(parsedRelatedId) ? parsedRelatedId : relatedStringId;
|
|
42
39
|
|
|
43
40
|
const relatedEntity = await strapi.db.query(uid).findOne(relatedId);
|
|
44
41
|
if (!relatedEntity) {
|
|
45
42
|
throw new PluginError(400, `Relation for field "related" does not exist. Check your payload please.`);
|
|
46
43
|
}
|
|
47
44
|
|
|
48
|
-
const
|
|
45
|
+
const isApprovalFlowEnabled = this.getCommonService().getConfig('approvalFlow', []).includes(uid) ||
|
|
46
|
+
relatedEntity.requireCommentsApproval;
|
|
47
|
+
|
|
48
|
+
const linkToThread = !isNil(threadOf) ? await this.getCommonService().findOne({
|
|
49
49
|
id: threadOf,
|
|
50
|
-
related,
|
|
50
|
+
related: relation,
|
|
51
51
|
}) : true;
|
|
52
52
|
const validContext = isValidUserContext(user);
|
|
53
53
|
|
|
@@ -87,7 +87,8 @@ module.exports = ({ strapi }) => ({
|
|
|
87
87
|
throw new PluginError(400, 'Object: "author" is invalid. Check your payload');
|
|
88
88
|
}
|
|
89
89
|
|
|
90
|
-
if (
|
|
90
|
+
if (isApprovalFlowEnabled &&
|
|
91
|
+
(!isNil(data.approvalStatus) && (data.approvalStatus !== APPROVAL_STATUS.PENDING))) {
|
|
91
92
|
throw new PluginError(400, 'Invalid approval status');
|
|
92
93
|
}
|
|
93
94
|
|
|
@@ -95,23 +96,23 @@ module.exports = ({ strapi }) => ({
|
|
|
95
96
|
data: {
|
|
96
97
|
...rest,
|
|
97
98
|
...authorData,
|
|
98
|
-
related,
|
|
99
|
+
related: relation,
|
|
100
|
+
approvalStatus: isApprovalFlowEnabled ? APPROVAL_STATUS.PENDING : null,
|
|
99
101
|
}
|
|
100
102
|
});
|
|
101
|
-
return this.getCommonService().sanitizeCommentEntity(
|
|
103
|
+
return this.getCommonService().sanitizeCommentEntity({
|
|
104
|
+
...entity,
|
|
105
|
+
threadOf: !isNil(threadOf) ? linkToThread : null,
|
|
106
|
+
});
|
|
102
107
|
}
|
|
103
108
|
throw new PluginError(400, 'No content received');
|
|
104
109
|
},
|
|
105
110
|
|
|
106
111
|
// Update a comment
|
|
107
112
|
async update(id, relation, data, user) {
|
|
108
|
-
const { content
|
|
113
|
+
const { content } = data;
|
|
109
114
|
|
|
110
|
-
const singleRelationFulfilled =
|
|
111
|
-
|
|
112
|
-
if (relation !== related) {
|
|
113
|
-
throw new PluginError(400, `Insonsistent relation uid`);
|
|
114
|
-
}
|
|
115
|
+
const singleRelationFulfilled = relation && REGEX.relatedUid.test(relation)
|
|
115
116
|
|
|
116
117
|
if(!singleRelationFulfilled) {
|
|
117
118
|
throw new PluginError(400, `Field "related" got incorrect format, use format like "api::<collection name>.<content type name>:<entity id>"`);
|
|
@@ -119,7 +120,7 @@ module.exports = ({ strapi }) => ({
|
|
|
119
120
|
|
|
120
121
|
const existingEntity = await this.getCommonService().findOne({
|
|
121
122
|
id,
|
|
122
|
-
related,
|
|
123
|
+
related: relation,
|
|
123
124
|
});
|
|
124
125
|
const validContext = isValidUserContext(user);
|
|
125
126
|
|
|
@@ -132,6 +133,7 @@ module.exports = ({ strapi }) => ({
|
|
|
132
133
|
const entity = await strapi.db.query(getModelUid('comment')).update({
|
|
133
134
|
where: { id },
|
|
134
135
|
data: { content },
|
|
136
|
+
populate: { threadOf: true },
|
|
135
137
|
});
|
|
136
138
|
return this.getCommonService().sanitizeCommentEntity(entity);
|
|
137
139
|
}
|
|
@@ -144,11 +146,11 @@ module.exports = ({ strapi }) => ({
|
|
|
144
146
|
if (!isValidUserContext(user)) {
|
|
145
147
|
throw resolveUserContextError(user);
|
|
146
148
|
}
|
|
147
|
-
const
|
|
149
|
+
const reportAgainstEntity = await this.getCommonService().findOne({
|
|
148
150
|
id,
|
|
149
151
|
related: relation,
|
|
150
152
|
});
|
|
151
|
-
if (
|
|
153
|
+
if (reportAgainstEntity) {
|
|
152
154
|
const entity = await strapi.db.query(getModelUid('comment-report')).create({
|
|
153
155
|
data: {
|
|
154
156
|
...payload,
|
|
@@ -157,32 +159,46 @@ module.exports = ({ strapi }) => ({
|
|
|
157
159
|
}
|
|
158
160
|
});
|
|
159
161
|
if (entity) {
|
|
162
|
+
const response = {
|
|
163
|
+
...entity,
|
|
164
|
+
related: reportAgainstEntity,
|
|
165
|
+
};
|
|
160
166
|
try {
|
|
161
167
|
await this.sendAbuseReportEmail(entity.reason, entity.content); // Could also add some info about relation
|
|
168
|
+
return response
|
|
162
169
|
} catch (err) {
|
|
163
|
-
return
|
|
170
|
+
return response;
|
|
164
171
|
}
|
|
165
172
|
} else {
|
|
166
173
|
throw new PluginError(500, 'Report cannot be created');
|
|
167
174
|
}
|
|
168
|
-
return entity;
|
|
169
175
|
}
|
|
170
176
|
throw new PluginError(409, 'Action on that entity is not allowed');
|
|
171
177
|
},
|
|
172
178
|
|
|
173
|
-
async markAsRemoved(
|
|
179
|
+
async markAsRemoved(id, relation, authorId){
|
|
174
180
|
const entity = await this.getCommonService().findOne({
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
181
|
+
id,
|
|
182
|
+
related: relation,
|
|
183
|
+
$or: [{ authorUser: authorId }, { authorId }],
|
|
178
184
|
});
|
|
179
|
-
|
|
180
|
-
|
|
185
|
+
|
|
186
|
+
if (!entity) {
|
|
187
|
+
throw new PluginError(404, 'Entity not exist');
|
|
181
188
|
}
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
.
|
|
185
|
-
|
|
189
|
+
|
|
190
|
+
const removedEntity = await strapi.db.query(getModelUid('comment'))
|
|
191
|
+
.update({
|
|
192
|
+
where: {
|
|
193
|
+
id,
|
|
194
|
+
related: relation
|
|
195
|
+
},
|
|
196
|
+
data: { removed: true },
|
|
197
|
+
populate: { threadOf: true },
|
|
198
|
+
})
|
|
199
|
+
await this.markAsRemovedNested(id, true);
|
|
200
|
+
|
|
201
|
+
return this.getCommonService().sanitizeCommentEntity(removedEntity);
|
|
186
202
|
},
|
|
187
203
|
|
|
188
204
|
async sendAbuseReportEmail(reason, content) {
|
|
@@ -221,7 +237,7 @@ module.exports = ({ strapi }) => ({
|
|
|
221
237
|
}
|
|
222
238
|
},
|
|
223
239
|
|
|
224
|
-
markAsRemovedNested(id, status){
|
|
240
|
+
async markAsRemovedNested(id, status){
|
|
225
241
|
return this.getCommonService().modifiedNestedNestedComments(id, 'removed', status);
|
|
226
242
|
},
|
|
227
243
|
});
|
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
const { isArray,
|
|
3
|
+
const { isArray, isNumber, isObject, first, parseInt } = require('lodash');
|
|
4
4
|
// const { sanitizeEntity } = require('strapi-utils');
|
|
5
5
|
const PluginError = require('./../utils/error');
|
|
6
6
|
const {
|
|
7
|
-
extractMeta,
|
|
8
7
|
getModelUid,
|
|
9
8
|
getRelatedGroups,
|
|
10
9
|
buildNestedStructure,
|
|
11
|
-
filterOurResolvedReports
|
|
10
|
+
filterOurResolvedReports,
|
|
11
|
+
buildAuthorModel,
|
|
12
12
|
} = require('./utils/functions');
|
|
13
13
|
|
|
14
14
|
/**
|
|
@@ -16,62 +16,17 @@ const {
|
|
|
16
16
|
*/
|
|
17
17
|
|
|
18
18
|
module.exports = ({ strapi }) => ({
|
|
19
|
-
getAssociationModel(ormModel, related) {
|
|
20
|
-
const relatedAssociation = ormModel.associations.find(_ => _.alias === 'related');
|
|
21
|
-
if (relatedAssociation) {
|
|
22
|
-
return relatedAssociation.related.find(rel => rel.collectionName === related);
|
|
23
|
-
}
|
|
24
|
-
return null;
|
|
25
|
-
},
|
|
26
|
-
|
|
27
|
-
getMorphModel() {
|
|
28
|
-
const ormModel = strapi.query(getModelUid('comment'));
|
|
29
|
-
return ormModel.model.morph;
|
|
30
|
-
},
|
|
31
|
-
|
|
32
|
-
isMongoDB(){
|
|
33
|
-
return strapi.query(getModelUid('comment'))?.model?.orm === 'mongoose';
|
|
34
|
-
},
|
|
35
|
-
|
|
36
|
-
getMorphData(query) {
|
|
37
|
-
return this
|
|
38
|
-
.getMorphModel()
|
|
39
|
-
.query(function () {
|
|
40
|
-
this.where(query);
|
|
41
|
-
})
|
|
42
|
-
.fetchAll()
|
|
43
|
-
.then((entities) => entities.map(entity => entity.toJSON()));
|
|
44
|
-
},
|
|
45
|
-
|
|
46
|
-
checkEntityRelation(uid, entityId) {
|
|
47
|
-
const result = parsedRelation.some(ref => {
|
|
48
|
-
const model = strapi.query(ref);
|
|
49
|
-
return model && model.associations.length &&
|
|
50
|
-
model.associations.some(_ => _.plugin === 'comments' && _.collection === 'comment');
|
|
51
|
-
});
|
|
52
|
-
if (!result) {
|
|
53
|
-
throw new PluginError(400, 'Comment not have relation to this type content type.');
|
|
54
|
-
}
|
|
55
|
-
},
|
|
56
19
|
|
|
57
|
-
getConfig(prop) {
|
|
20
|
+
getConfig(prop, defaultValue) {
|
|
58
21
|
let queryProp = prop;
|
|
59
22
|
if (prop && isArray(prop)) {
|
|
60
23
|
queryProp = prop.join('.');
|
|
61
24
|
}
|
|
62
|
-
return strapi.config.get(`plugin.comments${ queryProp ? '.' + queryProp : ''}`);
|
|
25
|
+
return strapi.config.get(`plugin.comments${ queryProp ? '.' + queryProp : ''}`) || defaultValue;
|
|
63
26
|
},
|
|
64
27
|
|
|
65
28
|
// Find comments in the flat structure
|
|
66
|
-
async findAllFlat({ query, populate = {} },
|
|
67
|
-
const criteria = {
|
|
68
|
-
...query,
|
|
69
|
-
$or: [{ removed: { $null: true } }, { removed: false }],
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
if (isAdmin){
|
|
73
|
-
delete criteria.$or;
|
|
74
|
-
}
|
|
29
|
+
async findAllFlat({ query = {}, populate = {} }, relatedEntity = null) {
|
|
75
30
|
|
|
76
31
|
const defaultPopulate = {
|
|
77
32
|
authorUser: true,
|
|
@@ -80,7 +35,7 @@ module.exports = ({ strapi }) => ({
|
|
|
80
35
|
const entries = await strapi.db.query(getModelUid('comment'))
|
|
81
36
|
.findMany({
|
|
82
37
|
where: {
|
|
83
|
-
...
|
|
38
|
+
...query,
|
|
84
39
|
},
|
|
85
40
|
populate: {
|
|
86
41
|
...defaultPopulate,
|
|
@@ -122,9 +77,8 @@ module.exports = ({ strapi }) => ({
|
|
|
122
77
|
populate = {},
|
|
123
78
|
startingFromId = null,
|
|
124
79
|
dropBlockedThreads = false,
|
|
125
|
-
isAdmin = false,
|
|
126
80
|
}, relatedEntity) {
|
|
127
|
-
const entities = await this.findAllFlat({ query, populate },
|
|
81
|
+
const entities = await this.findAllFlat({ query, populate }, relatedEntity);
|
|
128
82
|
return buildNestedStructure(entities, startingFromId, 'threadOf', dropBlockedThreads, false);
|
|
129
83
|
},
|
|
130
84
|
|
|
@@ -135,24 +89,38 @@ module.exports = ({ strapi }) => ({
|
|
|
135
89
|
where: criteria
|
|
136
90
|
}, ['related', 'reports']);
|
|
137
91
|
if (!entity){
|
|
138
|
-
throw
|
|
92
|
+
throw new PluginError(404, 'Not found');
|
|
139
93
|
}
|
|
140
94
|
return filterOurResolvedReports(this.sanitizeCommentEntity(entity));
|
|
141
95
|
},
|
|
142
96
|
|
|
143
97
|
// Find all related entiries
|
|
144
98
|
async findRelatedEntitiesFor(entities = []) {
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
}
|
|
155
|
-
|
|
99
|
+
const data = entities.reduce((acc, cur) => {
|
|
100
|
+
const [relatedUid, relatedStringId] = getRelatedGroups(cur.related);
|
|
101
|
+
const parsedRelatedId = parseInt(relatedStringId);
|
|
102
|
+
const relatedId = isNumber(parsedRelatedId) ? parsedRelatedId : relatedStringId;
|
|
103
|
+
return {
|
|
104
|
+
...acc,
|
|
105
|
+
[relatedUid]: [...(acc[relatedUid] || []), relatedId]
|
|
106
|
+
};
|
|
107
|
+
},
|
|
108
|
+
{}
|
|
109
|
+
);
|
|
110
|
+
return Promise.all(
|
|
111
|
+
Object.entries(data)
|
|
112
|
+
.map(async ([relatedUid, relatedStringIds]) =>
|
|
113
|
+
strapi.db.query(relatedUid).findMany({
|
|
114
|
+
where: { id: Array.from(new Set(relatedStringIds)) }
|
|
115
|
+
}).then(relatedEntities => relatedEntities.map(_ =>
|
|
116
|
+
({
|
|
117
|
+
..._,
|
|
118
|
+
uid: relatedUid,
|
|
119
|
+
})
|
|
120
|
+
))
|
|
121
|
+
)
|
|
122
|
+
)
|
|
123
|
+
.then(result => result.flat(2));
|
|
156
124
|
},
|
|
157
125
|
|
|
158
126
|
// Merge related entity with comment
|
|
@@ -187,7 +155,10 @@ module.exports = ({ strapi }) => ({
|
|
|
187
155
|
},
|
|
188
156
|
|
|
189
157
|
sanitizeCommentEntity: (entity) => ({
|
|
190
|
-
...
|
|
158
|
+
...buildAuthorModel({
|
|
159
|
+
...entity,
|
|
160
|
+
threadOf: isObject(entity.threadOf) ? buildAuthorModel(entity.threadOf) : entity.threadOf,
|
|
161
|
+
}),
|
|
191
162
|
}),
|
|
192
163
|
|
|
193
164
|
// sanitizeCommentEntity: (entity) => ({
|