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;
20 use Friendica\Model\Notify;
22 function notifications_post(App $a)
25 $a->internalRedirect();
28 $request_id = (($a->argc > 1) ? $a->argv[1] : 0);
30 if ($request_id === 'all') {
35 /** @var Introduction $Intro */
36 $Intro = \Friendica\BaseObject::getClass(Introduction::class);
37 $Intro->fetch(['id' => $request_id, 'uid' => local_user()]);
39 switch ($_POST['submit']) {
40 case L10n::t('Discard'):
43 case L10n::t('Ignore'):
48 $a->internalRedirect('notifications/intros');
52 function notifications_content(App $a)
55 notice(L10n::t('Permission denied.') . EOL);
59 $page = ($_REQUEST['page'] ?? 0) ?: 1;
60 $show = ($_REQUEST['show'] ?? '') === 'all';
62 Nav::setSelected('notifications');
64 $json = (($a->argc > 1 && $a->argv[$a->argc - 1] === 'json') ? true : false);
69 // Get the nav tabs for the notification pages
70 $tabs = $nm->getTabs();
72 $notif_nocontent = '';
74 // Notification results per page
76 $startrec = ($page * $perpage) - $perpage;
78 $notif_header = L10n::t('Notifications');
83 if ((($a->argc > 1) && ($a->argv[1] == 'intros')) || (($a->argc == 1))) {
84 Nav::setSelected('introductions');
87 if (!empty($a->argv[2]) && intval($a->argv[2]) != 0) {
88 $id = (int)$a->argv[2];
91 $all = (($a->argc > 2) && ($a->argv[2] == 'all'));
93 $notifs = $nm->getIntroList($all, $startrec, $perpage, $id);
95 // Get the network notifications
96 } elseif (($a->argc > 1) && ($a->argv[1] == 'network')) {
97 $notif_header = L10n::t('Network Notifications');
98 $notifs = $nm->getNetworkList($show, $startrec, $perpage);
100 // Get the system notifications
101 } elseif (($a->argc > 1) && ($a->argv[1] == 'system')) {
102 $notif_header = L10n::t('System Notifications');
103 $notifs = $nm->getSystemList($show, $startrec, $perpage);
105 // Get the personal notifications
106 } elseif (($a->argc > 1) && ($a->argv[1] == 'personal')) {
107 $notif_header = L10n::t('Personal Notifications');
108 $notifs = $nm->getPersonalList($show, $startrec, $perpage);
110 // Get the home notifications
111 } elseif (($a->argc > 1) && ($a->argv[1] == 'home')) {
112 $notif_header = L10n::t('Home Notifications');
113 $notifs = $nm->getHomeList($show, $startrec, $perpage);
114 // fallback - redirect to main page
116 $a->internalRedirect('notifications');
120 $pager = new Pager($a->query_string, $perpage);
122 // Add additional informations (needed for json output)
123 $notifs['items_page'] = $pager->getItemsPerPage();
124 $notifs['page'] = $pager->getPage();
127 if (intval($json) === 1) {
128 System::jsonExit($notifs);
131 $notif_tpl = Renderer::getMarkupTemplate('notifications.tpl');
134 'href' => ($show ? 'notifications/' . $notifs['ident'] : 'notifications/' . $notifs['ident'] . '?show=all' ),
135 'text' => ($show ? L10n::t('Show unread') : L10n::t('Show all')),
138 // Process the data for template creation
139 if (($notifs['ident'] ?? '') == 'introductions') {
140 $sugg = Renderer::getMarkupTemplate('suggestions.tpl');
141 $tpl = Renderer::getMarkupTemplate('intros.tpl');
143 // The link to switch between ignored and normal connection requests
145 'href' => (!$all ? 'notifications/intros/all' : 'notifications/intros' ),
146 'text' => (!$all ? L10n::t('Show Ignored Requests') : L10n::t('Hide Ignored Requests'))
149 // Loop through all introduction notifications.This creates an array with the output html for each
151 foreach ($notifs['notifications'] as $notif) {
153 // There are two kind of introduction. Contacts suggested by other contacts and normal connection requests.
154 // We have to distinguish between these two because they use different data.
155 switch ($notif['label']) {
156 case 'friend_suggestion':
157 $notif_content[] = Renderer::replaceMacros($sugg, [
158 '$type' => $notif['label'],
159 '$str_notifytype' => L10n::t('Notification type:'),
160 '$notify_type'=> $notif['notify_type'],
161 '$intro_id' => $notif['intro_id'],
162 '$lbl_madeby' => L10n::t('Suggested by:'),
163 '$madeby' => $notif['madeby'],
164 '$madeby_url' => $notif['madeby_url'],
165 '$madeby_zrl' => $notif['madeby_zrl'],
166 '$madeby_addr'=> $notif['madeby_addr'],
167 '$contact_id' => $notif['contact_id'],
168 '$photo' => $notif['photo'],
169 '$fullname' => $notif['name'],
170 '$url' => $notif['url'],
171 '$zrl' => $notif['zrl'],
172 '$lbl_url' => L10n::t('Profile URL'),
173 '$addr' => $notif['addr'],
174 '$hidden' => ['hidden', L10n::t('Hide this contact from others'), ($notif['hidden'] == 1), ''],
175 '$knowyou' => $notif['knowyou'],
176 '$approve' => L10n::t('Approve'),
177 '$note' => $notif['note'],
178 '$request' => $notif['request'],
179 '$ignore' => L10n::t('Ignore'),
180 '$discard' => L10n::t('Discard'),
184 // Normal connection requests
186 $friend_selected = (($notif['network'] !== Protocol::OSTATUS) ? ' checked="checked" ' : ' disabled ');
187 $fan_selected = (($notif['network'] === Protocol::OSTATUS) ? ' checked="checked" disabled ' : '');
195 if ($notif['network'] === Protocol::DFRN) {
196 $lbl_knowyou = L10n::t('Claims to be known to you: ');
197 $knowyou = (($notif['knowyou']) ? L10n::t('yes') : L10n::t('no'));
198 $helptext = L10n::t('Shall your connection be bidirectional or not?');
199 $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']);
200 $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']);
201 } elseif ($notif['network'] === Protocol::DIASPORA) {
202 $helptext = L10n::t('Shall your connection be bidirectional or not?');
203 $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']);
204 $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']);
207 $dfrn_tpl = Renderer::getMarkupTemplate('netfriend.tpl');
208 $dfrn_text = Renderer::replaceMacros($dfrn_tpl, [
209 '$intro_id' => $notif['intro_id'],
210 '$friend_selected' => $friend_selected,
211 '$fan_selected'=> $fan_selected,
212 '$approve_as1' => $helptext,
213 '$approve_as2' => $helptext2,
214 '$approve_as3' => $helptext3,
215 '$as_friend' => L10n::t('Friend'),
216 '$as_fan' => (($notif['network'] == Protocol::DIASPORA) ? L10n::t('Sharer') : L10n::t('Subscriber'))
219 $contact = DBA::selectFirst('contact', ['network', 'protocol'], ['id' => $notif['contact_id']]);
221 if (($contact['network'] != Protocol::DFRN) || ($contact['protocol'] == Protocol::ACTIVITYPUB)) {
222 $action = 'follow_confirm';
224 $action = 'dfrn_confirm';
227 $header = $notif['name'];
229 if ($notif['addr'] != '') {
230 $header .= ' <' . $notif['addr'] . '>';
233 $header .= ' (' . ContactSelector::networkToName($notif['network'], $notif['url']) . ')';
235 if ($notif['network'] != Protocol::DIASPORA) {
236 $discard = L10n::t('Discard');
241 $notif_content[] = Renderer::replaceMacros($tpl, [
242 '$type' => $notif['label'],
243 '$header' => $header,
244 '$str_notifytype' => L10n::t('Notification type:'),
245 '$notify_type' => $notif['notify_type'],
246 '$dfrn_text' => $dfrn_text,
247 '$dfrn_id' => $notif['dfrn_id'],
248 '$uid' => $notif['uid'],
249 '$intro_id' => $notif['intro_id'],
250 '$contact_id' => $notif['contact_id'],
251 '$photo' => $notif['photo'],
252 '$fullname' => $notif['name'],
253 '$location' => $notif['location'],
254 '$lbl_location'=> L10n::t('Location:'),
255 '$about' => $notif['about'],
256 '$lbl_about' => L10n::t('About:'),
257 '$keywords' => $notif['keywords'],
258 '$lbl_keywords'=> L10n::t('Tags:'),
259 '$gender' => $notif['gender'],
260 '$lbl_gender' => L10n::t('Gender:'),
261 '$hidden' => ['hidden', L10n::t('Hide this contact from others'), ($notif['hidden'] == 1), ''],
262 '$url' => $notif['url'],
263 '$zrl' => $notif['zrl'],
264 '$lbl_url' => L10n::t('Profile URL'),
265 '$addr' => $notif['addr'],
266 '$lbl_knowyou' => $lbl_knowyou,
267 '$lbl_network' => L10n::t('Network:'),
268 '$network' => ContactSelector::networkToName($notif['network'], $notif['url']),
269 '$knowyou' => $knowyou,
270 '$approve' => L10n::t('Approve'),
271 '$note' => $notif['note'],
272 '$ignore' => L10n::t('Ignore'),
273 '$discard' => $discard,
274 '$action' => $action,
280 if (count($notifs['notifications']) == 0) {
281 info(L10n::t('No introductions.') . EOL);
284 // Normal notifications (no introductions)
285 } elseif (!empty($notifs['notifications'])) {
286 // Loop trough ever notification This creates an array with the output html for each
287 // notification and apply the correct template according to the notificationtype (label).
288 foreach ($notifs['notifications'] as $notif) {
289 $notification_templates = [
290 'like' => 'notifications_likes_item.tpl',
291 'dislike' => 'notifications_dislikes_item.tpl',
292 'attend' => 'notifications_attend_item.tpl',
293 'attendno' => 'notifications_attend_item.tpl',
294 'attendmaybe' => 'notifications_attend_item.tpl',
295 'friend' => 'notifications_friends_item.tpl',
296 'comment' => 'notifications_comments_item.tpl',
297 'post' => 'notifications_posts_item.tpl',
298 'notify' => 'notify.tpl',
301 $tpl_notif = Renderer::getMarkupTemplate($notification_templates[$notif['label']]);
303 $notif_content[] = Renderer::replaceMacros($tpl_notif, [
304 '$item_label' => $notif['label'],
305 '$item_link' => $notif['link'],
306 '$item_image' => $notif['image'],
307 '$item_url' => $notif['url'],
308 '$item_text' => $notif['text'],
309 '$item_when' => $notif['when'],
310 '$item_ago' => $notif['ago'],
311 '$item_seen' => $notif['seen'],
315 $notif_nocontent = L10n::t('No more %s notifications.', $notifs['ident']);
318 $o .= Renderer::replaceMacros($notif_tpl, [
319 '$notif_header' => $notif_header,
321 '$notif_content' => $notif_content,
322 '$notif_nocontent' => $notif_nocontent,
323 '$notif_show_lnk' => $notif_show_lnk,
324 '$notif_paginate' => $pager->renderMinimal(count($notif_content))