strapi-plugin-comments 2.0.0-beta.6 → 2.0.0-beta.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.
Files changed (43) hide show
  1. package/README.md +310 -7
  2. package/admin/src/components/DiscussionThreadItem/index.js +4 -8
  3. package/admin/src/components/DiscussionThreadItemMeta/index.js +16 -5
  4. package/admin/src/pages/Discover/components/DiscoverTableRow/index.js +8 -1
  5. package/admin/src/pages/Discover/index.js +4 -1
  6. package/admin/src/translations/en.json +1 -0
  7. package/admin/src/utils/index.js +2 -0
  8. package/admin/src/utils/renderInitials.js +5 -0
  9. package/package.json +2 -1
  10. package/server/config/index.js +5 -0
  11. package/server/controllers/client.js +4 -19
  12. package/server/controllers/utils/parsers.js +17 -0
  13. package/server/graphql/index.js +26 -0
  14. package/server/graphql/mutations/createAbuseReport.js +20 -0
  15. package/server/graphql/mutations/createComment.js +20 -0
  16. package/server/graphql/mutations/index.js +21 -0
  17. package/server/graphql/mutations/removeComment.js +20 -0
  18. package/server/graphql/mutations/updateComment.js +20 -0
  19. package/server/graphql/queries/findAllFlat.js +29 -0
  20. package/server/graphql/queries/findAllInHierarchy.js +31 -0
  21. package/server/graphql/queries/index.js +19 -0
  22. package/server/graphql/resolvers-config.js +10 -0
  23. package/server/graphql/types/comment-author-user.js +9 -0
  24. package/server/graphql/types/comment-author.js +9 -0
  25. package/server/graphql/types/comment-nested.js +16 -0
  26. package/server/graphql/types/comment.js +16 -0
  27. package/server/graphql/types/create-comment-author.js +9 -0
  28. package/server/graphql/types/create-comment.js +9 -0
  29. package/server/graphql/types/create-report.js +9 -0
  30. package/server/graphql/types/id.js +24 -0
  31. package/server/graphql/types/identify-comment-author.js +6 -0
  32. package/server/graphql/types/index.js +17 -0
  33. package/server/graphql/types/remove-comment.js +8 -0
  34. package/server/graphql/types/report-reason.js +10 -0
  35. package/server/graphql/types/report.js +11 -0
  36. package/server/graphql/types/update-comment.js +9 -0
  37. package/server/register.js +3 -1
  38. package/server/services/admin.js +8 -8
  39. package/server/services/client.js +35 -27
  40. package/server/services/common.js +10 -4
  41. package/server/services/graphql.js +190 -0
  42. package/server/services/index.js +2 -0
  43. package/server/services/utils/functions.js +17 -3
