]> git.mxchange.org Git - friendica.git/blob - src/Navigation/Notifications/Factory/FormattedNavNotification.php
Merge pull request #11333 from annando/feed-guid
[friendica.git] / src / Navigation / Notifications / Factory / FormattedNavNotification.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2022, the Friendica project
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19  *
20  */
21
22 namespace Friendica\Navigation\Notifications\Factory;
23
24 use Friendica\BaseFactory;
25 use Friendica\Core\Renderer;
26 use Friendica\Model\Contact;
27 use Friendica\Navigation\Notifications\Entity;
28 use Friendica\Navigation\Notifications\ValueObject;
29 use Friendica\Util\DateTimeFormat;
30 use Friendica\Util\Proxy;
31 use Friendica\Util\Temporal;
32 use GuzzleHttp\Psr7\Uri;
33 use Psr\Log\LoggerInterface;
34
35 /**
36  * Factory for creating notification objects based on items
37  */
38 class FormattedNavNotification extends BaseFactory
39 {
40         private static $contacts = [];
41
42         /** @var Notification */
43         private $notification;
44         /** @var \Friendica\App\BaseURL */
45         private $baseUrl;
46         /** @var \Friendica\Core\L10n */
47         private $l10n;
48         /** @var string */
49         private $tpl;
50
51         public function __construct(Notification $notification, \Friendica\App\BaseURL $baseUrl, \Friendica\Core\L10n $l10n, LoggerInterface $logger)
52         {
53                 parent::__construct($logger);
54
55                 $this->notification = $notification;
56                 $this->baseUrl      = $baseUrl;
57                 $this->l10n         = $l10n;
58
59                 $this->tpl = Renderer::getMarkupTemplate('notifications/nav/notify.tpl');
60         }
61
62         /**
63          * @param array     $contact A contact array with the following keys: name, url
64          * @param string    $message A notification message with the {0} placeholder for the contact name
65          * @param \DateTime $date
66          * @param Uri       $href
67          * @param bool      $seen
68          * @return ValueObject\FormattedNavNotification
69          * @throws \Friendica\Network\HTTPException\ServiceUnavailableException
70          */
71         public function createFromParams(array $contact, string $message, \DateTime $date, Uri $href, bool $seen = false): ValueObject\FormattedNavNotification
72         {
73                 $contact['photo'] = Contact::getAvatarUrlForUrl($contact['url'], local_user(), Proxy::SIZE_MICRO);
74
75                 $dateMySQL = $date->format(DateTimeFormat::MYSQL);
76
77                 $templateNotify = [
78                         'contact'   => $contact,
79                         'href'      => $href->__toString(),
80                         'message'   => $message,
81                         'seen'      => $seen,
82                         'localdate' => DateTimeFormat::local($dateMySQL),
83                         'ago'       => Temporal::getRelativeDate($dateMySQL),
84                         'richtext'  => Entity\Notify::formatMessage($contact['name'], $message),
85                 ];
86
87                 return new ValueObject\FormattedNavNotification(
88                         $contact,
89                         $date->getTimestamp(),
90                         strip_tags($templateNotify['richtext']),
91                         Renderer::replaceMacros($this->tpl, ['notify' => $templateNotify]),
92                         $href,
93                         $seen,
94                 );
95         }
96
97         public function createFromNotification(Entity\Notification $notification): ValueObject\FormattedNavNotification
98         {
99                 $message = $this->notification->getMessageFromNotification($notification);
100
101                 if (!isset(self::$contacts[$notification->actorId])) {
102                         self::$contacts[$notification->actorId] = Contact::getById($notification->actorId, ['name', 'url']);
103                 }
104
105                 return $this->createFromParams(
106                         self::$contacts[$notification->actorId],
107                         $message['notification'],
108                         $notification->created,
109                         new Uri($this->baseUrl->get() . '/notification/' . $notification->id),
110                         $notification->seen,
111                 );
112         }
113
114         public function createFromIntro(\Friendica\Contact\Introduction\Entity\Introduction $intro): ValueObject\FormattedNavNotification
115         {
116                 if (!isset(self::$contacts[$intro->cid])) {
117                         self::$contacts[$intro->cid] = Contact::getById($intro->cid, ['name', 'url']);
118                 }
119
120                 return $this->createFromParams(
121                         self::$contacts[$intro->cid],
122                         $this->l10n->t('{0}} wants to follow you'),
123                         new \DateTime($intro->datetime, new \DateTimeZone('UTC')),
124                         new Uri($this->baseUrl->get() . '/notifications/intros/' . $intro->id)
125                 );
126         }
127 }