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\NotificationsManager;
13 use Friendica\Core\Protocol;
14 use Friendica\Core\Renderer;
15 use Friendica\Core\System;
16 use Friendica\Database\DBA;
17 use Friendica\Module\Login;
19 function notifications_post(App $a)
22 $a->internalRedirect();
25 $request_id = (($a->argc > 1) ? $a->argv[1] : 0);
27 if ($request_id === 'all') {
32 $intro = DBA::selectFirst('intro', ['id', 'contact-id', 'fid'], ['id' => $request_id, 'uid' => local_user()]);
34 if (DBA::isResult($intro)) {
35 $intro_id = $intro['id'];
36 $contact_id = $intro['contact-id'];
38 notice(L10n::t('Invalid request identifier.') . EOL);
42 // If it is a friend suggestion, the contact is not a new friend but an existing friend
43 // that should not be deleted.
47 if ($_POST['submit'] == L10n::t('Discard')) {
48 DBA::delete('intro', ['id' => $intro_id]);
51 // The check for blocked and pending is in case the friendship was already approved
52 // and we just want to get rid of the now pointless notification
53 $condition = ['id' => $contact_id, 'uid' => local_user(),
54 'self' => false, 'blocked' => true, 'pending' => true];
55 DBA::delete('contact', $condition);
57 $a->internalRedirect('notifications/intros');
60 if ($_POST['submit'] == L10n::t('Ignore')) {
61 DBA::update('intro', ['ignore' => true], ['id' => $intro_id]);
62 $a->internalRedirect('notifications/intros');
67 function notifications_content(App $a)
70 notice(L10n::t('Permission denied.') . EOL);
74 $page = defaults($_REQUEST, 'page', 1);
75 $show = defaults($_REQUEST, 'show', 0);
77 Nav::setSelected('notifications');
79 $json = (($a->argc > 1 && $a->argv[$a->argc - 1] === 'json') ? true : false);
81 $nm = new NotificationsManager();
84 // Get the nav tabs for the notification pages
85 $tabs = $nm->getTabs();
87 $notif_nocontent = '';
89 // Notification results per page
91 $startrec = ($page * $perpage) - $perpage;
93 $notif_header = L10n::t('Notifications');
98 if ((($a->argc > 1) && ($a->argv[1] == 'intros')) || (($a->argc == 1))) {
99 Nav::setSelected('introductions');
102 if (!empty($a->argv[2]) && intval($a->argv[2]) != 0) {
103 $id = (int)$a->argv[2];
106 $all = (($a->argc > 2) && ($a->argv[2] == 'all'));
108 $notifs = $nm->introNotifs($all, $startrec, $perpage, $id);
110 // Get the network notifications
111 } elseif (($a->argc > 1) && ($a->argv[1] == 'network')) {
112 $notif_header = L10n::t('Network Notifications');
113 $notifs = $nm->networkNotifs($show, $startrec, $perpage);
115 // Get the system notifications
116 } elseif (($a->argc > 1) && ($a->argv[1] == 'system')) {
117 $notif_header = L10n::t('System Notifications');
118 $notifs = $nm->systemNotifs($show, $startrec, $perpage);
120 // Get the personal notifications
121 } elseif (($a->argc > 1) && ($a->argv[1] == 'personal')) {
122 $notif_header = L10n::t('Personal Notifications');
123 $notifs = $nm->personalNotifs($show, $startrec, $perpage);
125 // Get the home notifications
126 } elseif (($a->argc > 1) && ($a->argv[1] == 'home')) {
127 $notif_header = L10n::t('Home Notifications');
128 $notifs = $nm->homeNotifs($show, $startrec, $perpage);
129 // fallback - redirect to main page
131 $a->internalRedirect('notifications');
135 $pager = new Pager($a->query_string, $perpage);
137 // Add additional informations (needed for json output)
138 $notifs['items_page'] = $pager->getItemsPerPage();
139 $notifs['page'] = $pager->getPage();
142 if (intval($json) === 1) {
143 System::jsonExit($notifs);
146 $notif_tpl = Renderer::getMarkupTemplate('notifications.tpl');
149 'href' => ($show ? 'notifications/' . $notifs['ident'] : 'notifications/' . $notifs['ident'] . '?show=all' ),
150 'text' => ($show ? L10n::t('Show unread') : L10n::t('Show all')),
153 // Process the data for template creation
154 if (defaults($notifs, 'ident', '') === 'introductions') {
155 $sugg = Renderer::getMarkupTemplate('suggestions.tpl');
156 $tpl = Renderer::getMarkupTemplate('intros.tpl');
158 // The link to switch between ignored and normal connection requests
160 'href' => (!$all ? 'notifications/intros/all' : 'notifications/intros' ),
161 'text' => (!$all ? L10n::t('Show Ignored Requests') : L10n::t('Hide Ignored Requests'))
164 // Loop through all introduction notifications.This creates an array with the output html for each
166 foreach ($notifs['notifications'] as $notif) {
168 // There are two kind of introduction. Contacts suggested by other contacts and normal connection requests.
169 // We have to distinguish between these two because they use different data.
170 switch ($notif['label']) {
171 case 'friend_suggestion':
172 $notif_content[] = Renderer::replaceMacros($sugg, [
173 '$type' => $notif['label'],
174 '$str_notifytype' => L10n::t('Notification type:'),
175 '$notify_type'=> $notif['notify_type'],
176 '$intro_id' => $notif['intro_id'],
177 '$lbl_madeby' => L10n::t('Suggested by:'),
178 '$madeby' => $notif['madeby'],
179 '$madeby_url' => $notif['madeby_url'],
180 '$madeby_zrl' => $notif['madeby_zrl'],
181 '$madeby_addr'=> $notif['madeby_addr'],
182 '$contact_id' => $notif['contact_id'],
183 '$photo' => $notif['photo'],
184 '$fullname' => $notif['name'],
185 '$url' => $notif['url'],
186 '$zrl' => $notif['zrl'],
187 '$lbl_url' => L10n::t('Profile URL'),
188 '$addr' => $notif['addr'],
189 '$hidden' => ['hidden', L10n::t('Hide this contact from others'), ($notif['hidden'] == 1), ''],
190 '$knowyou' => $notif['knowyou'],
191 '$approve' => L10n::t('Approve'),
192 '$note' => $notif['note'],
193 '$request' => $notif['request'],
194 '$ignore' => L10n::t('Ignore'),
195 '$discard' => L10n::t('Discard'),
199 // Normal connection requests
201 $friend_selected = (($notif['network'] !== Protocol::OSTATUS) ? ' checked="checked" ' : ' disabled ');
202 $fan_selected = (($notif['network'] === Protocol::OSTATUS) ? ' checked="checked" disabled ' : '');
210 if ($notif['network'] === Protocol::DFRN) {
211 $lbl_knowyou = L10n::t('Claims to be known to you: ');
212 $knowyou = (($notif['knowyou']) ? L10n::t('yes') : L10n::t('no'));
213 $helptext = L10n::t('Shall your connection be bidirectional or not?');
214 $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']);
215 $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']);
216 } elseif ($notif['network'] === Protocol::DIASPORA) {
217 $helptext = L10n::t('Shall your connection be bidirectional or not?');
218 $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']);
219 $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']);
222 $dfrn_tpl = Renderer::getMarkupTemplate('netfriend.tpl');
223 $dfrn_text = Renderer::replaceMacros($dfrn_tpl, [
224 '$intro_id' => $notif['intro_id'],
225 '$friend_selected' => $friend_selected,
226 '$fan_selected'=> $fan_selected,
227 '$approve_as1' => $helptext,
228 '$approve_as2' => $helptext2,
229 '$approve_as3' => $helptext3,
230 '$as_friend' => L10n::t('Friend'),
231 '$as_fan' => (($notif['network'] == Protocol::DIASPORA) ? L10n::t('Sharer') : L10n::t('Subscriber'))
234 $contact = DBA::selectFirst('contact', ['network', 'protocol'], ['id' => $notif['contact_id']]);
236 if (($contact['network'] != Protocol::DFRN) || ($contact['protocol'] == Protocol::ACTIVITYPUB)) {
237 $action = 'follow_confirm';
239 $action = 'dfrn_confirm';
242 $header = $notif['name'];
244 if ($notif['addr'] != '') {
245 $header .= ' <' . $notif['addr'] . '>';
248 $header .= ' (' . ContactSelector::networkToName($notif['network'], $notif['url']) . ')';
250 if ($notif['network'] != Protocol::DIASPORA) {
251 $discard = L10n::t('Discard');
256 $notif_content[] = Renderer::replaceMacros($tpl, [
257 '$type' => $notif['label'],
258 '$header' => $header,
259 '$str_notifytype' => L10n::t('Notification type:'),
260 '$notify_type' => $notif['notify_type'],
261 '$dfrn_text' => $dfrn_text,
262 '$dfrn_id' => $notif['dfrn_id'],
263 '$uid' => $notif['uid'],
264 '$intro_id' => $notif['intro_id'],
265 '$contact_id' => $notif['contact_id'],
266 '$photo' => $notif['photo'],
267 '$fullname' => $notif['name'],
268 '$location' => $notif['location'],
269 '$lbl_location'=> L10n::t('Location:'),
270 '$about' => $notif['about'],
271 '$lbl_about' => L10n::t('About:'),
272 '$keywords' => $notif['keywords'],
273 '$lbl_keywords'=> L10n::t('Tags:'),
274 '$gender' => $notif['gender'],
275 '$lbl_gender' => L10n::t('Gender:'),
276 '$hidden' => ['hidden', L10n::t('Hide this contact from others'), ($notif['hidden'] == 1), ''],
277 '$url' => $notif['url'],
278 '$zrl' => $notif['zrl'],
279 '$lbl_url' => L10n::t('Profile URL'),
280 '$addr' => $notif['addr'],
281 '$lbl_knowyou' => $lbl_knowyou,
282 '$lbl_network' => L10n::t('Network:'),
283 '$network' => ContactSelector::networkToName($notif['network'], $notif['url']),
284 '$knowyou' => $knowyou,
285 '$approve' => L10n::t('Approve'),
286 '$note' => $notif['note'],
287 '$ignore' => L10n::t('Ignore'),
288 '$discard' => $discard,
289 '$action' => $action,
295 if (count($notifs['notifications']) == 0) {
296 info(L10n::t('No introductions.') . EOL);
299 // Normal notifications (no introductions)
300 } elseif (!empty($notifs['notifications'])) {
301 // Loop trough ever notification This creates an array with the output html for each
302 // notification and apply the correct template according to the notificationtype (label).
303 foreach ($notifs['notifications'] as $notif) {
304 $notification_templates = [
305 'like' => 'notifications_likes_item.tpl',
306 'dislike' => 'notifications_dislikes_item.tpl',
307 'attend' => 'notifications_attend_item.tpl',
308 'attendno' => 'notifications_attend_item.tpl',
309 'attendmaybe' => 'notifications_attend_item.tpl',
310 'friend' => 'notifications_friends_item.tpl',
311 'comment' => 'notifications_comments_item.tpl',
312 'post' => 'notifications_posts_item.tpl',
313 'notify' => 'notify.tpl',
316 $tpl_notif = Renderer::getMarkupTemplate($notification_templates[$notif['label']]);
318 $notif_content[] = Renderer::replaceMacros($tpl_notif, [
319 '$item_label' => $notif['label'],
320 '$item_link' => $notif['link'],
321 '$item_image' => $notif['image'],
322 '$item_url' => $notif['url'],
323 '$item_text' => $notif['text'],
324 '$item_when' => $notif['when'],
325 '$item_ago' => $notif['ago'],
326 '$item_seen' => $notif['seen'],
330 $notif_nocontent = L10n::t('No more %s notifications.', $notifs['ident']);
333 $o .= Renderer::replaceMacros($notif_tpl, [
334 '$notif_header' => $notif_header,
336 '$notif_content' => $notif_content,
337 '$notif_nocontent' => $notif_nocontent,
338 '$notif_show_lnk' => $notif_show_lnk,
339 '$notif_paginate' => $pager->renderMinimal(count($notif_content))