]> git.mxchange.org Git - friendica.git/blobdiff - src/Protocol/ActivityPub/Processor.php
normalise_link calls
[friendica.git] / src / Protocol / ActivityPub / Processor.php
index 166530f472729d2312e5b79769035b541e3c5364..bff8767f380a4a76a9ff43c6892b623f2110e1d7 100644 (file)
@@ -5,22 +5,23 @@
 namespace Friendica\Protocol\ActivityPub;
 
 use Friendica\Database\DBA;
+use Friendica\Content\Text\HTML;
+use Friendica\Core\Config;
+use Friendica\Core\Logger;
 use Friendica\Core\Protocol;
 use Friendica\Model\Conversation;
 use Friendica\Model\Contact;
 use Friendica\Model\APContact;
 use Friendica\Model\Item;
+use Friendica\Model\Event;
 use Friendica\Model\User;
-use Friendica\Content\Text\HTML;
-use Friendica\Util\JsonLD;
-use Friendica\Core\Config;
 use Friendica\Protocol\ActivityPub;
+use Friendica\Util\DateTimeFormat;
+use Friendica\Util\JsonLD;
+use Friendica\Util\Strings;
 
 /**
- * ActivityPub Protocol class
- *
- * To-Do:
- * - Store Diaspora signature
+ * ActivityPub Processor Protocol class
  */
 class Processor
 {
@@ -39,6 +40,23 @@ class Processor
                return $body;
        }
 
+       /**
+        * Replaces emojis in the body
+        *
+        * @param array $emojis
+        * @param string $body
+        *
+        * @return string with replaced emojis
+        */
+       public static function replaceEmojis($emojis, $body)
+       {
+               foreach ($emojis as $emoji) {
+                       $replace = '[class=emoji mastodon][img=' . $emoji['href'] . ']' . $emoji['name'] . '[/img][/class]';
+                       $body = str_replace($emoji['name'], $replace, $body);
+               }
+               return $body;
+       }
+
        /**
         * Constructs a string with tags for a given tag array
         *
@@ -103,6 +121,25 @@ class Processor
                return $item;
        }
 
+       /**
+        * Updates a message
+        *
+        * @param array  $activity Activity array
+        */
+       public static function updateItem($activity)
+       {
+               $item = [];
+               $item['changed'] = DateTimeFormat::utcNow();
+               $item['edited'] = $activity['updated'];
+               $item['title'] = HTML::toBBCode($activity['name']);
+               $item['content-warning'] = HTML::toBBCode($activity['summary']);
+               $content = self::replaceEmojis($activity['emojis'], HTML::toBBCode($activity['content']));
+               $item['body'] = self::convertMentions($content);
+               $item['tag'] = self::constructTagList($activity['tags'], $activity['sensitive']);
+
+               Item::update($item, ['uri' => $activity['id']]);
+       }
+
        /**
         * Prepares data for a message
         *
@@ -123,25 +160,11 @@ class Processor
                }
 
                if (($activity['id'] != $activity['reply-to-id']) && !Item::exists(['uri' => $activity['reply-to-id']])) {
-                       logger('Parent ' . $activity['reply-to-id'] . ' not found. Try to refetch it.');
+                       Logger::log('Parent ' . $activity['reply-to-id'] . ' not found. Try to refetch it.');
                        self::fetchMissingActivity($activity['reply-to-id'], $activity);
                }
 
-               self::postItem($activity, $item);
-       }
-
-       /**
-        * Prepare the item array for a "like"
-        *
-        * @param array  $activity Activity array
-        */
-       public static function likeItem($activity)
-       {
-               $item = [];
-               $item['verb'] = ACTIVITY_LIKE;
-               $item['parent-uri'] = $activity['object_id'];
-               $item['gravity'] = GRAVITY_ACTIVITY;
-               $item['object-type'] = ACTIVITY_OBJ_NOTE;
+               $item['diaspora_signed_text'] = defaults($activity, 'diaspora:comment', '');
 
                self::postItem($activity, $item);
        }
