3 * @copyright Copyright (C) 2010-2022, 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\Model\Contact;
27 use Friendica\Navigation\Notifications\Entity;
28 use Friendica\Navigation\Notifications\Exception\NoMessageException;
29 use Friendica\Navigation\Notifications\ValueObject;
30 use Friendica\Util\DateTimeFormat;
31 use Friendica\Util\Proxy;
32 use Friendica\Util\Temporal;
33 use GuzzleHttp\Psr7\Uri;
34 use Psr\Log\LoggerInterface;
37 * Factory for creating notification objects based on items
39 class FormattedNavNotification extends BaseFactory
41 private static $contacts = [];
43 /** @var Notification */
44 private $notification;
45 /** @var \Friendica\App\BaseURL */
47 /** @var \Friendica\Core\L10n */
52 public function __construct(Notification $notification, \Friendica\App\BaseURL $baseUrl, \Friendica\Core\L10n $l10n, LoggerInterface $logger)
54 parent::__construct($logger);
56 $this->notification = $notification;
57 $this->baseUrl = $baseUrl;
60 $this->tpl = Renderer::getMarkupTemplate('notifications/nav/notify.tpl');
64 * @param array $contact A contact array with the following keys: name, url
65 * @param string $message A notification message with the {0} placeholder for the contact name
66 * @param \DateTime $date
69 * @return ValueObject\FormattedNavNotification
70 * @throws \Friendica\Network\HTTPException\ServiceUnavailableException
72 public function createFromParams(array $contact, string $message, \DateTime $date, Uri $href, bool $seen = false): ValueObject\FormattedNavNotification
74 $contact['photo'] = Contact::getAvatarUrlForUrl($contact['url'], local_user(), Proxy::SIZE_MICRO);
76 $dateMySQL = $date->format(DateTimeFormat::MYSQL);
79 'contact' => $contact,
80 'href' => $href->__toString(),
81 'message' => $message,
83 'localdate' => DateTimeFormat::local($dateMySQL),
84 'ago' => Temporal::getRelativeDate($dateMySQL),
85 'richtext' => Entity\Notify::formatMessage($contact['name'], $message),
88 return new ValueObject\FormattedNavNotification(
90 $date->getTimestamp(),
91 strip_tags($templateNotify['richtext']),
92 Renderer::replaceMacros($this->tpl, ['notify' => $templateNotify]),
99 * @param Entity\Notification $notification
100 * @return ValueObject\FormattedNavNotification
101 * @throws NoMessageException
102 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
103 * @throws \Friendica\Network\HTTPException\NotFoundException
104 * @throws \Friendica\Network\HTTPException\ServiceUnavailableException
106 public function createFromNotification(Entity\Notification $notification): ValueObject\FormattedNavNotification
108 $message = $this->notification->getMessageFromNotification($notification);
110 if (empty($message)) {
111 throw new NoMessageException();
114 if (!isset(self::$contacts[$notification->actorId])) {
115 self::$contacts[$notification->actorId] = Contact::getById($notification->actorId, ['name', 'url']);
118 return $this->createFromParams(
119 self::$contacts[$notification->actorId],
120 $message['notification'],
121 $notification->created,
122 new Uri($this->baseUrl->get() . '/notification/' . $notification->id),
127 public function createFromIntro(\Friendica\Contact\Introduction\Entity\Introduction $intro): ValueObject\FormattedNavNotification
129 if (!isset(self::$contacts[$intro->cid])) {
130 self::$contacts[$intro->cid] = Contact::getById($intro->cid, ['name', 'url']);
133 return $this->createFromParams(
134 self::$contacts[$intro->cid],
135 $this->l10n->t('{0} wants to follow you'),
137 new Uri($this->baseUrl->get() . '/notifications/intros/' . $intro->id)