Index: privatemsg.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/privatemsg/privatemsg.module,v
retrieving revision 1.70.2.30.2.91.2.65
diff -u -r1.70.2.30.2.91.2.65 privatemsg.module
--- privatemsg.module	11 Sep 2009 21:29:12 -0000	1.70.2.30.2.91.2.65
+++ privatemsg.module	12 Sep 2009 06:10:23 -0000
@@ -287,6 +287,7 @@
  * @ingroup api
  */
 function privatemsg_thread_load($thread_id, $account = NULL) {
+  static $threads = array();
   if ((int)$thread_id > 0) {
     $thread = array('thread_id' => $thread_id);
 
@@ -295,37 +296,44 @@
       $account = drupal_clone($user);
     }
 
-    // Load the list of participants.
-    $query = _privatemsg_assemble_query('participants', $thread_id);
-    $participants = db_query($query['query']);
-    $thread['participants'] = array();
-    while ($participant = db_fetch_object($participants)) {
-      $thread['participants'][$participant->uid] = $participant;
-    }
-    $thread['read_all'] = FALSE;
-    if (!array_key_exists($account->uid, $thread['participants']) && privatemsg_user_access('read all private messages', $account)) {
-      $thread['read_all'] = TRUE;
+    if (!isset($threads[$account->uid])) {
+      $threads[$account->uid] = array();
     }
 
-    // load messages returned by the messages query with _privatemsg_load().
-    $query = _privatemsg_assemble_query('messages', array($thread_id), $thread['read_all'] ? NULL : $account);
-    $conversation = db_query($query['query']);
-    while ($result = db_fetch_array($conversation)) {
-      if ($message = _privatemsg_load($result['mid'], $thread['read_all'] ? NULL : $account)) {
-        $thread['messages'][$result['mid']] = $message;
+    if (!array_key_exists($thread_id, $threads[$account->uid])) {
+      // Load the list of participants.
+      $query = _privatemsg_assemble_query('participants', $thread_id);
+      $participants = db_query($query['query']);
+      $thread['participants'] = array();
+      while ($participant = db_fetch_object($participants)) {
+        $thread['participants'][$participant->uid] = $participant;
+      }
+      $thread['read_all'] = FALSE;
+      if (!array_key_exists($account->uid, $thread['participants']) && privatemsg_user_access('read all private messages', $account)) {
+        $thread['read_all'] = TRUE;
       }
-    }
-    // if there are no messages, don't allow access to the thread.
-    if (empty($thread['messages'])) {
-      return FALSE;
-    }
 
-    // general data, assume subject is the same for all messages of that thread.
-    $thread['user'] = $account;
-    $message = current($thread['messages']);
-    $thread['subject'] = $message['subject'];
+      // load messages returned by the messages query with _privatemsg_load().
+      $query = _privatemsg_assemble_query('messages', array($thread_id), $thread['read_all'] ? NULL : $account);
+      $conversation = db_query($query['query']);
+      while ($result = db_fetch_array($conversation)) {
+        if ($message = _privatemsg_load($result['mid'], $thread['read_all'] ? NULL : $account)) {
+          $thread['messages'][$result['mid']] = $message;
+        }
+      }
+      // if there are no messages, don't allow access to the thread.
+      if (empty($thread['messages'])) {
+        $thread = FALSE;
+      } else {
+        // general data, assume subject is the same for all messages of that thread.
+        $thread['user'] = $account;
+        $message = current($thread['messages']);
+        $thread['subject'] = $message['subject'];
+      }
+      $threads[$account->uid][$thread_id] = $thread;
+    }
 
-    return $thread;
+    return $threads[$account->uid][$thread_id];
   }
   return FALSE;
 }

