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