@@ -0,0 +1,190 @@
1
+ 'use strict';
2
+
3
+ const { has, propEq, isNil, isDate, isObject } = require('lodash/fp');
4
+ const { inputObjectType } = require('nexus');
5
+
6
+ const virtualScalarAttributes = ['id'];
7
+
8
+ module.exports = ({ strapi }) => {
9
+ const { service: getService } = strapi.plugin('graphql');
10
+
11
+ const rootLevelOperators = () => {
12
+ const { operators } = strapi.plugin('graphql').service('builders').filters;
13
+
14
+ return [operators.and, operators.or, operators.not];
15
+ };
16
+
17
+ const buildContentTypeFilters = contentType => {
18
+ const utils = strapi.plugin('graphql').service('utils');
19
+ const extension = strapi.plugin('graphql').service('extension');
20
+
21
+ const { getFiltersInputTypeName, getScalarFilterInputTypeName } = utils.naming;
22
+ const { isStrapiScalar, isRelation } = utils.attributes;
23
+
24
+ const { attributes } = contentType;
25
+
26
+ const filtersTypeName = getFiltersInputTypeName(contentType);
27
+
28
+ return inputObjectType({
29
+ name: filtersTypeName,
30
+
31
+ definition(t) {
32
+ const validAttributes = Object.entries(attributes).filter(([attributeName]) =>
33
+ extension
34
+ .shadowCRUD(contentType.uid)
35
+ .field(attributeName)
36
+ .hasFiltersEnabeld()
37
+ );
38
+
39
+ const isIDFilterEnabled = extension
40
+ .shadowCRUD(contentType.uid)
41
+ .field('id')
42
+ .hasFiltersEnabeld();
43
+ // Add an ID filter to the collection types
44
+ if (contentType.kind === 'collectionType' && isIDFilterEnabled) {
45
+ t.field('id', { type: getScalarFilterInputTypeName('ID') });
46
+ }
47
+
48
+ // Add every defined attribute
49
+ for (const [attributeName, attribute] of validAttributes) {
50
+ // Handle scalars
51
+ if (isStrapiScalar(attribute)) {
52
+ addScalarAttribute(t, attributeName, attribute);
53
+ }
54
+
55
+ // Handle relations
56
+ else if (isRelation(attribute)) {
57
+ addRelationalAttribute(t, attributeName, attribute);
58
+ }
59
+ }
60
+
61
+ // Conditional clauses
62
+ for (const operator of rootLevelOperators()) {
63
+ operator.add(t, filtersTypeName);
64
+ }
65
+ },
66
+ });
67
+ };
68
+
69
+ const addScalarAttribute = (builder, attributeName, attribute) => {
70
+ const { naming, mappers } = strapi.plugin('graphql').service('utils');
71
+
72
+ const gqlType = mappers.strapiScalarToGraphQLScalar(attribute.type);
73
+
74
+ builder.field(attributeName, { type: naming.getScalarFilterInputTypeName(gqlType) });
75
+ };
76
+
77
+ const addRelationalAttribute = (builder, attributeName, attribute) => {
78
+ const utils = strapi.plugin('graphql').service('utils');
79
+ const extension = strapi.plugin('graphql').service('extension');
80
+ const { getFiltersInputTypeName } = utils.naming;
81
+ const { isMorphRelation } = utils.attributes;
82
+
83
+ const model = strapi.getModel(attribute.target);
84
+
85
+ // If there is no model corresponding to the attribute configuration
86
+ // or if the attribute is a polymorphic relation, then ignore it
87
+ if (!model || isMorphRelation(attribute)) return;
88
+
89
+ // If the target model is disabled, then ignore it too
90
+ if (extension.shadowCRUD(model.uid).isDisabled()) return;
91
+
92
+ builder.field(attributeName, { type: getFiltersInputTypeName(model) });
93
+ };
94
+
95
+ const recursivelyReplaceScalarOperators = data => {
96
+ const { operators } = getService('builders').filters;
97
+
98
+ if (Array.isArray(data)) {
99
+ return data.map(recursivelyReplaceScalarOperators);
100
+ }
101
+
102
+ // Note: We need to make an exception for date since GraphQL
103
+ // automatically cast date strings to date instances in args
104
+ if (isDate(data) || !isObject(data)) {
105
+ return data;
106
+ }
107
+
108
+ const result = {};
109
+
110
+ for (const [key, value] of Object.entries(data)) {
111
+ const isOperator = !!operators[key];
112
+
113
+ const newKey = isOperator ? operators[key].strapiOperator : key;
114
+
115
+ result[newKey] = recursivelyReplaceScalarOperators(value);
116
+ }
117
+
118
+ return result;
119
+ };
120
+
121
+ const graphQLFiltersToStrapiQuery = (filters, contentType = {}) => {
122
+ const { isStrapiScalar, isMedia, isRelation } = getService('utils').attributes;
123
+ const { operators } = getService('builders').filters;
124
+
125
+ const ROOT_LEVEL_OPERATORS = [operators.and, operators.or, operators.not];
126
+
127
+ // Handle unwanted scenario where there is no filters defined
128
+ if (isNil(filters)) {
129
+ return {};
130
+ }
131
+
132
+ // If filters is a collection, then apply the transformation to every item of the list
133
+ if (Array.isArray(filters)) {
134
+ return filters.map(filtersItem =>
135
+ graphQLFiltersToStrapiQuery(filtersItem, contentType)
136
+ );
137
+ }
138
+
139
+ const resultMap = {};
140
+ const { attributes } = contentType;
141
+
142
+ const isAttribute = attributeName => {
143
+ return virtualScalarAttributes.includes(attributeName) || has(attributeName, attributes);
144
+ };
145
+
146
+ for (const [key, value] of Object.entries(filters)) {
147
+ // If the key is an attribute, update the value
148
+ if (isAttribute(key)) {
149
+ const attribute = attributes[key];
150
+
151
+ // If it's a scalar attribute
152
+ if (virtualScalarAttributes.includes(key) || isStrapiScalar(attribute)) {
153
+ // Replace (recursively) every GraphQL scalar operator with the associated Strapi operator
154
+ resultMap[key] = recursivelyReplaceScalarOperators(value);
155
+ }
156
+
157
+ // If it's a deep filter on a relation
158
+ else if (isRelation(attribute) || isMedia(attribute)) {
159
+ // Fetch the model from the relation
160
+ const relModel = strapi.getModel(attribute.target);
161
+
162
+ // Recursively apply the mapping to the value using the fetched model,
163
+ // and update the value within `resultMap`
164
+ resultMap[key] = graphQLFiltersToStrapiQuery(value, relModel);
165
+ }
166
+ }
167
+
168
+ // Handle the case where the key is not an attribute (operator, ...)
169
+ else {
170
+ const rootLevelOperator = ROOT_LEVEL_OPERATORS.find(propEq('fieldName', key));
171
+
172
+ // If it's a root level operator (AND, NOT, OR, ...)
173
+ if (rootLevelOperator) {
174
+ const { strapiOperator } = rootLevelOperator;
175
+
176
+ // Transform the current value recursively and add it to the resultMap
177
+ // object using the strapiOperator equivalent of the GraphQL key
178
+ resultMap[strapiOperator] = graphQLFiltersToStrapiQuery(value, contentType);
179
+ }
180
+ }
181
+ }
182
+
183
+ return resultMap;
184
+ };
185
+
186
+ return {
187
+ buildContentTypeFilters,
188
+ graphQLFiltersToStrapiQuery,
189
+ };
190
+ };
@@ -3,9 +3,11 @@
3
3
  const adminServices = require('./admin');
