From 84f8e76a311a24c14d7a1ff6f38bfb34c2a199f1 Mon Sep 17 00:00:00 2001
From: Sascha Grossenbacher <saschagros@gmail.com>
Date: Fri, 3 Jun 2011 00:54:06 +0200
Subject: [PATCH] Issue #147420 by heyrocker, Berdir: Added replied tracking for threads.

---
 privatemsg.install   |   23 +++++++++++++++++++++++
 privatemsg.module    |   18 ++++++++++++++++++
 privatemsg.pages.inc |   18 ++++++++++++++++++
 privatemsg.test      |    1 -
 4 files changed, 59 insertions(+), 1 deletions(-)

diff --git a/privatemsg.install b/privatemsg.install
index 0ac5f22..4204a05 100644
--- a/privatemsg.install
+++ b/privatemsg.install
@@ -108,6 +108,13 @@ function privatemsg_schema() {
         'unsigned'      => TRUE,
         'default'       => 0,
       ),
+      'reply_to_mid' => array(
+        'description'   => 'mid of the message this is a reply to (if any)',
+        'type'          => 'int',
+        'default'       => 0,
+        'not null'      => TRUE,
+        'unsigned'      => TRUE,
+      )
     ),
     'primary key'     => array('mid'),
   );
@@ -404,3 +411,19 @@ function privatemsg_update_7201() {
   return $ret;
 }
 
+
+/**
+ * Add field to track what message this is a reply to (if any).
+ */
+function privatemsg_update_7202() {
+  db_add_field('pm_message', 'reply_to_mid',
+    array(
+      'description'   => 'mid of the message this is a reply to (if any)',
+      'type'          => 'int',
+      'default'       => 0,
+      'not null'      => TRUE,
+      'unsigned'      => TRUE,
+    )
+  );
+}
+
diff --git a/privatemsg.module b/privatemsg.module
index 664f386..387f20b 100644
--- a/privatemsg.module
+++ b/privatemsg.module
@@ -1003,6 +1003,9 @@ function privatemsg_sql_list($account, $argument = 'list') {
   $query->addExpression('MAX(pm.has_tokens)', 'has_tokens');
   $query->addExpression('SUM(pmi.is_new)', 'is_new');
 
+  // Needed to for tracking replies.
+  $query->addExpression('MAX(pm.reply_to_mid)', 'last_reply_to_mid');
+
   // Load enabled columns
   $fields = privatemsg_get_enabled_headers();
 
@@ -1666,6 +1669,10 @@ function privatemsg_reply($thread_id, $body, $options = array()) {
     );
   }
 
+  // Add the reply_to_mid, so we can mark that message as replied
+  $thread = privatemsg_thread_load($thread_id);
+  $message->reply_to_mid = end($thread['messages'])->mid;
+
   $message->thread_id = $thread_id;
   // Load participants
   $message->recipients = _privatemsg_load_thread_participants($thread_id, $message->author);
@@ -1677,6 +1684,7 @@ function privatemsg_reply($thread_id, $body, $options = array()) {
       _privatemsg_handle_recipients($validated['message']->mid, $validated['message']->recipients, FALSE);
     }
   }
+
   return $validated;
 }
 
@@ -1817,6 +1825,10 @@ function _privatemsg_send($message) {
     $args['format'] = $message->format;
     $args['timestamp'] = $message->timestamp;
     $args['has_tokens'] = (int)$message->has_tokens;
+    if (isset($message->reply_to_mid)) {
+      $args['reply_to_mid'] = $message->reply_to_mid;
+    }
+
     $mid = db_insert('pm_message')
       ->fields($args)
       ->execute();
@@ -2085,6 +2097,12 @@ function _privatemsg_list_thread($tableselect) {
       $row['#attributes']['class'][] = 'privatemsg-unread';
     }
 
+    if (!empty($thread['is_replied'])) {
+      // Set the css class in the tr tag.
+      $row['#attributes']['class'][] = 'privatemsg-replied';
+    }
+
+
     foreach ($enabled_headers as $key) {
       // Theme each enabled field.
       $row[$key] = theme($headers[$key]['#theme'], array('thread' => $thread));
diff --git a/privatemsg.pages.inc b/privatemsg.pages.inc
index 162aba1..7efb0f5 100644
--- a/privatemsg.pages.inc
+++ b/privatemsg.pages.inc
@@ -191,6 +191,7 @@ function privatemsg_list($form, &$form_state, $argument, $account) {
   $query = _privatemsg_assemble_query('list', $account, $argument);
   $i = 0;
   foreach ($query->execute() as $row) {
+
     // Store the raw row data.
     $form['updated']['list']['#options'][$row->thread_id] = (array)$row;
     // Tableselect sorts the options, set a weight so that the order doesn't get
@@ -198,6 +199,16 @@ function privatemsg_list($form, &$form_state, $argument, $account) {
     $form['updated']['list']['#options'][$row->thread_id]['#weight'] = $i++;
   }
 
+  // Load the last reply that is not from the current user.
+  $result = db_query('SELECT pmi.thread_id, MAX(pm.mid) AS last_message FROM {pm_message} pm INNER JOIN {pm_index} pmi ON pm.mid = pmi.mid WHERE pmi.thread_id IN (:thread_ids) AND pm.author <> :current_uid GROUP BY pmi.thread_id', array(':current_uid' => $account->uid, ':thread_ids' => array_keys($form['updated']['list']['#options'])));
+  foreach ($result as $row) {
+    // Set replied flag if there is no newer message from another user than
+    // the last replied.
+    if ($row->last_message <= $form['updated']['list']['#options'][$row->thread_id]['last_reply_to_mid']) {
+      $form['updated']['list']['#options'][$row->thread_id]['is_replied'] = TRUE;
+    }
+  }
+
   if (!empty($form['updated']['list']['#options'])) {
     $form['updated']['actions'] = _privatemsg_action_form($argument);
   }
@@ -393,6 +404,13 @@ function privatemsg_form_reply($form, &$form_state, $thread) {
     '#weight' => 20,
   );
 
+  // Include the mid of the message we're responding to so we can mark it as
+  // replied when the form is submitted.
+  $reply_to_mid = end($thread['messages'])->mid;
+  $form['reply_to_mid'] = array(
+    '#type' => 'value',
+    '#value' => $reply_to_mid,
+  );
   $form['thread_id'] = array(
     '#type' => 'value',
     '#value' => $thread['thread_id'],
diff --git a/privatemsg.test b/privatemsg.test
index 66a1cac..a04d4da 100644
--- a/privatemsg.test
+++ b/privatemsg.test
@@ -1297,7 +1297,6 @@ class PrivatemsgAPITestCase extends DrupalWebTestCase {
     $resultf2 = privatemsg_reply($thread_row->mid, 'Test Body', array('author' => $recipient3));
     $errormessage = t('@user is not allowed to write messages.', array('@user' => $recipient3->name));
     $this->assertEqual($errormessage, $resultf2['messages']['error'][0], 'API denied to send message from user without permission');
-
   }
 
   /**
-- 
1.7.4.1

