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;
16 use Friendica\Module\Login;
17 use Friendica\Model\Contact;
18 use Friendica\Model\Notify;
20 function notifications_post(App $a)
23 $a->internalRedirect();
26 $request_id = (($a->argc > 1) ? $a->argv[1] : 0);
28 if ($request_id === 'all') {
33 $intro = DBA::selectFirst('intro', ['id', 'contact-id', 'fid'], ['id' => $request_id, 'uid' => local_user()]);
35 if (DBA::isResult($intro)) {
36 $intro_id = $intro['id'];
37 $contact_id = $intro['contact-id'];
39 notice(L10n::t('Invalid request identifier.') . EOL);
43 // If it is a friend suggestion, the contact is not a new friend but an existing friend
44 // that should not be deleted.
48 if ($_POST['submit'] == L10n::t('Discard')) {
49 DBA::delete('intro', ['id' => $intro_id]);
51 // When the contact entry had been created just for that intro, we want to get rid of it now
52 $condition = ['id' => $contact_id, 'uid' => local_user(),
53 'self' => false, 'pending' => true, 'rel' => [0, Contact::FOLLOWER]];
54 $contact_pending = DBA::exists('contact', $condition);
56 // Remove the "pending" to stop the reappearing in any case
57 DBA::update('contact', ['pending' => false], ['id' => $contact_id]);
59 if ($contact_pending) {
60 Contact::remove($contact_id);
63 $a->internalRedirect('notifications/intros');
66 if ($_POST['submit'] == L10n::t('Ignore')) {
67 DBA::update('intro', ['ignore' => true], ['id' => $intro_id]);
68 $a->internalRedirect('notifications/intros');
73 function notifications_content(App $a)
76 notice(L10n::t('Permission denied.') . EOL);
80 $page = ($_REQUEST['page'] ?? 0) ?: 1;
81 $show = ($_REQUEST['show'] ?? '') === 'all';
83 Nav::setSelected('notifications');
85 $json = (($a->argc > 1 && $a->argv[$a->argc - 1] === 'json') ? true : false);
87 /** @var Notify $nm */
88 $nm = \Friendica\BaseObject::getClass(Notify::class);
91 // Get the nav tabs for the notification pages
92 $tabs = $nm->getTabs();
94 $notif_nocontent = '';
96 // Notification results per page
98 $startrec = ($page * $perpage) - $perpage;
100 $notif_header = L10n::t('Notifications');
105 if ((($a->argc > 1) && ($a->argv[1] == 'intros')) || (($a->argc == 1))) {
106 Nav::setSelected('introductions');
109 if (!empty($a->argv[2]) && intval($a->argv[2]) != 0) {
110 $id = (int)$a->argv[2];
113 $all = (($a->argc > 2) && ($a->argv[2] == 'all'));
115 $notifs = $nm->getIntroList($all, $startrec, $perpage, $id);
117 // Get the network notifications
118 } elseif (($a->argc > 1) && ($a->argv[1] == 'network')) {
119 $notif_header = L10n::t('Network Notifications');
120 $notifs = $nm->getNetworkList($show, $startrec, $perpage);
122 // Get the system notifications
123 } elseif (($a->argc > 1) && ($a->argv[1] == 'system')) {
124 $notif_header = L10n::t('System Notifications');
125 $notifs = $nm->getSystemList($show, $startrec, $perpage);
127 // Get the personal notifications
128 } elseif (($a->argc > 1) && ($a->argv[1] == 'personal')) {
129 $notif_header = L10n::t('Personal Notifications');
130 $notifs = $nm->getPersonalList($show, $startrec, $perpage);
132 // Get the home notifications
133 } elseif (($a->argc > 1) && ($a->argv[1] == 'home')) {
134 $notif_header = L10n::t('Home Notifications');
135 $notifs = $nm->getHomeList($show, $startrec, $perpage);
136 // fallback - redirect to main page
138 $a->internalRedirect('notifications');
142 $pager = new Pager($a->query_string, $perpage);
144 // Add additional informations (needed for json output)
145 $notifs['items_page'] = $pager->getItemsPerPage();
146 $notifs['page'] = $pager->getPage();
149 if (intval($json) === 1) {
150 System::jsonExit($notifs);
153 $notif_tpl = Renderer::getMarkupTemplate('notifications.tpl');
156 'href' => ($show ? 'notifications/' . $notifs['ident'] : 'notifications/' . $notifs['ident'] . '?show=all' ),
157 'text' => ($show ? L10n::t('Show unread') : L10n::t('Show all')),
160 // Process the data for template creation
161 if (($notifs['ident'] ?? '') == 'introductions') {
162 $sugg = Renderer::getMarkupTemplate('suggestions.tpl');
163 $tpl = Renderer::getMarkupTemplate('intros.tpl');
165 // The link to switch between ignored and normal connection requests
167 'href' => (!$all ? 'notifications/intros/all' : 'notifications/intros' ),
168 'text' => (!$all ? L10n::t('Show Ignored Requests') : L10n::t('Hide Ignored Requests'))
171 // Loop through all introduction notifications.This creates an array with the output html for each
173 foreach ($notifs['notifications'] as $notif) {
175 // There are two kind of introduction. Contacts suggested by other contacts and normal connection requests.
176 // We have to distinguish between these two because they use different data.
177 switch ($notif['label']) {
178 case 'friend_suggestion':
179 $notif_content[] = Renderer::replaceMacros($sugg, [
180 '$type' => $notif['label'],
181 '$str_notifytype' => L10n::t('Notification type:'),
182 '$notify_type'=> $notif['notify_type'],
183 '$intro_id' => $notif['intro_id'],
184 '$lbl_madeby' => L10n::t('Suggested by:'),
185 '$madeby' => $notif['madeby'],
186 '$madeby_url' => $notif['madeby_url'],
187 '$madeby_zrl' => $notif['madeby_zrl'],
188 '$madeby_addr'=> $notif['madeby_addr'],
189 '$contact_id' => $notif['contact_id'],
190 '$photo' => $notif['photo'],
191 '$fullname' => $notif['name'],
192 '$url' => $notif['url'],
193 '$zrl' => $notif['zrl'],
194 '$lbl_url' => L10n::t('Profile URL'),
195 '$addr' => $notif['addr'],
196 '$hidden' => ['hidden', L10n::t('Hide this contact from others'), ($notif['hidden'] == 1), ''],
197 '$knowyou' => $notif['knowyou'],
198 '$approve' => L10n::t('Approve'),
199 '$note' => $notif['note'],
200 '$request' => $notif['request'],
201 '$ignore' => L10n::t('Ignore'),
202 '$discard' => L10n::t('Discard'),
206 // Normal connection requests
208 $friend_selected = (($notif['network'] !== Protocol::OSTATUS) ? ' checked="checked" ' : ' disabled ');
209 $fan_selected = (($notif['network'] === Protocol::OSTATUS) ? ' checked="checked" disabled ' : '');
217 if ($notif['network'] === Protocol::DFRN) {
218 $lbl_knowyou = L10n::t('Claims to be known to you: ');
219 $knowyou = (($notif['knowyou']) ? L10n::t('yes') : L10n::t('no'));
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 subscriber allows them to subscribe to your posts, but you will not receive updates from them in your news feed.', $notif['name']);
223 } elseif ($notif['network'] === Protocol::DIASPORA) {
224 $helptext = L10n::t('Shall your connection be bidirectional or not?');
225 $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']);
226 $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']);
229 $dfrn_tpl = Renderer::getMarkupTemplate('netfriend.tpl');
230 $dfrn_text = Renderer::replaceMacros($dfrn_tpl, [
231 '$intro_id' => $notif['intro_id'],
232 '$friend_selected' => $friend_selected,
233 '$fan_selected'=> $fan_selected,
234 '$approve_as1' => $helptext,
235 '$approve_as2' => $helptext2,
236 '$approve_as3' => $helptext3,
237 '$as_friend' => L10n::t('Friend'),
238 '$as_fan' => (($notif['network'] == Protocol::DIASPORA) ? L10n::t('Sharer') : L10n::t('Subscriber'))
241 $contact = DBA::selectFirst('contact', ['network', 'protocol'], ['id' => $notif['contact_id']]);
243 if (($contact['network'] != Protocol::DFRN) || ($contact['protocol'] == Protocol::ACTIVITYPUB)) {
244 $action = 'follow_confirm';
246 $action = 'dfrn_confirm';
249 $header = $notif['name'];
251 if ($notif['addr'] != '') {
252 $header .= ' <' . $notif['addr'] . '>';
255 $header .= ' (' . ContactSelector::networkToName($notif['network'], $notif['url']) . ')';
257 if ($notif['network'] != Protocol::DIASPORA) {
258 $discard = L10n::t('Discard');
263 $notif_content[] = Renderer::replaceMacros($tpl, [
264 '$type' => $notif['label'],
265 '$header' => $header,
266 '$str_notifytype' => L10n::t('Notification type:'),
267 '$notify_type' => $notif['notify_type'],
268 '$dfrn_text' => $dfrn_text,
269 '$dfrn_id' => $notif['dfrn_id'],
270 '$uid' => $notif['uid'],
271 '$intro_id' => $notif['intro_id'],
272 '$contact_id' => $notif['contact_id'],
273 '$photo' => $notif['photo'],
274 '$fullname' => $notif['name'],
275 '$location' => $notif['location'],
276 '$lbl_location'=> L10n::t('Location:'),
277 '$about' => $notif['about'],
278 '$lbl_about' => L10n::t('About:'),
279 '$keywords' => $notif['keywords'],
280 '$lbl_keywords'=> L10n::t('Tags:'),
281 '$gender' => $notif['gender'],
282 '$lbl_gender' => L10n::t('Gender:'),
283 '$hidden' => ['hidden', L10n::t('Hide this contact from others'), ($notif['hidden'] == 1), ''],
284 '$url' => $notif['url'],
285 '$zrl' => $notif['zrl'],
286 '$lbl_url' => L10n::t('Profile URL'),
287 '$addr' => $notif['addr'],
288 '$lbl_knowyou' => $lbl_knowyou,
289 '$lbl_network' => L10n::t('Network:'),
290 '$network' => ContactSelector::networkToName($notif['network'], $notif['url']),
291 '$knowyou' => $knowyou,
292 '$approve' => L10n::t('Approve'),
293 '$note' => $notif['note'],
294 '$ignore' => L10n::t('Ignore'),
295 '$discard' => $discard,
296 '$action' => $action,
302 if (count($notifs['notifications']) == 0) {
303 info(L10n::t('No introductions.') . EOL);
306 // Normal notifications (no introductions)
307 } elseif (!empty($notifs['notifications'])) {
308 // Loop trough ever notification This creates an array with the output html for each
309 // notification and apply the correct template according to the notificationtype (label).
310 foreach ($notifs['notifications'] as $notif) {
311 $notification_templates = [
312 'like' => 'notifications_likes_item.tpl',
313 'dislike' => 'notifications_dislikes_item.tpl',
314 'attend' => 'notifications_attend_item.tpl',
315 'attendno' => 'notifications_attend_item.tpl',
316 'attendmaybe' => 'notifications_attend_item.tpl',
317 'friend' => 'notifications_friends_item.tpl',
318 'comment' => 'notifications_comments_item.tpl',
319 'post' => 'notifications_posts_item.tpl',
320 'notify' => 'notify.tpl',
323 $tpl_notif = Renderer::getMarkupTemplate($notification_templates[$notif['label']]);
325 $notif_content[] = Renderer::replaceMacros($tpl_notif, [
326 '$item_label' => $notif['label'],
327 '$item_link' => $notif['link'],
328 '$item_image' => $notif['image'],
329 '$item_url' => $notif['url'],
330 '$item_text' => $notif['text'],
331 '$item_when' => $notif['when'],
332 '$item_ago' => $notif['ago'],
333 '$item_seen' => $notif['seen'],
337 $notif_nocontent = L10n::t('No more %s notifications.', $notifs['ident']);
340 $o .= Renderer::replaceMacros($notif_tpl, [
341 '$notif_header' => $notif_header,
343 '$notif_content' => $notif_content,
344 '$notif_nocontent' => $notif_nocontent,
345 '$notif_show_lnk' => $notif_show_lnk,
346 '$notif_paginate' => $pager->renderMinimal(count($notif_content))