]> git.mxchange.org Git - friendica.git/blob - src/Model/APContact.php
Merge pull request #12364 from MrPetovan/bug/warnings
[friendica.git] / src / Model / APContact.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2022, the Friendica project
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19  *
20  */
21
22 namespace Friendica\Model;
23
24 use Friendica\Content\Text\HTML;
25 use Friendica\Core\Cache\Enum\Duration;
26 use Friendica\Core\Logger;
27 use Friendica\Core\Protocol;
28 use Friendica\Core\System;
29 use Friendica\Database\DBA;
30 use Friendica\DI;
31 use Friendica\Model\Item;
32 use Friendica\Network\HTTPClient\Client\HttpClientAccept;
33 use Friendica\Network\HTTPException;
34 use Friendica\Network\Probe;
35 use Friendica\Protocol\ActivityNamespace;
36 use Friendica\Protocol\ActivityPub;
37 use Friendica\Protocol\ActivityPub\Transmitter;
38 use Friendica\Util\Crypto;
39 use Friendica\Util\DateTimeFormat;
40 use Friendica\Util\HTTPSignature;
41 use Friendica\Util\JsonLD;
42 use Friendica\Util\Network;
43 use GuzzleHttp\Psr7\Uri;
44
45 class APContact
46 {
47         /**
48          * Fetch webfinger data
49          *
50          * @param string $addr Address
51          * @return array webfinger data
52          */
53         private static function fetchWebfingerData(string $addr): array
54         {
55                 $addr_parts = explode('@', $addr);
56                 if (count($addr_parts) != 2) {
57                         return [];
58                 }
59
60                 if (Contact::isLocal($addr) && ($local_uid = User::getIdForURL($addr)) && ($local_owner = User::getOwnerDataById($local_uid))) {
61                         $data = [
62                                 'addr'      => $local_owner['addr'],
63                                 'baseurl'   => $local_owner['baseurl'],
64                                 'url'       => $local_owner['url'],
65                                 'subscribe' => $local_owner['baseurl'] . '/contact/follow?url={uri}'];
66
67                         if (!empty($local_owner['alias']) && ($local_owner['url'] != $local_owner['alias'])) {
68                                 $data['alias'] = $local_owner['alias'];
69                         }
70
71                         return $data;
72                 }
73
74                 $data = ['addr' => $addr];
75                 $template = 'https://' . $addr_parts[1] . '/.well-known/webfinger?resource=acct:' . urlencode($addr);
76                 $webfinger = Probe::webfinger(str_replace('{uri}', urlencode($addr), $template), HttpClientAccept::JRD_JSON);
77                 if (empty($webfinger['links'])) {
78                         $template = 'http://' . $addr_parts[1] . '/.well-known/webfinger?resource=acct:' . urlencode($addr);
79                         $webfinger = Probe::webfinger(str_replace('{uri}', urlencode($addr), $template), HttpClientAccept::JRD_JSON);
80                         if (empty($webfinger['links'])) {
81                                 return [];
82                         }
83                         $data['baseurl'] = 'http://' . $addr_parts[1];
84                 } else {
85                         $data['baseurl'] = 'https://' . $addr_parts[1];
86                 }
87
88                 foreach ($webfinger['links'] as $link) {
89                         if (empty($link['rel'])) {
90                                 continue;
91                         }
92
93                         if (!empty($link['template']) && ($link['rel'] == ActivityNamespace::OSTATUSSUB)) {
94                                 $data['subscribe'] = $link['template'];
95                         }
96
97                         if (!empty($link['href']) && !empty($link['type']) && ($link['rel'] == 'self') && ($link['type'] == 'application/activity+json')) {
98                                 $data['url'] = $link['href'];
99                         }
100
101                         if (!empty($link['href']) && !empty($link['type']) && ($link['rel'] == 'http://webfinger.net/rel/profile-page') && ($link['type'] == 'text/html')) {
102                                 $data['alias'] = $link['href'];
103                         }
104                 }
105
106                 if (!empty($data['url']) && !empty($data['alias']) && ($data['url'] == $data['alias'])) {
107                         unset($data['alias']);
108                 }
109
110                 return $data;
111         }
112
113         /**
114          * Fetches a profile from a given url
115          *
116          * @param string  $url    profile url
117          * @param boolean $update true = always update, false = never update, null = update when not found or outdated
118          * @return array profile array
119          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
120          * @throws \ImagickException
121          * @todo Rewrite parameter $update to avoid true|false|null (boolean is binary, null adds a third case)
122          */
123         public static function getByURL(string $url, $update = null): array
124         {
125                 if (empty($url) || Network::isUrlBlocked($url)) {
126                         Logger::info('Domain is blocked', ['url' => $url]);
127                         return [];
128                 }
129
130                 $fetched_contact = [];
131
132                 if (empty($update)) {
133                         if (is_null($update)) {
134                                 $ref_update = DateTimeFormat::utc('now - 1 month');
135                         } else {
136                                 $ref_update = DBA::NULL_DATETIME;
137                         }
138
139                         $apcontact = DBA::selectFirst('apcontact', [], ['url' => $url]);
140                         if (!DBA::isResult($apcontact)) {
141                                 $apcontact = DBA::selectFirst('apcontact', [], ['alias' => $url]);
142                         }
143
144                         if (!DBA::isResult($apcontact)) {
145                                 $apcontact = DBA::selectFirst('apcontact', [], ['addr' => $url]);
146                         }
147
148                         if (DBA::isResult($apcontact) && ($apcontact['updated'] > $ref_update) && !empty($apcontact['pubkey']) && !empty($apcontact['uri-id'])) {
149                                 return $apcontact;
150                         }
151
152                         if (!is_null($update)) {
153                                 return DBA::isResult($apcontact) ? $apcontact : [];
154                         }
155
156                         if (DBA::isResult($apcontact)) {
157                                 $fetched_contact = $apcontact;
158                         }
159                 }
160
161                 $apcontact = [];
162
163                 $webfinger = empty(parse_url($url, PHP_URL_SCHEME));
164                 if ($webfinger) {
165                         $apcontact = self::fetchWebfingerData($url);
166                         if (empty($apcontact['url'])) {
167                                 return $fetched_contact;
168                         }
169                         $url = $apcontact['url'];
170                 } elseif (empty(parse_url($url, PHP_URL_PATH))) {
171                         $apcontact['baseurl'] = $url;
172                 }
173
174                 // Detect multiple fast repeating request to the same address
175                 // See https://github.com/friendica/friendica/issues/9303
176                 $cachekey = 'apcontact:' . ItemURI::getIdByURI($url);
177                 $result = DI::cache()->get($cachekey);
178                 if (!is_null($result)) {
179                         Logger::notice('Multiple requests for the address', ['url' => $url, 'update' => $update, 'callstack' => System::callstack(20), 'result' => $result]);
180                         if (!empty($fetched_contact)) {
181                                 return $fetched_contact;
182                         }
183                 } else {
184                         DI::cache()->set($cachekey, System::callstack(20), Duration::FIVE_MINUTES);
185                 }
186
187                 if (Network::isLocalLink($url) && ($local_uid = User::getIdForURL($url))) {
188                         try {
189                                 $data = Transmitter::getProfile($local_uid);
190                                 $local_owner = User::getOwnerDataById($local_uid);
191                         } catch(HTTPException\NotFoundException $e) {
192                                 $data = null;
193                         }
194                 }
195
196                 if (empty($data)) {
197                         $local_owner = [];
198
199                         $curlResult = HTTPSignature::fetchRaw($url);
200                         $failed = empty($curlResult) || empty($curlResult->getBody()) ||
201                                 (!$curlResult->isSuccess() && ($curlResult->getReturnCode() != 410));
202
203                         if (!$failed) {
204                                 $data = json_decode($curlResult->getBody(), true);
205                                 $failed = empty($data) || !is_array($data);
206                         }
207
208                         if (!$failed && ($curlResult->getReturnCode() == 410)) {
209                                 $data = ['@context' => ActivityPub::CONTEXT, 'id' => $url, 'type' => 'Tombstone'];
210                         }
211
212                         if ($failed) {
213                                 self::markForArchival($fetched_contact ?: []);
214                                 return $fetched_contact;
215                         }
216                 }
217
218                 $compacted = JsonLD::compact($data);
219                 if (empty($compacted['@id'])) {
220                         return $fetched_contact;
221                 }
222
223                 $apcontact['url'] = $compacted['@id'];
224                 $apcontact['uuid'] = JsonLD::fetchElement($compacted, 'diaspora:guid', '@value');
225                 $apcontact['type'] = str_replace('as:', '', JsonLD::fetchElement($compacted, '@type'));
226                 $apcontact['following'] = JsonLD::fetchElement($compacted, 'as:following', '@id');
227                 $apcontact['followers'] = JsonLD::fetchElement($compacted, 'as:followers', '@id');
228                 $apcontact['inbox'] = (JsonLD::fetchElement($compacted, 'ldp:inbox', '@id') ?? '');
229                 self::unarchiveInbox($apcontact['inbox'], false);
230
231                 $apcontact['outbox'] = JsonLD::fetchElement($compacted, 'as:outbox', '@id');
232
233                 $apcontact['sharedinbox'] = '';
234                 if (!empty($compacted['as:endpoints'])) {
235                         $apcontact['sharedinbox'] = (JsonLD::fetchElement($compacted['as:endpoints'], 'as:sharedInbox', '@id') ?? '');
236                         self::unarchiveInbox($apcontact['sharedinbox'], true);
237                 }
238
239                 $apcontact['featured']      = JsonLD::fetchElement($compacted, 'toot:featured', '@id');
240                 $apcontact['featured-tags'] = JsonLD::fetchElement($compacted, 'toot:featuredTags', '@id');
241
242                 $apcontact['nick'] = JsonLD::fetchElement($compacted, 'as:preferredUsername', '@value') ?? '';
243                 $apcontact['name'] = JsonLD::fetchElement($compacted, 'as:name', '@value');
244
245                 if (empty($apcontact['name'])) {
246                         $apcontact['name'] = $apcontact['nick'];
247                 }
248
249                 $apcontact['about'] = HTML::toBBCode(JsonLD::fetchElement($compacted, 'as:summary', '@value') ?? '');
250
251                 $ims = JsonLD::fetchElementArray($compacted, 'vcard:hasInstantMessage');
252
253                 if (!empty($ims)) {
254                         foreach ($ims as $link) {
255                                 if (substr($link, 0, 5) == 'xmpp:') {
256                                         $apcontact['xmpp'] = substr($link, 5);
257                                 }
258                                 if (substr($link, 0, 7) == 'matrix:') {
259                                         $apcontact['matrix'] = substr($link, 7);
260                                 }
261                         }
262                 }
263
264                 $apcontact['photo'] = JsonLD::fetchElement($compacted, 'as:icon', '@id');
265                 if (is_array($apcontact['photo']) || !empty($compacted['as:icon']['as:url']['@id'])) {
266                         $apcontact['photo'] = JsonLD::fetchElement($compacted['as:icon'], 'as:url', '@id');
267                 }
268
269                 $apcontact['header'] = JsonLD::fetchElement($compacted, 'as:image', '@id');
270                 if (is_array($apcontact['header']) || !empty($compacted['as:image']['as:url']['@id'])) {
271                         $apcontact['header'] = JsonLD::fetchElement($compacted['as:image'], 'as:url', '@id');
272                 }
273
274                 if (empty($apcontact['alias'])) {
275                         $apcontact['alias'] = JsonLD::fetchElement($compacted, 'as:url', '@id');
276                         if (is_array($apcontact['alias'])) {
277                                 $apcontact['alias'] = JsonLD::fetchElement($compacted['as:url'], 'as:href', '@id');
278                         }
279                 }
280
281                 // Quit if none of the basic values are set
282                 if (empty($apcontact['url']) || empty($apcontact['type']) || (($apcontact['type'] != 'Tombstone') && empty($apcontact['inbox']))) {
283                         return $fetched_contact;
284                 } elseif ($apcontact['type'] == 'Tombstone') {
285                         // The "inbox" field must have a content
286                         $apcontact['inbox'] = '';
287                 }
288
289                 // Quit if this doesn't seem to be an account at all
290                 if (!in_array($apcontact['type'], ActivityPub::ACCOUNT_TYPES)) {
291                         return $fetched_contact;
292                 }
293
294                 if (empty($apcontact['addr'])) {
295                         try {
296                                 $apcontact['addr'] = $apcontact['nick'] . '@' . (new Uri($apcontact['url']))->getAuthority();
297                         } catch (\Throwable $e) {
298                                 Logger::warning('Unable to coerce APContact URL into a UriInterface object', ['url' => $apcontact['url'], 'error' => $e->getMessage()]);
299                                 $apcontact['addr'] = '';
300                         }
301                 }
302
303                 $apcontact['pubkey'] = null;
304                 if (!empty($compacted['w3id:publicKey'])) {
305                         $apcontact['pubkey'] = trim(JsonLD::fetchElement($compacted['w3id:publicKey'], 'w3id:publicKeyPem', '@value') ?? '');
306                         if (strpos($apcontact['pubkey'], 'RSA ') !== false) {
307                                 $apcontact['pubkey'] = Crypto::rsaToPem($apcontact['pubkey']);
308                         }
309                 }
310
311                 $apcontact['manually-approve'] = (int)JsonLD::fetchElement($compacted, 'as:manuallyApprovesFollowers');
312
313                 $apcontact['suspended'] = (int)JsonLD::fetchElement($compacted, 'toot:suspended');
314
315                 if (!empty($compacted['as:generator'])) {
316                         $apcontact['baseurl'] = JsonLD::fetchElement($compacted['as:generator'], 'as:url', '@id');
317                         $apcontact['generator'] = JsonLD::fetchElement($compacted['as:generator'], 'as:name', '@value');
318                 }
319
320                 if (!empty($apcontact['following'])) {
321                         if (!empty($local_owner)) {
322                                 $following = ActivityPub\Transmitter::getContacts($local_owner, [Contact::SHARING, Contact::FRIEND], 'following');
323                         } else {
324                                 $following = ActivityPub::fetchContent($apcontact['following']);
325                         }
326                         if (!empty($following['totalItems'])) {
327                                 // Mastodon seriously allows for this condition?
328                                 // Jul 14 2021 - See https://mastodon.social/@BLUW for a negative following count
329                                 if ($following['totalItems'] < 0) {
330                                         $following['totalItems'] = 0;
331                                 }
332                                 $apcontact['following_count'] = $following['totalItems'];
333                         }
334                 }
335
336                 if (!empty($apcontact['followers'])) {
337                         if (!empty($local_owner)) {
338                                 $followers = ActivityPub\Transmitter::getContacts($local_owner, [Contact::FOLLOWER, Contact::FRIEND], 'followers');
339                         } else {
340                                 $followers = ActivityPub::fetchContent($apcontact['followers']);
341                         }
342                         if (!empty($followers['totalItems'])) {
343                                 // Mastodon seriously allows for this condition?
344                                 // Jul 14 2021 - See https://mastodon.online/@goes11 for a negative followers count
345                                 if ($followers['totalItems'] < 0) {
346                                         $followers['totalItems'] = 0;
347                                 }
348                                 $apcontact['followers_count'] = $followers['totalItems'];
349                         }
350                 }
351
352                 if (!empty($apcontact['outbox'])) {
353                         if (!empty($local_owner)) {
354                                 $statuses_count = self::getStatusesCount($local_owner);
355                         } else {
356                                 $outbox = ActivityPub::fetchContent($apcontact['outbox']);
357                                 $statuses_count = $outbox['totalItems'] ?? 0;
358                         }
359                         if (!empty($statuses_count)) {
360                                 // Mastodon seriously allows for this condition?
361                                 // Jul 20 2021 - See https://chaos.social/@m11 for a negative posts count
362                                 if ($statuses_count < 0) {
363                                         $statuses_count = 0;
364                                 }
365                                 $apcontact['statuses_count'] = $statuses_count;
366                         }
367                 }
368
369                 $apcontact['discoverable'] = JsonLD::fetchElement($compacted, 'toot:discoverable', '@value');
370
371                 // To-Do
372
373                 // Unhandled
374                 // tag, attachment, image, nomadicLocations, signature, movedTo, liked
375
376                 // Unhandled from Misskey
377                 // sharedInbox, isCat
378
379                 // Unhandled from Kroeg
380                 // kroeg:blocks, updated
381
382                 // When the photo is too large, try to shorten it by removing parts
383                 if (strlen($apcontact['photo'] ?? '') > 255) {
384                         $parts = parse_url($apcontact['photo']);
385                         unset($parts['fragment']);
386                         $apcontact['photo'] = (string)Uri::fromParts($parts);
387
388                         if (strlen($apcontact['photo']) > 255) {
389                                 unset($parts['query']);
390                                 $apcontact['photo'] = (string)Uri::fromParts($parts);
391                         }
392
393                         if (strlen($apcontact['photo']) > 255) {
394                                 $apcontact['photo'] = substr($apcontact['photo'], 0, 255);
395                         }
396                 }
397
398                 if (!$webfinger && !empty($apcontact['addr'])) {
399                         $data = self::fetchWebfingerData($apcontact['addr']);
400                         if (!empty($data)) {
401                                 $apcontact['baseurl'] = $data['baseurl'];
402
403                                 if (empty($apcontact['alias']) && !empty($data['alias'])) {
404                                         $apcontact['alias'] = $data['alias'];
405                                 }
406                                 if (!empty($data['subscribe'])) {
407                                         $apcontact['subscribe'] = $data['subscribe'];
408                                 }
409                         } else {
410                                 $apcontact['addr'] = null;
411                         }
412                 }
413
414                 if (empty($apcontact['baseurl'])) {
415                         $apcontact['baseurl'] = null;
416                 }
417
418                 if (empty($apcontact['subscribe'])) {
419                         $apcontact['subscribe'] = null;
420                 }
421
422                 if (!empty($apcontact['baseurl']) && empty($fetched_contact['gsid'])) {
423                         $apcontact['gsid'] = GServer::getID($apcontact['baseurl']);
424                 } elseif (!empty($fetched_contact['gsid'])) {
425                         $apcontact['gsid'] = $fetched_contact['gsid'];
426                 } else {
427                         $apcontact['gsid'] = null;
428                 }
429
430                 if ($apcontact['url'] == $apcontact['alias']) {
431                         $apcontact['alias'] = null;
432                 }
433
434                 if (empty($apcontact['uuid'])) {
435                         $apcontact['uri-id'] = ItemURI::getIdByURI($apcontact['url']);
436                 } else {
437                         $apcontact['uri-id'] = ItemURI::insert(['uri' => $apcontact['url'], 'guid' => $apcontact['uuid']]);
438                 }
439
440                 foreach (APContact\Endpoint::ENDPOINT_NAMES as $type => $name) {
441                         $value = JsonLD::fetchElement($compacted, $name, '@id');
442                         if (empty($value)) {
443                                 continue;
444                         }
445                         APContact\Endpoint::update($apcontact['uri-id'], $type, $value);
446                 }
447
448                 if (!empty($compacted['as:endpoints'])) {
449                         foreach ($compacted['as:endpoints'] as $name => $endpoint) {
450                                 if (empty($endpoint['@id']) || !is_string($endpoint['@id'])) {
451                                         continue;
452                                 }
453
454                                 if (in_array($name, APContact\Endpoint::ENDPOINT_NAMES)) {
455                                         $key = array_search($name, APContact\Endpoint::ENDPOINT_NAMES);
456                                         APContact\Endpoint::update($apcontact['uri-id'], $key, $endpoint['@id']);
457                                         Logger::debug('Store endpoint', ['key' => $key, 'name' => $name, 'endpoint' => $endpoint['@id']]);
458                                 } elseif (!in_array($name, ['as:sharedInbox', 'as:uploadMedia', 'as:oauthTokenEndpoint', 'as:oauthAuthorizationEndpoint', 'litepub:oauthRegistrationEndpoint'])) {
459                                         Logger::debug('Unknown endpoint', ['name' => $name, 'endpoint' => $endpoint['@id']]);
460                                 }
461                         }
462                 }
463
464                 $apcontact['updated'] = DateTimeFormat::utcNow();
465
466                 // We delete the old entry when the URL is changed
467                 if ($url != $apcontact['url']) {
468                         Logger::info('Delete changed profile url', ['old' => $url, 'new' => $apcontact['url']]);
469                         DBA::delete('apcontact', ['url' => $url]);
470                 }
471
472                 // Limit the length on incoming fields
473                 $apcontact = DI::dbaDefinition()->truncateFieldsForTable('apcontact', $apcontact);
474
475                 if (DBA::exists('apcontact', ['url' => $apcontact['url']])) {
476                         DBA::update('apcontact', $apcontact, ['url' => $apcontact['url']]);
477                 } else {
478                         DBA::replace('apcontact', $apcontact);
479                 }
480
481                 Logger::info('Updated profile', ['url' => $url]);
482
483                 return DBA::selectFirst('apcontact', [], ['url' => $apcontact['url']]) ?: [];
484         }
485
486         /**
487          * Fetch the number of statuses for the given owner
488          *
489          * @param array $owner
490          *
491          * @return integer
492          */
493         private static function getStatusesCount(array $owner): int
494         {
495                 $condition = [
496                         'private'        => [Item::PUBLIC, Item::UNLISTED],
497                         'author-id'      => Contact::getIdForURL($owner['url'], 0, false),
498                         'gravity'        => [Item::GRAVITY_PARENT, Item::GRAVITY_COMMENT],
499                         'network'        => Protocol::DFRN,
500                         'parent-network' => Protocol::FEDERATED,
501                         'deleted'        => false,
502                         'visible'        => true,
503                 ];
504
505                 $count = Post::countPosts($condition);
506
507                 return $count;
508         }
509
510         /**
511          * Mark the given AP Contact as "to archive"
512          *
513          * @param array $apcontact
514          * @return void
515          */
516         public static function markForArchival(array $apcontact)
517         {
518                 if (!empty($apcontact['inbox'])) {
519                         Logger::info('Set inbox status to failure', ['inbox' => $apcontact['inbox']]);
520                         HTTPSignature::setInboxStatus($apcontact['inbox'], false);
521                 }
522
523                 if (!empty($apcontact['sharedinbox'])) {
524                         // Check if there are any available inboxes
525                         $available = DBA::exists('apcontact', ["`sharedinbox` = ? AnD `inbox` IN (SELECT `url` FROM `inbox-status` WHERE `success` > `failure`)",
526                                 $apcontact['sharedinbox']]);
527                         if (!$available) {
528                                 // If all known personal inboxes are failing then set their shared inbox to failure as well
529                                 Logger::info('Set shared inbox status to failure', ['sharedinbox' => $apcontact['sharedinbox']]);
530                                 HTTPSignature::setInboxStatus($apcontact['sharedinbox'], false, true);
531                         }
532                 }
533         }
534
535         /**
536          * Unmark the given AP Contact as "to archive"
537          *
538          * @param array $apcontact
539          * @return void
540          */
541         public static function unmarkForArchival(array $apcontact)
542         {
543                 if (!empty($apcontact['inbox'])) {
544                         Logger::info('Set inbox status to success', ['inbox' => $apcontact['inbox']]);
545                         HTTPSignature::setInboxStatus($apcontact['inbox'], true);
546                 }
547                 if (!empty($apcontact['sharedinbox'])) {
548                         Logger::info('Set shared inbox status to success', ['sharedinbox' => $apcontact['sharedinbox']]);
549                         HTTPSignature::setInboxStatus($apcontact['sharedinbox'], true, true);
550                 }
551         }
552
553         /**
554          * Unarchive inboxes
555          *
556          * @param string  $url    inbox url
557          * @param boolean $shared Shared Inbox
558          * @return void
559          */
560         private static function unarchiveInbox(string $url, bool $shared)
561         {
562                 if (empty($url)) {
563                         return;
564                 }
565
566                 HTTPSignature::setInboxStatus($url, true, $shared);
567         }
568
569         /**
570          * Check if the apcontact is a relay account
571          *
572          * @param array $apcontact
573          *
574          * @return bool
575          */
576         public static function isRelay(array $apcontact): bool
577         {
578                 if (empty($apcontact['nick']) || $apcontact['nick'] != 'relay') {
579                         return false;
580                 }
581
582                 if (!empty($apcontact['type']) && $apcontact['type'] == 'Application') {
583                         return true;
584                 }
585
586                 if (!empty($apcontact['type']) && in_array($apcontact['type'], ['Group', 'Service']) && is_null($apcontact['outbox'])) {
587                         return true;
588                 }
589
590                 return false;
591         }
592 }