]> git.mxchange.org Git - friendica.git/blob - src/Model/Notify.php
Merge pull request #7846 from annando/notes-warnings
[friendica.git] / src / Model / Notify.php
1 <?php
2
3 namespace Friendica\Model;
4
5 use Exception;
6 use Friendica\App;
7 use Friendica\BaseObject;
8 use Friendica\Content\Text\BBCode;
9 use Friendica\Content\Text\HTML;
10 use Friendica\Core\Config\PConfiguration;
11 use Friendica\Core\L10n\L10n;
12 use Friendica\Core\Protocol;
13 use Friendica\Core\System;
14 use Friendica\Database\Database;
15 use Friendica\Protocol\Activity;
16 use Friendica\Util\DateTimeFormat;
17 use Friendica\Util\Proxy as ProxyUtils;
18 use Friendica\Util\Temporal;
19 use Friendica\Util\XML;
20 use ImagickException;
21 use Psr\Log\LoggerInterface;
22 use Friendica\Network\HTTPException;
23
24 /**
25  * @brief Methods for read and write notifications from/to database
26  *  or for formatting notifications
27  */
28 final class Notify extends BaseObject
29 {
30         /** @var int The default limit of notifies per page */
31         const DEFAULT_PAGE_LIMIT = 80;
32
33         const NETWORK  = 'network';
34         const SYSTEM   = 'system';
35         const PERSONAL = 'personal';
36         const HOME     = 'home';
37         const INTRO    = 'intro';
38
39         /** @var array Array of URL parameters */
40         const URL_TYPES = [
41                 self::NETWORK  => 'network',
42                 self::SYSTEM   => 'system',
43                 self::HOME     => 'home',
44                 self::PERSONAL => 'personal',
45                 self::INTRO    => 'intros',
46         ];
47
48         /** @var array Array of the allowed notifies and their printable name */
49         const PRINT_TYPES = [
50                 self::NETWORK  => 'Network',
51                 self::SYSTEM   => 'System',
52                 self::HOME     => 'Home',
53                 self::PERSONAL => 'Personal',
54                 self::INTRO    => 'Introductions',
55         ];
56
57         /** @var array The array of access keys for notify pages */
58         const ACCESS_KEYS = [
59                 self::NETWORK  => 'w',
60                 self::SYSTEM   => 'y',
61                 self::HOME     => 'h',
62                 self::PERSONAL => 'r',
63                 self::INTRO    => 'i',
64         ];
65
66         /** @var Database */
67         private $dba;
68         /** @var L10n */
69         private $l10n;
70         /** @var App\Arguments */
71         private $args;
72         /** @var App\BaseURL */
73         private $baseUrl;
74         /** @var PConfiguration */
75         private $pConfig;
76         /** @var LoggerInterface */
77         private $logger;
78
79         public function __construct(Database $dba, L10n $l10n, App\Arguments $args, App\BaseURL $baseUrl,
80                                     PConfiguration $pConfig, LoggerInterface $logger)
81         {
82                 $this->dba     = $dba;
83                 $this->l10n    = $l10n;
84                 $this->args    = $args;
85                 $this->baseUrl = $baseUrl;
86                 $this->pConfig = $pConfig;
87                 $this->logger  = $logger;
88         }
89
90         /**
91          * Set some extra properties to note array from db:
92          *  - timestamp as int in default TZ
93          *  - date_rel : relative date string
94          *  - msg_html: message as html string
95          *  - msg_plain: message as plain text string
96          *
97          * @param array $notes array of note arrays from db
98          *
99          * @return array Copy of input array with added properties
100          *
101          * @throws Exception
102          */
103         private function setExtra(array $notes)
104         {
105                 $retNotes = [];
106                 foreach ($notes as $note) {
107                         $local_time        = DateTimeFormat::local($note['date']);
108                         $note['timestamp'] = strtotime($local_time);
109                         $note['date_rel']  = Temporal::getRelativeDate($note['date']);
110                         $note['msg_html']  = BBCode::convert($note['msg'], false);
111                         $note['msg_plain'] = explode("\n", trim(HTML::toPlaintext($note['msg_html'], 0)))[0];
112
113                         $retNotes[] = $note;
114                 }
115                 return $retNotes;
116         }
117
118         /**
119          * Get all notifications for local_user()
120          *
121          * @param array  $filter optional Array "column name"=>value: filter query by columns values
122          * @param array  $order  optional Array to order by
123          * @param string $limit  optional Query limits
124          *
125          * @return array|bool of results or false on errors
126          * @throws Exception
127          */
128         public function getAll(array $filter = [], array $order = ['date' => 'DESC'], string $limit = "")
129         {
130                 $params = [];
131
132                 $params['order'] = $order;
133
134                 if (!empty($limit)) {
135                         $params['limit'] = $limit;
136                 }
137
138                 $dbFilter = array_merge($filter, ['uid' => local_user()]);
139
140                 $stmtNotifies = $this->dba->select('notify', [], $dbFilter, $params);
141
142                 if ($this->dba->isResult($stmtNotifies)) {
143                         return $this->setExtra($this->dba->toArray($stmtNotifies));
144                 }
145
146                 return false;
147         }
148
149         /**
150          * Get one note for local_user() by $id value
151          *
152          * @param int $id identity
153          *
154          * @return array note values or null if not found
155          * @throws Exception
156          */
157         public function getByID(int $id)
158         {
159                 $stmtNotify = $this->dba->selectFirst('notify', [], ['id' => $id, 'uid' => local_user()]);
160                 if ($this->dba->isResult($stmtNotify)) {
161                         return $this->setExtra([$stmtNotify])[0];
162                 }
163                 return null;
164         }
165
166         /**
167          * @brief set seen state of $note of local_user()
168          *
169          * @param array $note note array
170          * @param bool  $seen optional true or false, default true
171          *
172          * @return bool true on success, false on errors
173          * @throws Exception
174          */
175         public function setSeen(array $note, bool $seen = true)
176         {
177                 return $this->dba->update('notify', ['seen' => $seen], [
178                         '(`link` = ? OR (`parent` != 0 AND `parent` = ? AND `otype` = ?)) AND `uid` = ?',
179                         $note['link'],
180                         $note['parent'],
181                         $note['otype'],
182                         local_user()
183                 ]);
184         }
185
186         /**
187          * Set seen state of all notifications of local_user()
188          *
189          * @param bool $seen optional true or false. default true
190          *
191          * @return bool true on success, false on error
192          * @throws Exception
193          */
194         public function setAllSeen(bool $seen = true)
195         {
196                 return $this->dba->update('notify', ['seen' => $seen], ['uid' => local_user()]);
197         }
198
199         /**
200          * @brief List of pages for the Notifications TabBar
201          *
202          * @return array with with notifications TabBar data
203          * @throws Exception
204          */
205         public function getTabs()
206         {
207                 $selected = $this->args->get(1, '');
208
209                 $tabs = [];
210
211                 foreach (self::URL_TYPES as $type => $url) {
212                         $tabs[] = [
213                                 'label'     => $this->l10n->t(self::PRINT_TYPES[$type]),
214                                 'url'       => 'notifications/' . $url,
215                                 'sel'       => (($selected == $url) ? 'active' : ''),
216                                 'id'        => $type . '-tab',
217                                 'accesskey' => self::ACCESS_KEYS[$type],
218                         ];
219                 }
220
221                 return $tabs;
222         }
223
224         /**
225          * Format the notification query in an usable array
226          *
227          * @param array  $notifies The array from the db query
228          * @param string $ident    The notifications identifier (e.g. network)
229          *
230          * @return array
231          *                       string 'label' => The type of the notification
232          *                       string 'link' => URL to the source
233          *                       string 'image' => The avatar image
234          *                       string 'url' => The profile url of the contact
235          *                       string 'text' => The notification text
236          *                       string 'when' => The date of the notification
237          *                       string 'ago' => T relative date of the notification
238          *                       bool 'seen' => Is the notification marked as "seen"
239          * @throws Exception
240          */
241         private function formatList(array $notifies, string $ident = "")
242         {
243                 $formattedNotifies = [];
244
245                 foreach ($notifies as $notify) {
246                         // Because we use different db tables for the notification query
247                         // we have sometimes $notify['unseen'] and sometimes $notify['seen].
248                         // So we will have to transform $notify['unseen']
249                         if (array_key_exists('unseen', $notify)) {
250                                 $notify['seen'] = ($notify['unseen'] > 0 ? false : true);
251                         }
252
253                         // For feed items we use the user's contact, since the avatar is mostly self choosen.
254                         if (!empty($notify['network']) && $notify['network'] == Protocol::FEED) {
255                                 $notify['author-avatar'] = $notify['contact-avatar'];
256                         }
257
258                         // Depending on the identifier of the notification we need to use different defaults
259                         switch ($ident) {
260                                 case self::SYSTEM:
261                                         $default_item_label = 'notify';
262                                         $default_item_link  = $this->baseUrl->get(true) . '/notify/view/' . $notify['id'];
263                                         $default_item_image = ProxyUtils::proxifyUrl($notify['photo'], false, ProxyUtils::SIZE_MICRO);
264                                         $default_item_url   = $notify['url'];
265                                         $default_item_text  = strip_tags(BBCode::convert($notify['msg']));
266                                         $default_item_when  = DateTimeFormat::local($notify['date'], 'r');
267                                         $default_item_ago   = Temporal::getRelativeDate($notify['date']);
268                                         break;
269
270                                 case self::HOME:
271                                         $default_item_label = 'comment';
272                                         $default_item_link  = $this->baseUrl->get(true) . '/display/' . $notify['parent-guid'];
273                                         $default_item_image = ProxyUtils::proxifyUrl($notify['author-avatar'], false, ProxyUtils::SIZE_MICRO);
274                                         $default_item_url   = $notify['author-link'];
275                                         $default_item_text  = $this->l10n->t("%s commented on %s's post", $notify['author-name'], $notify['parent-author-name']);
276                                         $default_item_when  = DateTimeFormat::local($notify['created'], 'r');
277                                         $default_item_ago   = Temporal::getRelativeDate($notify['created']);
278                                         break;
279
280                                 default:
281                                         $default_item_label = (($notify['id'] == $notify['parent']) ? 'post' : 'comment');
282                                         $default_item_link  = $this->baseUrl->get(true) . '/display/' . $notify['parent-guid'];
283                                         $default_item_image = ProxyUtils::proxifyUrl($notify['author-avatar'], false, ProxyUtils::SIZE_MICRO);
284                                         $default_item_url   = $notify['author-link'];
285                                         $default_item_text  = (($notify['id'] == $notify['parent'])
286                                                 ? $this->l10n->t("%s created a new post", $notify['author-name'])
287                                                 : $this->l10n->t("%s commented on %s's post", $notify['author-name'], $notify['parent-author-name']));
288                                         $default_item_when  = DateTimeFormat::local($notify['created'], 'r');
289                                         $default_item_ago   = Temporal::getRelativeDate($notify['created']);
290                         }
291
292                         // Transform the different types of notification in an usable array
293                         switch ($notify['verb']) {
294                                 case Activity::LIKE:
295                                         $formattedNotify = [
296                                                 'label' => 'like',
297                                                 'link'  => $this->baseUrl->get(true) . '/display/' . $notify['parent-guid'],
298                                                 'image' => ProxyUtils::proxifyUrl($notify['author-avatar'], false, ProxyUtils::SIZE_MICRO),
299                                                 'url'   => $notify['author-link'],
300                                                 'text'  => $this->l10n->t("%s liked %s's post", $notify['author-name'], $notify['parent-author-name']),
301                                                 'when'  => $default_item_when,
302                                                 'ago'   => $default_item_ago,
303                                                 'seen'  => $notify['seen']
304                                         ];
305                                         break;
306
307                                 case Activity::DISLIKE:
308                                         $formattedNotify = [
309                                                 'label' => 'dislike',
310                                                 'link'  => $this->baseUrl->get(true) . '/display/' . $notify['parent-guid'],
311                                                 'image' => ProxyUtils::proxifyUrl($notify['author-avatar'], false, ProxyUtils::SIZE_MICRO),
312                                                 'url'   => $notify['author-link'],
313                                                 'text'  => $this->l10n->t("%s disliked %s's post", $notify['author-name'], $notify['parent-author-name']),
314                                                 'when'  => $default_item_when,
315                                                 'ago'   => $default_item_ago,
316                                                 'seen'  => $notify['seen']
317                                         ];
318                                         break;
319
320                                 case Activity::ATTEND:
321                                         $formattedNotify = [
322                                                 'label' => 'attend',
323                                                 'link'  => $this->baseUrl->get(true) . '/display/' . $notify['parent-guid'],
324                                                 'image' => ProxyUtils::proxifyUrl($notify['author-avatar'], false, ProxyUtils::SIZE_MICRO),
325                                                 'url'   => $notify['author-link'],
326                                                 'text'  => $this->l10n->t("%s is attending %s's event", $notify['author-name'], $notify['parent-author-name']),
327                                                 'when'  => $default_item_when,
328                                                 'ago'   => $default_item_ago,
329                                                 'seen'  => $notify['seen']
330                                         ];
331                                         break;
332
333                                 case Activity::ATTENDNO:
334                                         $formattedNotify = [
335                                                 'label' => 'attendno',
336                                                 'link'  => $this->baseUrl->get(true) . '/display/' . $notify['parent-guid'],
337                                                 'image' => ProxyUtils::proxifyUrl($notify['author-avatar'], false, ProxyUtils::SIZE_MICRO),
338                                                 'url'   => $notify['author-link'],
339                                                 'text'  => $this->l10n->t("%s is not attending %s's event", $notify['author-name'], $notify['parent-author-name']),
340                                                 'when'  => $default_item_when,
341                                                 'ago'   => $default_item_ago,
342                                                 'seen'  => $notify['seen']
343                                         ];
344                                         break;
345
346                                 case Activity::ATTENDMAYBE:
347                                         $formattedNotify = [
348                                                 'label' => 'attendmaybe',
349                                                 'link'  => $this->baseUrl->get(true) . '/display/' . $notify['parent-guid'],
350                                                 'image' => ProxyUtils::proxifyUrl($notify['author-avatar'], false, ProxyUtils::SIZE_MICRO),
351                                                 'url'   => $notify['author-link'],
352                                                 'text'  => $this->l10n->t("%s may attend %s's event", $notify['author-name'], $notify['parent-author-name']),
353                                                 'when'  => $default_item_when,
354                                                 'ago'   => $default_item_ago,
355                                                 'seen'  => $notify['seen']
356                                         ];
357                                         break;
358
359                                 case Activity::FRIEND:
360                                         if (!isset($notify['object'])) {
361                                                 $formattedNotify = [
362                                                         'label' => 'friend',
363                                                         'link'  => $default_item_link,
364                                                         'image' => $default_item_image,
365                                                         'url'   => $default_item_url,
366                                                         'text'  => $default_item_text,
367                                                         'when'  => $default_item_when,
368                                                         'ago'   => $default_item_ago,
369                                                         'seen'  => $notify['seen']
370                                                 ];
371                                                 break;
372                                         }
373                                         /// @todo Check if this part here is used at all
374                                         $this->logger->info('Complete data.', ['notify' => $notify, 'callStack' => System::callstack(20)]);
375
376                                         $xmlHead         = "<" . "?xml version='1.0' encoding='UTF-8' ?" . ">";
377                                         $obj             = XML::parseString($xmlHead . $notify['object']);
378                                         $notify['fname'] = $obj->title;
379
380                                         $formattedNotify = [
381                                                 'label' => 'friend',
382                                                 'link'  => $this->baseUrl->get(true) . '/display/' . $notify['parent-guid'],
383                                                 'image' => ProxyUtils::proxifyUrl($notify['author-avatar'], false, ProxyUtils::SIZE_MICRO),
384                                                 'url'   => $notify['author-link'],
385                                                 'text'  => $this->l10n->t("%s is now friends with %s", $notify['author-name'], $notify['fname']),
386                                                 'when'  => $default_item_when,
387                                                 'ago'   => $default_item_ago,
388                                                 'seen'  => $notify['seen']
389                                         ];
390                                         break;
391
392                                 default:
393                                         $formattedNotify = [
394                                                 'label' => $default_item_label,
395                                                 'link'  => $default_item_link,
396                                                 'image' => $default_item_image,
397                                                 'url'   => $default_item_url,
398                                                 'text'  => $default_item_text,
399                                                 'when'  => $default_item_when,
400                                                 'ago'   => $default_item_ago,
401                                                 'seen'  => $notify['seen']
402                                         ];
403                         }
404
405                         $formattedNotifies[] = $formattedNotify;
406                 }
407
408                 return $formattedNotifies;
409         }
410
411         /**
412          * Get network notifications
413          *
414          * @param bool $seen    False => only include notifications into the query
415          *                            which aren't marked as "seen"
416          * @param int        $start   Start the query at this point
417          * @param int        $limit   Maximum number of query results
418          *
419          * @return array [string, array]
420          *    string 'ident' => Notification identifier
421          *    array 'notifications' => Network notifications
422          *
423          * @throws Exception
424          */
425         public function getNetworkList(bool $seen = false, int $start = 0, int $limit = self::DEFAULT_PAGE_LIMIT)
426         {
427                 $ident    = self::NETWORK;
428                 $notifies = [];
429
430                 $condition = ['wall' => false, 'uid' => local_user()];
431
432                 if (!$seen) {
433                         $condition['unseen'] = true;
434                 }
435
436                 $fields = ['id', 'parent', 'verb', 'author-name', 'unseen', 'author-link', 'author-avatar', 'contact-avatar',
437                         'network', 'created', 'object', 'parent-author-name', 'parent-author-link', 'parent-guid'];
438                 $params = ['order' => ['received' => true], 'limit' => [$start, $limit]];
439
440                 $items = Item::selectForUser(local_user(), $fields, $condition, $params);
441
442                 if ($this->dba->isResult($items)) {
443                         $notifies = $this->formatList(Item::inArray($items), $ident);
444                 }
445
446                 $arr = [
447                         'notifications' => $notifies,
448                         'ident'         => $ident,
449                 ];
450
451                 return $arr;
452         }
453
454         /**
455          * Get system notifications
456          *
457          * @param bool $seen    False => only include notifications into the query
458          *                            which aren't marked as "seen"
459          * @param int        $start   Start the query at this point
460          * @param int        $limit   Maximum number of query results
461          *
462          * @return array [string, array]
463          *    string 'ident' => Notification identifier
464          *    array 'notifications' => System notifications
465          *
466          * @throws Exception
467          */
468         public function getSystemList(bool $seen = false, int $start = 0, int $limit = self::DEFAULT_PAGE_LIMIT)
469         {
470                 $ident    = self::SYSTEM;
471                 $notifies = [];
472
473                 $filter = ['uid' => local_user()];
474                 if (!$seen) {
475                         $filter['seen'] = false;
476                 }
477
478                 $params          = [];
479                 $params['order'] = ['date' => 'DESC'];
480                 $params['limit'] = [$start, $limit];
481
482                 $stmtNotifies = $this->dba->select('notify',
483                         ['id', 'url', 'photo', 'msg', 'date', 'seen', 'verb'],
484                         $filter,
485                         $params);
486
487                 if ($this->dba->isResult($stmtNotifies)) {
488                         $notifies = $this->formatList($this->dba->toArray($stmtNotifies), $ident);
489                 }
490
491                 $arr = [
492                         'notifications' => $notifies,
493                         'ident'         => $ident,
494                 ];
495
496                 return $arr;
497         }
498
499         /**
500          * Get personal notifications
501          *
502          * @param bool $seen    False => only include notifications into the query
503          *                            which aren't marked as "seen"
504          * @param int        $start   Start the query at this point
505          * @param int        $limit   Maximum number of query results
506          *
507          * @return array [string, array]
508          *    string 'ident' => Notification identifier
509          *    array 'notifications' => Personal notifications
510          *
511          * @throws Exception
512          */
513         public function getPersonalList(bool $seen = false, int $start = 0, int $limit = self::DEFAULT_PAGE_LIMIT)
514         {
515                 $ident    = self::PERSONAL;
516                 $notifies = [];
517
518                 $myurl     = str_replace('http://', '', self::getApp()->contact['nurl']);
519                 $diasp_url = str_replace('/profile/', '/u/', $myurl);
520
521                 $condition = ["NOT `wall` AND `uid` = ? AND (`item`.`author-id` = ? OR `item`.`tag` REGEXP ? OR `item`.`tag` REGEXP ?)",
522                         local_user(), public_contact(), $myurl . '\\]', $diasp_url . '\\]'];
523
524                 if (!$seen) {
525                         $condition[0] .= " AND `unseen`";
526                 }
527
528                 $fields = ['id', 'parent', 'verb', 'author-name', 'unseen', 'author-link', 'author-avatar', 'contact-avatar',
529                         'network', 'created', 'object', 'parent-author-name', 'parent-author-link', 'parent-guid'];
530                 $params = ['order' => ['received' => true], 'limit' => [$start, $limit]];
531
532                 $items = Item::selectForUser(local_user(), $fields, $condition, $params);
533
534                 if ($this->dba->isResult($items)) {
535                         $notifies = $this->formatList(Item::inArray($items), $ident);
536                 }
537
538                 $arr = [
539                         'notifications' => $notifies,
540                         'ident'         => $ident,
541                 ];
542
543                 return $arr;
544         }
545
546         /**
547          * @brief Get home notifications
548          *
549          * @param bool $seen    False => only include notifications into the query
550          *                            which aren't marked as "seen"
551          * @param int        $start   Start the query at this point
552          * @param int        $limit   Maximum number of query results
553          *
554          * @return array [string, array]
555          *    string 'ident' => Notification identifier
556          *    array 'notifications' => Home notifications
557          *
558          * @throws Exception
559          */
560         public function getHomeList(bool $seen = false, int $start = 0, int $limit = self::DEFAULT_PAGE_LIMIT)
561         {
562                 $ident    = self::HOME;
563                 $notifies = [];
564
565                 $condition = ['wall' => true, 'uid' => local_user()];
566
567                 if (!$seen) {
568                         $condition['unseen'] = true;
569                 }
570
571                 $fields = ['id', 'parent', 'verb', 'author-name', 'unseen', 'author-link', 'author-avatar', 'contact-avatar',
572                         'network', 'created', 'object', 'parent-author-name', 'parent-author-link', 'parent-guid'];
573                 $params = ['order' => ['received' => true], 'limit' => [$start, $limit]];
574
575                 $items = Item::selectForUser(local_user(), $fields, $condition, $params);
576
577                 if ($this->dba->isResult($items)) {
578                         $notifies = $this->formatList(Item::inArray($items), $ident);
579                 }
580
581                 $arr = [
582                         'notifications' => $notifies,
583                         'ident'         => $ident,
584                 ];
585
586                 return $arr;
587         }
588
589         /**
590          * @brief Get introductions
591          *
592          * @param bool $all     If false only include introductions into the query
593          *                      which aren't marked as ignored
594          * @param int  $start   Start the query at this point
595          * @param int  $limit   Maximum number of query results
596          * @param int  $id      When set, only the introduction with this id is displayed
597          *
598          * @return array [string, array]
599          *    string 'ident' => Notification identifier
600          *    array 'notifications' => Introductions
601          *
602          * @throws ImagickException
603          * @throws Exception
604          */
605         public function getIntroList(bool $all = false, int $start = 0, int $limit = self::DEFAULT_PAGE_LIMIT, int $id = 0)
606         {
607                 /// @todo sanitize wording according to SELF::INTRO
608                 $ident     = 'introductions';
609                 $notifies  = [];
610                 $sql_extra = "";
611
612                 if (empty($id)) {
613                         if (!$all) {
614                                 $sql_extra = " AND NOT `ignore` ";
615                         }
616
617                         $sql_extra .= " AND NOT `intro`.`blocked` ";
618                 } else {
619                         $sql_extra = sprintf(" AND `intro`.`id` = %d ", intval($id));
620                 }
621
622                 /// @todo Fetch contact details by "Contact::getDetailsByUrl" instead of queries to contact, fcontact and gcontact
623                 $stmtNotifies = $this->dba->p(
624                         "SELECT `intro`.`id` AS `intro_id`, `intro`.*, `contact`.*,
625                                 `fcontact`.`name` AS `fname`, `fcontact`.`url` AS `furl`, `fcontact`.`addr` AS `faddr`,
626                                 `fcontact`.`photo` AS `fphoto`, `fcontact`.`request` AS `frequest`,
627                                 `gcontact`.`location` AS `glocation`, `gcontact`.`about` AS `gabout`,
628                                 `gcontact`.`keywords` AS `gkeywords`, `gcontact`.`gender` AS `ggender`,
629                                 `gcontact`.`network` AS `gnetwork`, `gcontact`.`addr` AS `gaddr`
630                         FROM `intro`
631                                 LEFT JOIN `contact` ON `contact`.`id` = `intro`.`contact-id`
632                                 LEFT JOIN `gcontact` ON `gcontact`.`nurl` = `contact`.`nurl`
633                                 LEFT JOIN `fcontact` ON `intro`.`fid` = `fcontact`.`id`
634                         WHERE `intro`.`uid` = ? $sql_extra
635                         LIMIT ?, ?",
636                         $_SESSION['uid'],
637                         $start,
638                         $limit
639                 );
640                 if ($this->dba->isResult($stmtNotifies)) {
641                         $notifies = $this->formatIntroList($this->dba->toArray($stmtNotifies));
642                 }
643
644                 $arr = [
645                         'ident'         => $ident,
646                         'notifications' => $notifies,
647                 ];
648
649                 return $arr;
650         }
651
652         /**
653          * @brief Format the notification query in an usable array
654          *
655          * @param array $intros The array from the db query
656          *
657          * @return array with the introductions
658          * @throws HTTPException\InternalServerErrorException
659          * @throws ImagickException
660          */
661         private function formatIntroList(array $intros)
662         {
663                 $knowyou = '';
664
665                 $formattedIntros = [];
666
667                 foreach ($intros as $intro) {
668                         // There are two kind of introduction. Contacts suggested by other contacts and normal connection requests.
669                         // We have to distinguish between these two because they use different data.
670                         // Contact suggestions
671                         if ($intro['fid']) {
672                                 $return_addr = bin2hex(self::getApp()->user['nickname'] . '@' .
673                                                        $this->baseUrl->getHostName() .
674                                                        (($this->baseUrl->getURLPath()) ? '/' . $this->baseUrl->getURLPath() : ''));
675
676                                 $intro = [
677                                         'label'          => 'friend_suggestion',
678                                         'notify_type'    => $this->l10n->t('Friend Suggestion'),
679                                         'intro_id'       => $intro['intro_id'],
680                                         'madeby'         => $intro['name'],
681                                         'madeby_url'     => $intro['url'],
682                                         'madeby_zrl'     => Contact::magicLink($intro['url']),
683                                         'madeby_addr'    => $intro['addr'],
684                                         'contact_id'     => $intro['contact-id'],
685                                         'photo'          => (!empty($intro['fphoto']) ? ProxyUtils::proxifyUrl($intro['fphoto'], false, ProxyUtils::SIZE_SMALL) : "images/person-300.jpg"),
686                                         'name'           => $intro['fname'],
687                                         'url'            => $intro['furl'],
688                                         'zrl'            => Contact::magicLink($intro['furl']),
689                                         'hidden'         => $intro['hidden'] == 1,
690                                         'post_newfriend' => (intval($this->pConfig->get(local_user(), 'system', 'post_newfriend')) ? '1' : 0),
691                                         'knowyou'        => $knowyou,
692                                         'note'           => $intro['note'],
693                                         'request'        => $intro['frequest'] . '?addr=' . $return_addr,
694                                 ];
695
696                                 // Normal connection requests
697                         } else {
698                                 $intro = $this->getMissingIntroData($intro);
699
700                                 if (empty($intro['url'])) {
701                                         continue;
702                                 }
703
704                                 // Don't show these data until you are connected. Diaspora is doing the same.
705                                 if ($intro['gnetwork'] === Protocol::DIASPORA) {
706                                         $intro['glocation'] = "";
707                                         $intro['gabout']    = "";
708                                         $intro['ggender']   = "";
709                                 }
710                                 $intro = [
711                                         'label'          => (($intro['network'] !== Protocol::OSTATUS) ? 'friend_request' : 'follower'),
712                                         'notify_type'    => (($intro['network'] !== Protocol::OSTATUS) ? $this->l10n->t('Friend/Connect Request') : $this->l10n->t('New Follower')),
713                                         'dfrn_id'        => $intro['issued-id'],
714                                         'uid'            => $_SESSION['uid'],
715                                         'intro_id'       => $intro['intro_id'],
716                                         'contact_id'     => $intro['contact-id'],
717                                         'photo'          => (!empty($intro['photo']) ? ProxyUtils::proxifyUrl($intro['photo'], false, ProxyUtils::SIZE_SMALL) : "images/person-300.jpg"),
718                                         'name'           => $intro['name'],
719                                         'location'       => BBCode::convert($intro['glocation'], false),
720                                         'about'          => BBCode::convert($intro['gabout'], false),
721                                         'keywords'       => $intro['gkeywords'],
722                                         'gender'         => $intro['ggender'],
723                                         'hidden'         => $intro['hidden'] == 1,
724                                         'post_newfriend' => (intval($this->pConfig->get(local_user(), 'system', 'post_newfriend')) ? '1' : 0),
725                                         'url'            => $intro['url'],
726                                         'zrl'            => Contact::magicLink($intro['url']),
727                                         'addr'           => $intro['gaddr'],
728                                         'network'        => $intro['gnetwork'],
729                                         'knowyou'        => $intro['knowyou'],
730                                         'note'           => $intro['note'],
731                                 ];
732                         }
733
734                         $formattedIntros[] = $intro;
735                 }
736
737                 return $formattedIntros;
738         }
739
740         /**
741          * @brief Check for missing contact data and try to fetch the data from
742          *     from other sources
743          *
744          * @param array $intro The input array with the intro data
745          *
746          * @return array The array with the intro data
747          * @throws HTTPException\InternalServerErrorException
748          */
749         private function getMissingIntroData(array $intro)
750         {
751                 // If the network and the addr isn't available from the gcontact
752                 // table entry, take the one of the contact table entry
753                 if (empty($intro['gnetwork']) && !empty($intro['network'])) {
754                         $intro['gnetwork'] = $intro['network'];
755                 }
756                 if (empty($intro['gaddr']) && !empty($intro['addr'])) {
757                         $intro['gaddr'] = $intro['addr'];
758                 }
759
760                 // If the network and addr is still not available
761                 // get the missing data data from other sources
762                 if (empty($intro['gnetwork']) || empty($intro['gaddr'])) {
763                         $ret = Contact::getDetailsByURL($intro['url']);
764
765                         if (empty($intro['gnetwork']) && !empty($ret['network'])) {
766                                 $intro['gnetwork'] = $ret['network'];
767                         }
768                         if (empty($intro['gaddr']) && !empty($ret['addr'])) {
769                                 $intro['gaddr'] = $ret['addr'];
770                         }
771                 }
772
773                 return $intro;
774         }
775 }