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