]> git.mxchange.org Git - friendica.git/blobdiff - src/Navigation/Notifications/Repository/Notify.php
Legacy "include" fragments have been removed
[friendica.git] / src / Navigation / Notifications / Repository / Notify.php
index 6d56258b37baa2b45e2888c88a986f70e19352df..5a6bf668967fcc8d10a81824e8e1d0bb9202d80f 100644 (file)
@@ -23,24 +23,31 @@ 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\PConfig\Capability\IManagePersonalConfigValues;
 use Friendica\Core\Hook;
 use Friendica\Core\L10n;
 use Friendica\Core\System;
 use Friendica\Database\Database;
 use Friendica\Database\DBA;
+use Friendica\Factory\Api\Mastodon\Notification as NotificationFactory;
 use Friendica\Model;
 use Friendica\Navigation\Notifications\Collection;
 use Friendica\Navigation\Notifications\Entity;
 use Friendica\Navigation\Notifications\Exception;
 use Friendica\Navigation\Notifications\Factory;
 use Friendica\Network\HTTPException;
+use Friendica\Object\Api\Mastodon\Notification;
 use Friendica\Protocol\Activity;
 use Friendica\Util\DateTimeFormat;
 use Friendica\Util\Emailer;
 use Psr\Log\LoggerInterface;
 
