3 * @copyright Copyright (C) 2010-2021, the Friendica project
5 * @license GNU AGPL version 3 or any later version
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.
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.
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/>.
22 namespace Friendica\Protocol\ActivityPub;
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;
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;
51 * ActivityPub Processor Protocol class
56 * Converts mentions from Pleroma into the Friendica format
60 * @return string converted body
62 private static function convertMentions($body)
64 $URLSearchString = "^\[\]";
65 $body = preg_replace("/\[url\=([$URLSearchString]*)\]([#@!])(.*?)\[\/url\]/ism", '$2[url=$1]$3[/url]', $body);
71 * Convert the language array into a language JSON
73 * @param array $languages
74 * @return string language JSON
76 private static function processLanguages(array $languages)
78 $codes = array_keys($languages);
80 foreach ($codes as $code) {
88 return json_encode($lang);
91 * Replaces emojis in the body
93 * @param array $emojis
96 * @return string with replaced emojis
98 private static function replaceEmojis($body, array $emojis)
102 array_column($emojis, 'name'),
103 array_map(function ($emoji) {
104 return '[class=emoji mastodon][img=' . $emoji['href'] . ']' . $emoji['name'] . '[/img][/class]';
113 * Store attached media files in the post-media table
116 * @param array $attachment
119 private static function storeAttachmentAsMedia(int $uriid, array $attachment)
121 if (empty($attachment['url'])) {
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;
134 Post\Media::insert($data);
138 * Add attachment data to the item array
140 * @param array $activity
143 * @return array array
145 private static function constructAttachList($activity, $item)
147 if (empty($activity['attachments'])) {
154 foreach ($activity['attachments'] as $attach) {
155 switch ($attach['type']) {
158 'url' => $attach['url'],
159 'type' => $attach['type'],
160 'title' => $attach['title'] ?? '',
161 'text' => $attach['desc'] ?? '',
162 'image' => $attach['image'] ?? '',
166 $item['body'] = PageInfo::appendDataToBody($item['body'], $data);
169 self::storeAttachmentAsMedia($item['uri-id'], $attach);
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'])) {
180 if (strpos($activity['source'], $attach['url'])) {
185 // image is the preview/thumbnail URL
186 if (!empty($attach['image'])) {
187 $media = '[url=' . $attach['url'] . ']';
188 $attach['url'] = $attach['image'];
193 if (empty($attach['name'])) {
194 $media .= '[img]' . $attach['url'] . '[/img]';
196 $media .= '[img=' . $attach['url'] . ']' . $attach['name'] . '[/img]';
199 if (!empty($attach['image'])) {
203 if ($item['post-type'] == Item::PT_IMAGE) {
208 } elseif ($filetype == 'audio') {
209 if (!empty($activity['source']) && strpos($activity['source'], $attach['url'])) {
213 if ($item['post-type'] == Item::PT_AUDIO) {
214 $leading .= '[audio]' . $attach['url'] . "[/audio]\n";
216 $trailing .= '[audio]' . $attach['url'] . "[/audio]\n";
218 } elseif ($filetype == 'video') {
219 if (!empty($activity['source']) && strpos($activity['source'], $attach['url'])) {
223 if ($item['post-type'] == Item::PT_VIDEO) {
224 $leading .= '[video]' . $attach['url'] . "[/video]\n";
226 $trailing .= '[video]' . $attach['url'] . "[/video]\n";
232 if (!empty($leading) && !empty(trim($item['body']))) {
233 $item['body'] = $leading . "[hr]\n" . $item['body'];
234 } elseif (!empty($leading)) {
235 $item['body'] = $leading;
238 if (!empty($trailing) && !empty(trim($item['body']))) {
239 $item['body'] = $item['body'] . "\n[hr]" . $trailing;
240 } elseif (!empty($trailing)) {
241 $item['body'] = $trailing;
250 * @param array $activity Activity array
251 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
253 public static function updateItem($activity)
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);
263 $item['changed'] = DateTimeFormat::utcNow();
264 $item['edited'] = DateTimeFormat::utc($activity['updated']);
266 $item = self::processContent($activity, $item);
268 $item = self::constructAttachList($activity, $item);
274 Item::update($item, ['uri' => $activity['id']]);
278 * Prepares data for a message
280 * @param array $activity Activity array
281 * @return array Internal item
282 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
283 * @throws \ImagickException
285 public static function createItem($activity)
288 $item['verb'] = Activity::POST;
289 $item['thr-parent'] = $activity['reply-to-id'];
291 if ($activity['reply-to-id'] == $activity['id']) {
292 $item['gravity'] = GRAVITY_PARENT;
293 $item['object-type'] = Activity\ObjectType::NOTE;
295 $item['gravity'] = GRAVITY_COMMENT;
296 $item['object-type'] = Activity\ObjectType::COMMENT;
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);
304 $item['diaspora_signed_text'] = $activity['diaspora:comment'] ?? '';
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']]);
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']);
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;
323 $item['private'] = Item::PRIVATE;
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'] ?? '';
332 if (isset($activity['push'])) {
333 $item['direction'] = $activity['push'] ? Conversation::PUSH : Conversation::PULL;
337 if (!empty($activity['from-relay'])) {
338 $item['direction'] = Conversation::RELAY;
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;
356 $item['post-type'] = Item::PT_NOTE;
359 $item['isForum'] = false;
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']]);
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']]);
374 $item['owner-link'] = $item['author-link'];
375 $item['owner-id'] = $item['author-id'];
377 $actor = APContact::getByURL($item['owner-link'], false);
378 $item['isForum'] = ($actor['type'] == 'Group');
381 $item['uri'] = $activity['id'];
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;
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']]);
394 $item = self::processContent($activity, $item);
396 Logger::info('Message was not processed');
400 $item['plink'] = $activity['alternate-url'] ?? $item['uri'];
402 $item = self::constructAttachList($activity, $item);
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);
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);
423 * @param array $activity
424 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
425 * @throws \ImagickException
427 public static function deleteItem($activity)
429 $owner = Contact::getIdForURL($activity['actor']);
431 Logger::info('Deleting item', ['object' => $activity['object_id'], 'owner' => $owner]);
432 Item::markForDeletion(['uri' => $activity['object_id'], 'owner-id' => $owner]);
436 * Prepare the item array for an activity
438 * @param array $activity Activity array
439 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
440 * @throws \ImagickException
442 public static function addTag($activity)
444 if (empty($activity['object_content']) || empty($activity['object_id'])) {
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
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']]);
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']]);
466 * Prepare the item array for an activity
468 * @param array $activity Activity array
469 * @param string $verb Activity verb
470 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
471 * @throws \ImagickException
473 public static function createActivity($activity, $verb)
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;
482 $item['diaspora_signed_text'] = $activity['diaspora:like'] ?? '';
484 self::postItem($activity, $item);
490 * @param array $activity Activity array
494 public static function createEvent($activity, $item)
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'];
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'];
521 $event_id = Event::store($event);
522 Logger::info('Event was stored', ['id' => $event_id]);
526 * Process the content
528 * @param array $activity Activity array
530 * @return array|bool Returns the item array or false if there was an unexpected occurrence
533 private static function processContent($activity, $item)
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'];
542 // By default assume "text/html"
543 $item['title'] = HTML::toBBCode($activity['name']);
544 $content = HTML::toBBCode($activity['content']);
547 if (!empty($activity['languages'])) {
548 $item['language'] = self::processLanguages($activity['languages']);
551 if (!empty($activity['emojis'])) {
552 $content = self::replaceEmojis($content, $activity['emojis']);
555 $content = self::convertMentions($content);
557 if (!empty($activity['source'])) {
558 $item['body'] = $activity['source'];
559 $item['raw-body'] = $content;
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']]);
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']]);
573 $content = self::removeImplicitMentionsFromBody($content, $parent);
575 $item['content-warning'] = HTML::toBBCode($activity['summary']);
576 $item['raw-body'] = $item['body'] = $content;
579 self::storeFromBody($item);
580 self::storeTags($item['uri-id'], $activity['tags']);
582 $item['location'] = $activity['location'];
584 if (!empty($activity['latitude']) && !empty($activity['longitude'])) {
585 $item['coord'] = $activity['latitude'] . ' ' . $activity['longitude'];
588 $item['app'] = $activity['generator'];
594 * Store hashtags and mentions
598 private static function storeFromBody(array $item)
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']]);
603 Tag::storeFromBody($item['uri-id'], $item['body'], '@!');
607 * Generate a GUID out of an URL
609 * @param string $url message URL
610 * @return string with GUID
612 private static function getGUIDByURL(string $url)
614 $parsed = parse_url($url);
616 $host_hash = hash('crc32', $parsed['host']);
618 unset($parsed["scheme"]);
619 unset($parsed["host"]);
621 $path = implode("/", $parsed);
623 return $host_hash . '-'. hash('fnv164', $path) . '-'. hash('joaat', $path);
627 * Creates an item post
629 * @param array $activity Activity data
630 * @param array $item item array
631 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
632 * @throws \ImagickException
634 public static function postItem(array $activity, array $item)
641 ksort($activity['receiver']);
643 foreach ($activity['receiver'] as $receiver) {
644 if ($receiver == -1) {
648 $item['uid'] = $receiver;
650 $type = $activity['reception_type'][$receiver] ?? Receiver::TARGET_UNKNOWN;
652 case Receiver::TARGET_TO:
653 $item['post-reason'] = Item::PR_TO;
655 case Receiver::TARGET_CC:
656 $item['post-reason'] = Item::PR_CC;
658 case Receiver::TARGET_BTO:
659 $item['post-reason'] = Item::PR_BTO;
661 case Receiver::TARGET_BCC:
662 $item['post-reason'] = Item::PR_BCC;
664 case Receiver::TARGET_FOLLOWER:
665 $item['post-reason'] = Item::PR_FOLLOWER;
667 case Receiver::TARGET_ANSWER:
668 $item['post-reason'] = Item::PR_COMMENT;
670 case Receiver::TARGET_GLOBAL:
671 $item['post-reason'] = Item::PR_GLOBAL;
674 $item['post-reason'] = Item::PR_NONE;
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;
683 if ($item['isForum'] ?? false) {
684 $item['contact-id'] = Contact::getIdForURL($activity['actor'], $receiver);
686 $item['contact-id'] = Contact::getIdForURL($activity['author'], $receiver);
689 if (($receiver != 0) && empty($item['contact-id'])) {
690 $item['contact-id'] = Contact::getIdForURL($activity['author']);
693 if (!empty($activity['directmessage'])) {
694 self::postMail($activity, $item);
698 if (DI::pConfig()->get($receiver, 'system', 'accept_only_sharer', false) && ($receiver != 0) && ($item['gravity'] == GRAVITY_PARENT)) {
699 $skip = !Contact::isSharingByURL($activity['author'], $receiver);
701 if ($skip && (($activity['type'] == 'as:Announce') || ($item['isForum'] ?? false))) {
702 $skip = !Contact::isSharingByURL($activity['actor'], $receiver);
706 Logger::info('Skipping post', ['uid' => $receiver, 'url' => $item['uri']]);
710 Logger::info('Accepting post', ['uid' => $receiver, 'url' => $item['uri']]);
713 if (($item['gravity'] != GRAVITY_ACTIVITY) && ($activity['object_type'] == 'as:Event')) {
714 self::createEvent($activity, $item);
717 $item_id = Item::insert($item);
719 Logger::info('Item insertion successful', ['user' => $item['uid'], 'item_id' => $item_id]);
721 Logger::notice('Item insertion aborted', ['user' => $item['uid']]);
724 if ($item['uid'] == 0) {
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']);
741 * Store tags and mentions into the tag table
743 * @param integer $uriid
746 private static function storeTags(int $uriid, array $tags = null)
748 foreach ($tags as $tag) {
749 if (empty($tag['name']) || empty($tag['type']) || !in_array($tag['type'], ['Mention', 'Hashtag'])) {
753 $hash = substr($tag['name'], 0, 1);
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);
761 $type = Tag::IMPLICIT_MENTION;
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'];
769 } elseif ($tag['type'] == 'Hashtag') {
770 if ($hash == Tag::TAG_CHARACTER[Tag::HASHTAG]) {
771 $tag['name'] = substr($tag['name'], 1);
773 $type = Tag::HASHTAG;
776 if (empty($tag['name'])) {
780 Tag::store($uriid, $type, $tag['name'], $tag['href']);
785 * Creates an mail post
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
792 private static function postMail($activity, $item)
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']]);
799 Logger::info('Direct Message', $item);
802 $msg['uid'] = $item['uid'];
804 $msg['contact-id'] = $item['contact-id'];
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'];
811 $msg['uri'] = $item['uri'];
812 $msg['created'] = $item['created'];
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'];
819 $msg['parent-uri'] = $item['thr-parent'];
821 if (!empty($item['title'])) {
822 $msg['title'] = $item['title'];
823 } elseif (!empty($item['content-warning'])) {
824 $msg['title'] = $item['content-warning'];
826 // Trying to generate a title out of the body
827 $title = $item['body'];
829 while (preg_match('#^(@\[url=([^\]]+)].*?\[\/url]\s)(.*)#is', $title, $matches)) {
830 $title = $matches[3];
833 $title = trim(HTML::toPlaintext(BBCode::convert($title, false, BBCode::API, true), 0));
835 if (strlen($title) > 20) {
836 $title = substr($title, 0, 20) . '...';
839 $msg['title'] = $title;
842 $msg['body'] = $item['body'];
844 return Mail::insert($msg);
848 * Fetches missing posts
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
856 public static function fetchMissingActivity(string $url, array $child = [], string $relay_actor = '')
858 if (!empty($child['receiver'])) {
859 $uid = ActivityPub\Receiver::getFirstUserFromReceivers($child['receiver']);
864 $object = ActivityPub::fetchContent($url, $uid);
865 if (empty($object)) {
866 Logger::log('Activity ' . $url . ' was not fetchable, aborting.');
870 if (empty($object['id'])) {
871 Logger::log('Activity ' . $url . ' has got not id, aborting. ' . json_encode($object));
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');
888 $signer = [$object_actor];
890 if (!empty($child['author'])) {
891 $actor = $child['author'];
894 $actor = $object_actor;
897 if (!empty($object['published'])) {
898 $published = $object['published'];
899 } elseif (!empty($child['published'])) {
900 $published = $child['published'];
902 $published = DateTimeFormat::utcNow();
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';
916 $ldactivity = JsonLD::compact($activity);
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'];
923 $ldactivity['thread-completion'] = Contact::getIdForURL($actor);
926 if (!empty($relay_actor) && !self::acceptIncomingMessage($ldactivity, $object['id'])) {
930 ActivityPub\Receiver::processActivity($ldactivity, json_encode($activity), $uid, true, false, $signer);
932 Logger::notice('Activity had been fetched and processed.', ['url' => $url, 'object' => $activity['id']]);
934 return $activity['id'];
938 * Test if incoming relay messages should be accepted
940 * @param array $activity activity array
941 * @param string $id object ID
942 * @return boolean true if message is accepted
944 private static function acceptIncomingMessage(array $activity, string $id)
946 if (empty($activity['as:object'])) {
947 Logger::info('No object field in activity - accepted', ['id' => $id]);
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]);
958 $attributed_to = JsonLD::fetchElement($activity['as:object'], 'as:attributedTo', '@id');
959 $authorid = Contact::getIdForURL($attributed_to);
961 $body = HTML::toBBCode(JsonLD::fetchElement($activity['as:object'], 'as:content', '@value'));
964 $tags = Receiver::processTags(JsonLD::fetchElementArray($activity['as:object'], 'as:tag') ?? []);
966 foreach ($tags as $tag) {
967 if ($tag['type'] != 'Hashtag') {
970 $messageTags[] = ltrim(mb_strtolower($tag['name']), '#');
974 return Relay::isSolicitedPost($messageTags, $body, $authorid, $id, Protocol::ACTIVITYPUB);
978 * perform a "follow" request
980 * @param array $activity
981 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
982 * @throws \ImagickException
984 public static function followUser($activity)
986 $uid = User::getIdForURL($activity['object_id']);
991 $owner = User::getOwnerDataById($uid);
996 $cid = Contact::getIdForURL($activity['actor'], $uid);
998 self::switchContact($cid);
999 DBA::update('contact', ['hub-verify' => $activity['id'], 'protocol' => Protocol::ACTIVITYPUB], ['id' => $cid]);
1002 $item = ['author-id' => Contact::getIdForURL($activity['actor']),
1003 'author-link' => $activity['actor']];
1005 // Ensure that the contact has got the right network type
1006 self::switchContact($item['author-id']);
1008 $result = Contact::addRelationship($owner, [], $item, false, $activity['content'] ?? '');
1009 if ($result === true) {
1010 ActivityPub\Transmitter::sendContactAccept($item['author-link'], $activity['id'], $owner['uid']);
1013 $cid = Contact::getIdForURL($activity['actor'], $uid);
1018 if (empty($contact)) {
1019 DBA::update('contact', ['hub-verify' => $activity['id'], 'protocol' => Protocol::ACTIVITYPUB], ['id' => $cid]);
1022 Logger::log('Follow user ' . $uid . ' from contact ' . $cid . ' with id ' . $activity['id']);
1026 * Update the given profile
1028 * @param array $activity
1029 * @throws \Exception
1031 public static function updatePerson($activity)
1033 if (empty($activity['object_id'])) {
1037 Logger::info('Updating profile', ['object' => $activity['object_id']]);
1038 Contact::updateFromProbeByURL($activity['object_id']);
1042 * Delete the given profile
1044 * @param array $activity
1045 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1047 public static function deletePerson($activity)
1049 if (empty($activity['object_id']) || empty($activity['actor'])) {
1050 Logger::info('Empty object id or actor.');
1054 if ($activity['object_id'] != $activity['actor']) {
1055 Logger::info('Object id does not match actor.');
1059 $contacts = DBA::select('contact', ['id'], ['nurl' => Strings::normaliseLink($activity['object_id'])]);
1060 while ($contact = DBA::fetch($contacts)) {
1061 Contact::remove($contact['id']);
1063 DBA::close($contacts);
1065 Logger::info('Deleted contact', ['object' => $activity['object_id']]);
1069 * Accept a follow request
1071 * @param array $activity
1072 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1073 * @throws \ImagickException
1075 public static function acceptFollowUser($activity)
1077 $uid = User::getIdForURL($activity['object_actor']);
1082 $cid = Contact::getIdForURL($activity['actor'], $uid);
1084 Logger::info('No contact found', ['actor' => $activity['actor']]);
1088 self::switchContact($cid);
1090 $fields = ['pending' => false];
1092 $contact = DBA::selectFirst('contact', ['rel'], ['id' => $cid]);
1093 if ($contact['rel'] == Contact::FOLLOWER) {
1094 $fields['rel'] = Contact::FRIEND;
1097 $condition = ['id' => $cid];
1098 DBA::update('contact', $fields, $condition);
1099 Logger::info('Accept contact request', ['contact' => $cid, 'user' => $uid]);
1103 * Reject a follow request
1105 * @param array $activity
1106 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1107 * @throws \ImagickException
1109 public static function rejectFollowUser($activity)
1111 $uid = User::getIdForURL($activity['object_actor']);
1116 $cid = Contact::getIdForURL($activity['actor'], $uid);
1118 Logger::info('No contact found', ['actor' => $activity['actor']]);
1122 self::switchContact($cid);
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]);
1128 Logger::info('Rejected contact request', ['contact' => $cid, 'user' => $uid]);
1133 * Undo activity like "like" or "dislike"
1135 * @param array $activity
1136 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1137 * @throws \ImagickException
1139 public static function undoActivity($activity)
1141 if (empty($activity['object_id'])) {
1145 if (empty($activity['object_actor'])) {
1149 $author_id = Contact::getIdForURL($activity['object_actor']);
1150 if (empty($author_id)) {
1154 Item::markForDeletion(['uri' => $activity['object_id'], 'author-id' => $author_id, 'gravity' => GRAVITY_ACTIVITY]);
1158 * Activity to remove a follower
1160 * @param array $activity
1161 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1162 * @throws \ImagickException
1164 public static function undoFollowUser($activity)
1166 $uid = User::getIdForURL($activity['object_object']);
1171 $owner = User::getOwnerDataById($uid);
1172 if (empty($owner)) {
1176 $cid = Contact::getIdForURL($activity['actor'], $uid);
1178 Logger::info('No contact found', ['actor' => $activity['actor']]);
1182 self::switchContact($cid);
1184 $contact = DBA::selectFirst('contact', [], ['id' => $cid]);
1185 if (!DBA::isResult($contact)) {
1189 Contact::removeFollower($owner, $contact);
1190 Logger::info('Undo following request', ['contact' => $cid, 'user' => $uid]);
1194 * Switches a contact to AP if needed
1196 * @param integer $cid Contact ID
1197 * @throws \Exception
1199 private static function switchContact($cid)
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'])) {
1206 Logger::info('Change existing contact', ['cid' => $cid, 'previous' => $contact['network']]);
1207 Contact::updateFromProbe($cid);
1211 * Collects implicit mentions like:
1212 * - the author of the parent item
1213 * - all the mentioned conversants in the parent item
1215 * @param array $parent Item array with at least ['id', 'author-link', 'alias']
1217 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1219 private static function getImplicitMentionList(array $parent)
1221 $parent_terms = Tag::getByURIId($parent['uri-id'], [Tag::MENTION, Tag::IMPLICIT_MENTION, Tag::EXCLUSIVE_MENTION]);
1223 $parent_author = Contact::getByURL($parent['author-link'], false, ['url', 'nurl', 'alias']);
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']]);
1229 $implicit_mentions[] = $parent_author['url'];
1230 $implicit_mentions[] = $parent_author['nurl'];
1231 $implicit_mentions[] = $parent_author['alias'];
1234 if (!empty($parent['alias'])) {
1235 $implicit_mentions[] = $parent['alias'];
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'];
1247 return $implicit_mentions;
1251 * Strips from the body prepended implicit mentions
1253 * @param string $body
1254 * @param array $parent
1257 private static function removeImplicitMentionsFromBody(string $body, array $parent)
1259 if (DI::config()->get('system', 'disable_implicit_mentions')) {
1263 $potential_mentions = self::getImplicitMentionList($parent);
1265 $kept_mentions = [];
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];
1273 $body = $matches[3];
1276 // Re-appending the kept mentions to the body after extraction
1277 $kept_mentions[] = $body;
1279 return implode('', $kept_mentions);