Hi,
message_notify module is also have the functionality like notifications, And Now used by drupal-commons and drupal-commerce and will be used in GDO. So Its need integration with mailcomment to post comments via mail. I am started working on this.

I have some questions :-

1) function mailcomment_notifications_message_alter() is used to add header, why not mail_alter is used for this. As I am looking for message_notify I was thinking hook_mail_alter() will be good way to add headers and get values (nid, cid etc) from mail.

2) I have create a patch for message_notify to start work with it http://drupal.org/node/1730648#comment-6349032. Now This mail headers will edited in mailcomment module. So I think mail_alter will be good.

3) Mailcomment need some generalization to not stick with notifications. It should work with all email sending module (If that is notifying about an event or if reply is possible).

Attaching a patch (just to start work.).

Thank in advance.

Comments

crazyrohila’s picture

Status: Active » Needs work

changing status.

nbchip’s picture

Hi,

This is somehow related issue http://drupal.org/node/1579488

paolomainardi’s picture

@crazyrohila any progress on this ?

grota’s picture

Attached you'll find a patch, for the integration with message_notify.
The key design issue is that the decision to continue altering or not, and the calculation of the parameters has been offloaded to an alter hook. In other words using the attached path you need to implement something like the following in your module.

/**
 * Implements hook_mailcomment_prepare_message_alter
 *
 * @param $message
 *   The message array as passed into hook_mail_alter
 *
 * @param $messageid_params
 *  This array is used to create the signature of mailcomment
 *  The array has the following keys:
 *    "nid"  (the nid the comment is referring to)
 *    "uid"  (the uid the comment is referring to)
 *    "cid"  (if this is a message specific for a comment set the cid here, otherwise set this to 0)
 *    "time" (a timestamp used for expiring purposes, use either the new entity (node or comment) timestamp)
 *
 * @param $from_alter
 * An array that tells the mailcomment whether to proceed of not to alter the outgoing message
 * The array has a key "proceed" with value TRUE or FALSE
 */
function yourmodule_mailcomment_prepare_message_alter(&$message, &$messageid_params, &$from_alter) {
  $enabled_messages = array(
    'my_message_type_1_node',
    'my_message_type_2_comment',
  );
  if (!in_array($message['key'], $enabled_messages)) {
    $from_alter['proceed'] = FALSE;
    return;
  }
  if (empty($message['params']['message_entity']->field_target_nodes[LANGUAGE_NONE][0]['target_id'])) {
    $message_log = t('Received a notification email for a message without a node owner');
    watchdog('mailcomment', $message_log, array(), WATCHDOG_WARNING);
    $from_alter['proceed'] = FALSE;
    return;
  }
  $node_of_message_nid = $message['params']['message_entity']->field_target_nodes[LANGUAGE_NONE][0]['target_id'];
  $node_of_message = node_load($node_of_message_nid);
  if ($node_of_message->comment != 2) {
    $from_alter['proceed'] = FALSE;
    return;
  }
  $recipient_uid = $message['params']['message_entity']->uid;
  $recipient = user_load($recipient_uid);
  switch ($message['key']) {
    case 'my_message_type_1_node':
      $messageid_params['uid'] = $recipient->uid;
      $messageid_params['nid'] = $node_of_message->nid;
      $messageid_params['cid'] = 0;
      $messageid_params['time'] = $node_of_message->created;
      if (variable_get('mailcomment_alter_subjects', 1)) {
        $subject = $message['subject'];
        $subject = variable_get('site_name', '') ? '[' . variable_get('site_name', '') . '] ' . $node_of_message->title : $subject;
        $message['subject'] = $subject;
      }
      break;
    case 'my_message_type_2_comment':
      $cid = $message['params']['message_entity']->field_target_comments['und'][0]['target_id'];
      $comment = comment_load($cid);
      $messageid_params['uid'] = $comment->uid;
      $messageid_params['cid'] = $comment->cid;
      $messageid_params['nid'] = $comment->nid;
      $ancestor_msg_id = mailcomment_mail_comment_ancestor_message_id($messageid_params['nid'], $messageid_params['cid']);
      $messageid_params['time'] = $comment->created;

      if (variable_get('mailcomment_alter_subjects', 1)) {
        $subject = $message['subject'];
        $subject = t('Re:') . ' ' . $subject;
        $message['subject'] = $subject;
      }
      break;
    default:
      return;
  }
  if (isset($ancestor_msg_id)) {
    $message['headers']['In-Reply-To'] = $ancestor_msg_id;
  }
}
danepowell’s picture

FYI, I may be receiving sponsorship to integrate this into Mail Comment. Please stand by. Thanks to those of you who have already contributed this code.

mrfelton’s picture

I used the patch from #4 with some success for a drupal commons mailing list module over at https://drupal.org/node/1964510#comment-7718265

danepowell’s picture

Just for the record - the short answer to 'why use hook_notifications_message_alter()' is 'because that's how we did it in versions 6.x-2.x and (to some extent) 6.x-1.x' :)

The longer answer is that (I think) there is no standard way that entities are embedded in outgoing emails. Notifications does it one way, Message Notify does it another, but either way we need to implement custom code to extract the entity uid / nid / cid / etc... to generate the MC signature. In the long run, it probably makes sense to just use hook_mail_alter for everything, but let's start a separate issue for that: #2061719: Use hook_mail_alter() instead of hook_notifications_message_alter()

I think the way forward here is to finish #1579488: Move Notifications integration into submodule., then work on Message Notify integration based on patches in OP / #4 / #6

danepowell’s picture

Title: Integration with Message_notify » Basic integration with Message Notify
Status: Needs work » Fixed

http://drupalcode.org/project/mailcomment.git/commit/a9e3ceb

Okay, I committed a basic version of this, along with a basic simpletest. It's still a little hackish and untested. I'd like to clean it up so that the base module provides more of an API that the submodules interface with, rather than having the submodules basically just copy code from one another.

So, the next steps for this are to finish #2061719: Use hook_mail_alter() instead of hook_notifications_message_alter(), and then #2063449: API for submodules.

danepowell’s picture

@crazyrohila - I just checked, and the reason we have to use hook_notifications_message_alter() instead of hook_mail_alter() is because Notifications doesn't embed event/object info (such as the corresponding node id) in the outgoing message.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.