From ba2cfa5a81b52522dd5c9262899b6d1eaaf4be68 Mon Sep 17 00:00:00 2001 From: Marcel Werk Date: Mon, 5 Feb 2024 16:19:22 +0100 Subject: [PATCH] Make use of the new API for the conversation feed (#192) See https://github.com/WoltLab/WCF/pull/5790 --- .php-cs-fixer.dist.php | 2 +- .../data/conversation/Conversation.class.php | 30 +-------- .../conversation/ConversationAction.class.php | 3 +- .../conversation/FeedConversation.class.php | 1 + .../FeedConversationList.class.php | 1 + .../UserConversationList.class.php | 12 +++- files/lib/page/ConversationFeedPage.class.php | 11 ++++ .../page/ConversationRssFeedPage.class.php | 66 +++++++++++++++++++ .../ConversationMessageRuntimeCache.class.php | 26 ++++++++ templates/conversationList.tpl | 4 +- 10 files changed, 122 insertions(+), 34 deletions(-) create mode 100644 files/lib/page/ConversationRssFeedPage.class.php create mode 100644 files/lib/system/cache/runtime/ConversationMessageRuntimeCache.class.php diff --git a/.php-cs-fixer.dist.php b/.php-cs-fixer.dist.php index 60d7da8..52d79e6 100644 --- a/.php-cs-fixer.dist.php +++ b/.php-cs-fixer.dist.php @@ -57,7 +57,7 @@ return (new PhpCsFixer\Config()) 'nullable_type_declaration_for_default_null_value' => true, 'static_lambda' => true, - 'fully_qualified_strict_types' => true, + 'fully_qualified_strict_types' => ['leading_backslash_in_global_namespace' => true], 'no_unused_imports' => true, 'ordered_imports' => true, diff --git a/files/lib/data/conversation/Conversation.class.php b/files/lib/data/conversation/Conversation.class.php index 1f59d14..abb8fb2 100644 --- a/files/lib/data/conversation/Conversation.class.php +++ b/files/lib/data/conversation/Conversation.class.php @@ -8,6 +8,7 @@ use wcf\data\IPopoverObject; use wcf\data\user\group\UserGroup; use wcf\data\user\ignore\UserIgnore; use wcf\data\user\UserProfile; +use wcf\system\cache\runtime\ConversationMessageRuntimeCache; use wcf\system\cache\runtime\UserProfileRuntimeCache; use wcf\system\conversation\ConversationHandler; use wcf\system\database\util\PreparedStatementConditionBuilder; @@ -76,12 +77,6 @@ class Conversation extends DatabaseObject implements IPopoverObject, IRouteContr */ protected $canAddUnrestricted; - /** - * first message object - * @var ConversationMessage - */ - protected $firstMessage; - /** * true if the current user is an active participant of this conversation * @var bool @@ -313,28 +308,9 @@ class Conversation extends DatabaseObject implements IPopoverObject, IRouteContr return $this->canAddUnrestricted; } - /** - * Returns the first message in this conversation. - * - * @return ConversationMessage - */ - public function getFirstMessage() - { - if ($this->firstMessage === null) { - $this->firstMessage = new ConversationMessage($this->firstMessageID); - } - - return $this->firstMessage; - } - - /** - * Sets the first message. - * - * @param ConversationMessage $message - */ - public function setFirstMessage(ConversationMessage $message) + public function getFirstMessage(): ?ConversationMessage { - $this->firstMessage = $message; + return ConversationMessageRuntimeCache::getInstance()->getObject($this->firstMessageID); } /** diff --git a/files/lib/data/conversation/ConversationAction.class.php b/files/lib/data/conversation/ConversationAction.class.php index 335ecbb..328fdca 100644 --- a/files/lib/data/conversation/ConversationAction.class.php +++ b/files/lib/data/conversation/ConversationAction.class.php @@ -134,7 +134,6 @@ class ConversationAction extends AbstractDatabaseObjectAction implements 'firstMessageID' => $resultValues['returnValues']->messageID, ]); - $conversation->setFirstMessage($resultValues['returnValues']); if (!$conversation->isDraft) { // fire notification event $notificationRecipients = \array_merge( @@ -144,7 +143,7 @@ class ConversationAction extends AbstractDatabaseObjectAction implements UserNotificationHandler::getInstance()->fireEvent( 'conversation', 'com.woltlab.wcf.conversation.notification', - new ConversationUserNotificationObject($conversation), + new ConversationUserNotificationObject(new Conversation($conversation->conversationID)), $notificationRecipients ); } diff --git a/files/lib/data/conversation/FeedConversation.class.php b/files/lib/data/conversation/FeedConversation.class.php index 3789a4e..fc7b097 100644 --- a/files/lib/data/conversation/FeedConversation.class.php +++ b/files/lib/data/conversation/FeedConversation.class.php @@ -15,6 +15,7 @@ use wcf\system\request\LinkHandler; * * @method Conversation getDecoratedObject() * @mixin Conversation + * @deprecated 6.1 */ class FeedConversation extends DatabaseObjectDecorator implements IFeedEntry { diff --git a/files/lib/data/conversation/FeedConversationList.class.php b/files/lib/data/conversation/FeedConversationList.class.php index 7575d6a..4d1c732 100644 --- a/files/lib/data/conversation/FeedConversationList.class.php +++ b/files/lib/data/conversation/FeedConversationList.class.php @@ -16,6 +16,7 @@ use wcf\system\WCF; * @method FeedConversation|null getSingleObject() * @method FeedConversation|null search($objectID) * @property FeedConversation[] $objects + * @deprecated 6.1 */ class FeedConversationList extends ConversationList { diff --git a/files/lib/data/conversation/UserConversationList.class.php b/files/lib/data/conversation/UserConversationList.class.php index 305250a..8a759ad 100644 --- a/files/lib/data/conversation/UserConversationList.class.php +++ b/files/lib/data/conversation/UserConversationList.class.php @@ -4,6 +4,7 @@ namespace wcf\data\conversation; use wcf\data\conversation\label\ConversationLabel; use wcf\data\conversation\label\ConversationLabelList; +use wcf\system\cache\runtime\ConversationMessageRuntimeCache; use wcf\system\cache\runtime\UserProfileRuntimeCache; use wcf\system\database\util\PreparedStatementConditionBuilder; use wcf\system\WCF; @@ -216,7 +217,7 @@ class UserConversationList extends ConversationList $labels = $this->loadLabelAssignments(); - $userIDs = []; + $userIDs = $messageIDs = []; foreach ($this->objects as $conversationID => $conversation) { if (isset($labels[$conversationID])) { foreach ($labels[$conversationID] as $label) { @@ -230,11 +231,18 @@ class UserConversationList extends ConversationList if ($conversation->lastPosterID) { $userIDs[] = $conversation->lastPosterID; } + + if ($conversation->firstMessageID) { + $messageIDs[] = $conversation->firstMessageID; + } } - if (!empty($userIDs)) { + if ($userIDs !== []) { UserProfileRuntimeCache::getInstance()->cacheObjectIDs($userIDs); } + if ($messageIDs !== []) { + ConversationMessageRuntimeCache::getInstance()->cacheObjectIDs($userIDs); + } } } diff --git a/files/lib/page/ConversationFeedPage.class.php b/files/lib/page/ConversationFeedPage.class.php index 3a0956a..ed57984 100644 --- a/files/lib/page/ConversationFeedPage.class.php +++ b/files/lib/page/ConversationFeedPage.class.php @@ -11,6 +11,7 @@ use wcf\system\WCF; * @author Alexander Ebert * @copyright 2001-2019 WoltLab GmbH * @license GNU Lesser General Public License + * @deprecated 6.1 use `ConversationRssFeedPage` instead */ class ConversationFeedPage extends AbstractFeedPage { @@ -19,6 +20,16 @@ class ConversationFeedPage extends AbstractFeedPage */ public $loginRequired = true; + /** + * @inheritDoc + */ + public function readParameters() + { + parent::readParameters(); + + $this->redirectToNewPage(ConversationRssFeedPage::class); + } + /** * @inheritDoc */ diff --git a/files/lib/page/ConversationRssFeedPage.class.php b/files/lib/page/ConversationRssFeedPage.class.php new file mode 100644 index 0000000..955ab98 --- /dev/null +++ b/files/lib/page/ConversationRssFeedPage.class.php @@ -0,0 +1,66 @@ + + */ +class ConversationRssFeedPage extends AbstractRssFeedPage +{ + /** + * @inheritDoc + */ + public $loginRequired = true; + + protected UserConversationList $conversations; + + #[\Override] + public function readData() + { + parent::readData(); + + $this->conversations = new UserConversationList(WCF::getUser()->userID); + $this->conversations->sqlLimit = 20; + $this->conversations->sqlOrderBy = 'conversation.lastPostTime DESC'; + $this->conversations->readObjects(); + } + + #[\Override] + protected function getRssFeed(): RssFeed + { + $feed = new RssFeed(); + $channel = $this->getDefaultChannel(); + $channel->title(WCF::getLanguage()->get('wcf.conversation.conversations')); + + if ($this->conversations->valid()) { + $channel->lastBuildDateFromTimestamp($this->conversations->current()->lastPostTime); + } + $feed->channel($channel); + + foreach ($this->conversations as $conversation) { + $item = new RssFeedItem(); + $item + ->title($conversation->getTitle()) + ->link($conversation->getLink()) + ->description($conversation->getFirstMessage()->getExcerpt()) + ->pubDateFromTimestamp($conversation->lastPostTime) + ->creator($conversation->lastPoster) + ->guid($conversation->getLink()) + ->contentEncoded($conversation->getFirstMessage()->getSimplifiedFormattedMessage()) + ->slashComments($conversation->replies); + + $channel->item($item); + } + + return $feed; + } +} diff --git a/files/lib/system/cache/runtime/ConversationMessageRuntimeCache.class.php b/files/lib/system/cache/runtime/ConversationMessageRuntimeCache.class.php new file mode 100644 index 0000000..82a1c36 --- /dev/null +++ b/files/lib/system/cache/runtime/ConversationMessageRuntimeCache.class.php @@ -0,0 +1,26 @@ + + * @since 6.1 + * + * @method ConversationMessage[] getCachedObjects() + * @method ConversationMessage getObject($objectID) + * @method ConversationMessage[] getObjects(array $objectIDs) + */ +class ConversationMessageRuntimeCache extends AbstractRuntimeCache +{ + /** + * @inheritDoc + */ + protected $listClassName = ConversationMessageList::class; +} diff --git a/templates/conversationList.tpl b/templates/conversationList.tpl index 3db3728..268c1eb 100644 --- a/templates/conversationList.tpl +++ b/templates/conversationList.tpl @@ -27,7 +27,7 @@ {/capture} {capture assign='headContent'} - + {/capture} {capture assign='sidebarRight'} @@ -143,7 +143,7 @@ {/capture} {capture assign='contentInteractionDropdownItems'} -
  • {lang}wcf.global.button.rss{/lang}
  • +
  • {lang}wcf.global.button.rss{/lang}
  • {/capture} {include file='header'} -- 2.20.1