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