Adds missing conversation rebuild if messages are deleted
authorMatthias Schmidt <gravatronics@live.com>
Sun, 28 Jul 2013 22:40:16 +0000 (00:40 +0200)
committerMatthias Schmidt <gravatronics@live.com>
Sun, 28 Jul 2013 22:40:16 +0000 (00:40 +0200)
files/lib/data/conversation/ConversationAction.class.php
files/lib/data/conversation/ConversationEditor.class.php
files/lib/data/conversation/message/ConversationMessageAction.class.php

index 47a3e7dcf1d125db942ffa26591fbcfe2da0a176..0ffb72dee36dec7fc388909e40a60bf3e079f522 100644 (file)
@@ -756,6 +756,49 @@ class ConversationAction extends AbstractDatabaseObjectAction implements IClipbo
                );
        }
        
+       /**
+        * Rebuilds the conversation data of the relevant conversations.
+        */
+       public function rebuild() {
+               if (empty($this->objects)) {
+                       $this->readObjects();
+               }
+               
+               // collect number of messages for each conversation
+               $conditionBuilder = new PreparedStatementConditionBuilder();
+               $conditionBuilder->add('conversation_message.conversationID IN (?)', array($this->objectIDs));
+               $sql = "SELECT          conversationID, COUNT(messageID) AS messages
+                       FROM            wcf".WCF_N."_conversation_message conversation_message
+                       ".$conditionBuilder."
+                       GROUP BY        conversationID";
+               $statement = WCF::getDB()->prepareStatement($sql);
+               $statement->execute($conditionBuilder->getParameters());
+               
+               $objectIDs = array();
+               while ($row = $statement->fetchArray()) {
+                       if (!$row['messages']) {
+                               continue;
+                       }
+                       $objectIDs[] = $row['conversationID'];
+                       
+                       $conversationEditor = new ConversationEditor(new Conversation(null, array(
+                               'conversationID' => $row['conversationID']
+                       )));
+                       $conversationEditor->update(array(
+                               'replies' => $row['messages'] - 1
+                       ));
+                       $conversationEditor->updateFirstMessage();
+                       $conversationEditor->updateLastMessage();
+               }
+               
+               // delete conversations without messages
+               $deleteConversationIDs = array_diff($this->objectIDs, $objectIDs);
+               if (!empty($deleteConversationIDs)) {
+                       $conversationAction = new ConversationAction($deleteConversationIDs, 'delete');
+                       $conversationAction->executeAction();
+               }
+       }
+       
        /**
         * Adds conversation modification data.
         * 
index 2e3a88de7767dfae6d9a88e21e669296ca9dcb2b..3b747b74740a0be77698129b6fe1e438773e8039 100644 (file)
@@ -182,6 +182,45 @@ class ConversationEditor extends DatabaseObjectEditor {
                }
        }
        
+       /**
+        * Updates the first message of this conversation.
+        */
+       public function updateFirstMessage() {
+               $sql = "SELECT          messageID
+                       FROM            wcf".WCF_N."_conversation_message
+                       WHERE           conversationID = ?
+                       ORDER BY        time ASC";
+               $statement = WCF::getDB()->prepareStatement($sql, 1);
+               $statement->execute(array(
+                       $this->conversationID
+               ));
+               
+               $this->update(array(
+                       'firstMessageID' => $statement->fetchColumn()
+               ));
+       }
+       
+       /**
+        * Updates the last message of this conversation.
+        */
+       public function updateLastMessage() {
+               $sql = "SELECT          time, userID, username
+                       FROM            wcf".WCF_N."_conversation_message
+                       WHERE           conversationID = ?
+                       ORDER BY        time DESC";
+               $statement = WCF::getDB()->prepareStatement($sql, 1);
+               $statement->execute(array(
+                       $this->conversationID
+               ));
+               $row = $statement->fetchArray();
+               
+               $this->update(array(
+                       'lastPostTime' => $row['time'],
+                       'lastPosterID' => $row['userID'],
+                       'lastPoster' => $row['username']
+               ));
+       }
+       
        /**
         * Updates the participant summary of the given conversations.
         * 
index af86fe6db38c2adefda27f77ff0fdba787e93701..bd40b161ef96a52b1b85652a88035b7963bfe4f0 100644 (file)
@@ -1,6 +1,7 @@
 <?php
 namespace wcf\data\conversation\message;
 use wcf\data\conversation\Conversation;
+use wcf\data\conversation\ConversationAction;
 use wcf\data\conversation\ConversationEditor;
 use wcf\data\smiley\SmileyCache;
 use wcf\data\AbstractDatabaseObjectAction;
@@ -167,18 +168,27 @@ class ConversationMessageAction extends AbstractDatabaseObjectAction implements
        public function delete() {
                $count = parent::delete();
                
-               $attachmentMessageIDs = array();
+               $attachmentMessageIDs = $conversationIDs = array();
                foreach ($this->objects as $message) {
+                       if (!in_array($message->conversationID, $conversationIDs)) {
+                               $conversationIDs[] = $message->conversationID;
+                       }
+                       
                        if ($message->attachments) {
                                $attachmentMessageIDs[] = $message->messageID;
-                               
                        }
                }
                
+               // rebuild conversations
+               if (!empty($conversationIDs)) {
+                       $conversationAction = new ConversationAction($conversationIDs, 'rebuild');
+                       $conversationAction->executeAction();
+               }
+               
                if (!empty($this->objectIDs)) {
                        // delete notifications
                        UserNotificationHandler::getInstance()->deleteNotifications('conversationMessage', 'com.woltlab.wcf.conversation.message.notification', array(), $this->objectIDs);
-               
+                       
                        // update search index
                        SearchIndexManager::getInstance()->delete('com.woltlab.wcf.conversation.message', $this->objectIDs);