Message Subscribe is ignoring flag and sending notifications to everyone

Created on 16 November 2021, over 2 years ago
Updated 2 February 2024, 5 months ago

I'm developing a Drupal based forum and I'm using the Message stack modules combined with a tweaked version of this custom module. https://github.com/Lullabot/message_integration
The idea is that all users get an email when a new thread is posted, but only users to subscribe to a thread get notification for comments left in that thread.
The part where people get emails for new threads is working, but when a comment is left, all users get an email notification about that too. At this point I can't tell if the issue is with the custom module or with Message Subscribe. I've turned on debug but I don't see any messages about it deciding who to and not send the emails to, just that its preparing the message and sending it.

My custom module isn't that different from what Lullabot set up.

This is the code that creates the subscriber list:

function message_integration_subscribe_options_comment() {
  $query = \Drupal::entityQuery('user')
    ->condition('status', 1);
  $query->condition('uid', [0, 1], 'NOT IN');
  $subscribe_uids = $query->execute();

  $notifiers = ['email'];
  $subscribe_options = [
    'notify message owner' => FALSE,
    'uids' => [],
  ];
  foreach ($subscribe_uids as $uid) {
    $subscribe_options['uids'][$uid] = new DeliveryCandidate([], $notifiers, $uid);
  }
  return $subscribe_options;
}

And here's the code for when a comment is left:

function message_integration_comment_insert(Comment $comment) {

  // See if this handling should be skipped.
  $config = \Drupal::config('message_integration.settings');
  $skip = $config->get('skip');
  if (!empty($skip)) {
    return;
  }

  $node = $comment->get('entity_id')->first()->get('entity')->getTarget()->getValue();
  if (!in_array($node->bundle(), MESSAGE_INTEGRATION_CONTENT_TYPES)) {
    return;
  }

  // Create a message.
  $message = Message::create([
    'template' => 'create_comment',
    'uid' => $node->getOwnerId(),
  ]);
  $message->set('field_comment_reference', $comment);
  $message->set('field_published', $comment->isPublished());
  $message->save();

  // Queue messages to notify all the node subscribers about new comment.
  $subscribers = \Drupal::service('message_subscribe.subscribers');
  
  // Create a custom subscriber list to notify all active users.
  // This is an end run around the normal flag subscription system since we
  // don't want to subscribe every user to every piece of content.
  $subscribe_options = message_integration_subscribe_options_comment();
  $options = $subscribe_options;
  foreach ($subscribe_options['uids'] as $uid => $values) {
    $options['uids'] = [$uid => $values];
    $subscribers->sendMessage($node, $message, [], $options);
  }
}

I've been going in circles and pulling my hair out, so any help would be appreciated.

πŸ’¬ Support request
Status

Fixed

Version

1.1

Component

Documentation

Created by

πŸ‡ΊπŸ‡ΈUnited States fallenturtle

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • πŸ‡ΊπŸ‡ΈUnited States bluegeek9
  • Status changed to Fixed 5 months ago
  • πŸ‡ΊπŸ‡ΈUnited States bluegeek9

    The function `message_integration_subscribe_options_comment` creates a list of all active users that are not uid=1

      $query = \Drupal::entityQuery('user')
        ->condition('status', 1);
      $query->condition('uid', [0, 1], 'NOT IN');
      $subscribe_uids = $query->execute();
    

    Send notifications to subscribers like this:

      $subscribers = \Drupal::service('message_subscribe.subscribers');
      $subscribers->sendMessage($node, $message)
    
  • Automatically closed - issue fixed for 2 weeks with no activity.

Production build 0.69.0 2024