strapi-plugin-comments 2.1.5 → 2.1.8

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.
@@ -7,7 +7,6 @@ const lodash_1 = require("lodash");
7
7
  const error_1 = __importDefault(require("./../utils/error"));
8
8
  const functions_2 = require("./utils/functions");
9
9
  const constants_1 = require("./../utils/constants");
10
- const DEFAULT_AUTHOR_POPULATE = { avatar: true };
11
10
  module.exports = ({ strapi }) => ({
12
11
  getCommonService() {
13
12
  return (0, functions_1.getPluginService)("common");
@@ -66,11 +65,12 @@ module.exports = ({ strapi }) => ({
66
65
  async restart() {
67
66
  setImmediate(() => strapi.reload());
68
67
  },
69
- async findAll({ related, entity, ...query }) {
68
+ async findAll({ ...query }) {
70
69
  const { _q, orderBy, pageSize = 10, page = 1, filters, } = (0, functions_1.parseParams)(query);
71
70
  const defaultWhere = {
72
71
  $or: [{ removed: false }, { removed: null }],
73
72
  };
73
+ const defaultAuthorUserPopulate = this.getDefaultAuthorPopulate();
74
74
  let params = {
75
75
  where: !(0, lodash_1.isEmpty)(filters)
76
76
  ? {
@@ -98,7 +98,7 @@ module.exports = ({ strapi }) => ({
98
98
  .findMany({
99
99
  ...params,
100
100
  populate: {
101
- authorUser: { populate: DEFAULT_AUTHOR_POPULATE },
101
+ authorUser: defaultAuthorUserPopulate,
102
102
  threadOf: true,
103
103
  reports: {
104
104
  where: {
@@ -112,7 +112,7 @@ module.exports = ({ strapi }) => ({
112
112
  });
113
113
  const relatedEntities = await this.getCommonService().findRelatedEntitiesFor(entities);
114
114
  const result = entities
115
- .map((_) => (0, functions_2.filterOurResolvedReports)(this.getCommonService().sanitizeCommentEntity(_, DEFAULT_AUTHOR_POPULATE)))
115
+ .map((_) => (0, functions_2.filterOurResolvedReports)(this.getCommonService().sanitizeCommentEntity(_, defaultAuthorUserPopulate?.populate)))
116
116
  .map((_) => this.getCommonService().mergeRelatedEntityTo(_, relatedEntities));
117
117
  const pageCount = Math.floor(total / pageSize);
118
118
  return {
@@ -126,6 +126,7 @@ module.exports = ({ strapi }) => ({
126
126
  };
127
127
  },
128
128
  async findOneAndThread(id, { removed, ...query }) {
129
+ const defaultAuthorUserPopulate = this.getDefaultAuthorPopulate();
129
130
  const defaultWhere = !removed
130
131
  ? {
131
132
  $or: [{ removed: false }, { removed: null }],
@@ -140,14 +141,10 @@ module.exports = ({ strapi }) => ({
140
141
  };
141
142
  const defaultPopulate = {
142
143
  populate: {
143
- authorUser: {
144
- populate: DEFAULT_AUTHOR_POPULATE,
145
- },
144
+ authorUser: defaultAuthorUserPopulate,
146
145
  threadOf: {
147
146
  populate: {
148
- authorUser: {
149
- populate: DEFAULT_AUTHOR_POPULATE,
150
- },
147
+ authorUser: defaultAuthorUserPopulate,
151
148
  ...reportsPopulation,
152
149
  },
153
150
  },
@@ -197,7 +194,7 @@ module.exports = ({ strapi }) => ({
197
194
  const selectedEntity = this.getCommonService().sanitizeCommentEntity({
198
195
  ...entity,
199
196
  threadOf: entity.threadOf || null,
200
- }, DEFAULT_AUTHOR_POPULATE);
197
+ }, defaultAuthorUserPopulate?.populate);
201
198
  return {
202
199
  entity: relatedEntity,
203
200
  selected: selectedEntity,
@@ -265,5 +262,38 @@ module.exports = ({ strapi }) => ({
265
262
  data: { resolved: true },
266
263
  });
267
264
  },
265
+ async resolveMultipleAbuseReports(ids, commentId) {
266
+ const reportsToResolve = await strapi.db
267
+ .query((0, functions_2.getModelUid)("comment-report"))
268
+ .findMany({
269
+ where: {
270
+ id: ids,
271
+ related: commentId,
272
+ },
273
+ populate: ['related']
274
+ });
275
+ if (reportsToResolve.length === ids.length) {
276
+ return strapi.db
277
+ .query((0, functions_2.getModelUid)("comment-report"))
278
+ .updateMany({
279
+ where: { id: ids },
280
+ data: { resolved: true },
281
+ });
282
+ }
283
+ throw new error_1.default(400, 'At least one of selected reports got invalid comment entity relation. Try again.');
284
+ },
285
+ getDefaultAuthorPopulate() {
286
+ const strapiUserTypeUid = 'plugin::users-permissions.user';
287
+ const allowedTypes = ['media', 'relation'];
288
+ const { attributes } = strapi.contentTypes[strapiUserTypeUid] || {};
289
+ const relationTypes = Object.keys(attributes)
290
+ ?.filter((key) => allowedTypes.includes(attributes[key]?.type));
291
+ if (relationTypes.includes('avatar')) {
292
+ return {
293
+ populate: { avatar: true },
294
+ };
295
+ }
296
+ return undefined;
297
+ }
268
298
  });
269
299
  //# sourceMappingURL=admin.js.map
@@ -24,7 +24,7 @@ module.exports = ({ strapi }) => ({
24
24
  }
25
25
  const relatedEntity = await strapi.db
26
26
  .query(uid)
27
- .findOne(relatedId);
27
+ .findOne({ where: { id: relatedId } });
28
28
  if (!relatedEntity) {
29
29
  throw new error_1.default(400, `Relation for field "related" does not exist. Check your payload please.`);
30
30
  }
@@ -90,10 +90,17 @@ module.exports = ({ strapi }) => ({
90
90
  authorUser: true,
91
91
  },
92
92
  });
93
- return this.getCommonService().sanitizeCommentEntity({
93
+ const sanitizedEntity = this.getCommonService().sanitizeCommentEntity({
94
94
  ...entity,
95
95
  threadOf: linkToThread,
96
96
  });
97
+ try {
98
+ await this.sendResponseNotification(sanitizedEntity);
99
+ }
100
+ catch (e) {
101
+ console.error(e);
102
+ }
103
+ return sanitizedEntity;
97
104
  }
98
105
  throw new error_1.default(400, "No content received");
99
106
  },
@@ -255,5 +262,53 @@ module.exports = ({ strapi }) => ({
255
262
  async markAsRemovedNested(id, status) {
256
263
  return this.getCommonService().modifiedNestedNestedComments(id, "removed", status);
257
264
  },
265
+ async sendResponseNotification(entity) {
266
+ if (entity.threadOf) {
267
+ const thread = (0, lodash_1.isObject)(entity.threadOf) ? entity.threadOf : await this.getCommonService().findOne({ id: entity.threadOf });
268
+ let emailRecipient = thread?.author?.email;
269
+ if (thread.authorUser && !emailRecipient) {
270
+ const strapiUser = (0, lodash_1.isObject)(thread.authorUser) ? thread.authorUser : await strapi.db.query("api::user").findOne({
271
+ where: { id: thread.authorUser }
272
+ });
273
+ emailRecipient = strapiUser?.email;
274
+ }
275
+ if (emailRecipient) {
276
+ const superAdmin = await strapi.db
277
+ .query("admin::user")
278
+ .findOne({
279
+ where: {
280
+ roles: { code: "strapi-super-admin" },
281
+ },
282
+ });
283
+ const emailSender = await this.getCommonService().getConfig('client.contactEmail', superAdmin.email);
284
+ const clientAppUrl = await this.getCommonService().getConfig('client.url', 'our site');
285
+ try {
286
+ await strapi
287
+ .plugin("email")
288
+ .service("email")
289
+ .send({
290
+ to: [emailRecipient],
291
+ from: emailSender,
292
+ subject: "You've got a new response to your comment",
293
+ text: `Hello ${thread?.author?.name || emailRecipient}!
294
+ You've got a new response to your comment by ${entity?.author?.name || entity?.author?.email}.
295
+
296
+ ------
297
+
298
+ "${entity.content}"
299
+
300
+ ------
301
+
302
+ Visit ${clientAppUrl} and continue the discussion.
303
+ `,
304
+ });
305
+ }
306
+ catch (err) {
307
+ strapi.log.error(err);
308
+ throw err;
309
+ }
310
+ }
311
+ }
312
+ },
258
313
  });
259
314
  //# sourceMappingURL=client.js.map
@@ -224,10 +224,10 @@ module.exports = ({ strapi }) => ({
224
224
  where: { id: entitiesToChange.map((_) => _.id) },
225
225
  data: { [fieldName]: value },
226
226
  });
227
- if (entitiesToChange.length === changedEntities.length &&
228
- changedEntities.length > 0) {
229
- const nestedTransactions = await Promise.all(changedEntities.map((item) => this.modifiedNestedNestedComments(item.id, fieldName, value)));
230
- return nestedTransactions.length === changedEntities.length;
227
+ if (entitiesToChange.length === changedEntities.count &&
228
+ changedEntities.count > 0) {
229
+ const nestedTransactions = await Promise.all(entitiesToChange.map((item) => this.modifiedNestedNestedComments(item.id, fieldName, value)));
230
+ return nestedTransactions.length === changedEntities.count;
231
231
  }
232
232
  return true;
233
233
  }
@@ -149,6 +149,8 @@ declare const _default: () => {
149
149
  entryLabel: PluginConfigEntryLabels;
150
150
  reportReasons: PluginConfigReportReasons;
151
151
  badWords?: boolean | undefined;
152
+ client?: import("./types").PluginConfigClientService | undefined;
153
+ gql?: import("./types").PluginConfigGraphQL | undefined;
152
154
  };
153
155
  validator: (config: CommentsPluginConfig) => void;
154
156
  };