]> git.mxchange.org Git - friendica.git/blob - src/Navigation/Notifications/Factory/FormattedNavNotification.php
cfb798ac7287f9c5d61ca151ad3c90778e5d11d0
[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\Core\Session\Capability\IHandleUserSessions;
27 use Friendica\Model\Contact;
28 use Friendica\Navigation\Notifications\Entity;
29 use Friendica\Navigation\Notifications\Exception\NoMessageException;
30 use Friendica\Navigation\Notifications\ValueObject;
31 use Friendica\Network\HTTPException\ServiceUnavailableException;
32 use Friendica\Util\DateTimeFormat;
33 use Friendica\Util\Proxy;
34 use Friendica\Util\Temporal;
35 use GuzzleHttp\Psr7\Uri;
36 use Psr\Log\LoggerInterface;
37
38 /**
39  * Factory for creating notification objects based on items
40  */
41 class FormattedNavNotification extends BaseFactory
42 {
43         private static $contacts = [];
44
45         /** @var Notification */
46         private $notification;
47         /** @var \Friendica\App\BaseURL */
48         private $baseUrl;
49         /** @var \Friendica\Core\L10n */
50         private $l10n;
51         /** @var IHandleUserSessions */
52         private $userSession;
53         /** @var string */
54         private $tpl;
55
56         public function __construct(Notification $notification, \Friendica\App\BaseURL $baseUrl, \Friendica\Core\L10n $l10n, LoggerInterface $logger, IHandleUserSessions $userSession)
57         {
58                 parent::__construct($logger);
59
60                 $this->notification = $notification;
61                 $this->baseUrl      = $baseUrl;
62                 $this->l10n         = $l10n;
63                 $this->userSession  = $userSession;
64
65                 $this->tpl = Renderer::getMarkupTemplate('notifications/nav/notify.tpl');
66         }
67
68         /**
69          * @param string    $contact_name
70          * @param string    $contact_url
71          * @param string    $message A notification message with the {0} placeholder for the contact name
72          * @param \DateTime $date
73          * @param Uri       $href
74          * @param bool      $seen
75          * @return ValueObject\FormattedNavNotification
76          * @throws ServiceUnavailableException
77          */
78         public function createFromParams(string $contact_name, string $contact_url, string $message, \DateTime $date, Uri $href, bool $seen = false): ValueObject\FormattedNavNotification
79         {
80                 $contact_photo = Contact::getAvatarUrlForUrl($contact_url, $this->userSession->getLocalUserId(), Proxy::SIZE_MICRO);
81
82                 $dateMySQL = $date->format(DateTimeFormat::MYSQL);
83
84                 $templateNotify = [
85                         'contact' => [
86                                 'name'  => $contact_name,
87                                 'url'   => $contact_url,
88                                 'photo' => $contact_photo,
89                         ],
90                         'href'      => $href->__toString(),
91                         'message'   => $message,
92                         'seen'      => $seen,
93                         'localdate' => DateTimeFormat::local($dateMySQL),
94                         'ago'       => Temporal::getRelativeDate($dateMySQL),
95                         'richtext'  => Entity\Notify::formatMessage($contact_name, $message),
96                 ];
97
98                 return new ValueObject\FormattedNavNotification(
99                         $contact_name,
100                         $contact_url,
101                         $contact_photo,
102                         $date->getTimestamp(),
103                         strip_tags($templateNotify['richtext']),
104                         Renderer::replaceMacros($this->tpl, ['notify' => $templateNotify]),
105                         $href,
106                         $seen,
107                 );
108         }
109
110         /**
111          * @param Entity\Notification $notification
112          * @return ValueObject\FormattedNavNotification
113          * @throws NoMessageException
114          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
115          * @throws \Friendica\Network\HTTPException\NotFoundException
116          * @throws \Friendica\Network\HTTPException\ServiceUnavailableException
117          */
118         public function createFromNotification(Entity\Notification $notification): ValueObject\FormattedNavNotification
119         {
120                 $message = $this->notification->getMessageFromNotification($notification);
121
122                 if (empty($message)) {
123                         throw new NoMessageException();
124                 }
125
126                 if (!isset(self::$contacts[$notification->actorId])) {
127                         self::$contacts[$notification->actorId] = Contact::getById($notification->actorId, ['name', 'url']);
128                 }
129
130                 return $this->createFromParams(
131                         self::$contacts[$notification->actorId]['name'],
132                         self::$contacts[$notification->actorId]['url'],
133                         $message['notification'],
134                         $notification->created,
135                         new Uri($this->baseUrl->get() . '/notification/' . $notification->id),
136                         $notification->seen,
137                 );
138         }
139
140         public function createFromIntro(\Friendica\Contact\Introduction\Entity\Introduction $intro): ValueObject\FormattedNavNotification
141         {
142                 if (!isset(self::$contacts[$intro->cid])) {
143                         self::$contacts[$intro->cid] = Contact::getById($intro->cid, ['name', 'url', 'pending']);
144                 }
145
146                 if (self::$contacts[$intro->cid]['pending']) {
147                         $msg = $this->l10n->t('{0} wants to follow you');
148                 } else {
149                         $msg = $this->l10n->t('{0} has started following you');
150                 }
151
152                 return $this->createFromParams(
153                         self::$contacts[$intro->cid]['name'],
154                         self::$contacts[$intro->cid]['url'],
155                         $msg,
156                         $intro->datetime,
157                         new Uri($this->baseUrl->get() . '/notifications/intros/' . $intro->id)
158                 );
159         }
160 }