verbb / comments

A Craft CMS plugin for managing comments directly within the CMS.
Other
137 stars 35 forks source link

Notifications not sent while moderator Notifications enabled #274

Open romainpoirier opened 1 year ago

romainpoirier commented 1 year ago

Describe the bug

When replying to comments, notifications are not sent while moderator Notifications are enabled.

The log is not very explicit: [INFO] Moderator Notifications disabled.

I can confirm that other Craft system emails are sent normally, and that $event->isValid = false (as discussed in #273) is not added.

Here's the content of my config/comments.php file:

<?php

use Craft;
use craft\elements\User;

$notificationAdmins = [];
foreach (User::find()->groupId(getenv('MODERATOR_USER_GROUP'))->all() as $user) {
   $notificationAdmins[] = [
         'email' => $user->email,
         'enabled' => true
   ];
}

return [
   '*' => [
      'indexSidebarLimit' => 25,
      'indexSidebarGroup' => true,
      'indexSidebarIndividualElements' => false,
      'defaultQueryStatus' => ['approved'],

      // General
      'allowGuest' => false,
      'guestNotice' => '',
      'guestRequireEmailName' => true,
      'guestShowEmailName' => true,
      'requireModeration' => false,
      'moderatorUserGroup' => getenv('MODERATOR_USER_GROUP'),
      'autoCloseDays' => '',
      'maxReplyDepth' => '',
      'maxUserComments' => '',

      // Voting
      'allowVoting' => false,
      'allowGuestVoting' => false,
      'downvoteCommentLimit' => 5,
      'hideVotingForThreshold' => false,

      // Flagging
      'allowFlagging' => false,
      'allowGuestFlagging' => false,
      'flaggedCommentLimit' => 5,

      // Templates - Default
      'showAvatar' => true,
      'placeholderAvatar' => '',
      'showTimeAgo' => true,
      'outputDefaultCss' => true,
      'outputDefaultJs' => true,

      // Templates - Custom
      'templateFolderOverride' => '_comments',
      'templateEmail' => '',

      // Security
      'enableSpamChecks' => false,
      'securityMaxLength' => '',
      'securityFlooding' => 5,
      'securityModeration' => '',
      'securitySpamlist' => '',
      'securityBanned' => '',
      'securityMatchExact' => false,
      'recaptchaEnabled' => false,
      'recaptchaKey' => '',
      'recaptchaSecret' => '',
      'recaptchaMinScore' => 0.5,

      // Notifications
      'notificationAuthorEnabled' => true,
      'notificationReplyEnabled' => true,
      'notificationSubscribeAuto' => true,
      'notificationSubscribeDefault' => true,
      'notificationSubscribeEnabled' => false,
      'notificationSubscribeCommentEnabled' => false,
      'notificationModeratorEnabled' => true,
      'notificationModeratorApprovedEnabled' => false,
      'notificationAdmins' => $notificationAdmins,
      'notificationAdminEnabled' => true,
      'notificationFlaggedEnabled' => false,

      // Permissions
      'permissions' => [],

      // Custom Fields
      'showCustomFieldNames' => false,
      'showCustomFieldInstructions' => false,

      // CP Sort
      'sortDefaultKey' => 'structure',
      'sortDefaultDirection' => 'asc',
   ]
];

As you can see, moderation (requireModeration) is disabled, and auto-subscribe (notificationSubscribeAuto) is enabled.

Steps to reproduce

  1. Set the config (config/comments.php) as described
  2. On front-end, post a comment with one account, reply to it with another account

Craft CMS version

4.3.5

Plugin version

2.0.4

Multi-site?

Yes

Additional context

No response

engram-design commented 1 year ago

That's because requireModeration is false, so you won't receive moderator emails. The status of a comment will be automatically set to approved instead of pending, despite the value of notificationModeratorEnabled.

romainpoirier commented 1 year ago

Sorry, my question was not clear and complete: I don't need moderation, comments have to be published immediately. However, I still need the notifications to be sent for both moderators and public users.

How can I send moderator notifications even if comments are set automatically to true? I want them to be notified when there's a new comment, but the comment should be displayed without moderation.

Furthermore, independently of moderation, I need that every user get a notification when its comment gets replies: this should be independent of moderation, and isn't working in my case.

In the setup previously copied, at least notificationReplyEnabled, notificationSubscribeAuto and notificationSubscribeDefault are still not sending emails to public users as expected while set to true.

romainpoirier commented 1 year ago

Despite requireModeration set to true doesn't fit what I'm looking for (because I need direct publish), I tried it this test scenario:

  1. Public user write a comment;
  2. The moderator Save the comment to Approved in the CP;
  3. The public user is not getting its email notification;

At step 1., I get this in the comments logs: [INFO] Not sending reply or author notification - marked as pending (to be moderated). While notificationModeratorApprovedEnabled setting is also set to true. Can you confirm that I should not expect an email notification at this step?

At step 3. I get this in the comments logs:

[INFO] Prepare Moderator Notifications.
[ERROR] Error rendering email template: Variable "language" does not exist.
[INFO] Email sent successfully to moderator (user@xxx.com)
[INFO] Prepare Admin Notifications.
[ERROR] Error rendering email template: Variable "language" does not exist.
[INFO] Email sent successfully to admin (moderator@xxx.com)
[INFO] Request context:
$_GET = [
    'p' => 'admin/actions/queue/run'
    'site' => 'fr'
    'v' => '1672389045776'
]

Despite the Email sent successfully infos, emails are not sent (I anonymized the address in my example for confidentiality). I can't also fix the Error rendering email template: Variable "language" does not exist. error, as my 'templateEmail' => '' setting is set to default. Commenting this line doesn't change anything.

FYI, no custom HTML Email Template in set in my Email Settings (admin/settings/email), and I don't use the language variable in the System Messages content (/admin/utilities/system-messages).

I have also tried to uninstall and re-install the Comments plugin from scratch, and still gets the same error. FYI, when using the Uninstall > Remove from Plugins (/admin/settings/plugins) feature, and if I leave comments before the uninstall, there're appearing again after the re-install and I can't delete them. They are appearing while the craft_comments_ tables were deleted by the uninstallation. I found their ID that I deleted manually from the craft_elements and craft_elements_sites tables, but the comments are still showing up (and return a 404 on click).

romainpoirier commented 1 year ago

I have re-installed the prod DB in my local env, and now the expected emails are sent.

After looking up the difference, I finally found that this is the Use Queue for Notifications setting (admin/comments/settings) enabled that is blocking the emails.

FYI, this is the log I now get when this setting is disabled:

[INFO] Moderator Notifications disabled.
[INFO] Prepare Author Notifications.
[INFO] Cannot send element author notification: Commenter #1 same as author #1.
[INFO] Prepare Reply Notifications.
[INFO] Email sent successfully comment author (author@xxx.com)
[INFO] Prepare Subscribe Notifications.
[INFO] Email sent successfully to subscriber (subscriber@xxx.com)
[INFO] Prepare Admin Notifications.
[INFO] Email sent successfully to admin (admin@xxx.com)

And this is what I only get in the log when this setting is enabled (and is not sending the emails as expected): [INFO] Moderator Notifications disabled.

Please can you fix this? If it's disabled, the submit of a comment takes a bit too much time because of the multiples emails directly sent.

By the way, why is it the only setting that we can't set from the config/comments.php file?

engram-design commented 1 year ago

How can I send moderator notifications even if comments are set automatically to true? I want them to be notified when there's a new comment, but the comment should be displayed without moderation.

At the moment, this isn't possible. In my mind, there's no need for any moderator notification as moderation isn't enabled. These notifications are geared to be "actionable" (the moderator needs to moderate the comment).

Furthermore, independently of moderation, I need that every user get a notification when its comment gets replies: this should be independent of moderation, and isn't working in my case.

This should work regardless of moderation settings, as you describe. This is controlled by notificationReplyEnabled. The only logic for sending this is if moderation is disabled and notificationReplyEnabled is enabled and you're replying to a comment. Any other checks will be logged (you won't get a notification when replying to your own comment, etc).

