);
}
+ /**
+ * 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.
*
}
}
+ /**
+ * 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.
*
<?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;
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);