]> git.mxchange.org Git - friendica.git/blob - mod/notifications.php
Issue 7285: Perform duplicate check for item URI also with AP
[friendica.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\Logger;
16 use Friendica\Core\System;
17 use Friendica\Database\DBA;
18 use Friendica\Module\Login;
19 use Friendica\Model\Contact;
20
21 function notifications_post(App $a)
22 {
23         if (!local_user()) {
24                 $a->internalRedirect();
25         }
26
27         $request_id = (($a->argc > 1) ? $a->argv[1] : 0);
28
29         if ($request_id === 'all') {
30                 return;
31         }
32
33         if ($request_id) {
34                 $intro = DBA::selectFirst('intro', ['id', 'contact-id', 'fid'], ['id' => $request_id, 'uid' => local_user()]);
35
36                 if (DBA::isResult($intro)) {
37                         $intro_id = $intro['id'];
38                         $contact_id = $intro['contact-id'];
39                 } else {
40                         notice(L10n::t('Invalid request identifier.') . EOL);
41                         return;
42                 }
43
44                 // If it is a friend suggestion, the contact is not a new friend but an existing friend
45                 // that should not be deleted.
46
47                 $fid = $intro['fid'];
48
49                 if ($_POST['submit'] == L10n::t('Discard')) {
50                         DBA::delete('intro', ['id' => $intro_id]);
51                         if (!$fid) {
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' => [0, Contact::FOLLOWER]];
56                                 if (DBA::exists('contact', $condition)) {
57                                         Contact::remove($contact_id);
58                                 }
59                         }
60                         $a->internalRedirect('notifications/intros');
61                 }
62
63                 if ($_POST['submit'] == L10n::t('Ignore')) {
64                         DBA::update('intro', ['ignore' => true], ['id' => $intro_id]);
65                         $a->internalRedirect('notifications/intros');
66                 }
67         }
68 }
69
70 function notifications_content(App $a)
71 {
72         if (!local_user()) {
73                 notice(L10n::t('Permission denied.') . EOL);
74                 return Login::form();
75         }
76
77         $page = defaults($_REQUEST, 'page', 1);
78         $show = defaults($_REQUEST, 'show', 0);
79
80         Nav::setSelected('notifications');
81
82         $json = (($a->argc > 1 && $a->argv[$a->argc - 1] === 'json') ? true : false);
83
84         $nm = new NotificationsManager();
85
86         $o = '';
87         // Get the nav tabs for the notification pages
88         $tabs = $nm->getTabs();
89         $notif_content = [];
90         $notif_nocontent = '';
91
92         // Notification results per page
93         $perpage = 20;
94         $startrec = ($page * $perpage) - $perpage;
95
96         $notif_header = L10n::t('Notifications');
97
98         $all = false;
99
100         // Get introductions
101         if ((($a->argc > 1) && ($a->argv[1] == 'intros')) || (($a->argc == 1))) {
102                 Nav::setSelected('introductions');
103
104                 $id = 0;
105                 if (!empty($a->argv[2]) && intval($a->argv[2]) != 0) {
106                         $id = (int)$a->argv[2];
107                 }
108
109                 $all = (($a->argc > 2) && ($a->argv[2] == 'all'));
110
111                 $notifs = $nm->introNotifs($all, $startrec, $perpage, $id);
112
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);
117
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);
122
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);
127
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
133         } else {
134                 $a->internalRedirect('notifications');
135         }
136
137         // Set the pager
138         $pager = new Pager($a->query_string, $perpage);
139
140         // Add additional informations (needed for json output)
141         $notifs['items_page'] = $pager->getItemsPerPage();
142         $notifs['page'] = $pager->getPage();
143
144         // Json output
145         if (intval($json) === 1) {
146                 System::jsonExit($notifs);
147         }
148
149         $notif_tpl = Renderer::getMarkupTemplate('notifications.tpl');
150
151         $notif_show_lnk = [
152                 'href' => ($show ? 'notifications/' . $notifs['ident'] : 'notifications/' . $notifs['ident'] . '?show=all' ),
153                 'text' => ($show ? L10n::t('Show unread') : L10n::t('Show all')),
154         ];
155
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');
160
161                 // The link to switch between ignored and normal connection requests
162                 $notif_show_lnk = [
163                         'href' => (!$all ? 'notifications/intros/all' : 'notifications/intros' ),
164                         'text' => (!$all ? L10n::t('Show Ignored Requests') : L10n::t('Hide Ignored Requests'))
165                 ];
166
167                 // Loop through all introduction notifications.This creates an array with the output html for each
168                 // introduction
169                 foreach ($notifs['notifications'] as $notif) {
170
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'),
199                                         ]);
200                                         break;
201
202                                 // Normal connection requests
203                                 default:
204                                         $friend_selected = (($notif['network'] !== Protocol::OSTATUS) ? ' checked="checked" ' : ' disabled ');
205                                         $fan_selected = (($notif['network'] === Protocol::OSTATUS) ? ' checked="checked" disabled ' : '');
206
207                                         $lbl_knowyou = '';
208                                         $knowyou     = '';
209                                         $helptext    = '';
210                                         $helptext2   = '';
211                                         $helptext3   = '';
212
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']);
223                                         }
224
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'))
235                                         ]);
236
237                                         $contact = DBA::selectFirst('contact', ['network', 'protocol'], ['id' => $notif['contact_id']]);
238
239                                         if (($contact['network'] != Protocol::DFRN) || ($contact['protocol'] == Protocol::ACTIVITYPUB)) {
240                                                 $action = 'follow_confirm';
241                                         } else {
242                                                 $action = 'dfrn_confirm';
243                                         }
244
245                                         $header = $notif['name'];
246
247                                         if ($notif['addr'] != '') {
248                                                 $header .= ' <' . $notif['addr'] . '>';
249                                         }
250
251                                         $header .= ' (' . ContactSelector::networkToName($notif['network'], $notif['url']) . ')';
252
253                                         if ($notif['network'] != Protocol::DIASPORA) {
254                                                 $discard = L10n::t('Discard');
255                                         } else {
256                                                 $discard = '';
257                                         }
258
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,
293                                         ]);
294                                         break;
295                         }
296                 }
297
298                 if (count($notifs['notifications']) == 0) {
299                         info(L10n::t('No introductions.') . EOL);
300                 }
301
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',
317                         ];
318
319                         $tpl_notif = Renderer::getMarkupTemplate($notification_templates[$notif['label']]);
320
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'],
330                         ]);
331                 }
332         } else {
333                 $notif_nocontent = L10n::t('No more %s notifications.', $notifs['ident']);
334         }
335
336         $o .= Renderer::replaceMacros($notif_tpl, [
337                 '$notif_header'    => $notif_header,
338                 '$tabs'            => $tabs,
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))
343         ]);
344
345         return $o;
346 }