I can't also fix the Error rendering email template: Variable "language" does not exist. error, as my 'templateEmail' => '' setting is set to default. Commenting this line doesn't change anything.

This was just fixed in 2.0.5

After looking up the difference, I finally found that this is the Use Queue for Notifications setting (admin/comments/settings) enabled that is blocking the emails.

Glad you've got your emails working. Using the queue should be no different - provided you've either got a queue processing setup, or are visiting the control panel to trigger the queue sending. It's working on my end to send emails via the queue (which is recommended, but not the default to prevent a breaking change)

By the way, why is it the only setting that we can't set from the config/comments.php file?

You can! Looks like it was just missing from the docs.

romainpoirier commented 1 year ago

At the moment, this isn't possible. In my mind, there's no need for any moderator notification as moderation isn't enabled. These notifications are geared to be "actionable" (the moderator needs to moderate the comment).

In my case, the comments are used in a professional area, where's there's not so much activity, and where users and safe users (their account are created by admins). There's no need for moderation, but the moderators/admins would like to be informed each time a user is commenting something.

I was finally able to send the intended notifications using this:


use Craft;
use craft\elements\User;

$moderators = [];
foreach (User::find()->group('moderators')->all() as $user) {
  $moderators[] = [
      'email' => $user->email,
      'enabled' => true
  ];
}

'notificationModeratorApprovedEnabled' => false,
'notificationAdmins' => $moderators,
'notificationAdminEnabled' => true,

Thank you for all the other points, I can confirm that now I can make working everything as expected.