Refactor logic in ConversationAction::markAsRead()
authorTim Düsterhus <duesterhus@woltlab.com>
Thu, 23 Sep 2021 08:49:52 +0000 (10:49 +0200)
committerTim Düsterhus <duesterhus@woltlab.com>
Thu, 23 Sep 2021 08:49:52 +0000 (10:49 +0200)
see WoltLab/com.woltlab.wbb#455

files/lib/data/conversation/ConversationAction.class.php

index 742491ef03d93e68f03b3854a6a54aad6d2b5b5e..0a0adc24b62906fa1f52fb968d9a7d8aa3b6e1cb 100644 (file)
@@ -304,63 +304,42 @@ class ConversationAction extends AbstractDatabaseObjectAction implements
 
         // mark notifications as confirmed
         if (!empty($conversationIDs)) {
-            // conversation start notification
-            $conditionBuilder = new PreparedStatementConditionBuilder();
-            $conditionBuilder->add('notification.eventID = ?', [
-                UserNotificationHandler::getInstance()
-                    ->getEvent('com.woltlab.wcf.conversation.notification', 'conversation')
-                    ->eventID,
-            ]);
-            $conditionBuilder->add('notification.objectID = conversation.conversationID');
-            $conditionBuilder->add('notification.userID = ?', [$this->parameters['userID']]);
-            $conditionBuilder->add('conversation.conversationID IN (?)', [$conversationIDs]);
-            $conditionBuilder->add('conversation.time <= ?', [$this->parameters['visitTime']]);
-
-            $sql = "SELECT  conversation.conversationID
-                    FROM    wcf" . WCF_N . "_conversation conversation,
-                            wcf" . WCF_N . "_user_notification notification
-                    " . $conditionBuilder;
-            $statement = WCF::getDB()->prepareStatement($sql);
-            $statement->execute($conditionBuilder->getParameters());
-            $notificationObjectIDs = $statement->fetchAll(\PDO::FETCH_COLUMN);
-
-            if (!empty($notificationObjectIDs)) {
-                UserNotificationHandler::getInstance()->markAsConfirmed(
-                    'conversation',
-                    'com.woltlab.wcf.conversation.notification',
-                    [$this->parameters['userID']],
-                    $notificationObjectIDs
-                );
-            }
+            // 1) Mark notifications about new conversations as read.
+            UserNotificationHandler::getInstance()->markAsConfirmed(
+                'conversation',
+                'com.woltlab.wcf.conversation.notification',
+                [$this->parameters['userID']],
+                $conversationIDs
+            );
 
-            // conversation reply notification
-            $conditionBuilder = new PreparedStatementConditionBuilder();
-            $conditionBuilder->add('notification.eventID = ?', [
-                UserNotificationHandler::getInstance()
-                    ->getEvent('com.woltlab.wcf.conversation.message.notification', 'conversationMessage')
-                    ->eventID,
+            // 2) Mark notifications about new replies as read.
+            $eventID = UserNotificationHandler::getInstance()
+                ->getEvent('com.woltlab.wcf.conversation.message.notification', 'conversationMessage')
+                ->eventID;
+
+            $condition = new PreparedStatementConditionBuilder();
+            $condition->add('notification.userID = ?', [$this->parameters['userID']]);
+            $condition->add('notification.confirmTime = ?', [0]);
+            $condition->add('notification.eventID = ?', [$eventID]);
+            $condition->add("notification.objectID IN (
+                SELECT  messageID
+                FROM    wcf" . WCF_N . "_conversation_message
+                WHERE   conversationID IN (?)
+                    AND time <= ?
+            )", [
+                $conversationIDs,
+                $this->parameters['visitTime'],
             ]);
-            $conditionBuilder->add('notification.objectID = conversation_message.messageID');
-            $conditionBuilder->add('notification.userID = ?', [$this->parameters['userID']]);
-            $conditionBuilder->add('conversation_message.conversationID IN (?)', [$conversationIDs]);
-            $conditionBuilder->add('conversation_message.time <= ?', [$this->parameters['visitTime']]);
-
-            $sql = "SELECT  conversation_message.messageID
-                    FROM    wcf" . WCF_N . "_conversation_message conversation_message,
-                            wcf" . WCF_N . "_user_notification notification
-                    " . $conditionBuilder;
+
+            $sql = "SELECT  notificationID
+                    FROM    wcf" . WCF_N . "_user_notification notification
+                    {$condition}";
             $statement = WCF::getDB()->prepareStatement($sql);
-            $statement->execute($conditionBuilder->getParameters());
-            $notificationObjectIDs = $statement->fetchAll(\PDO::FETCH_COLUMN);
-
-            if (!empty($notificationObjectIDs)) {
-                UserNotificationHandler::getInstance()->markAsConfirmed(
-                    'conversationMessage',
-                    'com.woltlab.wcf.conversation.message.notification',
-                    [$this->parameters['userID']],
-                    $notificationObjectIDs
-                );
-            }
+            $statement->execute($condition->getParameters());
+
+            UserNotificationHandler::getInstance()->markAsConfirmedByIDs(
+                $statement->fetchAll(\PDO::FETCH_COLUMN)
+            );
         }
 
         if (!empty($conversationIDs)) {