Merge pull request #11533 from annando/notification-cache
Cache notifications
This commit is contained in:
commit
87e14d9d28
3 changed files with 30 additions and 19 deletions
|
@ -308,7 +308,7 @@ class UserNotification
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
$notification = (new Notifications\Factory\Notification(DI::baseUrl(), DI::l10n(), DI::localRelationship(), DI::logger()))->createForUser(
|
$notification = DI::notificationFactory()->createForUser(
|
||||||
$uid,
|
$uid,
|
||||||
$item['vid'],
|
$item['vid'],
|
||||||
$type,
|
$type,
|
||||||
|
@ -336,7 +336,7 @@ class UserNotification
|
||||||
*/
|
*/
|
||||||
public static function insertNotification(int $actor, string $verb, int $uid): bool
|
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,
|
$uid,
|
||||||
$actor,
|
$actor,
|
||||||
$verb
|
$verb
|
||||||
|
|
|
@ -25,7 +25,10 @@ use Friendica\App\BaseURL;
|
||||||
use Friendica\BaseFactory;
|
use Friendica\BaseFactory;
|
||||||
use Friendica\Capabilities\ICanCreateFromTableRow;
|
use Friendica\Capabilities\ICanCreateFromTableRow;
|
||||||
use Friendica\Contact\LocalRelationship\Repository\LocalRelationship;
|
use Friendica\Contact\LocalRelationship\Repository\LocalRelationship;
|
||||||
|
use Friendica\Content\Text\BBCode;
|
||||||
use Friendica\Content\Text\Plaintext;
|
use Friendica\Content\Text\Plaintext;
|
||||||
|
use Friendica\Core\Cache\Enum\Duration;
|
||||||
|
use Friendica\Core\Cache\Capability\ICanCache;
|
||||||
use Friendica\Core\L10n;
|
use Friendica\Core\L10n;
|
||||||
use Friendica\Model\Contact;
|
use Friendica\Model\Contact;
|
||||||
use Friendica\Model\Post;
|
use Friendica\Model\Post;
|
||||||
|
@ -43,14 +46,17 @@ class Notification extends BaseFactory implements ICanCreateFromTableRow
|
||||||
private $l10n;
|
private $l10n;
|
||||||
/** @var LocalRelationship */
|
/** @var LocalRelationship */
|
||||||
private $localRelationshipRepo;
|
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)
|
public function __construct(\Friendica\App\BaseURL $baseUrl, \Friendica\Core\L10n $l10n, \Friendica\Contact\LocalRelationship\Repository\LocalRelationship $localRelationshipRepo, LoggerInterface $logger, ICanCache $cache)
|
||||||
{
|
{
|
||||||
parent::__construct($logger);
|
parent::__construct($logger);
|
||||||
|
|
||||||
$this->baseUrl = $baseUrl;
|
$this->baseUrl = $baseUrl;
|
||||||
$this->l10n = $l10n;
|
$this->l10n = $l10n;
|
||||||
$this->localRelationshipRepo = $localRelationshipRepo;
|
$this->localRelationshipRepo = $localRelationshipRepo;
|
||||||
|
$this->cache = $cache;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function createFromTableRow(array $row): Entity\Notification
|
public function createFromTableRow(array $row): Entity\Notification
|
||||||
|
@ -107,6 +113,12 @@ class Notification extends BaseFactory implements ICanCreateFromTableRow
|
||||||
{
|
{
|
||||||
$message = [];
|
$message = [];
|
||||||
|
|
||||||
|
$cachekey = 'Notification:' . $Notification->id;
|
||||||
|
$result = $this->cache->get($cachekey);
|
||||||
|
if (!is_null($result)) {
|
||||||
|
return $result;
|
||||||
|
}
|
||||||
|
|
||||||
$causer = $author = Contact::getById($Notification->actorId, ['id', 'name', 'url', 'contact-type', 'pending']);
|
$causer = $author = Contact::getById($Notification->actorId, ['id', 'name', 'url', 'contact-type', 'pending']);
|
||||||
if (empty($causer)) {
|
if (empty($causer)) {
|
||||||
$this->logger->info('Causer not found', ['contact' => $Notification->actorId]);
|
$this->logger->info('Causer not found', ['contact' => $Notification->actorId]);
|
||||||
|
@ -132,7 +144,7 @@ class Notification extends BaseFactory implements ICanCreateFromTableRow
|
||||||
$this->logger->info('Thread is ignored', ['parent-uri-id' => $Notification->parentUriId, 'type' => $Notification->type]);
|
$this->logger->info('Thread is ignored', ['parent-uri-id' => $Notification->parentUriId, 'type' => $Notification->type]);
|
||||||
return $message;
|
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])) {
|
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]]);
|
$item = Post::selectFirst([], ['uri-id' => $Notification->parentUriId, 'uid' => [0, $Notification->uid]], ['order' => ['uid' => true]]);
|
||||||
if (empty($item)) {
|
if (empty($item)) {
|
||||||
|
@ -170,11 +182,10 @@ class Notification extends BaseFactory implements ICanCreateFromTableRow
|
||||||
|
|
||||||
$link = $this->baseUrl . '/display/' . urlencode($link_item['guid']);
|
$link = $this->baseUrl . '/display/' . urlencode($link_item['guid']);
|
||||||
|
|
||||||
$content = Plaintext::getPost($item, 70);
|
$body = BBCode::toPlaintext($item['body'], false);
|
||||||
if (!empty($content['text'])) {
|
$title = Plaintext::shorten($body, 70);
|
||||||
$title = '"' . trim(str_replace("\n", " ", $content['text'])) . '"';
|
if (!empty($title)) {
|
||||||
} else {
|
$title = '"' . trim(str_replace("\n", " ", $title)) . '"';
|
||||||
$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]);
|
$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 +317,7 @@ class Notification extends BaseFactory implements ICanCreateFromTableRow
|
||||||
'[url=' . $link . ']' . $title . '[/url]',
|
'[url=' . $link . ']' . $title . '[/url]',
|
||||||
'[url=' . $author['url'] . ']' . $author['name'] . '[/url]');
|
'[url=' . $author['url'] . ']' . $author['name'] . '[/url]');
|
||||||
$message['link'] = $link;
|
$message['link'] = $link;
|
||||||
|
$this->cache->set($cachekey, $message, Duration::HOUR);
|
||||||
} else {
|
} else {
|
||||||
$this->logger->debug('Unhandled notification', ['notification' => $Notification]);
|
$this->logger->debug('Unhandled notification', ['notification' => $Notification]);
|
||||||
}
|
}
|
||||||
|
|
|
@ -23,6 +23,7 @@ namespace Friendica\Navigation\Notifications\Repository;
|
||||||
|
|
||||||
use Friendica\App\BaseURL;
|
use Friendica\App\BaseURL;
|
||||||
use Friendica\BaseRepository;
|
use Friendica\BaseRepository;
|
||||||
|
use Friendica\Content\Text\BBCode;
|
||||||
use Friendica\Content\Text\Plaintext;
|
use Friendica\Content\Text\Plaintext;
|
||||||
use Friendica\Core\Config\Capability\IManageConfigValues;
|
use Friendica\Core\Config\Capability\IManageConfigValues;
|
||||||
use Friendica\Core\Hook;
|
use Friendica\Core\Hook;
|
||||||
|
@ -301,11 +302,10 @@ class Notify extends BaseRepository
|
||||||
|
|
||||||
$item_post_type = Model\Item::postType($item, $l10n);
|
$item_post_type = Model\Item::postType($item, $l10n);
|
||||||
|
|
||||||
$content = Plaintext::getPost($item, 70);
|
$body = BBCode::toPlaintext($item['body'], false);
|
||||||
if (!empty($content['text'])) {
|
$title = Plaintext::shorten($body, 70);
|
||||||
$title = '"' . trim(str_replace("\n", " ", $content['text'])) . '"';
|
if (!empty($title)) {
|
||||||
} else {
|
$title = '"' . trim(str_replace("\n", " ", $title)) . '"';
|
||||||
$title = '';
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// First go for the general message
|
// First go for the general message
|
||||||
|
@ -710,11 +710,10 @@ class Notify extends BaseRepository
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
$content = Plaintext::getPost($item, 70);
|
$body = BBCode::toPlaintext($item['body'], false);
|
||||||
if (!empty($content['text'])) {
|
$title = Plaintext::shorten($body, 70);
|
||||||
$title = '"' . trim(str_replace("\n", " ", $content['text'])) . '"';
|
if (!empty($title)) {
|
||||||
} else {
|
$title = '"' . trim(str_replace("\n", " ", $title)) . '"';
|
||||||
$title = $item['title'];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Some mail software relies on subject field for threading.
|
// Some mail software relies on subject field for threading.
|
||||||
|
|
Loading…
Reference in a new issue