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\Module\Notifications;
25 use Friendica\App\Arguments;
26 use Friendica\App\Mode;
27 use Friendica\Content\ContactSelector;
28 use Friendica\Content\Nav;
29 use Friendica\Content\Text\BBCode;
30 use Friendica\Core\L10n;
31 use Friendica\Core\Protocol;
32 use Friendica\Core\Renderer;
33 use Friendica\Core\Session\Capability\IHandleUserSessions;
35 use Friendica\Model\User;
36 use Friendica\Module\BaseNotifications;
37 use Friendica\Module\Response;
38 use Friendica\Navigation\Notifications\Factory\Introduction as IntroductionFactory;
39 use Friendica\Navigation\Notifications\ValueObject\Introduction;
40 use Friendica\Util\Profiler;
41 use Psr\Log\LoggerInterface;
44 * Prints notifications about introduction
46 class Introductions extends BaseNotifications
48 /** @var IntroductionFactory */
49 protected $notificationIntro;
53 public function __construct(L10n $l10n, App\BaseURL $baseUrl, Arguments $args, LoggerInterface $logger, Profiler $profiler, Response $response, Mode $mode, IntroductionFactory $notificationIntro, IHandleUserSessions $userSession, array $server, array $parameters = [])
55 parent::__construct($l10n, $baseUrl, $args, $logger, $profiler, $response, $userSession, $server, $parameters);
57 $this->notificationIntro = $notificationIntro;
64 public function getNotifications()
66 $id = (int)$this->args->get(2, 0);
67 $all = $this->args->get(2) == 'all';
70 'ident' => 'introductions',
71 'notifications' => $this->notificationIntro->getList($all, $this->firstItemNum, self::ITEMS_PER_PAGE, $id),
75 'header' => $this->t('Notifications'),
76 'notifications' => $notifications,
80 protected function content(array $request = []): string
82 Nav::setSelected('introductions');
84 $all = $this->args->get(2) == 'all';
86 $notificationContent = [];
87 $notificationNoContent = '';
89 $notificationResult = $this->getNotifications();
90 $notifications = $notificationResult['notifications'] ?? [];
91 $notificationHeader = $notificationResult['header'] ?? '';
93 $notificationSuggestions = Renderer::getMarkupTemplate('notifications/suggestions.tpl');
94 $notificationTemplate = Renderer::getMarkupTemplate('notifications/intros.tpl');
96 // The link to switch between ignored and normal connection requests
97 $notificationShowLink = [
98 'href' => (!$all ? 'notifications/intros/all' : 'notifications/intros'),
99 'text' => (!$all ? $this->t('Show Ignored Requests') : $this->t('Hide Ignored Requests')),
102 $owner = User::getOwnerDataById(DI::userSession()->getLocalUserId());
104 // Loop through all introduction notifications.This creates an array with the output html for each
106 /** @var Introduction $Introduction */
107 foreach ($notifications['notifications'] as $Introduction) {
109 // There are two kind of introduction. Contacts suggested by other contacts and normal connection requests.
110 // We have to distinguish between these two because they use different data.
111 switch ($Introduction->getLabel()) {
112 case 'friend_suggestion':
113 $notificationContent[] = Renderer::replaceMacros($notificationSuggestions, [
114 '$type' => $Introduction->getLabel(),
115 '$str_notification_type' => $this->t('Notification type:'),
116 '$str_type' => $Introduction->getType(),
117 '$intro_id' => $Introduction->getIntroId(),
118 '$lbl_madeby' => $this->t('Suggested by:'),
119 '$madeby' => $Introduction->getMadeBy(),
120 '$madeby_url' => $Introduction->getMadeByUrl(),
121 '$madeby_zrl' => $Introduction->getMadeByZrl(),
122 '$madeby_addr' => $Introduction->getMadeByAddr(),
123 '$contact_id' => $Introduction->getContactId(),
124 '$photo' => $Introduction->getPhoto(),
125 '$fullname' => $Introduction->getName(),
126 '$dfrn_url' => $owner['url'],
127 '$url' => $Introduction->getUrl(),
128 '$zrl' => $Introduction->getZrl(),
129 '$lbl_url' => $this->t('Profile URL'),
130 '$addr' => $Introduction->getAddr(),
131 '$action' => 'contact/follow',
132 '$approve' => $this->t('Approve'),
133 '$note' => $Introduction->getNote(),
134 '$ignore' => $this->t('Ignore'),
135 '$discard' => $this->t('Discard'),
136 '$is_mobile' => $this->mode->isMobile(),
140 // Normal connection requests
142 if ($Introduction->getNetwork() === Protocol::DFRN) {
143 $lbl_knowyou = $this->t('Claims to be known to you: ');
144 $knowyou = ($Introduction->getKnowYou() ? $this->t('Yes') : $this->t('No'));
150 $convertedName = BBCode::convert($Introduction->getName());
152 $helptext = $this->t('Shall your connection be bidirectional or not?');
153 $helptext2 = $this->t('Accepting %s as a friend allows %s to subscribe to your posts, and you will also receive updates from them in your news feed.', $convertedName, $convertedName);
154 $helptext3 = $this->t('Accepting %s as a subscriber allows them to subscribe to your posts, but you will not receive updates from them in your news feed.', $convertedName);
156 $friend = ['duplex', $this->t('Friend'), '1', $helptext2, true];
157 $follower = ['duplex', $this->t('Subscriber'), '0', $helptext3, false];
159 $action = 'follow_confirm';
161 $header = $Introduction->getName();
163 if ($Introduction->getAddr() != '') {
164 $header .= ' <' . $Introduction->getAddr() . '>';
167 $header .= ' (' . ContactSelector::networkToName($Introduction->getNetwork(), $Introduction->getUrl()) . ')';
169 if ($Introduction->getNetwork() != Protocol::DIASPORA) {
170 $discard = $this->t('Discard');
175 $notificationContent[] = Renderer::replaceMacros($notificationTemplate, [
176 '$type' => $Introduction->getLabel(),
177 '$header' => $header,
178 '$str_notification_type' => $this->t('Notification type:'),
179 '$str_type' => $Introduction->getType(),
180 '$dfrn_id' => $Introduction->getDfrnId(),
181 '$uid' => $Introduction->getUid(),
182 '$intro_id' => $Introduction->getIntroId(),
183 '$contact_id' => $Introduction->getContactId(),
184 '$photo' => $Introduction->getPhoto(),
185 '$fullname' => $Introduction->getName(),
186 '$location' => $Introduction->getLocation(),
187 '$lbl_location' => $this->t('Location:'),
188 '$about' => $Introduction->getAbout(),
189 '$lbl_about' => $this->t('About:'),
190 '$keywords' => $Introduction->getKeywords(),
191 '$lbl_keywords' => $this->t('Tags:'),
192 '$hidden' => ['hidden', $this->t('Hide this contact from others'), $Introduction->isHidden(), ''],
193 '$lbl_connection_type' => $helptext,
194 '$friend' => $friend,
195 '$follower' => $follower,
196 '$url' => $Introduction->getUrl(),
197 '$zrl' => $Introduction->getZrl(),
198 '$lbl_url' => $this->t('Profile URL'),
199 '$addr' => $Introduction->getAddr(),
200 '$lbl_knowyou' => $lbl_knowyou,
201 '$lbl_network' => $this->t('Network:'),
202 '$network' => ContactSelector::networkToName($Introduction->getNetwork(), $Introduction->getUrl()),
203 '$knowyou' => $knowyou,
204 '$approve' => $this->t('Approve'),
205 '$note' => $Introduction->getNote(),
206 '$ignore' => $this->t('Ignore'),
207 '$discard' => $discard,
208 '$action' => $action,
209 '$is_mobile' => $this->mode->isMobile(),
215 if (count($notifications['notifications']) == 0) {
216 DI::sysmsg()->addNotice($this->t('No introductions.'));
217 $notificationNoContent = $this->t('No more %s notifications.', $notifications['ident']);
220 return $this->printContent($notificationHeader, $notificationContent, $notificationNoContent, $notificationShowLink);