Fix opening draft conversations
[GitHub/WoltLab/com.woltlab.wcf.conversation.git] / files / lib / page / ConversationPage.class.php
index 62d0ae8adee8b35f4abb3fb443a8813811e5903f..fe520b14eeb17b1a1aa988972d7709994e786e88 100644 (file)
@@ -70,7 +70,7 @@ class ConversationPage extends MultipleLinkPage
 
     /**
      * conversation id
-     * @var integer
+     * @var int
      */
     public $conversationID = 0;
 
@@ -88,7 +88,7 @@ class ConversationPage extends MultipleLinkPage
 
     /**
      * message id
-     * @var integer
+     * @var int
      */
     public $messageID = 0;
 
@@ -250,11 +250,12 @@ class ConversationPage extends MultipleLinkPage
                 $userIDs[] = $message->userID;
             }
         }
+        $userIDs = \array_unique($userIDs);
 
         // fetch special trophies
         if (MODULE_TROPHY) {
             if (!empty($userIDs)) {
-                UserProfile::prepareSpecialTrophies(\array_unique($userIDs));
+                UserProfile::prepareSpecialTrophies($userIDs);
             }
         }
 
@@ -281,11 +282,11 @@ class ConversationPage extends MultipleLinkPage
         if ($count > 1) {
             $this->objectList->seek($count - 1);
             if ($this->objectList->current()->time < $this->conversation->lastPostTime) {
-                $sql = "SELECT          time
-                                       FROM            wcf" . WCF_N . "_conversation_message
-                                       WHERE           conversationID = ?
-                                                       AND time > ?
-                                       ORDER BY        time";
+                $sql = "SELECT      time
+                        FROM        wcf" . WCF_N . "_conversation_message
+                        WHERE       conversationID = ?
+                                AND time > ?
+                        ORDER BY    time";
                 $statement = WCF::getDB()->prepareStatement($sql, 1);
                 $statement->execute([$this->conversationID, $this->objectList->current()->time]);
                 $endTime = $statement->fetchSingleColumn() - 1;
@@ -293,27 +294,28 @@ class ConversationPage extends MultipleLinkPage
         }
         $this->objectList->rewind();
 
-        // get invisible participants
-        $invisibleParticipantIDs = [];
-        if (WCF::getUser()->userID != $this->conversation->userID) {
-            foreach ($this->participantList as $participant) {
-                if ($participant->isInvisible) {
-                    $invisibleParticipantIDs[] = $participant->userID;
-                }
+        // get visible participants
+        $visibleParticipantIDs = [];
+        foreach ($this->participantList as $participant) {
+            /** @noinspection PhpUndefinedFieldInspection */
+            if (!$participant->isInvisible || WCF::getUser()->userID == $this->conversation->userID) {
+                $visibleParticipantIDs[] = $participant->userID;
             }
         }
 
+        // Drafts do not store their participants in conversation_to_user.
+        if ($this->conversation->isDraft) {
+            $visibleParticipantIDs[] = $this->conversation->userID;
+        }
+
         // load modification log entries
         $this->modificationLogList = new ConversationLogModificationLogList($this->conversation->conversationID);
         $this->modificationLogList->getConditionBuilder()
             ->add("modification_log.time BETWEEN ? AND ?", [$startTime, $endTime]);
-
-        if (!empty($invisibleParticipantIDs)) {
-            $this->modificationLogList->getConditionBuilder()->add(
-                "(modification_log.action <> ? OR modification_log.userID NOT IN (?))",
-                ['leave', $invisibleParticipantIDs]
-            );
-        }
+        $this->modificationLogList->getConditionBuilder()->add(
+            "modification_log.userID IN (?)",
+            [$visibleParticipantIDs]
+        );
 
         $this->modificationLogList->readObjects();
     }
@@ -360,9 +362,9 @@ class ConversationPage extends MultipleLinkPage
         $conditionBuilder = clone $this->objectList->getConditionBuilder();
         $conditionBuilder->add('time ' . ($this->sortOrder == 'ASC' ? '<=' : '>=') . ' ?', [$this->message->time]);
 
-        $sql = "SELECT COUNT(*) AS messages
-                       FROM    wcf" . WCF_N . "_conversation_message conversation_message
-                       " . $conditionBuilder;
+        $sql = "SELECT  COUNT(*) AS messages
+                FROM    wcf" . WCF_N . "_conversation_message conversation_message
+                " . $conditionBuilder;
         $statement = WCF::getDB()->prepareStatement($sql);
         $statement->execute($conditionBuilder->getParameters());
         $row = $statement->fetchArray();
@@ -374,10 +376,10 @@ class ConversationPage extends MultipleLinkPage
      */
     protected function goToLastPost()
     {
-        $sql = "SELECT         conversation_message.messageID
-                       FROM            wcf" . WCF_N . "_conversation_message conversation_message
-                       " . $this->objectList->getConditionBuilder() . "
-                       ORDER BY        time " . ($this->sortOrder == 'ASC' ? 'DESC' : 'ASC');
+        $sql = "SELECT      conversation_message.messageID
+                FROM        wcf" . WCF_N . "_conversation_message conversation_message
+                " . $this->objectList->getConditionBuilder() . "
+                ORDER BY    time " . ($this->sortOrder == 'ASC' ? 'DESC' : 'ASC');
         $statement = WCF::getDB()->prepareStatement($sql, 1);
         $statement->execute($this->objectList->getConditionBuilder()->getParameters());
         $row = $statement->fetchArray();
@@ -403,10 +405,10 @@ class ConversationPage extends MultipleLinkPage
         $conditionBuilder = clone $this->objectList->getConditionBuilder();
         $conditionBuilder->add('time > ?', [$this->conversation->lastVisitTime]);
 
-        $sql = "SELECT         conversation_message.messageID
-                       FROM            wcf" . WCF_N . "_conversation_message conversation_message
-                       " . $conditionBuilder . "
-                       ORDER BY        time ASC";
+        $sql = "SELECT      conversation_message.messageID
+                FROM        wcf" . WCF_N . "_conversation_message conversation_message
+                " . $conditionBuilder . "
+                ORDER BY    time ASC";
         $statement = WCF::getDB()->prepareStatement($sql, 1);
         $statement->execute($conditionBuilder->getParameters());
         $row = $statement->fetchArray();