3 * @copyright Copyright (C) 2010-2023, the Friendica project
5 * @license GNU AGPL version 3 or any later version
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.
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.
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/>.
22 namespace Friendica\Navigation\Notifications\Factory;
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;
39 * Factory for creating notification objects based on items
41 class FormattedNavNotification extends BaseFactory
43 private static $contacts = [];
45 /** @var Notification */
46 private $notification;
47 /** @var \Friendica\App\BaseURL */
49 /** @var \Friendica\Core\L10n */
51 /** @var IHandleUserSessions */
56 public function __construct(Notification $notification, \Friendica\App\BaseURL $baseUrl, \Friendica\Core\L10n $l10n, LoggerInterface $logger, IHandleUserSessions $userSession)
58 parent::__construct($logger);
60 $this->notification = $notification;
61 $this->baseUrl = $baseUrl;
63 $this->userSession = $userSession;
65 $this->tpl = Renderer::getMarkupTemplate('notifications/nav/notify.tpl');
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
75 * @return ValueObject\FormattedNavNotification
76 * @throws HTTPException\ServiceUnavailableException
78 public function createFromParams(string $contact_name, string $contact_url, string $message, \DateTime $date, Uri $href, bool $seen = false): ValueObject\FormattedNavNotification
80 $contact_photo = Contact::getAvatarUrlForUrl($contact_url, $this->userSession->getLocalUserId(), Proxy::SIZE_MICRO);
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);
86 $dateMySQL = $date->format(DateTimeFormat::MYSQL);
90 'name' => $contact_name,
91 'url' => $contact_url,
92 'photo' => $contact_photo,
94 'href' => $href->__toString(),
95 'message' => $message,
97 'localdate' => DateTimeFormat::local($dateMySQL),
98 'ago' => Temporal::getRelativeDate($dateMySQL),
99 'richtext' => Entity\Notify::formatMessage($contact_name, $message),
102 return new ValueObject\FormattedNavNotification(
106 $date->getTimestamp(),
107 strip_tags($templateNotify['richtext']),
108 Renderer::replaceMacros($this->tpl, ['notify' => $templateNotify]),
115 * @param Entity\Notification $notification
116 * @return ValueObject\FormattedNavNotification
117 * @throws NoMessageException
118 * @throws HTTPException\InternalServerErrorException
119 * @throws HTTPException\NotFoundException
120 * @throws HTTPException\ServiceUnavailableException
122 public function createFromNotification(Entity\Notification $notification): ValueObject\FormattedNavNotification
124 $message = $this->notification->getMessageFromNotification($notification);
126 if (empty($message)) {
127 throw new NoMessageException();
130 if (!isset(self::$contacts[$notification->actorId])) {
131 self::$contacts[$notification->actorId] = Contact::getById($notification->actorId, ['name', 'url', 'pending']);
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 . '/notification/' . $notification->id),
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
150 public function createFromIntro(\Friendica\Contact\Introduction\Entity\Introduction $intro): ValueObject\FormattedNavNotification
152 if (empty(self::$contacts[$intro->cid])) {
153 if ($contact = Contact::getById($intro->cid, ['name', 'url', 'pending'])) {
154 self::$contacts[$intro->cid] = $contact;
156 throw new HTTPException\NotFoundException('Contact not found with id' . $intro->cid);
160 if (self::$contacts[$intro->cid]['pending']) {
161 $msg = $this->l10n->t('{0} wants to follow you');
163 $msg = $this->l10n->t('{0} has started following you');
166 return $this->createFromParams(
167 self::$contacts[$intro->cid]['name'],
168 self::$contacts[$intro->cid]['url'],
171 new Uri($this->baseUrl . '/notifications/intros/' . $intro->id)