]> git.mxchange.org Git - friendica.git/blob - src/Module/Notifications/Introductions.php
Adhere feedback
[friendica.git] / src / Module / Notifications / Introductions.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2023, 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\Module\Notifications;
23
24 use Friendica\App;
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;
34 use Friendica\DI;
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;
42
43 /**
44  * Prints notifications about introduction
45  */
46 class Introductions extends BaseNotifications
47 {
48         /** @var IntroductionFactory */
49         protected $notificationIntro;
50         /** @var Mode */
51         protected $mode;
52
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 = [])
54         {
55                 parent::__construct($l10n, $baseUrl, $args, $logger, $profiler, $response, $userSession, $server, $parameters);
56
57                 $this->notificationIntro = $notificationIntro;
58                 $this->mode              = $mode;
59         }
60
61         /**
62          * @inheritDoc
63          */
64         public function getNotifications()
65         {
66                 $id  = (int)$this->args->get(2, 0);
67                 $all = $this->args->get(2) == 'all';
68
69                 $notifications = [
70                         'ident'         => 'introductions',
71                         'notifications' => $this->notificationIntro->getList($all, $this->firstItemNum, self::ITEMS_PER_PAGE, $id),
72                 ];
73
74                 return [
75                         'header'        => $this->t('Notifications'),
76                         'notifications' => $notifications,
77                 ];
78         }
79
80         protected function content(array $request = []): string
81         {
82                 Nav::setSelected('introductions');
83
84                 $all = $this->args->get(2) == 'all';
85
86                 $notificationContent   = [];
87                 $notificationNoContent = '';
88
89                 $notificationResult = $this->getNotifications();
90                 $notifications      = $notificationResult['notifications'] ?? [];
91                 $notificationHeader = $notificationResult['header'] ?? '';
92
93                 $notificationSuggestions = Renderer::getMarkupTemplate('notifications/suggestions.tpl');
94                 $notificationTemplate    = Renderer::getMarkupTemplate('notifications/intros.tpl');
95
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')),
100                 ];
101
102                 $owner = User::getOwnerDataById(DI::userSession()->getLocalUserId());
103
104                 // Loop through all introduction notifications.This creates an array with the output html for each
105                 // introduction
106                 /** @var Introduction $Introduction */
107                 foreach ($notifications['notifications'] as $Introduction) {
108
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(),
137                                         ]);
138                                         break;
139
140                                 // Normal connection requests
141                                 default:
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'));
145                                         } else {
146                                                 $lbl_knowyou = '';
147                                                 $knowyou = '';
148                                         }
149
150                                         $convertedName = BBCode::convert($Introduction->getName());
151
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);
155
156                                         $friend = ['duplex', $this->t('Friend'), '1', $helptext2, true];
157                                         $follower = ['duplex', $this->t('Subscriber'), '0', $helptext3, false];
158
159                                         $action = 'follow_confirm';
160
161                                         $header = $Introduction->getName();
162
163                                         if ($Introduction->getAddr() != '') {
164                                                 $header .= ' <' . $Introduction->getAddr() . '>';
165                                         }
166
167                                         $header .= ' (' . ContactSelector::networkToName($Introduction->getNetwork(), $Introduction->getUrl()) . ')';
168
169                                         if ($Introduction->getNetwork() != Protocol::DIASPORA) {
170                                                 $discard = $this->t('Discard');
171                                         } else {
172                                                 $discard = '';
173                                         }
174
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(),
210                                         ]);
211                                         break;
212                         }
213                 }
214
215                 if (count($notifications['notifications']) == 0) {
216                         DI::sysmsg()->addNotice($this->t('No introductions.'));
217                         $notificationNoContent = $this->t('No more %s notifications.', $notifications['ident']);
218                 }
219
220                 return $this->printContent($notificationHeader, $notificationContent, $notificationNoContent, $notificationShowLink);
221         }
222 }