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