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\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;
36 * Factory for creating notification objects based on items
38 class FormattedNavNotification extends BaseFactory
40 private static $contacts = [];
42 /** @var Notification */
43 private $notification;
44 /** @var \Friendica\App\BaseURL */
46 /** @var \Friendica\Core\L10n */
51 public function __construct(Notification $notification, \Friendica\App\BaseURL $baseUrl, \Friendica\Core\L10n $l10n, LoggerInterface $logger)
53 parent::__construct($logger);
55 $this->notification = $notification;
56 $this->baseUrl = $baseUrl;
59 $this->tpl = Renderer::getMarkupTemplate('notifications/nav/notify.tpl');
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
68 * @return ValueObject\FormattedNavNotification
69 * @throws \Friendica\Network\HTTPException\ServiceUnavailableException
71 public function createFromParams(array $contact, string $message, \DateTime $date, Uri $href, bool $seen = false): ValueObject\FormattedNavNotification
73 $contact['photo'] = Contact::getAvatarUrlForUrl($contact['url'], local_user(), Proxy::SIZE_MICRO);
75 $dateMySQL = $date->format(DateTimeFormat::MYSQL);
78 'contact' => $contact,
79 'href' => $href->__toString(),
80 'message' => $message,
82 'localdate' => DateTimeFormat::local($dateMySQL),
83 'ago' => Temporal::getRelativeDate($dateMySQL),
84 'richtext' => Entity\Notify::formatMessage($contact['name'], $message),
87 return new ValueObject\FormattedNavNotification(
89 $date->getTimestamp(),
90 strip_tags($templateNotify['richtext']),
91 Renderer::replaceMacros($this->tpl, ['notify' => $templateNotify]),
97 public function createFromNotification(Entity\Notification $notification): ValueObject\FormattedNavNotification
99 $message = $this->notification->getMessageFromNotification($notification);
101 if (!isset(self::$contacts[$notification->actorId])) {
102 self::$contacts[$notification->actorId] = Contact::getById($notification->actorId, ['name', 'url']);
105 return $this->createFromParams(
106 self::$contacts[$notification->actorId],
107 $message['notification'],
108 $notification->created,
109 new Uri($this->baseUrl->get() . '/notification/' . $notification->id),
114 public function createFromIntro(\Friendica\Contact\Introduction\Entity\Introduction $intro): ValueObject\FormattedNavNotification
116 if (!isset(self::$contacts[$intro->cid])) {
117 self::$contacts[$intro->cid] = Contact::getById($intro->cid, ['name', 'url']);
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)