Maintain single thread between two users in privatemsg module

11 years ago, May 30, 2013
Reading time: 5 mins

If you need Facebook like single thread messaging between two users even while using new message form, using privatemsg in your drupal website.

I have a patch for the privatemsg module.

There is a better way though, If you dont want to patch the privatemsg module,

you can use hook_privatemsg_message_presave_alter();

You can try db_query() to get an existing thread between current user and the recipient and then change the $message->thread_id there. 

For sake of easier illustration I have created a patch


Index: privatemsg.module
===================================================================
--- privatemsg.module	
+++ privatemsg.module	
@@ -1832,7 +1836,7 @@
       ->fields($args)
       ->execute();
     $message->mid = $mid;
-
+    
     // 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;
@@ -1841,6 +1845,21 @@
     // 2) Save message to recipients.
     // Each recipient gets a record in the pm_index table.
     foreach ($message->recipients as $recipient) {
+      // To continue old msg thread between two users when submitting new msg
+      if(count($message->recipients) == 1){
+         $res = db_query("select author, recipient, thread_id , name from {pm_message} msg
+              left join {pm_index} ind on msg.mid = ind.mid
+              left join {users} u on u.uid = ind.recipient
+              where ((recipient = :recipient and author = :uid) or (author = :recipient and recipient = :uid)) 
+              and (select count(distinct(recipient)) from {pm_index} where thread_id=ind.thread_id ) < 3
+              order by thread_id desc",array(':recipient'=>$recipient->recipient, ':uid' => $message->author->uid));
+         if($thread = $res->fetchObject()){
+           $thread_id = $thread->thread_id;
+           $_REQUEST['destination'] = 'messages/view/' . $thread_id;
+           $message->thread_id = $thread_id;
+         }
+         
+      }
       $query->values(array(
         'mid' => $mid,
         'thread_id' => $message->thread_id,
Index: privatemsg.pages.inc
===================================================================
--- privatemsg.pages.inc	(revision 14816)
+++ privatemsg.pages.inc	(working copy)
@@ -638,6 +638,10 @@
       elseif (!empty($redirect)) {
         $form_state['redirect'] = $redirect;
       }
+    }else{
+      if($_REQUEST['destination'])
+        $form_state['redirect'] = $_REQUEST['destination'];
     }
   }
   catch (Exception $e) {

Here I have tested if the message is sent to a single recipient, and done the query to get one existing thread between the current user and recipient and then assign the thread_id to the $message object.

<?php 
if(count($message->recipients) == 1){
         $res = db_query("select author, recipient, thread_id , name from {pm_message} msg
              left join {pm_index} ind on msg.mid = ind.mid
              left join {users} u on u.uid = ind.recipient
              where ((recipient = :recipient and author = :uid) or (author = :recipient and recipient = :uid)) 
              and (select count(distinct(recipient)) from {pm_index} where thread_id=ind.thread_id ) < 3
              order by thread_id desc",array(':recipient'=>$recipient->recipient, ':uid' => $message->author->uid));
         if($thread = $res->fetchObject()){
           $thread_id = $thread->thread_id;
           $_REQUEST['destination'] = 'messages/view/' . $thread_id;
           $message->thread_id = $thread_id;
         }
         
      }
?>

Nothing much on privatemsg.pages.inc I have added the redirect to the thread view page, assigned previously in _privatemsg_send().

+           $_REQUEST['destination'] = 'messages/view/' . $thread_id;

so that after new msg is sent the user gets into the thread view.

+      if($_REQUEST['destination'])
+        $form_state['redirect'] = $_REQUEST['destination'];
Previous
Scroll Horizontally on mouse wheel scroll – Example: Horizontal scroller showcase
Next
Select all contacts with Javascript code to easily invite friends to a page on facebook
© 2024 Anil Maharjan