+/**
+ * @deprecated since 2022.05 Use \Friendica\Navigation\Notifications\Repository\Notification instead
+ */
 class Notify extends BaseRepository
 {
        /** @var Factory\Notify  */
@@ -55,6 +62,9 @@ class Notify extends BaseRepository
        /** @var IManageConfigValues */
        protected $config;
 
+       /** @var IManagePersonalConfigValues */
+       private $pConfig;
+
        /** @var Emailer */
        protected $emailer;
 
@@ -63,11 +73,12 @@ class Notify extends BaseRepository
 
        protected static $table_name = 'notify';
 
-       public function __construct(Database $database, LoggerInterface $logger, L10n $l10n, BaseURL $baseUrl, IManageConfigValues $config, Emailer $emailer, Factory\Notification $notification, Factory\Notify $factory = null)
+       public function __construct(Database $database, LoggerInterface $logger, L10n $l10n, BaseURL $baseUrl, IManageConfigValues $config, IManagePersonalConfigValues $pConfig, Emailer $emailer, Factory\Notification $notification, Factory\Notify $factory = null)
        {
                $this->l10n         = $l10n;
                $this->baseUrl      = $baseUrl;
                $this->config       = $config;
+               $this->pConfig      = $pConfig;
                $this->emailer      = $emailer;
                $this->notification = $notification;
 
@@ -77,6 +88,7 @@ class Notify extends BaseRepository
        /**
         * @param array $condition
         * @param array $params
+        *
         * @return Entity\Notify
         * @throws HTTPException\NotFoundException
         */
@@ -106,6 +118,7 @@ class Notify extends BaseRepository
 
        /**
         * @param int $id
+        *
         * @return Entity\Notify
         * @throws HTTPException\NotFoundException
         */
@@ -142,6 +155,7 @@ class Notify extends BaseRepository
 
        /**
         * @param Entity\Notify $Notify
+        *
         * @return Entity\Notify
         * @throws HTTPException\NotFoundException
         * @throws HTTPException\InternalServerErrorException
@@ -298,11 +312,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
@@ -348,25 +361,6 @@ class Notify extends BaseRepository
                                $itemlink =  $params['link'];
                                break;
 
-                       case Model\Notification\Type::POKE:
-                               $subject = $l10n->t('%1$s %2$s poked you', $subjectPrefix, $params['source_name']);
-
-                               $preamble = $l10n->t('%1$s poked you at %2$s', $params['source_name'], $sitename);
-                               $epreamble = $l10n->t('%1$s [url=%2$s]poked you[/url].',
-                                       '[url='.$params['source_link'].']'.$params['source_name'].'[/url]',
-                                       $params['link']
-                               );
-
-                               $subject = str_replace('poked', $l10n->t($params['activity']), $subject);
-                               $preamble = str_replace('poked', $l10n->t($params['activity']), $preamble);
-                               $epreamble = str_replace('poked', $l10n->t($params['activity']), $epreamble);
-
-                               $sitelink = $l10n->t('Please visit %s to view and/or reply to the conversation.');
-                               $tsitelink = sprintf($sitelink, $siteurl);
-                               $hsitelink = sprintf($sitelink, '<a href="'.$siteurl.'">'.$sitename.'</a>');
-                               $itemlink =  $params['link'];
-                               break;
-
                        case Model\Notification\Type::INTRO:
                                $itemlink = $params['link'];
                                $subject = $l10n->t('%s Introduction received', $subjectPrefix);
@@ -468,7 +462,7 @@ class Notify extends BaseRepository
 
                        case Model\Notification\Type::SYSTEM:
                                switch($params['event']) {
-                                       case "SYSTEM_REGISTER_REQUEST":
+                                       case 'SYSTEM_REGISTER_REQUEST':
                                                $itemlink =  $params['link'];
                                                $subject = $l10n->t('[Friendica System Notify]') . ' ' . $l10n->t('registration request');
 
@@ -486,9 +480,10 @@ class Notify extends BaseRepository
 
                                                $sitelink = $l10n->t('Please visit %s to approve or reject the request.');
                                                $tsitelink = sprintf($sitelink, $params['link']);
-                                               $hsitelink = sprintf($sitelink, '<a href="'.$params['link'].'">'.$sitename.'</a><br><br>');
+                                               $hsitelink = sprintf($sitelink, '<a href="' . $params['link'] . '">' . $sitename . '</a><br><br>');
                                                break;
-                                       case "SYSTEM_DB_UPDATE_FAIL":
+
+                                       case 'SYSTEM_DB_UPDATE_FAIL': // @TODO Unused (only here)
                                                break;
                                }
                                break;
@@ -501,7 +496,7 @@ class Notify extends BaseRepository
                return $this->storeAndSend($params, $sitelink, $tsitelink, $hsitelink, $title, $subject, $preamble, $epreamble, $body, $itemlink, $show_in_notification_page);
        }
 
-       private function storeAndSend($params, $sitelink, $tsitelink, $hsitelink, $title, $subject, $preamble, $epreamble, $body, $itemlink, $show_in_notification_page)
+       private function storeAndSend(array $params, string $sitelink, string $tsitelink, string $hsitelink, string $title, string $subject, string $preamble, string $epreamble, string $body, string $itemlink, bool $show_in_notification_page): bool
        {
                $item_id = $params['item']['id'] ?? 0;
                $uri_id = $params['item']['uri-id'] ?? null;
@@ -567,7 +562,7 @@ class Notify extends BaseRepository
                        $Notify->updateMsgFromPreamble($epreamble);
                        $Notify = $this->save($Notify);
 
-                       $itemlink  = $this->baseUrl->get() . '/notification/' . $Notify->id;
+                       $itemlink  = $this->baseUrl->get() . '/notify/' . $Notify->id;
                        $notify_id = $Notify->id;
                }
 
@@ -590,7 +585,7 @@ class Notify extends BaseRepository
                                        DBA::insert('notify-threads', $fields);
 
                                        $emailBuilder->setHeader('Message-ID', $message_id);
-                                       $log_msg = "include/enotify: No previous notification found for this parent:\n" .
+                                       $log_msg = "No previous notification found for this parent:\n" .
                                                "  parent: ${params['parent']}\n" . "  uid   : ${params['uid']}\n";
                                        $this->logger->info($log_msg);
                                } else {
@@ -648,7 +643,39 @@ class Notify extends BaseRepository
                return false;
        }
 
-       public function createFromNotification(Entity\Notification $Notification)
+       public function NotifyOnDesktop(Entity\Notification $Notification, string $type = null): bool
+       {
+               if (is_null($type)) {
+                       $type = NotificationFactory::getType($Notification);
+               }
+
+               if (in_array($Notification->type, [Model\Post\UserNotification::TYPE_FOLLOW, Model\Post\UserNotification::TYPE_SHARED])) {
+                       return true;
+               }
+
+               if ($this->pConfig->get($Notification->uid, 'system', 'notify_like') && ($type == Notification::TYPE_LIKE)) {
+                       return true;
+               }
+
+               if ($this->pConfig->get($Notification->uid, 'system', 'notify_announce') && ($type == Notification::TYPE_RESHARE)) {
+                       return true;
+               }
+
+               $notify_type = $this->pConfig->get($Notification->uid, 'system', 'notify_type');
+
+               // Fallback for the case when the notify type isn't set at all
+               if (is_null($notify_type) && !in_array($type, [Notification::TYPE_RESHARE, Notification::TYPE_LIKE])) {
+                               return true;
+               }
+
+               if (!is_null($notify_type) && ($notify_type & $Notification->type)) {
+                       return true;
+               }
+
+               return false;
+       }
+
+       public function createFromNotification(Entity\Notification $Notification): bool
        {
                $this->logger->info('Start', ['uid' => $Notification->uid, 'id' => $Notification->id, 'type' => $Notification->type]);
 
@@ -707,11 +734,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.
@@ -729,7 +755,7 @@ class Notify extends BaseRepository
                        $subject        = $l10n->t('%1$s Comment to conversation #%2$d by %3$s', $subjectPrefix, $item['parent'], $contact['name']);
                }
 
-               $msg = $this->notification->getMessageFromNotification($Notification, $this->baseUrl, $l10n);
+               $msg = $this->notification->getMessageFromNotification($Notification);
                if (empty($msg)) {
                        $this->logger->info('No notification message, quitting', ['uid' => $Notification->uid, 'id' => $Notification->id, 'type' => $Notification->type]);
                        return false;