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\Logger;
16 use Friendica\Core\System;
17 use Friendica\Database\DBA;
18 use Friendica\Module\Login;
19 use Friendica\Model\Contact;
21 function notifications_post(App $a)
24 $a->internalRedirect();
27 $request_id = (($a->argc > 1) ? $a->argv[1] : 0);
29 if ($request_id === 'all') {
34 $intro = DBA::selectFirst('intro', ['id', 'contact-id', 'fid'], ['id' => $request_id, 'uid' => local_user()]);
36 if (DBA::isResult($intro)) {
37 $intro_id = $intro['id'];
38 $contact_id = $intro['contact-id'];
40 notice(L10n::t('Invalid request identifier.') . EOL);
44 // If it is a friend suggestion, the contact is not a new friend but an existing friend
45 // that should not be deleted.
49 if ($_POST['submit'] == L10n::t('Discard')) {
50 DBA::delete('intro', ['id' => $intro_id]);
52 // The check for pending is in case the friendship was already approved
53 // and we just want to get rid of the pending contact
54 $condition = ['id' => $contact_id, 'uid' => local_user(),
55 'self' => false, 'pending' => true, 'rel' => Contact::FOLLOWER];
56 if (DBA::exists('contact', $condition)) {
57 Contact::remove($contact_id);
60 $a->internalRedirect('notifications/intros');
63 if ($_POST['submit'] == L10n::t('Ignore')) {
64 DBA::update('intro', ['ignore' => true], ['id' => $intro_id]);
65 $a->internalRedirect('notifications/intros');
70 function notifications_content(App $a)
73 notice(L10n::t('Permission denied.') . EOL);
77 $page = defaults($_REQUEST, 'page', 1);
78 $show = defaults($_REQUEST, 'show', 0);
80 Nav::setSelected('notifications');
82 $json = (($a->argc > 1 && $a->argv[$a->argc - 1] === 'json') ? true : false);
84 $nm = new NotificationsManager();
87 // Get the nav tabs for the notification pages
88 $tabs = $nm->getTabs();
90 $notif_nocontent = '';
92 // Notification results per page
94 $startrec = ($page * $perpage) - $perpage;
96 $notif_header = L10n::t('Notifications');
101 if ((($a->argc > 1) && ($a->argv[1] == 'intros')) || (($a->argc == 1))) {
102 Nav::setSelected('introductions');
105 if (!empty($a->argv[2]) && intval($a->argv[2]) != 0) {
106 $id = (int)$a->argv[2];
109 $all = (($a->argc > 2) && ($a->argv[2] == 'all'));
111 $notifs = $nm->introNotifs($all, $startrec, $perpage, $id);
113 // Get the network notifications
114 } elseif (($a->argc > 1) && ($a->argv[1] == 'network')) {
115 $notif_header = L10n::t('Network Notifications');
116 $notifs = $nm->networkNotifs($show, $startrec, $perpage);
118 // Get the system notifications
119 } elseif (($a->argc > 1) && ($a->argv[1] == 'system')) {
120 $notif_header = L10n::t('System Notifications');
121 $notifs = $nm->systemNotifs($show, $startrec, $perpage);
123 // Get the personal notifications
124 } elseif (($a->argc > 1) && ($a->argv[1] == 'personal')) {
125 $notif_header = L10n::t('Personal Notifications');
126 $notifs = $nm->personalNotifs($show, $startrec, $perpage);
128 // Get the home notifications
129 } elseif (($a->argc > 1) && ($a->argv[1] == 'home')) {
130 $notif_header = L10n::t('Home Notifications');
131 $notifs = $nm->homeNotifs($show, $startrec, $perpage);
132 // fallback - redirect to main page
134 $a->internalRedirect('notifications');
138 $pager = new Pager($a->query_string, $perpage);
140 // Add additional informations (needed for json output)
141 $notifs['items_page'] = $pager->getItemsPerPage();
142 $notifs['page'] = $pager->getPage();
145 if (intval($json) === 1) {
146 System::jsonExit($notifs);
149 $notif_tpl = Renderer::getMarkupTemplate('notifications.tpl');
152 'href' => ($show ? 'notifications/' . $notifs['ident'] : 'notifications/' . $notifs['ident'] . '?show=all' ),
153 'text' => ($show ? L10n::t('Show unread') : L10n::t('Show all')),
156 // Process the data for template creation
157 if (defaults($notifs, 'ident', '') === 'introductions') {
158 $sugg = Renderer::getMarkupTemplate('suggestions.tpl');
159 $tpl = Renderer::getMarkupTemplate('intros.tpl');
161 // The link to switch between ignored and normal connection requests
163 'href' => (!$all ? 'notifications/intros/all' : 'notifications/intros' ),
164 'text' => (!$all ? L10n::t('Show Ignored Requests') : L10n::t('Hide Ignored Requests'))
167 // Loop through all introduction notifications.This creates an array with the output html for each
169 foreach ($notifs['notifications'] as $notif) {
171 // There are two kind of introduction. Contacts suggested by other contacts and normal connection requests.
172 // We have to distinguish between these two because they use different data.
173 switch ($notif['label']) {
174 case 'friend_suggestion':
175 $notif_content[] = Renderer::replaceMacros($sugg, [
176 '$type' => $notif['label'],
177 '$str_notifytype' => L10n::t('Notification type:'),
178 '$notify_type'=> $notif['notify_type'],
179 '$intro_id' => $notif['intro_id'],
180 '$lbl_madeby' => L10n::t('Suggested by:'),
181 '$madeby' => $notif['madeby'],
182 '$madeby_url' => $notif['madeby_url'],
183 '$madeby_zrl' => $notif['madeby_zrl'],
184 '$madeby_addr'=> $notif['madeby_addr'],
185 '$contact_id' => $notif['contact_id'],
186 '$photo' => $notif['photo'],
187 '$fullname' => $notif['name'],
188 '$url' => $notif['url'],
189 '$zrl' => $notif['zrl'],
190 '$lbl_url' => L10n::t('Profile URL'),
191 '$addr' => $notif['addr'],
192 '$hidden' => ['hidden', L10n::t('Hide this contact from others'), ($notif['hidden'] == 1), ''],
193 '$knowyou' => $notif['knowyou'],
194 '$approve' => L10n::t('Approve'),
195 '$note' => $notif['note'],
196 '$request' => $notif['request'],
197 '$ignore' => L10n::t('Ignore'),
198 '$discard' => L10n::t('Discard'),
202 // Normal connection requests
204 $friend_selected = (($notif['network'] !== Protocol::OSTATUS) ? ' checked="checked" ' : ' disabled ');
205 $fan_selected = (($notif['network'] === Protocol::OSTATUS) ? ' checked="checked" disabled ' : '');
213 if ($notif['network'] === Protocol::DFRN) {
214 $lbl_knowyou = L10n::t('Claims to be known to you: ');
215 $knowyou = (($notif['knowyou']) ? L10n::t('yes') : L10n::t('no'));
216 $helptext = L10n::t('Shall your connection be bidirectional or not?');
217 $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']);
218 $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']);
219 } elseif ($notif['network'] === Protocol::DIASPORA) {
220 $helptext = L10n::t('Shall your connection be bidirectional or not?');
221 $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']);
222 $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']);
225 $dfrn_tpl = Renderer::getMarkupTemplate('netfriend.tpl');
226 $dfrn_text = Renderer::replaceMacros($dfrn_tpl, [
227 '$intro_id' => $notif['intro_id'],
228 '$friend_selected' => $friend_selected,
229 '$fan_selected'=> $fan_selected,
230 '$approve_as1' => $helptext,
231 '$approve_as2' => $helptext2,
232 '$approve_as3' => $helptext3,
233 '$as_friend' => L10n::t('Friend'),
234 '$as_fan' => (($notif['network'] == Protocol::DIASPORA) ? L10n::t('Sharer') : L10n::t('Subscriber'))
237 $contact = DBA::selectFirst('contact', ['network', 'protocol'], ['id' => $notif['contact_id']]);
239 if (($contact['network'] != Protocol::DFRN) || ($contact['protocol'] == Protocol::ACTIVITYPUB)) {
240 $action = 'follow_confirm';
242 $action = 'dfrn_confirm';
245 $header = $notif['name'];
247 if ($notif['addr'] != '') {
248 $header .= ' <' . $notif['addr'] . '>';
251 $header .= ' (' . ContactSelector::networkToName($notif['network'], $notif['url']) . ')';
253 if ($notif['network'] != Protocol::DIASPORA) {
254 $discard = L10n::t('Discard');
259 $notif_content[] = Renderer::replaceMacros($tpl, [
260 '$type' => $notif['label'],
261 '$header' => $header,
262 '$str_notifytype' => L10n::t('Notification type:'),
263 '$notify_type' => $notif['notify_type'],
264 '$dfrn_text' => $dfrn_text,
265 '$dfrn_id' => $notif['dfrn_id'],
266 '$uid' => $notif['uid'],
267 '$intro_id' => $notif['intro_id'],
268 '$contact_id' => $notif['contact_id'],
269 '$photo' => $notif['photo'],
270 '$fullname' => $notif['name'],
271 '$location' => $notif['location'],
272 '$lbl_location'=> L10n::t('Location:'),
273 '$about' => $notif['about'],
274 '$lbl_about' => L10n::t('About:'),
275 '$keywords' => $notif['keywords'],
276 '$lbl_keywords'=> L10n::t('Tags:'),
277 '$gender' => $notif['gender'],
278 '$lbl_gender' => L10n::t('Gender:'),
279 '$hidden' => ['hidden', L10n::t('Hide this contact from others'), ($notif['hidden'] == 1), ''],
280 '$url' => $notif['url'],
281 '$zrl' => $notif['zrl'],
282 '$lbl_url' => L10n::t('Profile URL'),
283 '$addr' => $notif['addr'],
284 '$lbl_knowyou' => $lbl_knowyou,
285 '$lbl_network' => L10n::t('Network:'),
286 '$network' => ContactSelector::networkToName($notif['network'], $notif['url']),
287 '$knowyou' => $knowyou,
288 '$approve' => L10n::t('Approve'),
289 '$note' => $notif['note'],
290 '$ignore' => L10n::t('Ignore'),
291 '$discard' => $discard,
292 '$action' => $action,
298 if (count($notifs['notifications']) == 0) {
299 info(L10n::t('No introductions.') . EOL);
302 // Normal notifications (no introductions)
303 } elseif (!empty($notifs['notifications'])) {
304 // Loop trough ever notification This creates an array with the output html for each
305 // notification and apply the correct template according to the notificationtype (label).
306 foreach ($notifs['notifications'] as $notif) {
307 $notification_templates = [
308 'like' => 'notifications_likes_item.tpl',
309 'dislike' => 'notifications_dislikes_item.tpl',
310 'attend' => 'notifications_attend_item.tpl',
311 'attendno' => 'notifications_attend_item.tpl',
312 'attendmaybe' => 'notifications_attend_item.tpl',
313 'friend' => 'notifications_friends_item.tpl',
314 'comment' => 'notifications_comments_item.tpl',
315 'post' => 'notifications_posts_item.tpl',
316 'notify' => 'notify.tpl',
319 $tpl_notif = Renderer::getMarkupTemplate($notification_templates[$notif['label']]);
321 $notif_content[] = Renderer::replaceMacros($tpl_notif, [
322 '$item_label' => $notif['label'],
323 '$item_link' => $notif['link'],
324 '$item_image' => $notif['image'],
325 '$item_url' => $notif['url'],
326 '$item_text' => $notif['text'],
327 '$item_when' => $notif['when'],
328 '$item_ago' => $notif['ago'],
329 '$item_seen' => $notif['seen'],
333 $notif_nocontent = L10n::t('No more %s notifications.', $notifs['ident']);
336 $o .= Renderer::replaceMacros($notif_tpl, [
337 '$notif_header' => $notif_header,
339 '$notif_content' => $notif_content,
340 '$notif_nocontent' => $notif_nocontent,
341 '$notif_show_lnk' => $notif_show_lnk,
342 '$notif_paginate' => $pager->renderMinimal(count($notif_content))