Issue 7142: Prevent respawn of "remote self" items
[friendica.git/.git] / mod / notifications.php
1 <?php
2 /**
3  * @file mod/notifications.php
4  * @brief The notifications module
5  */
6
7 use Friendica\App;
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;
18
19 function notifications_post(App $a)
20 {
21         if (!local_user()) {
22                 $a->internalRedirect();
23         }
24
25         $request_id = (($a->argc > 1) ? $a->argv[1] : 0);
26
27         if ($request_id === 'all') {
28                 return;
29         }
30
31         if ($request_id) {
32                 $intro = DBA::selectFirst('intro', ['id', 'contact-id', 'fid'], ['id' => $request_id, 'uid' => local_user()]);
33
34                 if (DBA::isResult($intro)) {
35                         $intro_id = $intro['id'];
36                         $contact_id = $intro['contact-id'];
37                 } else {
38                         notice(L10n::t('Invalid request identifier.') . EOL);
39                         return;
40                 }
41
42                 // If it is a friend suggestion, the contact is not a new friend but an existing friend
43                 // that should not be deleted.
44
45                 $fid = $intro['fid'];
46
47                 if ($_POST['submit'] == L10n::t('Discard')) {
48                         DBA::delete('intro', ['id' => $intro_id]);
49
50                         if (!$fid) {
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);
56                         }
57                         $a->internalRedirect('notifications/intros');
58                 }
59
60                 if ($_POST['submit'] == L10n::t('Ignore')) {
61                         DBA::update('intro', ['ignore' => true], ['id' => $intro_id]);
62                         $a->internalRedirect('notifications/intros');
63                 }
64         }
65 }
66
67 function notifications_content(App $a)
68 {
69         if (!local_user()) {
70                 notice(L10n::t('Permission denied.') . EOL);
71                 return Login::form();
72         }
73
74         $page = defaults($_REQUEST, 'page', 1);
75         $show = defaults($_REQUEST, 'show', 0);
76
77         Nav::setSelected('notifications');
78
79         $json = (($a->argc > 1 && $a->argv[$a->argc - 1] === 'json') ? true : false);
80
81         $nm = new NotificationsManager();
82
83         $o = '';
84         // Get the nav tabs for the notification pages
85         $tabs = $nm->getTabs();
86         $notif_content = [];
87         $notif_nocontent = '';
88
89         // Notification results per page
90         $perpage = 20;
91         $startrec = ($page * $perpage) - $perpage;
92
93         $notif_header = L10n::t('Notifications');
94
95         $all = false;
96
97         // Get introductions
98         if ((($a->argc > 1) && ($a->argv[1] == 'intros')) || (($a->argc == 1))) {
99                 Nav::setSelected('introductions');
100
101                 $all = (($a->argc > 2) && ($a->argv[2] == 'all'));
102
103                 $notifs = $nm->introNotifs($all, $startrec, $perpage);
104
105         // Get the network notifications
106         } elseif (($a->argc > 1) && ($a->argv[1] == 'network')) {
107                 $notif_header = L10n::t('Network Notifications');
108                 $notifs = $nm->networkNotifs($show, $startrec, $perpage);
109
110         // Get the system notifications
111         } elseif (($a->argc > 1) && ($a->argv[1] == 'system')) {
112                 $notif_header = L10n::t('System Notifications');
113                 $notifs = $nm->systemNotifs($show, $startrec, $perpage);
114
115         // Get the personal notifications
116         } elseif (($a->argc > 1) && ($a->argv[1] == 'personal')) {
117                 $notif_header = L10n::t('Personal Notifications');
118                 $notifs = $nm->personalNotifs($show, $startrec, $perpage);
119
120         // Get the home notifications
121         } elseif (($a->argc > 1) && ($a->argv[1] == 'home')) {
122                 $notif_header = L10n::t('Home Notifications');
123                 $notifs = $nm->homeNotifs($show, $startrec, $perpage);
124         // fallback - redirect to main page
125         } else {
126                 $a->internalRedirect('notifications');
127         }
128
129         // Set the pager
130         $pager = new Pager($a->query_string, $perpage);
131
132         // Add additional informations (needed for json output)
133         $notifs['items_page'] = $pager->getItemsPerPage();
134         $notifs['page'] = $pager->getPage();
135
136         // Json output
137         if (intval($json) === 1) {
138                 System::jsonExit($notifs);
139         }
140
141         $notif_tpl = Renderer::getMarkupTemplate('notifications.tpl');
142
143         $notif_show_lnk = [
144                 'href' => ($show ? 'notifications/' . $notifs['ident'] : 'notifications/' . $notifs['ident'] . '?show=all' ),
145                 'text' => ($show ? L10n::t('Show unread') : L10n::t('Show all')),
146         ];
147
148         // Process the data for template creation
149         if (defaults($notifs, 'ident', '') === 'introductions') {
150                 $sugg = Renderer::getMarkupTemplate('suggestions.tpl');
151                 $tpl = Renderer::getMarkupTemplate('intros.tpl');
152
153                 // The link to switch between ignored and normal connection requests
154                 $notif_show_lnk = [
155                         'href' => (!$all ? 'notifications/intros/all' : 'notifications/intros' ),
156                         'text' => (!$all ? L10n::t('Show Ignored Requests') : L10n::t('Hide Ignored Requests'))
157                 ];
158
159                 // Loop through all introduction notifications.This creates an array with the output html for each
160                 // introduction
161                 foreach ($notifs['notifications'] as $notif) {
162
163                         // There are two kind of introduction. Contacts suggested by other contacts and normal connection requests.
164                         // We have to distinguish between these two because they use different data.
165                         switch ($notif['label']) {
166                                 case 'friend_suggestion':
167                                         $notif_content[] = Renderer::replaceMacros($sugg, [
168                                                 '$type'       => $notif['label'],
169                                                 '$str_notifytype' => L10n::t('Notification type:'),
170                                                 '$notify_type'=> $notif['notify_type'],
171                                                 '$intro_id'   => $notif['intro_id'],
172                                                 '$lbl_madeby' => L10n::t('Suggested by:'),
173                                                 '$madeby'     => $notif['madeby'],
174                                                 '$madeby_url' => $notif['madeby_url'],
175                                                 '$madeby_zrl' => $notif['madeby_zrl'],
176                                                 '$madeby_addr'=> $notif['madeby_addr'],
177                                                 '$contact_id' => $notif['contact_id'],
178                                                 '$photo'      => $notif['photo'],
179                                                 '$fullname'   => $notif['name'],
180                                                 '$url'        => $notif['url'],
181                                                 '$zrl'        => $notif['zrl'],
182                                                 '$lbl_url'    => L10n::t('Profile URL'),
183                                                 '$addr'       => $notif['addr'],
184                                                 '$hidden'     => ['hidden', L10n::t('Hide this contact from others'), ($notif['hidden'] == 1), ''],
185                                                 '$knowyou'    => $notif['knowyou'],
186                                                 '$approve'    => L10n::t('Approve'),
187                                                 '$note'       => $notif['note'],
188                                                 '$request'    => $notif['request'],
189                                                 '$ignore'     => L10n::t('Ignore'),
190                                                 '$discard'    => L10n::t('Discard'),
191                                         ]);
192                                         break;
193
194                                 // Normal connection requests
195                                 default:
196                                         $friend_selected = (($notif['network'] !== Protocol::OSTATUS) ? ' checked="checked" ' : ' disabled ');
197                                         $fan_selected = (($notif['network'] === Protocol::OSTATUS) ? ' checked="checked" disabled ' : '');
198
199                                         $lbl_knowyou = '';
200                                         $knowyou     = '';
201                                         $helptext    = '';
202                                         $helptext2   = '';
203                                         $helptext3   = '';
204
205                                         if ($notif['network'] === Protocol::DFRN) {
206                                                 $lbl_knowyou = L10n::t('Claims to be known to you: ');
207                                                 $knowyou   = (($notif['knowyou']) ? L10n::t('yes') : L10n::t('no'));
208                                                 $helptext  = L10n::t('Shall your connection be bidirectional or not?');
209                                                 $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']);
210                                                 $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']);
211                                         } elseif ($notif['network'] === Protocol::DIASPORA) {
212                                                 $helptext  = L10n::t('Shall your connection be bidirectional or not?');
213                                                 $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']);
214                                                 $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']);
215                                         }
216
217                                         $dfrn_tpl = Renderer::getMarkupTemplate('netfriend.tpl');
218                                         $dfrn_text = Renderer::replaceMacros($dfrn_tpl, [
219                                                 '$intro_id'    => $notif['intro_id'],
220                                                 '$friend_selected' => $friend_selected,
221                                                 '$fan_selected'=> $fan_selected,
222                                                 '$approve_as1' => $helptext,
223                                                 '$approve_as2' => $helptext2,
224                                                 '$approve_as3' => $helptext3,
225                                                 '$as_friend'   => L10n::t('Friend'),
226                                                 '$as_fan'      => (($notif['network'] == Protocol::DIASPORA) ? L10n::t('Sharer') : L10n::t('Subscriber'))
227                                         ]);
228
229                                         $contact = DBA::selectFirst('contact', ['network', 'protocol'], ['id' => $notif['contact_id']]);
230
231                                         if (($contact['network'] != Protocol::DFRN) || ($contact['protocol'] == Protocol::ACTIVITYPUB)) {
232                                                 $action = 'follow_confirm';
233                                         } else {
234                                                 $action = 'dfrn_confirm';
235                                         }
236
237                                         $header = $notif['name'];
238
239                                         if ($notif['addr'] != '') {
240                                                 $header .= ' <' . $notif['addr'] . '>';
241                                         }
242
243                                         $header .= ' (' . ContactSelector::networkToName($notif['network'], $notif['url']) . ')';
244
245                                         if ($notif['network'] != Protocol::DIASPORA) {
246                                                 $discard = L10n::t('Discard');
247                                         } else {
248                                                 $discard = '';
249                                         }
250
251                                         $notif_content[] = Renderer::replaceMacros($tpl, [
252                                                 '$type'        => $notif['label'],
253                                                 '$header'      => $header,
254                                                 '$str_notifytype' => L10n::t('Notification type:'),
255                                                 '$notify_type' => $notif['notify_type'],
256                                                 '$dfrn_text'   => $dfrn_text,
257                                                 '$dfrn_id'     => $notif['dfrn_id'],
258                                                 '$uid'         => $notif['uid'],
259                                                 '$intro_id'    => $notif['intro_id'],
260                                                 '$contact_id'  => $notif['contact_id'],
261                                                 '$photo'       => $notif['photo'],
262                                                 '$fullname'    => $notif['name'],
263                                                 '$location'    => $notif['location'],
264                                                 '$lbl_location'=> L10n::t('Location:'),
265                                                 '$about'       => $notif['about'],
266                                                 '$lbl_about'   => L10n::t('About:'),
267                                                 '$keywords'    => $notif['keywords'],
268                                                 '$lbl_keywords'=> L10n::t('Tags:'),
269                                                 '$gender'      => $notif['gender'],
270                                                 '$lbl_gender'  => L10n::t('Gender:'),
271                                                 '$hidden'      => ['hidden', L10n::t('Hide this contact from others'), ($notif['hidden'] == 1), ''],
272                                                 '$url'         => $notif['url'],
273                                                 '$zrl'         => $notif['zrl'],
274                                                 '$lbl_url'     => L10n::t('Profile URL'),
275                                                 '$addr'        => $notif['addr'],
276                                                 '$lbl_knowyou' => $lbl_knowyou,
277                                                 '$lbl_network' => L10n::t('Network:'),
278                                                 '$network'     => ContactSelector::networkToName($notif['network'], $notif['url']),
279                                                 '$knowyou'     => $knowyou,
280                                                 '$approve'     => L10n::t('Approve'),
281                                                 '$note'        => $notif['note'],
282                                                 '$ignore'      => L10n::t('Ignore'),
283                                                 '$discard'     => $discard,
284                                                 '$action'      => $action,
285                                         ]);
286                                         break;
287                         }
288                 }
289
290                 if (count($notifs['notifications']) == 0) {
291                         info(L10n::t('No introductions.') . EOL);
292                 }
293
294                 // Normal notifications (no introductions)
295         } elseif (!empty($notifs['notifications'])) {
296                 // Loop trough ever notification This creates an array with the output html for each
297                 // notification and apply the correct template according to the notificationtype (label).
298                 foreach ($notifs['notifications'] as $notif) {
299                         $notification_templates = [
300                                 'like'        => 'notifications_likes_item.tpl',
301                                 'dislike'     => 'notifications_dislikes_item.tpl',
302                                 'attend'      => 'notifications_attend_item.tpl',
303                                 'attendno'    => 'notifications_attend_item.tpl',
304                                 'attendmaybe' => 'notifications_attend_item.tpl',
305                                 'friend'      => 'notifications_friends_item.tpl',
306                                 'comment'     => 'notifications_comments_item.tpl',
307                                 'post'        => 'notifications_posts_item.tpl',
308                                 'notify'      => 'notify.tpl',
309                         ];
310
311                         $tpl_notif = Renderer::getMarkupTemplate($notification_templates[$notif['label']]);
312
313                         $notif_content[] = Renderer::replaceMacros($tpl_notif, [
314                                 '$item_label' => $notif['label'],
315                                 '$item_link'  => $notif['link'],
316                                 '$item_image' => $notif['image'],
317                                 '$item_url'   => $notif['url'],
318                                 '$item_text'  => $notif['text'],
319                                 '$item_when'  => $notif['when'],
320                                 '$item_ago'   => $notif['ago'],
321                                 '$item_seen'  => $notif['seen'],
322                         ]);
323                 }
324         } else {
325                 $notif_nocontent = L10n::t('No more %s notifications.', $notifs['ident']);
326         }
327
328         $o .= Renderer::replaceMacros($notif_tpl, [
329                 '$notif_header'    => $notif_header,
330                 '$tabs'            => $tabs,
331                 '$notif_content'   => $notif_content,
332                 '$notif_nocontent' => $notif_nocontent,
333                 '$notif_show_lnk'  => $notif_show_lnk,
334                 '$notif_paginate'  => $pager->renderMinimal(count($notif_content))
335         ]);
336
337         return $o;
338 }