]> git.mxchange.org Git - friendica.git/blob - src/Protocol/ActivityPub/Processor.php
82eb4d0294f40e4d37946438aae58a0a8d122c8f
[friendica.git] / src / Protocol / ActivityPub / Processor.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2020, Friendica
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\Protocol\ActivityPub;
23
24 use Friendica\Content\PageInfo;
25 use Friendica\Content\Text\BBCode;
26 use Friendica\Content\Text\HTML;
27 use Friendica\Content\Text\Markdown;
28 use Friendica\Core\Logger;
29 use Friendica\Core\Protocol;
30 use Friendica\Database\DBA;
31 use Friendica\DI;
32 use Friendica\Model\APContact;
33 use Friendica\Model\Contact;
34 use Friendica\Model\Conversation;
35 use Friendica\Model\Event;
36 use Friendica\Model\Item;
37 use Friendica\Model\ItemURI;
38 use Friendica\Model\Mail;
39 use Friendica\Model\Tag;
40 use Friendica\Model\User;
41 use Friendica\Model\Post;
42 use Friendica\Protocol\Activity;
43 use Friendica\Protocol\ActivityPub;
44 use Friendica\Protocol\Relay;
45 use Friendica\Util\DateTimeFormat;
46 use Friendica\Util\JsonLD;
47 use Friendica\Util\Strings;
48
49 /**
50  * ActivityPub Processor Protocol class
51  */
52 class Processor
53 {
54         /**
55          * Converts mentions from Pleroma into the Friendica format
56          *
57          * @param string $body
58          *
59          * @return string converted body
60          */
61         private static function convertMentions($body)
62         {
63                 $URLSearchString = "^\[\]";
64                 $body = preg_replace("/\[url\=([$URLSearchString]*)\]([#@!])(.*?)\[\/url\]/ism", '$2[url=$1]$3[/url]', $body);
65
66                 return $body;
67         }
68
69         /**
70          * Convert the language array into a language JSON
71          *
72          * @param array $languages
73          * @return string language JSON
74          */
75         private static function processLanguages(array $languages)
76         {
77                 $codes = array_keys($languages);
78                 $lang = [];
79                 foreach ($codes as $code) {
80                         $lang[$code] = 1;
81                 }
82
83                 if (empty($lang)) {
84                         return '';
85                 }
86
87                 return json_encode($lang);
88         }
89         /**
90          * Replaces emojis in the body
91          *
92          * @param array $emojis
93          * @param string $body
94          *
95          * @return string with replaced emojis
96          */
97         private static function replaceEmojis($body, array $emojis)
98         {
99                 $body = strtr($body,
100                         array_combine(
101                                 array_column($emojis, 'name'),
102                                 array_map(function ($emoji) {
103                                         return '[class=emoji mastodon][img=' . $emoji['href'] . ']' . $emoji['name'] . '[/img][/class]';
104                                 }, $emojis)
105                         )
106                 );
107
108                 return $body;
109         }
110
111         /**
112          * Store attached media files in the post-media table
113          *
114          * @param int $uriid
115          * @param array $attachment
116          * @return void
117          */
118         private static function storeAttachmentAsMedia(int $uriid, array $attachment)
119         {
120                 if (empty($attachment['url'])) {
121                         return;
122                 }
123
124                 $data = ['uri-id' => $uriid];
125
126                 $filetype = strtolower(substr($attachment['mediaType'], 0, strpos($attachment['mediaType'], '/')));
127                 if ($filetype == 'image') {
128                         $data['type'] = Post\Media::IMAGE;
129                 } elseif ($filetype == 'video') {
130                         $data['type'] = Post\Media::VIDEO;
131                 } elseif ($filetype == 'audio') {
132                         $data['type'] = Post\Media::AUDIO;
133                 } elseif (in_array($attachment['mediaType'], ['application/x-bittorrent', 'application/x-bittorrent;x-scheme-handler/magnet'])) {
134                         $data['type'] = Post\Media::TORRENT;
135                 } else {
136                         Logger::info('Unknown type', ['attachment' => $attachment]);
137                         return;
138                 }
139
140                 $data['url'] = $attachment['url'];
141                 $data['mimetype'] = $attachment['mediaType'];
142                 $data['height'] = $attachment['height'] ?? null;
143                 $data['size'] = $attachment['size'] ?? null;
144                 $data['preview'] = $attachment['image'] ?? null;
145                 $data['description'] = $attachment['name'] ?? null;
146
147                 Post\Media::insert($data);
148         }
149
150         /**
151          * Add attachment data to the item array
152          *
153          * @param array   $activity
154          * @param array   $item
155          *
156          * @return array array
157          */
158         private static function constructAttachList($activity, $item)
159         {
160                 if (empty($activity['attachments'])) {
161                         return $item;
162                 }
163
164                 foreach ($activity['attachments'] as $attach) {
165                         switch ($attach['type']) {
166                                 case 'link':
167                                         $data = [
168                                                 'url'      => $attach['url'],
169                                                 'type'     => $attach['type'],
170                                                 'title'    => $attach['title'] ?? '',
171                                                 'text'     => $attach['desc']  ?? '',
172                                                 'image'    => $attach['image'] ?? '',
173                                                 'images'   => [],
174                                                 'keywords' => [],
175                                         ];
176                                         $item['body'] = PageInfo::appendDataToBody($item['body'], $data);
177                                         break;
178                                 default:
179                                         self::storeAttachmentAsMedia($item['uri-id'], $attach);
180
181                                         $filetype = strtolower(substr($attach['mediaType'], 0, strpos($attach['mediaType'], '/')));
182                                         if ($filetype == 'image') {
183                                                 if (!empty($activity['source'])) {
184                                                         foreach ([0, 1, 2] as $size) {
185                                                                 if (preg_match('#/photo/.*-' . $size . '\.#ism', $attach['url']) && 
186                                                                         strpos(preg_replace('#(/photo/.*)-[012]\.#ism', '$1-' . $size . '.', $activity['source']), $attach['url'])) {
187                                                                         continue 3;
188                                                                 }
189                                                         }
190                                                         if (strpos($activity['source'], $attach['url'])) {
191                                                                 continue 2;
192                                                         }
193                                                 }
194
195                                                 $item['body'] .= "\n";
196
197                                                 // image is the preview/thumbnail URL
198                                                 if (!empty($attach['image'])) {
199                                                         $item['body'] .= '[url=' . $attach['url'] . ']';
200                                                         $attach['url'] = $attach['image'];
201                                                 }
202
203                                                 if (empty($attach['name'])) {
204                                                         $item['body'] .= '[img]' . $attach['url'] . '[/img]';
205                                                 } else {
206                                                         $item['body'] .= '[img=' . $attach['url'] . ']' . $attach['name'] . '[/img]';
207                                                 }
208
209                                                 if (!empty($attach['image'])) {
210                                                         $item['body'] .= '[/url]';
211                                                 }
212                                         } elseif ($filetype == 'audio') {
213                                                 if (!empty($activity['source']) && strpos($activity['source'], $attach['url'])) {
214                                                         continue 2;
215                                                 }
216
217                                                 $item['body'] = '[audio]' . $attach['url'] . "[/audio]\n" . $item['body'];
218                                         } elseif ($filetype == 'video') {
219                                                 if (!empty($activity['source']) && strpos($activity['source'], $attach['url'])) {
220                                                         continue 2;
221                                                 }
222
223                                                 $item['body'] = '[video]' . $attach['url'] . "[/video]\n" . $item['body'];
224                                         }
225                         }
226                 }
227
228                 return $item;
229         }
230
231         /**
232          * Updates a message
233          *
234          * @param array $activity Activity array
235          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
236          */
237         public static function updateItem($activity)
238         {
239                 $item = Post::selectFirst(['uri', 'uri-id', 'thr-parent', 'gravity'], ['uri' => $activity['id']]);
240                 if (!DBA::isResult($item)) {
241                         Logger::warning('No existing item, item will be created', ['uri' => $activity['id']]);
242                         $item = self::createItem($activity);
243                         self::postItem($activity, $item);
244                         return;
245                 }
246
247                 $item['changed'] = DateTimeFormat::utcNow();
248                 $item['edited'] = DateTimeFormat::utc($activity['updated']);
249
250                 $item = self::processContent($activity, $item);
251
252                 $item = self::constructAttachList($activity, $item);
253
254                 if (empty($item)) {
255                         return;
256                 }
257
258                 Item::update($item, ['uri' => $activity['id']]);
259         }
260
261         /**
262          * Prepares data for a message
263          *
264          * @param array $activity Activity array
265          * @return array Internal item
266          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
267          * @throws \ImagickException
268          */
269         public static function createItem($activity)
270         {
271                 $item = [];
272                 $item['verb'] = Activity::POST;
273                 $item['thr-parent'] = $activity['reply-to-id'];
274
275                 if ($activity['reply-to-id'] == $activity['id']) {
276                         $item['gravity'] = GRAVITY_PARENT;
277                         $item['object-type'] = Activity\ObjectType::NOTE;
278                 } else {
279                         $item['gravity'] = GRAVITY_COMMENT;
280                         $item['object-type'] = Activity\ObjectType::COMMENT;
281                 }
282
283                 if (empty($activity['directmessage']) && ($activity['id'] != $activity['reply-to-id']) && !Post::exists(['uri' => $activity['reply-to-id']])) {
284                         Logger::notice('Parent not found. Try to refetch it.', ['parent' => $activity['reply-to-id']]);
285                         self::fetchMissingActivity($activity['reply-to-id'], $activity);
286                 }
287
288                 $item['diaspora_signed_text'] = $activity['diaspora:comment'] ?? '';
289
290                 /// @todo What to do with $activity['context']?
291                 if (empty($activity['directmessage']) && ($item['gravity'] != GRAVITY_PARENT) && !Post::exists(['uri' => $item['thr-parent']])) {
292                         Logger::info('Parent not found, message will be discarded.', ['thr-parent' => $item['thr-parent']]);
293                         return [];
294                 }
295
296                 $item['network'] = Protocol::ACTIVITYPUB;
297                 $item['author-link'] = $activity['author'];
298                 $item['author-id'] = Contact::getIdForURL($activity['author']);
299                 $item['owner-link'] = $activity['actor'];
300                 $item['owner-id'] = Contact::getIdForURL($activity['actor']);
301
302                 if (in_array(0, $activity['receiver']) && !empty($activity['unlisted'])) {
303                         $item['private'] = Item::UNLISTED;
304                 } elseif (in_array(0, $activity['receiver'])) {
305                         $item['private'] = Item::PUBLIC;
306                 } else {
307                         $item['private'] = Item::PRIVATE;
308                 }
309
310                 if (!empty($activity['raw'])) {
311                         $item['source'] = $activity['raw'];
312                         $item['protocol'] = Conversation::PARCEL_ACTIVITYPUB;
313                         $item['conversation-href'] = $activity['context'] ?? '';
314                         $item['conversation-uri'] = $activity['conversation'] ?? '';
315
316                         if (isset($activity['push'])) {
317                                 $item['direction'] = $activity['push'] ? Conversation::PUSH : Conversation::PULL;
318                         }
319                 }
320
321                 if (!empty($activity['from-relay'])) {
322                         $item['direction'] = Conversation::RELAY;
323                 }
324
325                 $item['isForum'] = false;
326
327                 if (!empty($activity['thread-completion'])) {
328                         if ($activity['thread-completion'] != $item['owner-id']) {
329                                 $actor = Contact::getById($activity['thread-completion'], ['url']);
330                                 $item['causer-link'] = $actor['url'];
331                                 $item['causer-id'] = $activity['thread-completion'];
332                                 Logger::info('Use inherited actor as causer.', ['id' => $item['owner-id'], 'activity' => $activity['thread-completion'], 'owner' => $item['owner-link'], 'actor' => $actor['url']]);
333                         } else {
334                                 // Store the original actor in the "causer" fields to enable the check for ignored or blocked contacts
335                                 $item['causer-link'] = $item['owner-link'];
336                                 $item['causer-id'] = $item['owner-id'];
337                                 Logger::info('Use actor as causer.', ['id' => $item['owner-id'], 'actor' => $item['owner-link']]);
338                         }
339
340                         $item['owner-link'] = $item['author-link'];
341                         $item['owner-id'] = $item['author-id'];
342                 } else {
343                         $actor = APContact::getByURL($item['owner-link'], false);
344                         $item['isForum'] = ($actor['type'] == 'Group');
345                 }
346
347                 $item['uri'] = $activity['id'];
348
349                 $item['created'] = DateTimeFormat::utc($activity['published']);
350                 $item['edited'] = DateTimeFormat::utc($activity['updated']);
351                 $guid = $activity['sc:identifier'] ?: self::getGUIDByURL($item['uri']);
352                 $item['guid'] = $activity['diaspora:guid'] ?: $guid;
353
354                 $item['uri-id'] = ItemURI::insert(['uri' => $item['uri'], 'guid' => $item['guid']]);
355                 if (empty($item['uri-id'])) {
356                         Logger::warning('Unable to get a uri-id for an item uri', ['uri' => $item['uri'], 'guid' => $item['guid']]);
357                         return [];
358                 }
359
360                 $item = self::processContent($activity, $item);
361                 if (empty($item)) {
362                         Logger::info('Message was not processed');
363                         return [];
364                 }
365
366                 $item['plink'] = $activity['alternate-url'] ?? $item['uri'];
367
368                 $item = self::constructAttachList($activity, $item);
369
370                 return $item;
371         }
372
373         /**
374          * Delete items
375          *
376          * @param array $activity
377          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
378          * @throws \ImagickException
379          */
380         public static function deleteItem($activity)
381         {
382                 $owner = Contact::getIdForURL($activity['actor']);
383
384                 Logger::info('Deleting item', ['object' => $activity['object_id'], 'owner'  => $owner]);
385                 Item::markForDeletion(['uri' => $activity['object_id'], 'owner-id' => $owner]);
386         }
387
388         /**
389          * Prepare the item array for an activity
390          *
391          * @param array $activity Activity array
392          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
393          * @throws \ImagickException
394          */
395         public static function addTag($activity)
396         {
397                 if (empty($activity['object_content']) || empty($activity['object_id'])) {
398                         return;
399                 }
400
401                 foreach ($activity['receiver'] as $receiver) {
402                         $item = Post::selectFirst(['id', 'uri-id', 'origin', 'author-link'], ['uri' => $activity['target_id'], 'uid' => $receiver]);
403                         if (!DBA::isResult($item)) {
404                                 // We don't fetch missing content for this purpose
405                                 continue;
406                         }
407
408                         if (($item['author-link'] != $activity['actor']) && !$item['origin']) {
409                                 Logger::info('Not origin, not from the author, skipping update', ['id' => $item['id'], 'author' => $item['author-link'], 'actor' => $activity['actor']]);
410                                 continue;
411                         }
412
413                         Tag::store($item['uri-id'], Tag::HASHTAG, $activity['object_content'], $activity['object_id']);
414                         Logger::info('Tagged item', ['id' => $item['id'], 'tag' => $activity['object_content'], 'uri' => $activity['target_id'], 'actor' => $activity['actor']]);
415                 }
416         }
417
418         /**
419          * Prepare the item array for an activity
420          *
421          * @param array  $activity Activity array
422          * @param string $verb     Activity verb
423          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
424          * @throws \ImagickException
425          */
426         public static function createActivity($activity, $verb)
427         {
428                 $item = self::createItem($activity);
429                 $item['verb'] = $verb;
430                 $item['thr-parent'] = $activity['object_id'];
431                 $item['gravity'] = GRAVITY_ACTIVITY;
432                 $item['object-type'] = Activity\ObjectType::NOTE;
433
434                 $item['diaspora_signed_text'] = $activity['diaspora:like'] ?? '';
435
436                 self::postItem($activity, $item);
437         }
438
439         /**
440          * Create an event
441          *
442          * @param array $activity Activity array
443          * @param array $item
444          * @throws \Exception
445          */
446         public static function createEvent($activity, $item)
447         {
448                 $event['summary']   = HTML::toBBCode($activity['name']);
449                 $event['desc']      = HTML::toBBCode($activity['content']);
450                 $event['start']     = $activity['start-time'];
451                 $event['finish']    = $activity['end-time'];
452                 $event['nofinish']  = empty($event['finish']);
453                 $event['location']  = $activity['location'];
454                 $event['adjust']    = true;
455                 $event['cid']       = $item['contact-id'];
456                 $event['uid']       = $item['uid'];
457                 $event['uri']       = $item['uri'];
458                 $event['edited']    = $item['edited'];
459                 $event['private']   = $item['private'];
460                 $event['guid']      = $item['guid'];
461                 $event['plink']     = $item['plink'];
462                 $event['network']   = $item['network'];
463                 $event['protocol']  = $item['protocol'];
464                 $event['direction'] = $item['direction'];
465                 $event['source']    = $item['source'];
466
467                 $condition = ['uri' => $item['uri'], 'uid' => $item['uid']];
468                 $ev = DBA::selectFirst('event', ['id'], $condition);
469                 if (DBA::isResult($ev)) {
470                         $event['id'] = $ev['id'];
471                 }
472
473                 $event_id = Event::store($event);
474                 Logger::info('Event was stored', ['id' => $event_id]);
475         }
476
477         /**
478          * Process the content
479          *
480          * @param array $activity Activity array
481          * @param array $item
482          * @return array|bool Returns the item array or false if there was an unexpected occurrence
483          * @throws \Exception
484          */
485         private static function processContent($activity, $item)
486         {
487                 if (!empty($activity['mediatype']) && ($activity['mediatype'] == 'text/markdown')) {
488                         $item['title'] = Markdown::toBBCode($activity['name']);
489                         $content = Markdown::toBBCode($activity['content']);
490                 } elseif (!empty($activity['mediatype']) && ($activity['mediatype'] == 'text/bbcode')) {
491                         $item['title'] = $activity['name'];
492                         $content = $activity['content'];
493                 } else {
494                         // By default assume "text/html"
495                         $item['title'] = HTML::toBBCode($activity['name']);
496                         $content = HTML::toBBCode($activity['content']);
497                 }
498
499                 if (!empty($activity['languages'])) {
500                         $item['language'] = self::processLanguages($activity['languages']);
501                 }
502
503                 if (!empty($activity['emojis'])) {
504                         $content = self::replaceEmojis($content, $activity['emojis']);
505                 }
506
507                 $content = self::convertMentions($content);
508
509                 if (!empty($activity['source'])) {
510                         $item['body'] = $activity['source'];
511                         $item['raw-body'] = $content;
512                 } else {
513                         if (empty($activity['directmessage']) && ($item['thr-parent'] != $item['uri']) && ($item['gravity'] == GRAVITY_COMMENT)) {
514                                 $item_private = !in_array(0, $activity['item_receiver']);
515                                 $parent = Post::selectFirst(['id', 'uri-id', 'private', 'author-link', 'alias'], ['uri' => $item['thr-parent']]);
516                                 if (!DBA::isResult($parent)) {
517                                         Logger::warning('Unknown parent item.', ['uri' => $item['thr-parent']]);
518                                         return false;
519                                 }
520                                 if ($item_private && ($parent['private'] != Item::PRIVATE)) {
521                                         Logger::warning('Item is private but the parent is not. Dropping.', ['item-uri' => $item['uri'], 'thr-parent' => $item['thr-parent']]);
522                                         return false;
523                                 }
524
525                                 $content = self::removeImplicitMentionsFromBody($content, $parent);
526                         }
527                         $item['content-warning'] = HTML::toBBCode($activity['summary']);
528                         $item['raw-body'] = $item['body'] = $content;
529                 }
530
531                 self::storeFromBody($item);
532                 self::storeTags($item['uri-id'], $activity['tags']);
533
534                 $item['location'] = $activity['location'];
535
536                 if (!empty($activity['latitude']) && !empty($activity['longitude'])) {
537                         $item['coord'] = $activity['latitude'] . ' ' . $activity['longitude'];
538                 }
539
540                 $item['app'] = $activity['generator'];
541
542                 return $item;
543         }
544
545         /**
546          * Store hashtags and mentions
547          *
548          * @param array $item
549          */
550         private static function storeFromBody(array $item)
551         {
552                 // Make sure to delete all existing tags (can happen when called via the update functionality)
553                 DBA::delete('post-tag', ['uri-id' => $item['uri-id']]);
554
555                 Tag::storeFromBody($item['uri-id'], $item['body'], '@!');
556         }
557
558         /**
559          * Generate a GUID out of an URL
560          *
561          * @param string $url message URL
562          * @return string with GUID
563          */
564         private static function getGUIDByURL(string $url)
565         {
566                 $parsed = parse_url($url);
567
568                 $host_hash = hash('crc32', $parsed['host']);
569
570                 unset($parsed["scheme"]);
571                 unset($parsed["host"]);
572
573                 $path = implode("/", $parsed);
574
575                 return $host_hash . '-'. hash('fnv164', $path) . '-'. hash('joaat', $path);
576         }
577
578         /**
579          * Creates an item post
580          *
581          * @param array $activity Activity data
582          * @param array $item     item array
583          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
584          * @throws \ImagickException
585          */
586         public static function postItem(array $activity, array $item)
587         {
588                 if (empty($item)) {
589                         return;
590                 }
591
592                 $stored = false;
593                 ksort($activity['receiver']);
594
595                 foreach ($activity['receiver'] as $receiver) {
596                         if ($receiver == -1) {
597                                 continue;
598                         }
599
600                         $item['uid'] = $receiver;
601
602                         $type = $activity['reception_type'][$receiver] ?? Receiver::TARGET_UNKNOWN;
603                         switch($type) {
604                                 case Receiver::TARGET_TO:
605                                         $item['post-type'] = Item::PT_TO;
606                                         break;
607                                 case Receiver::TARGET_CC:
608                                         $item['post-type'] = Item::PT_CC;
609                                         break;
610                                 case Receiver::TARGET_BTO:
611                                         $item['post-type'] = Item::PT_BTO;
612                                         break;
613                                 case Receiver::TARGET_BCC:
614                                         $item['post-type'] = Item::PT_BCC;
615                                         break;
616                                 case Receiver::TARGET_FOLLOWER:
617                                         $item['post-type'] = Item::PT_FOLLOWER;
618                                         break;
619                                 case Receiver::TARGET_ANSWER:
620                                         $item['post-type'] = Item::PT_COMMENT;
621                                         break;
622                                 case Receiver::TARGET_GLOBAL:
623                                         $item['post-type'] = Item::PT_GLOBAL;
624                                         break;
625                                 default:
626                                         $item['post-type'] = Item::PT_ARTICLE;
627                         }
628
629                         if (!empty($activity['from-relay'])) {
630                                 $item['post-type'] = Item::PT_RELAY;
631                         } elseif (!empty($activity['thread-completion'])) {
632                                 $item['post-type'] = Item::PT_FETCHED;
633                         }
634
635                         if ($item['isForum'] ?? false) {
636                                 $item['contact-id'] = Contact::getIdForURL($activity['actor'], $receiver);
637                         } else {
638                                 $item['contact-id'] = Contact::getIdForURL($activity['author'], $receiver);
639                         }
640
641                         if (($receiver != 0) && empty($item['contact-id'])) {
642                                 $item['contact-id'] = Contact::getIdForURL($activity['author']);
643                         }
644
645                         if (!empty($activity['directmessage'])) {
646                                 self::postMail($activity, $item);
647                                 continue;
648                         }
649
650                         if (DI::pConfig()->get($receiver, 'system', 'accept_only_sharer', false) && ($receiver != 0) && ($item['gravity'] == GRAVITY_PARENT)) {
651                                 $skip = !Contact::isSharingByURL($activity['author'], $receiver);
652
653                                 if ($skip && (($activity['type'] == 'as:Announce') || ($item['isForum'] ?? false))) {
654                                         $skip = !Contact::isSharingByURL($activity['actor'], $receiver);
655                                 }
656
657                                 if ($skip) {
658                                         Logger::info('Skipping post', ['uid' => $receiver, 'url' => $item['uri']]);
659                                         continue;
660                                 }
661
662                                 Logger::info('Accepting post', ['uid' => $receiver, 'url' => $item['uri']]);
663                         }
664
665                         if (($item['gravity'] != GRAVITY_ACTIVITY) && ($activity['object_type'] == 'as:Event')) {
666                                 self::createEvent($activity, $item);
667                         }
668
669                         $item_id = Item::insert($item);
670                         if ($item_id) {
671                                 Logger::info('Item insertion successful', ['user' => $item['uid'], 'item_id' => $item_id]);
672                         } else {
673                                 Logger::notice('Item insertion aborted', ['user' => $item['uid']]);
674                         }
675
676                         if ($item['uid'] == 0) {
677                                 $stored = $item_id;
678                         }
679                 }
680
681                 // Store send a follow request for every reshare - but only when the item had been stored
682                 if ($stored && ($item['private'] != Item::PRIVATE) && ($item['gravity'] == GRAVITY_PARENT) && ($item['author-link'] != $item['owner-link'])) {
683                         $author = APContact::getByURL($item['owner-link'], false);
684                         // We send automatic follow requests for reshared messages. (We don't need though for forum posts)
685                         if ($author['type'] != 'Group') {
686                                 Logger::info('Send follow request', ['uri' => $item['uri'], 'stored' => $stored, 'to' => $item['author-link']]);
687                                 ActivityPub\Transmitter::sendFollowObject($item['uri'], $item['author-link']);
688                         }
689                 }
690         }
691
692         /**
693          * Store tags and mentions into the tag table
694          *
695          * @param integer $uriid
696          * @param array $tags
697          */
698         private static function storeTags(int $uriid, array $tags = null)
699         {
700                 foreach ($tags as $tag) {
701                         if (empty($tag['name']) || empty($tag['type']) || !in_array($tag['type'], ['Mention', 'Hashtag'])) {
702                                 continue;
703                         }
704
705                         $hash = substr($tag['name'], 0, 1);
706
707                         if ($tag['type'] == 'Mention') {
708                                 if (in_array($hash, [Tag::TAG_CHARACTER[Tag::MENTION],
709                                         Tag::TAG_CHARACTER[Tag::EXCLUSIVE_MENTION],
710                                         Tag::TAG_CHARACTER[Tag::IMPLICIT_MENTION]])) {
711                                         $tag['name'] = substr($tag['name'], 1);
712                                 }
713                                 $type = Tag::IMPLICIT_MENTION;
714
715                                 if (!empty($tag['href'])) {
716                                         $apcontact = APContact::getByURL($tag['href']);
717                                         if (!empty($apcontact['name']) || !empty($apcontact['nick'])) {
718                                                 $tag['name'] = $apcontact['name'] ?: $apcontact['nick'];
719                                         }
720                                 }
721                         } elseif ($tag['type'] == 'Hashtag') {
722                                 if ($hash == Tag::TAG_CHARACTER[Tag::HASHTAG]) {
723                                         $tag['name'] = substr($tag['name'], 1);
724                                 }
725                                 $type = Tag::HASHTAG;
726                         }
727
728                         if (empty($tag['name'])) {
729                                 continue;
730                         }
731
732                         Tag::store($uriid, $type, $tag['name'], $tag['href']);
733                 }
734         }
735
736         /**
737          * Creates an mail post
738          *
739          * @param array $activity Activity data
740          * @param array $item     item array
741          * @return int|bool New mail table row id or false on error
742          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
743          */
744         private static function postMail($activity, $item)
745         {
746                 if (($item['gravity'] != GRAVITY_PARENT) && !DBA::exists('mail', ['uri' => $item['thr-parent'], 'uid' => $item['uid']])) {
747                         Logger::info('Parent not found, mail will be discarded.', ['uid' => $item['uid'], 'uri' => $item['thr-parent']]);
748                         return false;
749                 }
750
751                 Logger::info('Direct Message', $item);
752
753                 $msg = [];
754                 $msg['uid'] = $item['uid'];
755
756                 $msg['contact-id'] = $item['contact-id'];
757
758                 $contact = Contact::getById($item['contact-id'], ['name', 'url', 'photo']);
759                 $msg['from-name'] = $contact['name'];
760                 $msg['from-url'] = $contact['url'];
761                 $msg['from-photo'] = $contact['photo'];
762
763                 $msg['uri'] = $item['uri'];
764                 $msg['created'] = $item['created'];
765
766                 $parent = DBA::selectFirst('mail', ['parent-uri', 'title'], ['uri' => $item['thr-parent']]);
767                 if (DBA::isResult($parent)) {
768                         $msg['parent-uri'] = $parent['parent-uri'];
769                         $msg['title'] = $parent['title'];
770                 } else {
771                         $msg['parent-uri'] = $item['thr-parent'];
772
773                         if (!empty($item['title'])) {
774                                 $msg['title'] = $item['title'];
775                         } elseif (!empty($item['content-warning'])) {
776                                 $msg['title'] = $item['content-warning'];
777                         } else {
778                                 // Trying to generate a title out of the body
779                                 $title = $item['body'];
780
781                                 while (preg_match('#^(@\[url=([^\]]+)].*?\[\/url]\s)(.*)#is', $title, $matches)) {
782                                         $title = $matches[3];
783                                 }
784
785                                 $title = trim(HTML::toPlaintext(BBCode::convert($title, false, BBCode::API, true), 0));
786
787                                 if (strlen($title) > 20) {
788                                         $title = substr($title, 0, 20) . '...';
789                                 }
790
791                                 $msg['title'] = $title;
792                         }
793                 }
794                 $msg['body'] = $item['body'];
795
796                 return Mail::insert($msg);
797         }
798
799         /**
800          * Fetches missing posts
801          *
802          * @param string $url         message URL
803          * @param array  $child       activity array with the child of this message
804          * @param string $relay_actor Relay actor
805          * @return string fetched message URL
806          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
807          */
808         public static function fetchMissingActivity(string $url, array $child = [], string $relay_actor = '')
809         {
810                 if (!empty($child['receiver'])) {
811                         $uid = ActivityPub\Receiver::getFirstUserFromReceivers($child['receiver']);
812                 } else {
813                         $uid = 0;
814                 }
815
816                 $object = ActivityPub::fetchContent($url, $uid);
817                 if (empty($object)) {
818                         Logger::log('Activity ' . $url . ' was not fetchable, aborting.');
819                         return '';
820                 }
821
822                 if (empty($object['id'])) {
823                         Logger::log('Activity ' . $url . ' has got not id, aborting. ' . json_encode($object));
824                         return '';
825                 }
826
827                 if (!empty($object['actor'])) {
828                         $object_actor = $object['actor'];
829                 } elseif (!empty($object['attributedTo'])) {
830                         $object_actor = $object['attributedTo'];
831                         if (is_array($object_actor)) {
832                                 $compacted = JsonLD::compact($object);
833                                 $object_actor = JsonLD::fetchElement($compacted, 'as:attributedTo', '@id');
834                         }
835                 } else {
836                         // Shouldn't happen
837                         $object_actor = '';
838                 }
839
840                 $signer = [$object_actor];
841
842                 if (!empty($child['author'])) {
843                         $actor = $child['author'];
844                         $signer[] = $actor;
845                 } else {
846                         $actor = $object_actor;
847                 }
848
849                 if (!empty($object['published'])) {
850                         $published = $object['published'];
851                 } elseif (!empty($child['published'])) {
852                         $published = $child['published'];
853                 } else {
854                         $published = DateTimeFormat::utcNow();
855                 }
856
857                 $activity = [];
858                 $activity['@context'] = $object['@context'] ?? ActivityPub::CONTEXT;
859                 unset($object['@context']);
860                 $activity['id'] = $object['id'];
861                 $activity['to'] = $object['to'] ?? [];
862                 $activity['cc'] = $object['cc'] ?? [];
863                 $activity['actor'] = $actor;
864                 $activity['object'] = $object;
865                 $activity['published'] = $published;
866                 $activity['type'] = 'Create';
867
868                 $ldactivity = JsonLD::compact($activity);
869
870                 if (!empty($relay_actor)) {
871                         $ldactivity['thread-completion'] = $ldactivity['from-relay'] = Contact::getIdForURL($relay_actor);
872                 } elseif (!empty($child['thread-completion'])) {
873                         $ldactivity['thread-completion'] = $child['thread-completion'];
874                 } else {
875                         $ldactivity['thread-completion'] = Contact::getIdForURL($actor);
876                 }
877
878                 if (!empty($relay_actor) && !self::acceptIncomingMessage($ldactivity, $object['id'])) {
879                         return '';
880                 }
881
882                 ActivityPub\Receiver::processActivity($ldactivity, json_encode($activity), $uid, true, false, $signer);
883
884                 Logger::notice('Activity had been fetched and processed.', ['url' => $url, 'object' => $activity['id']]);
885
886                 return $activity['id'];
887         }
888
889         /**
890          * Test if incoming relay messages should be accepted
891          *
892          * @param array $activity activity array
893          * @param string $id      object ID
894          * @return boolean true if message is accepted
895          */
896         private static function acceptIncomingMessage(array $activity, string $id)
897         {
898                 if (empty($activity['as:object'])) {
899                         Logger::info('No object field in activity - accepted', ['id' => $id]);
900                         return true;
901                 }
902
903                 $replyto = JsonLD::fetchElement($activity['as:object'], 'as:inReplyTo', '@id');
904                 $uriid = ItemURI::getIdByURI($replyto);
905                 if (Post::exists(['uri-id' => $uriid])) {
906                         Logger::info('Post is a reply to an existing post - accepted', ['id' => $id, 'uri-id' => $uriid, 'replyto' => $replyto]);
907                         return true;
908                 }
909
910                 $attributed_to = JsonLD::fetchElement($activity['as:object'], 'as:attributedTo', '@id');
911                 $authorid = Contact::getIdForURL($attributed_to);
912
913                 $body = HTML::toBBCode(JsonLD::fetchElement($activity['as:object'], 'as:content', '@value'));
914
915                 $messageTags = [];
916                 $tags = Receiver::processTags(JsonLD::fetchElementArray($activity['as:object'], 'as:tag') ?? []);
917                 if (!empty($tags)) {
918                         foreach ($tags as $tag) {
919                                 if ($tag['type'] != 'Hashtag') {
920                                         continue;
921                                 }
922                                 $messageTags[] = ltrim(mb_strtolower($tag['name']), '#');
923                         }
924                 }
925
926                 return Relay::isSolicitedPost($messageTags, $body, $authorid, $id, Protocol::ACTIVITYPUB);
927         }
928
929         /**
930          * perform a "follow" request
931          *
932          * @param array $activity
933          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
934          * @throws \ImagickException
935          */
936         public static function followUser($activity)
937         {
938                 $uid = User::getIdForURL($activity['object_id']);
939                 if (empty($uid)) {
940                         return;
941                 }
942
943                 $owner = User::getOwnerDataById($uid);
944                 if (empty($owner)) {
945                         return;
946                 }
947
948                 $cid = Contact::getIdForURL($activity['actor'], $uid);
949                 if (!empty($cid)) {
950                         self::switchContact($cid);
951                         DBA::update('contact', ['hub-verify' => $activity['id'], 'protocol' => Protocol::ACTIVITYPUB], ['id' => $cid]);
952                 }
953
954                 $item = ['author-id' => Contact::getIdForURL($activity['actor']),
955                         'author-link' => $activity['actor']];
956
957                 // Ensure that the contact has got the right network type
958                 self::switchContact($item['author-id']);
959
960                 $result = Contact::addRelationship($owner, [], $item, false, $activity['content'] ?? '');
961                 if ($result === true) {
962                         ActivityPub\Transmitter::sendContactAccept($item['author-link'], $activity['id'], $owner['uid']);
963                 }
964
965                 $cid = Contact::getIdForURL($activity['actor'], $uid);
966                 if (empty($cid)) {
967                         return;
968                 }
969
970                 if (empty($contact)) {
971                         DBA::update('contact', ['hub-verify' => $activity['id'], 'protocol' => Protocol::ACTIVITYPUB], ['id' => $cid]);
972                 }
973
974                 Logger::log('Follow user ' . $uid . ' from contact ' . $cid . ' with id ' . $activity['id']);
975         }
976
977         /**
978          * Update the given profile
979          *
980          * @param array $activity
981          * @throws \Exception
982          */
983         public static function updatePerson($activity)
984         {
985                 if (empty($activity['object_id'])) {
986                         return;
987                 }
988
989                 Logger::info('Updating profile', ['object' => $activity['object_id']]);
990                 Contact::updateFromProbeByURL($activity['object_id']);
991         }
992
993         /**
994          * Delete the given profile
995          *
996          * @param array $activity
997          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
998          */
999         public static function deletePerson($activity)
1000         {
1001                 if (empty($activity['object_id']) || empty($activity['actor'])) {
1002                         Logger::info('Empty object id or actor.');
1003                         return;
1004                 }
1005
1006                 if ($activity['object_id'] != $activity['actor']) {
1007                         Logger::info('Object id does not match actor.');
1008                         return;
1009                 }
1010
1011                 $contacts = DBA::select('contact', ['id'], ['nurl' => Strings::normaliseLink($activity['object_id'])]);
1012                 while ($contact = DBA::fetch($contacts)) {
1013                         Contact::remove($contact['id']);
1014                 }
1015                 DBA::close($contacts);
1016
1017                 Logger::info('Deleted contact', ['object' => $activity['object_id']]);
1018         }
1019
1020         /**
1021          * Accept a follow request
1022          *
1023          * @param array $activity
1024          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1025          * @throws \ImagickException
1026          */
1027         public static function acceptFollowUser($activity)
1028         {
1029                 $uid = User::getIdForURL($activity['object_actor']);
1030                 if (empty($uid)) {
1031                         return;
1032                 }
1033
1034                 $cid = Contact::getIdForURL($activity['actor'], $uid);
1035                 if (empty($cid)) {
1036                         Logger::info('No contact found', ['actor' => $activity['actor']]);
1037                         return;
1038                 }
1039
1040                 self::switchContact($cid);
1041
1042                 $fields = ['pending' => false];
1043
1044                 $contact = DBA::selectFirst('contact', ['rel'], ['id' => $cid]);
1045                 if ($contact['rel'] == Contact::FOLLOWER) {
1046                         $fields['rel'] = Contact::FRIEND;
1047                 }
1048
1049                 $condition = ['id' => $cid];
1050                 DBA::update('contact', $fields, $condition);
1051                 Logger::info('Accept contact request', ['contact' => $cid, 'user' => $uid]);
1052         }
1053
1054         /**
1055          * Reject a follow request
1056          *
1057          * @param array $activity
1058          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1059          * @throws \ImagickException
1060          */
1061         public static function rejectFollowUser($activity)
1062         {
1063                 $uid = User::getIdForURL($activity['object_actor']);
1064                 if (empty($uid)) {
1065                         return;
1066                 }
1067
1068                 $cid = Contact::getIdForURL($activity['actor'], $uid);
1069                 if (empty($cid)) {
1070                         Logger::info('No contact found', ['actor' => $activity['actor']]);
1071                         return;
1072                 }
1073
1074                 self::switchContact($cid);
1075
1076                 if (DBA::exists('contact', ['id' => $cid, 'rel' => Contact::SHARING])) {
1077                         Contact::remove($cid);
1078                         Logger::info('Rejected contact request - contact removed', ['contact' => $cid, 'user' => $uid]);
1079                 } else {
1080                         Logger::info('Rejected contact request', ['contact' => $cid, 'user' => $uid]);
1081                 }
1082         }
1083
1084         /**
1085          * Undo activity like "like" or "dislike"
1086          *
1087          * @param array $activity
1088          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1089          * @throws \ImagickException
1090          */
1091         public static function undoActivity($activity)
1092         {
1093                 if (empty($activity['object_id'])) {
1094                         return;
1095                 }
1096
1097                 if (empty($activity['object_actor'])) {
1098                         return;
1099                 }
1100
1101                 $author_id = Contact::getIdForURL($activity['object_actor']);
1102                 if (empty($author_id)) {
1103                         return;
1104                 }
1105
1106                 Item::markForDeletion(['uri' => $activity['object_id'], 'author-id' => $author_id, 'gravity' => GRAVITY_ACTIVITY]);
1107         }
1108
1109         /**
1110          * Activity to remove a follower
1111          *
1112          * @param array $activity
1113          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1114          * @throws \ImagickException
1115          */
1116         public static function undoFollowUser($activity)
1117         {
1118                 $uid = User::getIdForURL($activity['object_object']);
1119                 if (empty($uid)) {
1120                         return;
1121                 }
1122
1123                 $owner = User::getOwnerDataById($uid);
1124                 if (empty($owner)) {
1125                         return;
1126                 }
1127
1128                 $cid = Contact::getIdForURL($activity['actor'], $uid);
1129                 if (empty($cid)) {
1130                         Logger::info('No contact found', ['actor' => $activity['actor']]);
1131                         return;
1132                 }
1133
1134                 self::switchContact($cid);
1135
1136                 $contact = DBA::selectFirst('contact', [], ['id' => $cid]);
1137                 if (!DBA::isResult($contact)) {
1138                         return;
1139                 }
1140
1141                 Contact::removeFollower($owner, $contact);
1142                 Logger::info('Undo following request', ['contact' => $cid, 'user' => $uid]);
1143         }
1144
1145         /**
1146          * Switches a contact to AP if needed
1147          *
1148          * @param integer $cid Contact ID
1149          * @throws \Exception
1150          */
1151         private static function switchContact($cid)
1152         {
1153                 $contact = DBA::selectFirst('contact', ['network', 'url'], ['id' => $cid]);
1154                 if (!DBA::isResult($contact) || in_array($contact['network'], [Protocol::ACTIVITYPUB, Protocol::DFRN]) || Contact::isLocal($contact['url'])) {
1155                         return;
1156                 }
1157
1158                 Logger::info('Change existing contact', ['cid' => $cid, 'previous' => $contact['network']]);
1159                 Contact::updateFromProbe($cid);
1160         }
1161
1162         /**
1163          * Collects implicit mentions like:
1164          * - the author of the parent item
1165          * - all the mentioned conversants in the parent item
1166          *
1167          * @param array $parent Item array with at least ['id', 'author-link', 'alias']
1168          * @return array
1169          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1170          */
1171         private static function getImplicitMentionList(array $parent)
1172         {
1173                 $parent_terms = Tag::getByURIId($parent['uri-id'], [Tag::MENTION, Tag::IMPLICIT_MENTION, Tag::EXCLUSIVE_MENTION]);
1174
1175                 $parent_author = Contact::getByURL($parent['author-link'], false, ['url', 'nurl', 'alias']);
1176
1177                 $implicit_mentions = [];
1178                 if (empty($parent_author['url'])) {
1179                         Logger::notice('Author public contact unknown.', ['author-link' => $parent['author-link'], 'parent-id' => $parent['id']]);
1180                 } else {
1181                         $implicit_mentions[] = $parent_author['url'];
1182                         $implicit_mentions[] = $parent_author['nurl'];
1183                         $implicit_mentions[] = $parent_author['alias'];
1184                 }
1185
1186                 if (!empty($parent['alias'])) {
1187                         $implicit_mentions[] = $parent['alias'];
1188                 }
1189
1190                 foreach ($parent_terms as $term) {
1191                         $contact = Contact::getByURL($term['url'], false, ['url', 'nurl', 'alias']);
1192                         if (!empty($contact['url'])) {
1193                                 $implicit_mentions[] = $contact['url'];
1194                                 $implicit_mentions[] = $contact['nurl'];
1195                                 $implicit_mentions[] = $contact['alias'];
1196                         }
1197                 }
1198
1199                 return $implicit_mentions;
1200         }
1201
1202         /**
1203          * Strips from the body prepended implicit mentions
1204          *
1205          * @param string $body
1206          * @param array $parent
1207          * @return string
1208          */
1209         private static function removeImplicitMentionsFromBody(string $body, array $parent)
1210         {
1211                 if (DI::config()->get('system', 'disable_implicit_mentions')) {
1212                         return $body;
1213                 }
1214
1215                 $potential_mentions = self::getImplicitMentionList($parent);
1216
1217                 $kept_mentions = [];
1218
1219                 // Extract one prepended mention at a time from the body
1220                 while(preg_match('#^(@\[url=([^\]]+)].*?\[\/url]\s)(.*)#is', $body, $matches)) {
1221                         if (!in_array($matches[2], $potential_mentions)) {
1222                                 $kept_mentions[] = $matches[1];
1223                         }
1224
1225                         $body = $matches[3];
1226                 }
1227
1228                 // Re-appending the kept mentions to the body after extraction
1229                 $kept_mentions[] = $body;
1230
1231                 return implode('', $kept_mentions);
1232         }
1233 }