4
4
  const clientServices = require('./client');
5
5
  const commonServices = require('./common');
6
+ const graphQLService = require('./graphql');
6
7
 
7
8
  module.exports = {
8
9
  admin: adminServices,
9
10
  client: clientServices,
10
11
  common: commonServices,
12
+ gql: graphQLService,
11
13
  };
@@ -6,7 +6,7 @@ const { first, get, isObject, isNil } = require('lodash');
6
6
  const buildNestedStructure = (
7
7
  entities,
8
8
  id = null,
9
- field = 'parent',
9
+ field = 'threadOf',
10
10
  dropBlockedThreads = false,
11
11
  blockNestedThreads = false,
12
12
  ) =>
@@ -33,7 +33,7 @@ const buildNestedStructure = (
33
33
 
34
34
  module.exports = {
35
35
  isEqualEntity: (existing, data, user) => {
36
- const { authorUser, authorId } = existing;
36
+ const { authorUser, author: existingAuthor } = existing;
37
37
  const { author } = data;
38
38
 
39
39
  // Disallow approval status change by Client
@@ -47,7 +47,7 @@ module.exports = {
47
47
  const receivedUserId = user?.id || author;
48
48
  return existingUserId === receivedUserId;
49
49
  }
50
- return authorId === author?.id;
50
+ return existingAuthor.id === author?.id;
51
51
  },
52
52
 
53
53
  getRelatedGroups: related => related.split(REGEX.relatedUid).filter(s => s && s.length > 0),
@@ -90,6 +90,20 @@ module.exports = {
90
90
  return builtInContextEnabled ? !isNil(user.id) : true;
91
91
  },
92
92
 
93
+ buildAuthorModel: (item) => {
94
+ const { authorUser, authorId, authorName, authorEmail, authorAvatar, ...rest } = item;
95
+ const author = authorUser || {
96
+ id: authorId,
97
+ name: authorName,
98
+ email: authorEmail,
99
+ avatar: authorAvatar,
100
+ };
101
+ return {
102
+ ...rest,
103
+ author,
104
+ };
105
+ },
106
+
93
107
  resolveUserContextError: user => {
94
108
  if (user) {
95
109
  throw new PluginError(401, 'Not authenticated');