]> git.mxchange.org Git - friendica.git/blob - src/Navigation/Notifications/Factory/FormattedNavNotification.php
Merge pull request #12532 from annando/warning
[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;
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 HTTPException\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                 // Removing the RTL Override character to prevent a garbled notification message
83                 // See https://github.com/friendica/friendica/issues/12084
84                 $contact_name = str_replace("\xE2\x80\xAE", '', $contact_name);
85
86                 $dateMySQL = $date->format(DateTimeFormat::MYSQL);
87
88                 $templateNotify = [
89                         'contact' => [
90                                 'name'  => $contact_name,
91                                 'url'   => $contact_url,
92                                 'photo' => $contact_photo,
93                         ],
94                         'href'      => $href->__toString(),
95                         'message'   => $message,
96                         'seen'      => $seen,
97                         'localdate' => DateTimeFormat::local($dateMySQL),
98                         'ago'       => Temporal::getRelativeDate($dateMySQL),
99                         'richtext'  => Entity\Notify::formatMessage($contact_name, $message),
100                 ];
101
102                 return new ValueObject\FormattedNavNotification(
103                         $contact_name,
104                         $contact_url,
105                         $contact_photo,
106                         $date->getTimestamp(),
107                         strip_tags($templateNotify['richtext']),
108                         Renderer::replaceMacros($this->tpl, ['notify' => $templateNotify]),
109                         $href,
110                         $seen,
111                 );
112         }
113
114         /**
115          * @param Entity\Notification $notification
116          * @return ValueObject\FormattedNavNotification
117          * @throws NoMessageException
118          * @throws HTTPException\InternalServerErrorException
119          * @throws HTTPException\NotFoundException
120          * @throws HTTPException\ServiceUnavailableException
121          */
122         public function createFromNotification(Entity\Notification $notification): ValueObject\FormattedNavNotification
123         {
124                 $message = $this->notification->getMessageFromNotification($notification);
125
126                 if (empty($message)) {
127                         throw new NoMessageException();
128                 }
129
130                 if (!isset(self::$contacts[$notification->actorId])) {
131                         self::$contacts[$notification->actorId] = Contact::getById($notification->actorId, ['name', 'url']);
132                 }
133
134                 return $this->createFromParams(
135                         self::$contacts[$notification->actorId]['name'],
136                         self::$contacts[$notification->actorId]['url'],
137                         $message['notification'],
138                         $notification->created,
139                         new Uri($this->baseUrl->get() . '/notification/' . $notification->id),
140                         $notification->seen,
141                 );
142         }
143
144         /**
145          * @param \Friendica\Contact\Introduction\Entity\Introduction $intro
146          * @return ValueObject\FormattedNavNotification
147          * @throws HTTPException\NotFoundException when the contact record couldn't be located
148          * @throws HTTPException\ServiceUnavailableException
149          */
150         public function createFromIntro(\Friendica\Contact\Introduction\Entity\Introduction $intro): ValueObject\FormattedNavNotification
151         {
152                 if (empty(self::$contacts[$intro->cid])) {
153                         if ($contact = Contact::getById($intro->cid, ['name', 'url', 'pending'])) {
154                                 self::$contacts[$intro->cid] = $contact;
155                         } else {
156                                 throw new HTTPException\NotFoundException('Contact not found with id' . $intro->cid);
157                         }
158                 }
159
160                 if (self::$contacts[$intro->cid]['pending']) {
161                         $msg = $this->l10n->t('{0} wants to follow you');
162                 } else {
163                         $msg = $this->l10n->t('{0} has started following you');
164                 }
165
166                 return $this->createFromParams(
167                         self::$contacts[$intro->cid]['name'],
168                         self::$contacts[$intro->cid]['url'],
169                         $msg,
170                         $intro->datetime,
171                         new Uri($this->baseUrl->get() . '/notifications/intros/' . $intro->id)
172                 );
173         }
174 }