3 * @file mod/notifications.php
4 * @brief The notifications module
8 use Friendica\Content\ContactSelector;
9 use Friendica\Content\Nav;
10 use Friendica\Content\Pager;
11 use Friendica\Core\L10n;
12 use Friendica\Core\Protocol;
13 use Friendica\Core\Renderer;
14 use Friendica\Core\System;
15 use Friendica\Database\DBA;
17 use Friendica\Module\Security\Login;
18 use Friendica\Model\Contact;
19 use Friendica\Model\Introduction;
21 function notifications_post(App $a)
24 DI::baseUrl()->redirect();
27 $request_id = (($a->argc > 1) ? $a->argv[1] : 0);
29 if ($request_id === 'all') {
34 /** @var Introduction $Intro */
35 $Intro = \Friendica\BaseObject::getClass(Introduction::class);
36 $Intro->fetch(['id' => $request_id, 'uid' => local_user()]);
38 switch ($_POST['submit']) {
39 case L10n::t('Discard'):
42 case L10n::t('Ignore'):
47 DI::baseUrl()->redirect('notifications/intros');
51 function notifications_content(App $a)
54 notice(L10n::t('Permission denied.') . EOL);
58 $page = ($_REQUEST['page'] ?? 0) ?: 1;
59 $show = ($_REQUEST['show'] ?? '') === 'all';
61 Nav::setSelected('notifications');
63 $json = (($a->argc > 1 && $a->argv[$a->argc - 1] === 'json') ? true : false);
68 // Get the nav tabs for the notification pages
69 $tabs = $nm->getTabs();
71 $notif_nocontent = '';
73 // Notification results per page
75 $startrec = ($page * $perpage) - $perpage;
77 $notif_header = L10n::t('Notifications');
82 if ((($a->argc > 1) && ($a->argv[1] == 'intros')) || (($a->argc == 1))) {
83 Nav::setSelected('introductions');
86 if (!empty($a->argv[2]) && intval($a->argv[2]) != 0) {
87 $id = (int)$a->argv[2];
90 $all = (($a->argc > 2) && ($a->argv[2] == 'all'));
92 $notifs = $nm->getIntroList($all, $startrec, $perpage, $id);
94 // Get the network notifications
95 } elseif (($a->argc > 1) && ($a->argv[1] == 'network')) {
96 $notif_header = L10n::t('Network Notifications');
97 $notifs = $nm->getNetworkList($show, $startrec, $perpage);
99 // Get the system notifications
100 } elseif (($a->argc > 1) && ($a->argv[1] == 'system')) {
101 $notif_header = L10n::t('System Notifications');
102 $notifs = $nm->getSystemList($show, $startrec, $perpage);
104 // Get the personal notifications
105 } elseif (($a->argc > 1) && ($a->argv[1] == 'personal')) {
106 $notif_header = L10n::t('Personal Notifications');
107 $notifs = $nm->getPersonalList($show, $startrec, $perpage);
109 // Get the home notifications
110 } elseif (($a->argc > 1) && ($a->argv[1] == 'home')) {
111 $notif_header = L10n::t('Home Notifications');
112 $notifs = $nm->getHomeList($show, $startrec, $perpage);
113 // fallback - redirect to main page
115 DI::baseUrl()->redirect('notifications');
119 $pager = new Pager($a->query_string, $perpage);
121 // Add additional informations (needed for json output)
122 $notifs['items_page'] = $pager->getItemsPerPage();
123 $notifs['page'] = $pager->getPage();
126 if (intval($json) === 1) {
127 System::jsonExit($notifs);
130 $notif_tpl = Renderer::getMarkupTemplate('notifications.tpl');
133 'href' => ($show ? 'notifications/' . $notifs['ident'] : 'notifications/' . $notifs['ident'] . '?show=all' ),
134 'text' => ($show ? L10n::t('Show unread') : L10n::t('Show all')),
137 // Process the data for template creation
138 if (($notifs['ident'] ?? '') == 'introductions') {
139 $sugg = Renderer::getMarkupTemplate('suggestions.tpl');
140 $tpl = Renderer::getMarkupTemplate('intros.tpl');
142 // The link to switch between ignored and normal connection requests
144 'href' => (!$all ? 'notifications/intros/all' : 'notifications/intros' ),
145 'text' => (!$all ? L10n::t('Show Ignored Requests') : L10n::t('Hide Ignored Requests'))
148 // Loop through all introduction notifications.This creates an array with the output html for each
150 foreach ($notifs['notifications'] as $notif) {
152 // There are two kind of introduction. Contacts suggested by other contacts and normal connection requests.
153 // We have to distinguish between these two because they use different data.
154 switch ($notif['label']) {
155 case 'friend_suggestion':
156 $notif_content[] = Renderer::replaceMacros($sugg, [
157 '$type' => $notif['label'],
158 '$str_notifytype' => L10n::t('Notification type:'),
159 '$notify_type'=> $notif['notify_type'],
160 '$intro_id' => $notif['intro_id'],
161 '$lbl_madeby' => L10n::t('Suggested by:'),
162 '$madeby' => $notif['madeby'],
163 '$madeby_url' => $notif['madeby_url'],
164 '$madeby_zrl' => $notif['madeby_zrl'],
165 '$madeby_addr'=> $notif['madeby_addr'],
166 '$contact_id' => $notif['contact_id'],
167 '$photo' => $notif['photo'],
168 '$fullname' => $notif['name'],
169 '$url' => $notif['url'],
170 '$zrl' => $notif['zrl'],
171 '$lbl_url' => L10n::t('Profile URL'),
172 '$addr' => $notif['addr'],
173 '$hidden' => ['hidden', L10n::t('Hide this contact from others'), ($notif['hidden'] == 1), ''],
174 '$knowyou' => $notif['knowyou'],
175 '$approve' => L10n::t('Approve'),
176 '$note' => $notif['note'],
177 '$request' => $notif['request'],
178 '$ignore' => L10n::t('Ignore'),
179 '$discard' => L10n::t('Discard'),
183 // Normal connection requests
185 $friend_selected = (($notif['network'] !== Protocol::OSTATUS) ? ' checked="checked" ' : ' disabled ');
186 $fan_selected = (($notif['network'] === Protocol::OSTATUS) ? ' checked="checked" disabled ' : '');
194 if ($notif['network'] === Protocol::DFRN) {
195 $lbl_knowyou = L10n::t('Claims to be known to you: ');
196 $knowyou = (($notif['knowyou']) ? L10n::t('yes') : L10n::t('no'));
197 $helptext = L10n::t('Shall your connection be bidirectional or not?');
198 $helptext2 = 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.', $notif['name'], $notif['name']);
199 $helptext3 = 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.', $notif['name']);
200 } elseif ($notif['network'] === Protocol::DIASPORA) {
201 $helptext = L10n::t('Shall your connection be bidirectional or not?');
202 $helptext2 = 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.', $notif['name'], $notif['name']);
203 $helptext3 = L10n::t('Accepting %s as a sharer allows them to subscribe to your posts, but you will not receive updates from them in your news feed.', $notif['name']);
206 $dfrn_tpl = Renderer::getMarkupTemplate('netfriend.tpl');
207 $dfrn_text = Renderer::replaceMacros($dfrn_tpl, [
208 '$intro_id' => $notif['intro_id'],
209 '$friend_selected' => $friend_selected,
210 '$fan_selected'=> $fan_selected,
211 '$approve_as1' => $helptext,
212 '$approve_as2' => $helptext2,
213 '$approve_as3' => $helptext3,
214 '$as_friend' => L10n::t('Friend'),
215 '$as_fan' => (($notif['network'] == Protocol::DIASPORA) ? L10n::t('Sharer') : L10n::t('Subscriber'))
218 $contact = DBA::selectFirst('contact', ['network', 'protocol'], ['id' => $notif['contact_id']]);
220 if (($contact['network'] != Protocol::DFRN) || ($contact['protocol'] == Protocol::ACTIVITYPUB)) {
221 $action = 'follow_confirm';
223 $action = 'dfrn_confirm';
226 $header = $notif['name'];
228 if ($notif['addr'] != '') {
229 $header .= ' <' . $notif['addr'] . '>';
232 $header .= ' (' . ContactSelector::networkToName($notif['network'], $notif['url']) . ')';
234 if ($notif['network'] != Protocol::DIASPORA) {
235 $discard = L10n::t('Discard');
240 $notif_content[] = Renderer::replaceMacros($tpl, [
241 '$type' => $notif['label'],
242 '$header' => $header,
243 '$str_notifytype' => L10n::t('Notification type:'),
244 '$notify_type' => $notif['notify_type'],
245 '$dfrn_text' => $dfrn_text,
246 '$dfrn_id' => $notif['dfrn_id'],
247 '$uid' => $notif['uid'],
248 '$intro_id' => $notif['intro_id'],
249 '$contact_id' => $notif['contact_id'],
250 '$photo' => $notif['photo'],
251 '$fullname' => $notif['name'],
252 '$location' => $notif['location'],
253 '$lbl_location'=> L10n::t('Location:'),
254 '$about' => $notif['about'],
255 '$lbl_about' => L10n::t('About:'),
256 '$keywords' => $notif['keywords'],
257 '$lbl_keywords'=> L10n::t('Tags:'),
258 '$gender' => $notif['gender'],
259 '$lbl_gender' => L10n::t('Gender:'),
260 '$hidden' => ['hidden', L10n::t('Hide this contact from others'), ($notif['hidden'] == 1), ''],
261 '$url' => $notif['url'],
262 '$zrl' => $notif['zrl'],
263 '$lbl_url' => L10n::t('Profile URL'),
264 '$addr' => $notif['addr'],
265 '$lbl_knowyou' => $lbl_knowyou,
266 '$lbl_network' => L10n::t('Network:'),
267 '$network' => ContactSelector::networkToName($notif['network'], $notif['url']),
268 '$knowyou' => $knowyou,
269 '$approve' => L10n::t('Approve'),
270 '$note' => $notif['note'],
271 '$ignore' => L10n::t('Ignore'),
272 '$discard' => $discard,
273 '$action' => $action,
279 if (count($notifs['notifications']) == 0) {
280 info(L10n::t('No introductions.') . EOL);
283 // Normal notifications (no introductions)
284 } elseif (!empty($notifs['notifications'])) {
285 // Loop trough ever notification This creates an array with the output html for each
286 // notification and apply the correct template according to the notificationtype (label).
287 foreach ($notifs['notifications'] as $notif) {
288 $notification_templates = [
289 'like' => 'notifications_likes_item.tpl',
290 'dislike' => 'notifications_dislikes_item.tpl',
291 'attend' => 'notifications_attend_item.tpl',
292 'attendno' => 'notifications_attend_item.tpl',
293 'attendmaybe' => 'notifications_attend_item.tpl',
294 'friend' => 'notifications_friends_item.tpl',
295 'comment' => 'notifications_comments_item.tpl',
296 'post' => 'notifications_posts_item.tpl',
297 'notify' => 'notify.tpl',
300 $tpl_notif = Renderer::getMarkupTemplate($notification_templates[$notif['label']]);
302 $notif_content[] = Renderer::replaceMacros($tpl_notif, [
303 '$item_label' => $notif['label'],
304 '$item_link' => $notif['link'],
305 '$item_image' => $notif['image'],
306 '$item_url' => $notif['url'],
307 '$item_text' => $notif['text'],
308 '$item_when' => $notif['when'],
309 '$item_ago' => $notif['ago'],
310 '$item_seen' => $notif['seen'],
314 $notif_nocontent = L10n::t('No more %s notifications.', $notifs['ident']);
317 $o .= Renderer::replaceMacros($notif_tpl, [
318 '$notif_header' => $notif_header,
320 '$notif_content' => $notif_content,
321 '$notif_nocontent' => $notif_nocontent,
322 '$notif_show_lnk' => $notif_show_lnk,
323 '$notif_paginate' => $pager->renderMinimal(count($notif_content))