? .cvsignore
? pm_thread.patch
? privatemsg_statistics
Index: privatemsg.install
===================================================================
RCS file: /cvs/drupal/contributions/modules/privatemsg/privatemsg.install,v
retrieving revision 1.18
diff -u -p -r1.18 privatemsg.install
--- privatemsg.install	24 Jul 2010 20:21:05 -0000	1.18
+++ privatemsg.install	18 Aug 2010 15:18:52 -0000
@@ -61,6 +61,69 @@ function privatemsg_schema() {
     ),
   );
 
+  $schema['pm_thread'] = array(
+    'description'       => '{pm_thread} holds global information about a thread.',
+    'fields'        => array(
+      'thread_id'    => array(
+        'description'   => 'Private message thread ID.',
+        'type'          => 'int',
+        'not null'      => TRUE,
+        'unsigned'      => TRUE,
+      ),
+      'subject'    => array(
+        'description'   => 'Subject of the thread.',
+        'type'          => 'varchar',
+        'length'        => 255,
+        'not null'      => TRUE,
+      ),
+    ),
+    'primary key' => array('thread_id'),
+  );
+
+  $schema['pm_thread_recipient'] = array(
+    'description'       => '{pm_thread_recipient} holds information about a thread for a single recipient.',
+    'fields'        => array(
+      'thread_id'    => array(
+        'description'   => 'Private message thread ID.',
+        'type'          => 'int',
+        'not null'      => TRUE,
+        'unsigned'      => TRUE,
+      ),
+      'uid'    => array(
+        'description'   => 'User ID of the recipient',
+        'type'          => 'int',
+        'not null'      => TRUE,
+        'unsigned'      => TRUE,
+      ),
+      'started'    => array(
+        'description'   => 'When the first message of the thread was received.',
+        'type'          => 'int',
+        'not null'      => TRUE,
+        'unsigned'      => TRUE,
+      ),
+      'last_updated'    => array(
+        'description'   => 'When the last message of that thread was received.',
+        'type'          => 'int',
+        'not null'      => TRUE,
+        'unsigned'      => TRUE,
+      ),
+      'count'    => array(
+        'description'   => 'Number of messages in a thread.',
+        'type'          => 'int',
+        'not null'      => TRUE,
+        'unsigned'      => TRUE,
+      ),
+      'unread_count'    => array(
+        'description'   => 'Number of unread messages in a thread.',
+        'type'          => 'int',
+        'not null'      => TRUE,
+        'unsigned'      => TRUE,
+      ),
+    ),
+    'primary key' => array('thread_id', 'uid'),
+  );
+
+  
   $schema['pm_message'] = array(
     'description'       => '{pm_messages} holds the message information',
     'fields'        => array(
@@ -623,3 +686,76 @@ function privatemsg_update_6010() {
   db_add_primary_key($ret, 'pm_index', array('mid', 'recipient', 'type'));
   return $ret;
 }
+
+/**
+ * Add {pm_thread} and {pm_thread_recipient} table.
+ */
+function privatemsg_update_6011() {
+  $schema = array();
+  $schema['pm_thread'] = array(
+    'description'       => '{pm_thread} holds global information about a thread.',
+    'fields'        => array(
+      'thread_id'    => array(
+        'description'   => 'Private message thread ID.',
+        'type'          => 'int',
+        'not null'      => TRUE,
+        'unsigned'      => TRUE,
+      ),
+      'subject'    => array(
+        'description'   => 'Subject of the thread.',
+        'type'          => 'varchar',
+        'length'        => 255,
+        'not null'      => TRUE,
+      ),
+    ),
+    'primary key' => array('thread_id'),
+  );
+
+  $schema['pm_thread_recipient'] = array(
+    'description'       => '{pm_thread_recipient} holds information about a thread for a single recipient.',
+    'fields'        => array(
+      'thread_id'    => array(
+        'description'   => 'Private message thread ID.',
+        'type'          => 'int',
+        'not null'      => TRUE,
+        'unsigned'      => TRUE,
+      ),
+      'uid'    => array(
+        'description'   => 'User ID of the recipient',
+        'type'          => 'int',
+        'not null'      => TRUE,
+        'unsigned'      => TRUE,
+      ),
+      'started'    => array(
+        'description'   => 'When the first message of the thread was received.',
+        'type'          => 'int',
+        'not null'      => TRUE,
+        'unsigned'      => TRUE,
+      ),
+      'last_updated'    => array(
+        'description'   => 'When the last message of that thread was received.',
+        'type'          => 'int',
+        'not null'      => TRUE,
+        'unsigned'      => TRUE,
+      ),
+      'count'    => array(
+        'description'   => 'Number of messages in a thread.',
+        'type'          => 'int',
+        'not null'      => TRUE,
+        'unsigned'      => TRUE,
+      ),
+      'unread_count'    => array(
+        'description'   => 'Number of unread messages in a thread.',
+        'type'          => 'int',
+        'not null'      => TRUE,
+        'unsigned'      => TRUE,
+      ),
+    ),
+    'primary key' => array('thread_id', 'uid'),
+  );
+
+  $ret = array();
+  db_create_table($ret, 'pm_thread', $schema['pm_thread']);
+  db_create_table($ret, 'pm_thread_recipient', $schema['pm_thread_recipient']);
+  return $ret;
+}
\ No newline at end of file
Index: privatemsg.module
===================================================================
RCS file: /cvs/drupal/contributions/modules/privatemsg/privatemsg.module,v
retrieving revision 1.143
diff -u -p -r1.143 privatemsg.module
--- privatemsg.module	24 Jul 2010 20:21:05 -0000	1.143
+++ privatemsg.module	18 Aug 2010 15:18:52 -0000
@@ -1565,7 +1565,7 @@ function _privatemsg_send($message) {
     }
   }
 
-  // 1) Save the message body first.
+  // Save the message body and subject.
   $args = array();
   $args[] = $message['subject'];
   $args[] = $message['author']->uid;
@@ -1580,9 +1580,12 @@ function _privatemsg_send($message) {
   // Thread ID is the same as the mid if it's the first message in the thread.
   if (!isset($message['thread_id'])) {
     $message['thread_id'] = $mid;
+
+    // Create an entry for this thread in {pm_thread}
+    db_query("INSERT INTO {pm_thread} (thread_id, subject) VALUES(%d, '%s')", $message['thread_id'], $message['subject']);
   }
 
-  // 2) Save message to recipients.
+  // Save message to recipients.
   // Each recipient gets a record in the pm_index table.
   foreach ($message['recipients'] as $recipient) {
     if (!db_query($index_sql, $mid, $message['thread_id'], $recipient->recipient, $recipient->type, 1) ) {
@@ -1600,6 +1603,9 @@ function _privatemsg_send($message) {
     }
   }
 
+  // Update {pm_thread_recipients} table.
+  _privatemsg_thread_update($message['thread_id']);
+
   module_invoke_all('privatemsg_message_insert', $message);
 
   // If we reached here that means we were successful at writing all messages to db.
@@ -1607,6 +1613,19 @@ function _privatemsg_send($message) {
 }
 
 /**
+ * Updates the {pm_thread_recipient} table for all recipients.
+ */
+function _privatemsg_thread_update($thread_id) {
+  // First, delete all existing information.
+  db_query('DELETE FROM {pm_thread_recipient} WHERE thread_id = %d', $thread_id);
+
+  $insert_sql = 'INSERT INTO {pm_thread_recipient} (thread_id, uid, started, last_updated, count, unread_count)';
+  $select_sql = "SELECT pmi.thread_id, pmi.recipient, MIN(pm.timestamp), MAX(pm.timestamp), COUNT(pmi.thread_id), SUM(pmi.is_new) FROM {pm_index} pmi INNER JOIN {pm_message} ON pm.mid = pmi.mid WHERE pmi.thread_id = %d AND pmi.type IN ('user', 'hidden') GROUP BY pmi.thread_id, pmi.recipient";
+
+  db_query($insert_sql . ' ' . $select_sql, $thread_id);
+}
+
+/**
  * Returns a link to send message form for a specific users.
  *
  * Contains permission checks of author/recipient, blocking and
