]> git.mxchange.org Git - friendica.git/blob - src/Navigation/Notifications/Factory/FormattedNavNotification.php
Merge pull request #11334 from annando/guid-style
[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\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;
35
36 /**
37  * Factory for creating notification objects based on items
38  */
39 class FormattedNavNotification extends BaseFactory
40 {
41         private static $contacts = [];
42
43         /** @var Notification */
44         private $notification;
45         /** @var \Friendica\App\BaseURL */
46         private $baseUrl;
47         /** @var \Friendica\Core\L10n */
48         private $l10n;
49         /** @var string */
50         private $tpl;
51
52         public function __construct(Notification $notification, \Friendica\App\BaseURL $baseUrl, \Friendica\Core\L10n $l10n, LoggerInterface $logger)
53         {
54                 parent::__construct($logger);
55
56                 $this->notification = $notification;
57                 $this->baseUrl      = $baseUrl;
58                 $this->l10n         = $l10n;
59
60                 $this->tpl = Renderer::getMarkupTemplate('notifications/nav/notify.tpl');
61         }
62
63         /**
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
67          * @param Uri       $href
68          * @param bool      $seen
69          * @return ValueObject\FormattedNavNotification
70          * @throws \Friendica\Network\HTTPException\ServiceUnavailableException
71          */
72         public function createFromParams(array $contact, string $message, \DateTime $date, Uri $href, bool $seen = false): ValueObject\FormattedNavNotification
73         {
74                 $contact['photo'] = Contact::getAvatarUrlForUrl($contact['url'], local_user(), Proxy::SIZE_MICRO);
75
76                 $dateMySQL = $date->format(DateTimeFormat::MYSQL);
77
78                 $templateNotify = [
79                         'contact'   => $contact,
80                         'href'      => $href->__toString(),
81                         'message'   => $message,
82                         'seen'      => $seen,
83                         'localdate' => DateTimeFormat::local($dateMySQL),
84                         'ago'       => Temporal::getRelativeDate($dateMySQL),
85                         'richtext'  => Entity\Notify::formatMessage($contact['name'], $message),
86                 ];
87
88                 return new ValueObject\FormattedNavNotification(
89                         $contact,
90                         $date->getTimestamp(),
91                         strip_tags($templateNotify['richtext']),
92                         Renderer::replaceMacros($this->tpl, ['notify' => $templateNotify]),
93                         $href,
94                         $seen,
95                 );
96         }
97
98         /**
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
105          */
106         public function createFromNotification(Entity\Notification $notification): ValueObject\FormattedNavNotification
107         {
108                 $message = $this->notification->getMessageFromNotification($notification);
109
110                 if (empty($message)) {
111                         throw new NoMessageException();
112                 }
113
114                 if (!isset(self::$contacts[$notification->actorId])) {
115                         self::$contacts[$notification->actorId] = Contact::getById($notification->actorId, ['name', 'url']);
116                 }
117
118                 return $this->createFromParams(
119                         self::$contacts[$notification->actorId],
120                         $message['notification'],
121                         $notification->created,
122                         new Uri($this->baseUrl->get() . '/notification/' . $notification->id),
123                         $notification->seen,
124                 );
125         }
126
127         public function createFromIntro(\Friendica\Contact\Introduction\Entity\Introduction $intro): ValueObject\FormattedNavNotification
128         {
129                 if (!isset(self::$contacts[$intro->cid])) {
130                         self::$contacts[$intro->cid] = Contact::getById($intro->cid, ['name', 'url']);
131                 }
132
133                 return $this->createFromParams(
134                         self::$contacts[$intro->cid],
135                         $this->l10n->t('{0} wants to follow you'),
136                         new \DateTime($intro->datetime, new \DateTimeZone('UTC')),
137                         new Uri($this->baseUrl->get() . '/notifications/intros/' . $intro->id)
138                 );
139         }
140 }