]> git.mxchange.org Git - friendica.git/blob - src/Module/Notifications/Introductions.php
Merge pull request #12041 from nupplaphil/feat/usersession_Module
[friendica.git] / src / Module / Notifications / Introductions.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\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\DI;
34 use Friendica\Model\User;
35 use Friendica\Module\BaseNotifications;
36 use Friendica\Module\Response;
37 use Friendica\Navigation\Notifications\Factory\Introduction as IntroductionFactory;
38 use Friendica\Navigation\Notifications\ValueObject\Introduction;
39 use Friendica\Util\Profiler;
40 use Psr\Log\LoggerInterface;
41
42 /**
43  * Prints notifications about introduction
44  */
45 class Introductions extends BaseNotifications
46 {
47         /** @var IntroductionFactory */
48         protected $notificationIntro;
49         /** @var Mode */
50         protected $mode;
51
52         public function __construct(L10n $l10n, App\BaseURL $baseUrl, Arguments $args, LoggerInterface $logger, Profiler $profiler, Response $response, Mode $mode, IntroductionFactory $notificationIntro, array $server, array $parameters = [])
53         {
54                 parent::__construct($l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters);
55
56                 $this->notificationIntro = $notificationIntro;
57                 $this->mode              = $mode;
58         }
59
60         /**
61          * @inheritDoc
62          */
63         public function getNotifications()
64         {
65                 $id  = (int)$this->args->get(2, 0);
66                 $all = $this->args->get(2) == 'all';
67
68                 $notifications = [
69                         'ident'         => 'introductions',
70                         'notifications' => $this->notificationIntro->getList($all, $this->firstItemNum, self::ITEMS_PER_PAGE, $id),
71                 ];
72
73                 return [
74                         'header'        => $this->t('Notifications'),
75                         'notifications' => $notifications,
76                 ];
77         }
78
79         protected function content(array $request = []): string
80         {
81                 Nav::setSelected('introductions');
82
83                 $all = $this->args->get(2) == 'all';
84
85                 $notificationContent   = [];
86                 $notificationNoContent = '';
87
88                 $notificationResult = $this->getNotifications();
89                 $notifications      = $notificationResult['notifications'] ?? [];
90                 $notificationHeader = $notificationResult['header'] ?? '';
91
92                 $notificationSuggestions = Renderer::getMarkupTemplate('notifications/suggestions.tpl');
93                 $notificationTemplate    = Renderer::getMarkupTemplate('notifications/intros.tpl');
94
95                 // The link to switch between ignored and normal connection requests
96                 $notificationShowLink = [
97                         'href' => (!$all ? 'notifications/intros/all' : 'notifications/intros'),
98                         'text' => (!$all ? $this->t('Show Ignored Requests') : $this->t('Hide Ignored Requests')),
99                 ];
100
101                 $owner = User::getOwnerDataById(DI::userSession()->getLocalUserId());
102         
103                 // Loop through all introduction notifications.This creates an array with the output html for each
104                 // introduction
105                 /** @var Introduction $Introduction */
106                 foreach ($notifications['notifications'] as $Introduction) {
107
108                         // There are two kind of introduction. Contacts suggested by other contacts and normal connection requests.
109                         // We have to distinguish between these two because they use different data.
110                         switch ($Introduction->getLabel()) {
111                                 case 'friend_suggestion':
112                                         $notificationContent[] = Renderer::replaceMacros($notificationSuggestions, [
113                                                 '$type'                  => $Introduction->getLabel(),
114                                                 '$str_notification_type' => $this->t('Notification type:'),
115                                                 '$str_type'              => $Introduction->getType(),
116                                                 '$intro_id'              => $Introduction->getIntroId(),
117                                                 '$lbl_madeby'            => $this->t('Suggested by:'),
118                                                 '$madeby'                => $Introduction->getMadeBy(),
119                                                 '$madeby_url'            => $Introduction->getMadeByUrl(),
120                                                 '$madeby_zrl'            => $Introduction->getMadeByZrl(),
121                                                 '$madeby_addr'           => $Introduction->getMadeByAddr(),
122                                                 '$contact_id'            => $Introduction->getContactId(),
123                                                 '$photo'                 => $Introduction->getPhoto(),
124                                                 '$fullname'              => $Introduction->getName(),
125                                                 '$dfrn_url'              => $owner['url'],
126                                                 '$url'                   => $Introduction->getUrl(),
127                                                 '$zrl'                   => $Introduction->getZrl(),
128                                                 '$lbl_url'               => $this->t('Profile URL'),
129                                                 '$addr'                  => $Introduction->getAddr(),
130                                                 '$action'                => 'follow',
131                                                 '$approve'               => $this->t('Approve'),
132                                                 '$note'                  => $Introduction->getNote(),
133                                                 '$ignore'                => $this->t('Ignore'),
134                                                 '$discard'               => $this->t('Discard'),
135                                                 '$is_mobile'             => $this->mode->isMobile(),
136                                         ]);
137                                         break;
138
139                                 // Normal connection requests
140                                 default:
141                                         if ($Introduction->getNetwork() === Protocol::DFRN) {
142                                                 $lbl_knowyou = $this->t('Claims to be known to you: ');
143                                                 $knowyou     = ($Introduction->getKnowYou() ? $this->t('Yes') : $this->t('No'));
144                                         } else {
145                                                 $lbl_knowyou = '';
146                                                 $knowyou = '';
147                                         }
148
149                                         $convertedName = BBCode::convert($Introduction->getName());
150
151                                         $helptext  = $this->t('Shall your connection be bidirectional or not?');
152                                         $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);
153                                         $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);
154                 
155                                         $friend = ['duplex', $this->t('Friend'), '1', $helptext2, true];
156                                         $follower = ['duplex', $this->t('Subscriber'), '0', $helptext3, false];
157
158                                         $action = 'follow_confirm';
159
160                                         $header = $Introduction->getName();
161
162                                         if ($Introduction->getAddr() != '') {
163                                                 $header .= ' <' . $Introduction->getAddr() . '>';
164                                         }
165
166                                         $header .= ' (' . ContactSelector::networkToName($Introduction->getNetwork(), $Introduction->getUrl()) . ')';
167
168                                         if ($Introduction->getNetwork() != Protocol::DIASPORA) {
169                                                 $discard = $this->t('Discard');
170                                         } else {
171                                                 $discard = '';
172                                         }
173
174                                         $notificationContent[] = Renderer::replaceMacros($notificationTemplate, [
175                                                 '$type'                  => $Introduction->getLabel(),
176                                                 '$header'                => $header,
177                                                 '$str_notification_type' => $this->t('Notification type:'),
178                                                 '$str_type'              => $Introduction->getType(),
179                                                 '$dfrn_id'               => $Introduction->getDfrnId(),
180                                                 '$uid'                   => $Introduction->getUid(),
181                                                 '$intro_id'              => $Introduction->getIntroId(),
182                                                 '$contact_id'            => $Introduction->getContactId(),
183                                                 '$photo'                 => $Introduction->getPhoto(),
184                                                 '$fullname'              => $Introduction->getName(),
185                                                 '$location'              => $Introduction->getLocation(),
186                                                 '$lbl_location'          => $this->t('Location:'),
187                                                 '$about'                 => $Introduction->getAbout(),
188                                                 '$lbl_about'             => $this->t('About:'),
189                                                 '$keywords'              => $Introduction->getKeywords(),
190                                                 '$lbl_keywords'          => $this->t('Tags:'),
191                                                 '$hidden'                => ['hidden', $this->t('Hide this contact from others'), $Introduction->isHidden(), ''],
192                                                 '$lbl_connection_type'   => $helptext,
193                                                 '$friend'                => $friend,
194                                                 '$follower'              => $follower,
195                                                 '$url'                   => $Introduction->getUrl(),
196                                                 '$zrl'                   => $Introduction->getZrl(),
197                                                 '$lbl_url'               => $this->t('Profile URL'),
198                                                 '$addr'                  => $Introduction->getAddr(),
199                                                 '$lbl_knowyou'           => $lbl_knowyou,
200                                                 '$lbl_network'           => $this->t('Network:'),
201                                                 '$network'               => ContactSelector::networkToName($Introduction->getNetwork(), $Introduction->getUrl()),
202                                                 '$knowyou'               => $knowyou,
203                                                 '$approve'               => $this->t('Approve'),
204                                                 '$note'                  => $Introduction->getNote(),
205                                                 '$ignore'                => $this->t('Ignore'),
206                                                 '$discard'               => $discard,
207                                                 '$action'                => $action,
208                                                 '$is_mobile'             => $this->mode->isMobile(),
209                                         ]);
210                                         break;
211                         }
212                 }
213
214                 if (count($notifications['notifications']) == 0) {
215                         DI::sysmsg()->addNotice($this->t('No introductions.'));
216                         $notificationNoContent = $this->t('No more %s notifications.', $notifications['ident']);
217                 }
218
219                 return $this->printContent($notificationHeader, $notificationContent, $notificationNoContent, $notificationShowLink);
220         }
221 }