@@ -155,26 +178,62 @@ class Processor
        {
                $owner = Contact::getIdForURL($activity['actor']);
 
-               logger('Deleting item ' . $activity['object_id'] . ' from ' . $owner, LOGGER_DEBUG);
+               Logger::log('Deleting item ' . $activity['object_id'] . ' from ' . $owner, Logger::DEBUG);
                Item::delete(['uri' => $activity['object_id'], 'owner-id' => $owner]);
        }
 
        /**
-        * Prepare the item array for a "dislike"
+        * Prepare the item array for an activity
         *
         * @param array  $activity Activity array
+        * @param string $verb     Activity verb
         */
-       public static function dislikeItem($activity)
+       public static function createActivity($activity, $verb)
        {
                $item = [];
-               $item['verb'] = ACTIVITY_DISLIKE;
+               $item['verb'] = $verb;
                $item['parent-uri'] = $activity['object_id'];
                $item['gravity'] = GRAVITY_ACTIVITY;
                $item['object-type'] = ACTIVITY_OBJ_NOTE;
 
+               $item['diaspora_signed_text'] = defaults($activity, 'diaspora:like', '');
+
                self::postItem($activity, $item);
        }
 
+       /**
+        * Create an event
+        *
+        * @param array $activity Activity array
+        * @param array $item
+        */
+       public static function createEvent($activity, $item)
+       {
+               $event['summary'] = $activity['name'];
+               $event['desc'] = $activity['content'];
+               $event['start'] = $activity['start-time'];
+               $event['finish'] = $activity['end-time'];
+               $event['nofinish'] = empty($event['finish']);
+               $event['location'] = $activity['location'];
+               $event['adjust'] = true;
+               $event['cid'] = $item['contact-id'];
+               $event['uid'] = $item['uid'];
+               $event['uri'] = $item['uri'];
+               $event['edited'] = $item['edited'];
+               $event['private'] = $item['private'];
+               $event['guid'] = $item['guid'];
+               $event['plink'] = $item['plink'];
+
+               $condition = ['uri' => $item['uri'], 'uid' => $item['uid']];
+               $ev = DBA::selectFirst('event', ['id'], $condition);
+               if (DBA::isResult($ev)) {
+                       $event['id'] = $ev['id'];
+               }
+
+               $event_id = Event::store($event);
+               Logger::log('Event '.$event_id.' was stored', Logger::DEBUG);
+       }
+
        /**
         * Creates an item post
         *
@@ -186,7 +245,7 @@ class Processor
                /// @todo What to do with $activity['context']?
 
                if (($item['gravity'] != GRAVITY_PARENT) && !Item::exists(['uri' => $item['parent-uri']])) {
-                       logger('Parent ' . $item['parent-uri'] . ' not found, message will be discarded.', LOGGER_DEBUG);
+                       Logger::log('Parent ' . $item['parent-uri'] . ' not found, message will be discarded.', Logger::DEBUG);
                        return;
                }
 
@@ -199,7 +258,7 @@ class Processor
                        $item['owner-link'] = $activity['actor'];
                        $item['owner-id'] = Contact::getIdForURL($activity['actor'], 0, true);
                } else {
-                       logger('Ignoring actor because of thread completion.', LOGGER_DEBUG);
+                       Logger::log('Ignoring actor because of thread completion.', Logger::DEBUG);
                        $item['owner-link'] = $item['author-link'];
                        $item['owner-id'] = $item['author-id'];
                }
@@ -210,12 +269,22 @@ class Processor
                $item['guid'] = $activity['diaspora:guid'];
                $item['title'] = HTML::toBBCode($activity['name']);
                $item['content-warning'] = HTML::toBBCode($activity['summary']);
-               $item['body'] = self::convertMentions(HTML::toBBCode($activity['content']));
+               $content = self::replaceEmojis($activity['emojis'], HTML::toBBCode($activity['content']));
+               $item['body'] = self::convertMentions($content);
+
+               if (($activity['object_type'] == 'as:Video') && !empty($activity['alternate-url'])) {
+                       $item['body'] .= "\n[video]" . $activity['alternate-url'] . '[/video]';
+               }
+
                $item['location'] = $activity['location'];
+
+               if (!empty($item['latitude']) && !empty($item['longitude'])) {
+                       $item['coord'] = $item['latitude'] . ' ' . $item['longitude'];
+               }
+
                $item['tag'] = self::constructTagList($activity['tags'], $activity['sensitive']);
-               $item['app'] = $activity['service'];
+               $item['app'] = $activity['generator'];
                $item['plink'] = defaults($activity, 'alternate-url', $item['uri']);
-               $item['diaspora_signed_text'] = defaults($activity, 'diaspora:comment', '');
 
                $item = self::constructAttachList($activity['attachments'], $item);
 
@@ -231,8 +300,12 @@ class Processor
                                $item['contact-id'] = Contact::getIdForURL($activity['author'], 0, true);
                        }
 
+                       if ($activity['object_type'] == 'as:Event') {
+                               self::createEvent($activity, $item);
+                       }
+
                        $item_id = Item::insert($item);
-                       logger('Storing for user ' . $item['uid'] . ': ' . $item_id);
+                       Logger::log('Storing for user ' . $item['uid'] . ': ' . $item_id);
                }
        }
 
@@ -248,9 +321,11 @@ class Processor
                        return;
                }
 
-               $object = ActivityPub::fetchContent($url);
+               $uid = ActivityPub\Receiver::getFirstUserFromReceivers($child['receiver']);
+
+               $object = ActivityPub::fetchContent($url, $uid);
                if (empty($object)) {
-                       logger('Activity ' . $url . ' was not fetchable, aborting.');
+                       Logger::log('Activity ' . $url . ' was not fetchable, aborting.');
                        return;
                }
 
@@ -262,7 +337,7 @@ class Processor
                $activity['cc'] = defaults($object, 'cc', []);
                $activity['actor'] = $child['author'];
                $activity['object'] = $object;
-               $activity['published'] = $object['published'];
+               $activity['published'] = defaults($object, 'published', $child['published']);
                $activity['type'] = 'Create';
 
                $ldactivity = JsonLD::compact($activity);
@@ -270,7 +345,7 @@ class Processor
                $ldactivity['thread-completion'] = true;
 
                ActivityPub\Receiver::processActivity($ldactivity);
-               logger('Activity ' . $url . ' had been fetched and processed.');
+               Logger::log('Activity ' . $url . ' had been fetched and processed.');
        }
 
        /**
@@ -298,6 +373,9 @@ class Processor
                $item = ['author-id' => Contact::getIdForURL($activity['actor']),
                        'author-link' => $activity['actor']];
 
+               // Ensure that the contact has got the right network type
+               self::switchContact($item['author-id']);
+
                Contact::addRelationship($owner, $contact, $item);
                $cid = Contact::getIdForURL($activity['actor'], $uid);
                if (empty($cid)) {
@@ -305,7 +383,7 @@ class Processor
                }
 
                DBA::update('contact', ['hub-verify' => $activity['id']], ['id' => $cid]);
-               logger('Follow user ' . $uid . ' from contact ' . $cid . ' with id ' . $activity['id']);
+               Logger::log('Follow user ' . $uid . ' from contact ' . $cid . ' with id ' . $activity['id']);
        }
 
        /**
@@ -319,7 +397,7 @@ class Processor
                        return;
                }
 
-               logger('Updating profile for ' . $activity['object_id'], LOGGER_DEBUG);
+               Logger::log('Updating profile for ' . $activity['object_id'], Logger::DEBUG);
                APContact::getByURL($activity['object_id'], true);
        }
 
@@ -331,22 +409,22 @@ class Processor
        public static function deletePerson($activity)
        {
                if (empty($activity['object_id']) || empty($activity['actor'])) {
-                       logger('Empty object id or actor.', LOGGER_DEBUG);
+                       Logger::log('Empty object id or actor.', Logger::DEBUG);
                        return;
                }
 
                if ($activity['object_id'] != $activity['actor']) {
-                       logger('Object id does not match actor.', LOGGER_DEBUG);
+                       Logger::log('Object id does not match actor.', Logger::DEBUG);
                        return;
                }
 
-               $contacts = DBA::select('contact', ['id'], ['nurl' => normalise_link($activity['object_id'])]);
+               $contacts = DBA::select('contact', ['id'], ['nurl' => Strings::normaliseLink($activity['object_id'])]);
                while ($contact = DBA::fetch($contacts)) {
                        Contact::remove($contact['id']);
                }
                DBA::close($contacts);
 
-               logger('Deleted contact ' . $activity['object_id'], LOGGER_DEBUG);
+               Logger::log('Deleted contact ' . $activity['object_id'], Logger::DEBUG);
        }
 
        /**
@@ -365,7 +443,7 @@ class Processor
 
                $cid = Contact::getIdForURL($activity['actor'], $uid);
                if (empty($cid)) {
-                       logger('No contact found for ' . $activity['actor'], LOGGER_DEBUG);
+                       Logger::log('No contact found for ' . $activity['actor'], Logger::DEBUG);
                        return;
                }
 
@@ -380,7 +458,7 @@ class Processor
 
                $condition = ['id' => $cid];
                DBA::update('contact', $fields, $condition);
-               logger('Accept contact request from contact ' . $cid . ' for user ' . $uid, LOGGER_DEBUG);
+               Logger::log('Accept contact request from contact ' . $cid . ' for user ' . $uid, Logger::DEBUG);
        }
 
        /**
@@ -399,7 +477,7 @@ class Processor
 
                $cid = Contact::getIdForURL($activity['actor'], $uid);
                if (empty($cid)) {
-                       logger('No contact found for ' . $activity['actor'], LOGGER_DEBUG);
+                       Logger::log('No contact found for ' . $activity['actor'], Logger::DEBUG);
                        return;
                }
 
@@ -407,9 +485,9 @@ class Processor
 
                if (DBA::exists('contact', ['id' => $cid, 'rel' => Contact::SHARING, 'pending' => true])) {
                        Contact::remove($cid);
-                       logger('Rejected contact request from contact ' . $cid . ' for user ' . $uid . ' - contact had been removed.', LOGGER_DEBUG);
+                       Logger::log('Rejected contact request from contact ' . $cid . ' for user ' . $uid . ' - contact had been removed.', Logger::DEBUG);
                } else {
-                       logger('Rejected contact request from contact ' . $cid . ' for user ' . $uid . '.', LOGGER_DEBUG);
+                       Logger::log('Rejected contact request from contact ' . $cid . ' for user ' . $uid . '.', Logger::DEBUG);
                }
        }
 
@@ -452,7 +530,7 @@ class Processor
 
                $cid = Contact::getIdForURL($activity['actor'], $uid);
                if (empty($cid)) {
-                       logger('No contact found for ' . $activity['actor'], LOGGER_DEBUG);
+                       Logger::log('No contact found for ' . $activity['actor'], Logger::DEBUG);
                        return;
                }
 
@@ -464,7 +542,7 @@ class Processor
                }
 
                Contact::removeFollower($owner, $contact);
-               logger('Undo following request from contact ' . $cid . ' for user ' . $uid, LOGGER_DEBUG);
+               Logger::log('Undo following request from contact ' . $cid . ' for user ' . $uid, Logger::DEBUG);
        }
 
        /**
@@ -479,7 +557,7 @@ class Processor
                        return;
                }
 
-               logger('Change existing contact ' . $cid . ' from ' . $contact['network'] . ' to ActivityPub.');
+               Logger::log('Change existing contact ' . $cid . ' from ' . $contact['network'] . ' to ActivityPub.');
                Contact::updateFromProbe($cid, Protocol::ACTIVITYPUB);
        }
 }