From f81f8325d46180586d28bedb5e8c2cea85da666e Mon Sep 17 00:00:00 2001 From: Matthias Schmidt Date: Sun, 10 May 2020 16:28:33 +0200 Subject: [PATCH] Add notifications for article reactions Close #3269 --- com.woltlab.wcf/objectType.xml | 6 + com.woltlab.wcf/userNotificationEvent.xml | 7 + .../data/article/LikeableArticle.class.php | 22 ++- ...ArticleLikeUserNotificationEvent.class.php | 138 ++++++++++++++++++ wcfsetup/install/lang/de.xml | 5 + wcfsetup/install/lang/en.xml | 5 + 6 files changed, 182 insertions(+), 1 deletion(-) create mode 100644 wcfsetup/install/files/lib/system/user/notification/event/ArticleLikeUserNotificationEvent.class.php diff --git a/com.woltlab.wcf/objectType.xml b/com.woltlab.wcf/objectType.xml index ff97471cf8..98963f777e 100644 --- a/com.woltlab.wcf/objectType.xml +++ b/com.woltlab.wcf/objectType.xml @@ -114,6 +114,12 @@ wcf\system\user\notification\object\type\ArticleUserNotificationObjectType com.woltlab.wcf.page + + com.woltlab.wcf.likeableArticle.notification + com.woltlab.wcf.notification.objectType + wcf\system\user\notification\object\type\LikeUserNotificationObjectType + com.woltlab.wcf.page + com.woltlab.wcf.article.recentActivityEvent com.woltlab.wcf.user.recentActivityEvent diff --git a/com.woltlab.wcf/userNotificationEvent.xml b/com.woltlab.wcf/userNotificationEvent.xml index 1af648de6d..3a6d8bb871 100644 --- a/com.woltlab.wcf/userNotificationEvent.xml +++ b/com.woltlab.wcf/userNotificationEvent.xml @@ -114,6 +114,13 @@ wcf\system\user\notification\event\ArticleUserNotificationEvent 1 + + like + com.woltlab.wcf.likeableArticle.notification + wcf\system\user\notification\event\ArticleLikeUserNotificationEvent + module_like + 1 + registration diff --git a/wcfsetup/install/files/lib/data/article/LikeableArticle.class.php b/wcfsetup/install/files/lib/data/article/LikeableArticle.class.php index 1661cc337b..5b6f0ea149 100644 --- a/wcfsetup/install/files/lib/data/article/LikeableArticle.class.php +++ b/wcfsetup/install/files/lib/data/article/LikeableArticle.class.php @@ -1,6 +1,11 @@ getDecoratedObject()->userID != WCF::getUser()->userID) { + $notificationObject = new LikeUserNotificationObject($like); + UserNotificationHandler::getInstance()->fireEvent( + 'like', + 'com.woltlab.wcf.likeableArticle.notification', + $notificationObject, + [$this->getDecoratedObject()->userID] + ); + } + } } diff --git a/wcfsetup/install/files/lib/system/user/notification/event/ArticleLikeUserNotificationEvent.class.php b/wcfsetup/install/files/lib/system/user/notification/event/ArticleLikeUserNotificationEvent.class.php new file mode 100644 index 0000000000..85644c1d7c --- /dev/null +++ b/wcfsetup/install/files/lib/system/user/notification/event/ArticleLikeUserNotificationEvent.class.php @@ -0,0 +1,138 @@ + + * @package WoltLabSuite\Core\System\User\Notification\Event + * @since 5.3 + * + * @method LikeUserNotificationObject getUserNotificationObject() + */ +class ArticleLikeUserNotificationEvent extends AbstractSharedUserNotificationEvent implements ITestableUserNotificationEvent { + use TTestableLikeUserNotificationEvent { + TTestableLikeUserNotificationEvent::canBeTriggeredByGuests insteadof TTestableUserNotificationEvent; + } + use TTestableArticleUserNotificationEvent; + use TTestableCategorizedUserNotificationEvent; + use TTestableUserNotificationEvent; + use TReactionUserNotificationEvent; + + /** + * @inheritDoc + */ + protected $stackable = true; + + /** + * @inheritDoc + */ + protected function prepare() { + ViewableArticleRuntimeCache::getInstance()->cacheObjectID($this->getUserNotificationObject()->objectID); + } + + /** + * @inheritDoc + */ + public function getTitle() { + $count = count($this->getAuthors()); + if ($count > 1) { + return $this->getLanguage()->getDynamicVariable('wcf.article.like.notification.title.stacked', [ + 'count' => $count, + 'timesTriggered' => $this->notification->timesTriggered + ]); + } + + return $this->getLanguage()->getDynamicVariable('wcf.article.like.notification.title'); + } + + /** + * @inheritDoc + */ + public function getMessage() { + $article = ViewableArticleRuntimeCache::getInstance()->getObject($this->getUserNotificationObject()->objectID); + $authors = array_values($this->getAuthors()); + $count = count($authors); + + if ($count > 1) { + return $this->getLanguage()->getDynamicVariable('wcf.article.like.notification.message.stacked', [ + 'author' => $this->author, + 'authors' => $authors, + 'count' => $count, + 'others' => $count - 1, + 'article' => $article, + 'reactions' => $this->getReactionsForAuthors(), + ]); + } + + return $this->getLanguage()->getDynamicVariable('wcf.article.like.notification.message', [ + 'author' => $this->author, + 'article' => $article, + 'userNotificationObject' => $this->getUserNotificationObject(), + 'reactions' => $this->getReactionsForAuthors(), + ]); + } + + /** @noinspection PhpMissingParentCallCommonInspection */ + /** + * @inheritDoc + */ + public function getEmailMessage($notificationType = 'instant') { + // not supported + } + + /** + * @inheritDoc + */ + public function getLink() { + return ViewableArticleRuntimeCache::getInstance()->getObject($this->getUserNotificationObject()->objectID)->getLink(); + } + + /** @noinspection PhpMissingParentCallCommonInspection */ + /** + * @inheritDoc + */ + public function supportsEmailNotification() { + return false; + } + + /** @noinspection PhpMissingParentCallCommonInspection */ + /** + * @inheritDoc + */ + public function checkAccess() { + if (!ViewableArticleRuntimeCache::getInstance()->getObject($this->getUserNotificationObject()->objectID)->canRead()) { + UserStorageHandler::getInstance()->reset([WCF::getUser()->userID], 'wbbUnreadWatchedThreads'); + UserStorageHandler::getInstance()->reset([WCF::getUser()->userID], 'unreadArticles'); + UserStorageHandler::getInstance()->reset([WCF::getUser()->userID], 'unreadWatchedArticles'); + UserStorageHandler::getInstance()->reset([WCF::getUser()->userID], 'unreadArticlesByCategory'); + + return false; + } + + return true; + } + + /** + * @inheritDoc + */ + protected static function createTestLikeObject(UserProfile $recipient, UserProfile $author) { + return new LikeableArticle(self::getTestArticle(self::createTestCategory(ArticleCategory::OBJECT_TYPE_NAME), $author)); + } + + /** + * @inheritDoc + */ + protected static function getTestLikeableObjectTypeName() { + return 'com.woltlab.wcf.likeableArticle'; + } +} diff --git a/wcfsetup/install/lang/de.xml b/wcfsetup/install/lang/de.xml index 04fc630d73..587080849a 100644 --- a/wcfsetup/install/lang/de.xml +++ b/wcfsetup/install/lang/de.xml @@ -3251,6 +3251,10 @@ Benutzerkontos nun in vollem Umfang nutzen.]]> + + + getAnchorTag()} hat auf {if LANGUAGE_USE_INFORMAL_VARIANT}deinen{else}Ihren{/if} Artikel {$article->getTitle()} reagiert ({implode from=$reactions key=reactionID item=count}{@$__wcf->getReactionHandler()->getReactionTypeByID($reactionID)->renderIcon()}×{#$count}{/implode}).]]> + getAnchorTag()}{if $count == 2} und {else}, {/if}{@$authors[1]->getAnchorTag()}{if $count == 3} und {@$authors[2]->getAnchorTag()}{/if}{else}{@$authors[0]->getAnchorTag()} und {#$others} weitere{/if} haben auf {if LANGUAGE_USE_INFORMAL_VARIANT}deinen{else}Ihren{/if} Artikel {$article->getTitle()} reagiert ({implode from=$reactions key=reactionID item=count}{@$__wcf->getReactionHandler()->getReactionTypeByID($reactionID)->renderIcon()}×{#$count}{/implode}).]]> filesize|filesize}, {#$attachment->downloads} Mal heruntergeladen{if $attachment->downloads > 0}, zuletzt: {@$attachment->lastDownloadTime|time}{/if})]]> @@ -5089,6 +5093,7 @@ Benachrichtigungen auf {PAGE_TITLE|lang + userID}Ein Gast{else}{@$author->getAnchorTag()}{/if} hat einen Kommentar auf der Seite {$page->getTitle()} verfasst.]]> diff --git a/wcfsetup/install/lang/en.xml b/wcfsetup/install/lang/en.xml index 7a869322d5..4fa21cdece 100644 --- a/wcfsetup/install/lang/en.xml +++ b/wcfsetup/install/lang/en.xml @@ -3174,6 +3174,10 @@ full extend.]]> + + + getAnchorTag()} reacted to your article {$article->getTitle()} ({implode from=$reactions key=reactionID item=count}{@$__wcf->getReactionHandler()->getReactionTypeByID($reactionID)->renderIcon()}×{#$count}{/implode}).]]> + getAnchorTag()}{if $count == 2} and {else}, {/if}{@$authors[1]->getAnchorTag()}{if $count == 3} and {@$authors[2]->getAnchorTag()}{/if}{else}{@$authors[0]->getAnchorTag()} and {#$others} other users{/if} reacted to article {$article->getTitle()} ({implode from=$reactions key=reactionID item=count}{@$__wcf->getReactionHandler()->getReactionTypeByID($reactionID)->renderIcon()}×{#$count}{/implode}).]]> filesize|filesize}, downloaded {#$attachment->downloads} times{if $attachment->downloads > 0}, last: {@$attachment->lastDownloadTime|time}{/if})]]> @@ -5086,6 +5090,7 @@ your notifications on {PAGE_TITLE|langu + userID}A guest{else}{@$author->getAnchorTag()}{/if} commented on the page {$page->getTitle()}.]]> -- 2.20.1