From 2d96d96dc1464372a07cbf3fdcd03ddb970c5f8e Mon Sep 17 00:00:00 2001 From: Maciej Witkowski Date: Sat, 23 Oct 2021 12:31:08 +0200 Subject: [PATCH 1/3] feat: report email notification --- services/comments.js | 34 ++++++++++++++++++++++++++++++---- 1 file changed, 30 insertions(+), 4 deletions(-) diff --git a/services/comments.js b/services/comments.js index 5e645c01..2be69e91 100644 --- a/services/comments.js +++ b/services/comments.js @@ -325,14 +325,15 @@ module.exports = { }, // Report abuse in comment - async reportAbuse(id, relation, payload, user) { + async reportAbuse(id, relation, payload, user) { if (!isValidUserContext(user)) { throw resolveUserContextError(user); } const { pluginName, plugin } = extractMeta(strapi.plugins); const { report: reportModel } = plugin.models; - const existingEntity = await this.findOne(id, relation); - if (existingEntity) { + const existingEntity = await this.findOne(id, relation); + if (existingEntity) { + await this.sendAbuseReportEmail(payload.reason, payload.content); // Could also add some info about relation return strapi.query(reportModel.modelName, pluginName).create({ ...payload, resolved: false, @@ -508,5 +509,30 @@ module.exports = { sanitizeCommentEntity: (entity) => ({ ...entity, authorUser: sanitizeEntity(entity.authorUser, { model: strapi.plugins['users-permissions'].models.user }), - }), + }), + + async sendAbuseReportEmail(reason, content) { + const rolesToBeNotified = get(strapi.config.plugins, "comments.emailNotification.roles", []); + const allUsers = await strapi.plugins['users-permissions'].services.user.fetchAll(); + + const moderatorsEmails = allUsers.reduce((prev, user) => { + if (rolesToBeNotified.includes(user.role.name)) + return prev.concat(user.email); + else + return prev; + }, []); + + if (moderatorsEmails.length > 0) { + await strapi.plugins['email'].services.email.send({ + to: moderatorsEmails, + from: 'admin@strapi.io', + subject: 'New abuse report on comment', + text: ` + There was a new abuse report on your app. + Reason: ${reason} + Message: ${content} + `, + }); + } + } }; From ecb6fd96eb07ba4c10f595eb706f5fc774bcf8bd Mon Sep 17 00:00:00 2001 From: Maciej Witkowski Date: Mon, 25 Oct 2021 18:37:34 +0200 Subject: [PATCH 2/3] docs: report email notification --- README.md | 24 +++++++++++++----------- services/comments.js | 4 ++-- 2 files changed, 15 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index 004eb3b1..573fcd2c 100644 --- a/README.md +++ b/README.md @@ -46,7 +46,7 @@ Enjoy 🎉 ### 🖐 Requirements -Complete installation requirements are exact same as for Strapi itself and can be found in the documentation under Installation Requirements. +Complete installation requirements are exact same as for Strapi itself and can be found in the documentation under [Installation Requirements](https://strapi.io/documentation/v3.x/installation/cli.html#step-1-make-sure-requirements-are-met). **Supported Strapi versions**: @@ -64,13 +64,13 @@ Complete installation requirements are exact same as for Strapi itself and can b - **Any Content Type relation:** Comments can by linked to any of your Content Types by default. Simply, you're controlling it. - **Moderation Panel:** Search & Filter through the bucket with your auditory comments. Manage them by blocking single ones or full threads. All in combined list & hierarchical tree view of threads. - **Automated Bad Words filtering:** By detault end users are not allowed to post abusing comments where bad words have been used. -- **Abuse Reporting & Reviewing:** Don't allow inferior language, react to reports from your community +- **Abuse Reporting & Reviewing:** Don't allow inferior language, react to reports from your community, send email notifiactions about abuse reports ## Content Type model relation to Comment To enable Content Type to work with Comments, you've to add following field to your model `*.settings.json`: -``` +```json "comments": { "plugin": "comments", "collection": "comment", @@ -80,7 +80,7 @@ To enable Content Type to work with Comments, you've to add following field to y inside the `attributes` section like in example below: -``` +```json "attributes": { ..., "comments": { @@ -93,14 +93,15 @@ inside the `attributes` section like in example below: ``` ## Configuration -To setup amend default plugin configuration we recommend to put following snippet as part of `config/custom.js` or `config//custom.js` file. If you've got already configurations for other plugins stores by this way, use just the `navigation` part within exising `plugins` item. +To setup amend default plugin configuration we recommend to put following snippet as part of `config/custom.js` or `config//custom.js` file. If you've got already configurations for other plugins stores by this way, use just the `comments` part within exising `plugins` item. -``` +```js ... plugins: { comments: { enableUsers: true, - badWords: false + badWords: false, + moderatorRoles: ["Authenticated"] }, }, ... @@ -108,14 +109,15 @@ To setup amend default plugin configuration we recommend to put following snippe ### Properties - `enableUsers` - Enabled support for built-in Strapi users, if endpoints are exposed with usage of `Authenticated` policy or JWT tokens are in use by the Client App. Default value: `false`. -- `badWords` - Enabled support for (bad words filtering)[https://www.npmjs.com/package/bad-words]. Can be turned off or overwritten using (options reference)[https://www.npmjs.com/package/bad-words#constructor]. Default value: `true`. +- `badWords` - Enabled support for [bad words filtering](https://www.npmjs.com/package/bad-words). Can be turned off or overwritten using [options reference](https://www.npmjs.com/package/bad-words#constructor). Default value: `true`. +- `moderatorRoles` - Optional list of names of roles. Users with those roles will be notified by email when a new abuse report is created. This feature requires a built-in [Strapi email plugin](https://strapi.io/documentation/developer-docs/latest/development/plugins/email.html) configured. ## Additional GQL Configuration > **Note** > Introduced in `v1.0.2` -``` +```js ... plugins: { comments: { @@ -148,7 +150,7 @@ To setup amend default plugin configuration we recommend to put following snippe ## Public API Comment model ### Generic (non Strapi User) -``` +```json { "id": 1, "content": "My comment content", @@ -166,7 +168,7 @@ To setup amend default plugin configuration we recommend to put following snippe } ``` ### Strapi User -``` +```json { "id": 1, "content": "My comment content", diff --git a/services/comments.js b/services/comments.js index 2be69e91..4cb192ff 100644 --- a/services/comments.js +++ b/services/comments.js @@ -512,7 +512,7 @@ module.exports = { }), async sendAbuseReportEmail(reason, content) { - const rolesToBeNotified = get(strapi.config.plugins, "comments.emailNotification.roles", []); + const rolesToBeNotified = get(strapi.config, "custom.plugins.comments.moderatorRoles", []); const allUsers = await strapi.plugins['users-permissions'].services.user.fetchAll(); const moderatorsEmails = allUsers.reduce((prev, user) => { @@ -523,7 +523,7 @@ module.exports = { }, []); if (moderatorsEmails.length > 0) { - await strapi.plugins['email'].services.email.send({ + strapi.plugins['email'].services.email.send({ to: moderatorsEmails, from: 'admin@strapi.io', subject: 'New abuse report on comment', From caf935d55ccf23b18734c4fb91ee28ba1865ebb8 Mon Sep 17 00:00:00 2001 From: Maciej Witkowski Date: Wed, 10 Nov 2021 12:33:03 +0100 Subject: [PATCH 3/3] feat: report email notification - refactor --- README.md | 2 +- services/comments.js | 21 +++++++++++---------- 2 files changed, 12 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 573fcd2c..913aae67 100644 --- a/README.md +++ b/README.md @@ -101,7 +101,7 @@ To setup amend default plugin configuration we recommend to put following snippe comments: { enableUsers: true, badWords: false, - moderatorRoles: ["Authenticated"] + moderatorRoles: ["Authenticated"] }, }, ... diff --git a/services/comments.js b/services/comments.js index 4cb192ff..0291295e 100644 --- a/services/comments.js +++ b/services/comments.js @@ -512,27 +512,28 @@ module.exports = { }), async sendAbuseReportEmail(reason, content) { - const rolesToBeNotified = get(strapi.config, "custom.plugins.comments.moderatorRoles", []); - const allUsers = await strapi.plugins['users-permissions'].services.user.fetchAll(); + const pluginName = 'users-permissions'; + const userModel = 'user'; + const rolesToBeNotified = get(strapi.config, "plugins.comments.moderatorRoles", []); - const moderatorsEmails = allUsers.reduce((prev, user) => { - if (rolesToBeNotified.includes(user.role.name)) - return prev.concat(user.email); - else - return prev; - }, []); + const ormModel = strapi.query('user', pluginName); + const query = { 'role.name': rolesToBeNotified } + const users = await ormModel.find(query); + const moderatorsEmails = users.map(user => user.email); + const superAdmins = await strapi.query('user', 'admin').find({'roles.id': 1}) + if (moderatorsEmails.length > 0) { strapi.plugins['email'].services.email.send({ to: moderatorsEmails, - from: 'admin@strapi.io', + from: superAdmins[0].email, subject: 'New abuse report on comment', text: ` There was a new abuse report on your app. Reason: ${reason} Message: ${content} `, - }); + }).catch(err => strapi.log(err)); } } };