Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: report email notification #83

Merged
merged 4 commits into from
Nov 14, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 13 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ Enjoy 🎉

### 🖐 Requirements

Complete installation requirements are exact same as for Strapi itself and can be found in the documentation under <a href="https://strapi.io/documentation/v3.x/installation/cli.html#step-1-make-sure-requirements-are-met">Installation Requirements</a>.
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**:

Expand All @@ -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 &amp; 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",
Expand All @@ -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": {
Expand All @@ -93,29 +93,31 @@ 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/<env>/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/<env>/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"]
},
},
...
```

### 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: {
Expand Down Expand Up @@ -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",
Expand All @@ -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",
Expand Down
35 changes: 31 additions & 4 deletions services/comments.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -508,5 +509,31 @@ module.exports = {
sanitizeCommentEntity: (entity) => ({
...entity,
authorUser: sanitizeEntity(entity.authorUser, { model: strapi.plugins['users-permissions'].models.user }),
}),
}),

async sendAbuseReportEmail(reason, content) {
const pluginName = 'users-permissions';
const userModel = 'user';
const rolesToBeNotified = get(strapi.config, "plugins.comments.moderatorRoles", []);

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: 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));
}
}
};