From aa433d30267a16c95205ffcc3c50d1be4ca2be5b Mon Sep 17 00:00:00 2001
From: Sascha Grossenbacher <saschagros@gmail.com>
Date: Mon, 18 Apr 2011 11:31:37 +0200
Subject: [PATCH] Issue #750566 by Berdir, manarth: Added message delete changed hook and update privatemsg_thread_change_delete() to work like privatemsg_thread_change_status().

---
 privatemsg.api.php |   26 ++++++++++++++++++++++++++
 privatemsg.module  |   39 ++++++++++++++++++++++++++++++++-------
 2 files changed, 58 insertions(+), 7 deletions(-)

diff --git a/privatemsg.api.php b/privatemsg.api.php
index 72461c7..df7ea2f 100644
--- a/privatemsg.api.php
+++ b/privatemsg.api.php
@@ -393,6 +393,32 @@ function hook_privatemsg_message_status_changed($pmid, $status, $account) {
 }
 
 /**
+ * Allows response to a deleted change.
+ *
+ * Modules implementing this hook should be aware that messages are only
+ * marked as deleted and not removed from the database. They will only
+ * eventually be deleted by the flushing.
+ *
+ * Therefore, modules should not delete data in this hook but in
+ * hook_privatemsg_message_flush().
+ *
+ * @param $mid
+ *   Message id.
+ * @param $deleted
+ *   TRUE when the message was marked as deleted, FALSE when marked as not
+ *   deleted.
+ * @param $account
+ *   User object, if NULL then the message was marked as deleted for all users.
+ *
+ * @see privatemsg_message_change_delete()
+ * @see privatemsg_thread_change_delete()
+ *
+ */
+function hook_privatemsg_message_status_deleted($mid, $deleted, $account) {
+
+}
+
+/**
  * @}
  */
 
diff --git a/privatemsg.module b/privatemsg.module
index 78d46a4..1108005 100644
--- a/privatemsg.module
+++ b/privatemsg.module
@@ -1434,6 +1434,9 @@ function privatemsg_block_save($delta = '', $edit = array()) {
   if ($delta == 'privatemsg-new') {
     variable_set('privatemsg_no_messages_notification', $edit['notification']);
   }
+
+  // Allow modules to respond to the deleted changes.
+  module_invoke_all('privatemsg_message_status_delete', $pmid, $delete, $account);
 }
 
 function privatemsg_title_callback($title = NULL) {
@@ -1510,6 +1513,8 @@ function privatemsg_message_change_delete($pmid, $delete, $account = NULL) {
       ->condition('type', array('user', 'hidden'));
   }
   $update->execute();
+
+  module_invoke_all('privatemsg_message_status_deleted', $pmid, $delete, $account);
 }
 
 /**
@@ -2193,15 +2198,35 @@ function privatemsg_thread_change_delete($threads, $delete, $account = NULL) {
     $account = clone $user;
   }
 
-  // Merge status and uid with the threads list. array_merge() will not overwrite/ignore thread_id 1.
-  $params = array_merge(array($delete, $account->uid), $threads);
+  // Record which messages will be deleted.
+  $changed = db_select('pm_index', 'pmi')
+    ->fields('pmi', array('mid'))
+    ->condition('deleted', 0, $delete ? '=' : '>')
+    ->condition('recipient', $account->uid)
+    ->condition('type', array('user', 'hidden'))
+    ->condition('thread_id', $threads)
+    ->execute()
+    ->fetchCol();
 
-  // Load all messages of those threads including the deleted.
-  $query = _privatemsg_assemble_query('messages', $threads, $account, TRUE);
+  // Merge status and uid with the threads list. array_merge() will not overwrite/ignore thread_id 1.
+  $delete_value = 0;
+  if ($delete == TRUE) {
+    $delete_value = REQUEST_TIME;
+  }
+  $params = array_merge(array($delete_value, $account->uid), $threads);
+  // Update the status of the threads.
+  db_update('pm_index')
+    ->fields(array(
+      'deleted' => $delete_value
+    ))
+    ->condition('recipient', $account->uid)
+    ->condition('type', array('user', 'hidden'))
+    ->condition('thread_id', $threads)
+    ->execute();
 
-  // Delete each message. We need to do that to trigger the delete hook.
-  foreach ($query->execute() as $row) {
-    privatemsg_message_change_delete($row->mid, $delete, $account);
+  // Allow modules to respond to the deleted changes.
+  foreach ($changed as $mid) {
+    module_invoke_all('privatemsg_message_status_deleted', $mid, $delete, $account);
   }
 
   if ($delete) {
-- 
1.7.4.1

