From 536ce232a7674d55045ddd334a1ef9a84d8f2dd5 Mon Sep 17 00:00:00 2001 From: Michael Date: Wed, 18 May 2022 20:05:29 +0000 Subject: [PATCH 1/3] Cache notifications --- .../Notifications/Factory/Notification.php | 28 +++++++++++++++---- .../Notifications/Repository/Notify.php | 19 ++++++------- 2 files changed, 31 insertions(+), 16 deletions(-) diff --git a/src/Navigation/Notifications/Factory/Notification.php b/src/Navigation/Notifications/Factory/Notification.php index ea74357c03..84db494e53 100644 --- a/src/Navigation/Notifications/Factory/Notification.php +++ b/src/Navigation/Notifications/Factory/Notification.php @@ -25,8 +25,13 @@ use Friendica\App\BaseURL; use Friendica\BaseFactory; use Friendica\Capabilities\ICanCreateFromTableRow; use Friendica\Contact\LocalRelationship\Repository\LocalRelationship; +use Friendica\Content\Text\BBCode; use Friendica\Content\Text\Plaintext; +use Friendica\Core\Cache\Enum\Duration; +use Friendica\Core\Cache\Capability\ICanCache; use Friendica\Core\L10n; +use Friendica\Core\System; +use Friendica\DI; use Friendica\Model\Contact; use Friendica\Model\Post; use Friendica\Model\Verb; @@ -43,6 +48,8 @@ class Notification extends BaseFactory implements ICanCreateFromTableRow private $l10n; /** @var LocalRelationship */ private $localRelationshipRepo; + /** @var ICanCache */ + private $cache; public function __construct(\Friendica\App\BaseURL $baseUrl, \Friendica\Core\L10n $l10n, \Friendica\Contact\LocalRelationship\Repository\LocalRelationship $localRelationshipRepo, LoggerInterface $logger) { @@ -51,6 +58,7 @@ class Notification extends BaseFactory implements ICanCreateFromTableRow $this->baseUrl = $baseUrl; $this->l10n = $l10n; $this->localRelationshipRepo = $localRelationshipRepo; + $this->cache = DI::cache(); // @todo Add the correct mechanism for class construction here } public function createFromTableRow(array $row): Entity\Notification @@ -107,6 +115,13 @@ class Notification extends BaseFactory implements ICanCreateFromTableRow { $message = []; + $cachekey = 'Notification:' . $Notification->id; + $result = $this->cache->get($cachekey); + if (!is_null($result)) { + $this->logger->debug('Blubb-Cache', ['id' => $Notification->id, 'callstack' => System::callstack(20)]); + return $result; + } + $causer = $author = Contact::getById($Notification->actorId, ['id', 'name', 'url', 'contact-type', 'pending']); if (empty($causer)) { $this->logger->info('Causer not found', ['contact' => $Notification->actorId]); @@ -132,7 +147,7 @@ class Notification extends BaseFactory implements ICanCreateFromTableRow $this->logger->info('Thread is ignored', ['parent-uri-id' => $Notification->parentUriId, 'type' => $Notification->type]); return $message; } - + if (in_array($Notification->type, [Post\UserNotification::TYPE_THREAD_COMMENT, Post\UserNotification::TYPE_COMMENT_PARTICIPATION, Post\UserNotification::TYPE_ACTIVITY_PARTICIPATION, Post\UserNotification::TYPE_EXPLICIT_TAGGED])) { $item = Post::selectFirst([], ['uri-id' => $Notification->parentUriId, 'uid' => [0, $Notification->uid]], ['order' => ['uid' => true]]); if (empty($item)) { @@ -170,11 +185,10 @@ class Notification extends BaseFactory implements ICanCreateFromTableRow $link = $this->baseUrl . '/display/' . urlencode($link_item['guid']); - $content = Plaintext::getPost($item, 70); - if (!empty($content['text'])) { - $title = '"' . trim(str_replace("\n", " ", $content['text'])) . '"'; - } else { - $title = ''; + $body = BBCode::toPlaintext($item['body'], false); + $title = Plaintext::shorten($body, 70); + if (!empty($title)) { + $title = '"' . trim(str_replace("\n", " ", $title)) . '"'; } $this->logger->debug('Got verb and type', ['verb' => $Notification->verb, 'type' => $Notification->type, 'causer' => $causer['id'], 'author' => $author['id'], 'item' => $item['id'], 'uid' => $Notification->uid]); @@ -306,6 +320,8 @@ class Notification extends BaseFactory implements ICanCreateFromTableRow '[url=' . $link . ']' . $title . '[/url]', '[url=' . $author['url'] . ']' . $author['name'] . '[/url]'); $message['link'] = $link; + $this->logger->debug('Blubb-Set', ['id' => $Notification->id, 'callstack' => System::callstack(20)]); + $this->cache->set($cachekey, $message, Duration::HOUR); } else { $this->logger->debug('Unhandled notification', ['notification' => $Notification]); } diff --git a/src/Navigation/Notifications/Repository/Notify.php b/src/Navigation/Notifications/Repository/Notify.php index 9773b6446a..c114375be7 100644 --- a/src/Navigation/Notifications/Repository/Notify.php +++ b/src/Navigation/Notifications/Repository/Notify.php @@ -23,6 +23,7 @@ namespace Friendica\Navigation\Notifications\Repository; use Friendica\App\BaseURL; use Friendica\BaseRepository; +use Friendica\Content\Text\BBCode; use Friendica\Content\Text\Plaintext; use Friendica\Core\Config\Capability\IManageConfigValues; use Friendica\Core\Hook; @@ -301,11 +302,10 @@ class Notify extends BaseRepository $item_post_type = Model\Item::postType($item, $l10n); - $content = Plaintext::getPost($item, 70); - if (!empty($content['text'])) { - $title = '"' . trim(str_replace("\n", " ", $content['text'])) . '"'; - } else { - $title = ''; + $body = BBCode::toPlaintext($item['body'], false); + $title = Plaintext::shorten($body, 70); + if (!empty($title)) { + $title = '"' . trim(str_replace("\n", " ", $title)) . '"'; } // First go for the general message @@ -710,11 +710,10 @@ class Notify extends BaseRepository return false; } - $content = Plaintext::getPost($item, 70); - if (!empty($content['text'])) { - $title = '"' . trim(str_replace("\n", " ", $content['text'])) . '"'; - } else { - $title = $item['title']; + $body = BBCode::toPlaintext($item['body'], false); + $title = Plaintext::shorten($body, 70); + if (!empty($title)) { + $title = '"' . trim(str_replace("\n", " ", $title)) . '"'; } // Some mail software relies on subject field for threading. From 5aa798b1dcde3806af9c89d0b6465c760707e78c Mon Sep 17 00:00:00 2001 From: Michael Date: Wed, 18 May 2022 20:10:14 +0000 Subject: [PATCH 2/3] Remove test logging --- src/Navigation/Notifications/Factory/Notification.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/Navigation/Notifications/Factory/Notification.php b/src/Navigation/Notifications/Factory/Notification.php index 84db494e53..a41ba24ce1 100644 --- a/src/Navigation/Notifications/Factory/Notification.php +++ b/src/Navigation/Notifications/Factory/Notification.php @@ -118,7 +118,6 @@ class Notification extends BaseFactory implements ICanCreateFromTableRow $cachekey = 'Notification:' . $Notification->id; $result = $this->cache->get($cachekey); if (!is_null($result)) { - $this->logger->debug('Blubb-Cache', ['id' => $Notification->id, 'callstack' => System::callstack(20)]); return $result; } @@ -320,7 +319,6 @@ class Notification extends BaseFactory implements ICanCreateFromTableRow '[url=' . $link . ']' . $title . '[/url]', '[url=' . $author['url'] . ']' . $author['name'] . '[/url]'); $message['link'] = $link; - $this->logger->debug('Blubb-Set', ['id' => $Notification->id, 'callstack' => System::callstack(20)]); $this->cache->set($cachekey, $message, Duration::HOUR); } else { $this->logger->debug('Unhandled notification', ['notification' => $Notification]); From 49c47008d189617ec989b04c8ec42b3c1484e87f Mon Sep 17 00:00:00 2001 From: Michael Date: Thu, 19 May 2022 09:08:04 +0000 Subject: [PATCH 3/3] Convert class calls into DI calls --- src/Model/Post/UserNotification.php | 4 ++-- src/Navigation/Notifications/Factory/Notification.php | 6 ++---- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/src/Model/Post/UserNotification.php b/src/Model/Post/UserNotification.php index f4e700f0bc..d16ec4936f 100644 --- a/src/Model/Post/UserNotification.php +++ b/src/Model/Post/UserNotification.php @@ -308,7 +308,7 @@ class UserNotification return; } - $notification = (new Notifications\Factory\Notification(DI::baseUrl(), DI::l10n(), DI::localRelationship(), DI::logger()))->createForUser( + $notification = DI::notificationFactory()->createForUser( $uid, $item['vid'], $type, @@ -336,7 +336,7 @@ class UserNotification */ public static function insertNotification(int $actor, string $verb, int $uid): bool { - $notification = (new Notifications\Factory\Notification(DI::baseUrl(), DI::l10n(), DI::localRelationship(), DI::logger()))->createForRelationship( + $notification = DI::notificationFactory()->createForRelationship( $uid, $actor, $verb diff --git a/src/Navigation/Notifications/Factory/Notification.php b/src/Navigation/Notifications/Factory/Notification.php index a41ba24ce1..b2132beaff 100644 --- a/src/Navigation/Notifications/Factory/Notification.php +++ b/src/Navigation/Notifications/Factory/Notification.php @@ -30,8 +30,6 @@ use Friendica\Content\Text\Plaintext; use Friendica\Core\Cache\Enum\Duration; use Friendica\Core\Cache\Capability\ICanCache; use Friendica\Core\L10n; -use Friendica\Core\System; -use Friendica\DI; use Friendica\Model\Contact; use Friendica\Model\Post; use Friendica\Model\Verb; @@ -51,14 +49,14 @@ class Notification extends BaseFactory implements ICanCreateFromTableRow /** @var ICanCache */ private $cache; - public function __construct(\Friendica\App\BaseURL $baseUrl, \Friendica\Core\L10n $l10n, \Friendica\Contact\LocalRelationship\Repository\LocalRelationship $localRelationshipRepo, LoggerInterface $logger) + public function __construct(\Friendica\App\BaseURL $baseUrl, \Friendica\Core\L10n $l10n, \Friendica\Contact\LocalRelationship\Repository\LocalRelationship $localRelationshipRepo, LoggerInterface $logger, ICanCache $cache) { parent::__construct($logger); $this->baseUrl = $baseUrl; $this->l10n = $l10n; $this->localRelationshipRepo = $localRelationshipRepo; - $this->cache = DI::cache(); // @todo Add the correct mechanism for class construction here + $this->cache = $cache; } public function createFromTableRow(array $row): Entity\Notification