]> git.mxchange.org Git - friendica.git/blobdiff - src/Protocol/ActivityPub/Processor.php
The tag table is now really used
[friendica.git] / src / Protocol / ActivityPub / Processor.php
index c96a4e704c84505292d78945df5ec0156f406961..80b2b0dbab74207925dbc9b014933655dc890443 100644 (file)
@@ -1,22 +1,40 @@
 <?php
 /**
- * @file src/Protocol/ActivityPub/Processor.php
+ * @copyright Copyright (C) 2020, Friendica
+ *
+ * @license GNU AGPL version 3 or any later version
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program.  If not, see <https://www.gnu.org/licenses/>.
+ *
  */
+
 namespace Friendica\Protocol\ActivityPub;
 
 use Friendica\Content\Text\BBCode;
 use Friendica\Content\Text\HTML;
-use Friendica\Core\Config;
 use Friendica\Core\Logger;
-use Friendica\Core\PConfig;
 use Friendica\Core\Protocol;
 use Friendica\Database\DBA;
+use Friendica\DI;
 use Friendica\Model\APContact;
 use Friendica\Model\Contact;
+use Friendica\Model\Conversation;
 use Friendica\Model\Event;
 use Friendica\Model\Item;
+use Friendica\Model\ItemURI;
 use Friendica\Model\Mail;
-use Friendica\Model\Term;
+use Friendica\Model\Tag;
 use Friendica\Model\User;
 use Friendica\Protocol\Activity;
 use Friendica\Protocol\ActivityPub;
@@ -116,6 +134,18 @@ class Processor
                                } else {
                                        $item['body'] .= "\n[img=" . $attach['url'] . ']' . $attach['name'] . '[/img]';
                                }
+                       } elseif ($filetype == 'audio') {
+                               if (!empty($activity['source']) && strpos($activity['source'], $attach['url'])) {
+                                       continue;
+                               }
+
+                               $item['body'] .= "\n[audio]" . $attach['url'] . '[/audio]';
+                       } elseif ($filetype == 'video') {
+                               if (!empty($activity['source']) && strpos($activity['source'], $attach['url'])) {
+                                       continue;
+                               }
+
+                               $item['body'] .= "\n[video]" . $attach['url'] . '[/video]';
                        } else {
                                if (!empty($item["attach"])) {
                                        $item["attach"] .= ',';
@@ -140,9 +170,10 @@ class Processor
         */
        public static function updateItem($activity)
        {
-               $item = Item::selectFirst(['uri', 'thr-parent', 'gravity'], ['uri' => $activity['id']]);
+               $item = Item::selectFirst(['uri', 'uri-id', 'thr-parent', 'gravity'], ['uri' => $activity['id']]);
                if (!DBA::isResult($item)) {
-                       Logger::warning('Unknown item', ['uri' => $activity['id']]);
+                       Logger::warning('No existing item, item will be created', ['uri' => $activity['id']]);
+                       self::createItem($activity);
                        return;
                }
 
@@ -182,7 +213,7 @@ class Processor
                }
 
                if (empty($activity['directmessage']) && ($activity['id'] != $activity['reply-to-id']) && !Item::exists(['uri' => $activity['reply-to-id']])) {
-                       Logger::log('Parent ' . $activity['reply-to-id'] . ' not found. Try to refetch it.');
+                       Logger::notice('Parent not found. Try to refetch it.', ['parent' => $activity['reply-to-id']]);
                        self::fetchMissingActivity($activity['reply-to-id'], $activity);
                }
 
@@ -203,7 +234,7 @@ class Processor
                $owner = Contact::getIdForURL($activity['actor']);
 
                Logger::log('Deleting item ' . $activity['object_id'] . ' from ' . $owner, Logger::DEBUG);
-               Item::delete(['uri' => $activity['object_id'], 'owner-id' => $owner]);
+               Item::markForDeletion(['uri' => $activity['object_id'], 'owner-id' => $owner]);
        }
 
        /**
@@ -220,7 +251,7 @@ class Processor
                }
 
                foreach ($activity['receiver'] as $receiver) {
-                       $item = Item::selectFirst(['id', 'tag', 'origin', 'author-link'], ['uri' => $activity['target_id'], 'uid' => $receiver]);
+                       $item = Item::selectFirst(['id', 'uri-id', 'tag', 'origin', 'author-link'], ['uri' => $activity['target_id'], 'uid' => $receiver]);
                        if (!DBA::isResult($item)) {
                                // We don't fetch missing content for this purpose
                                continue;
@@ -231,6 +262,8 @@ class Processor
                                continue;
                        }
 
+                       Tag::store($item['uri-id'], Tag::HASHTAG, $activity['object_content'], $activity['object_id']);
+
                        // To-Do:
                        // - Check if "blocktag" is set
                        // - Check if actor is a contact
@@ -359,7 +392,7 @@ class Processor
                                        Logger::warning('Unknown parent item.', ['uri' => $item['thr-parent']]);
                                        return false;
                                }
-                               if ($item_private && !$parent['private']) {
+                               if ($item_private && ($parent['private'] == Item::PRIVATE)) {
                                        Logger::warning('Item is private but the parent is not. Dropping.', ['item-uri' => $item['uri'], 'thr-parent' => $item['thr-parent']]);
                                        return false;
                                }
@@ -370,14 +403,13 @@ class Processor
                        }
                        $item['content-warning'] = HTML::toBBCode($activity['summary']);
                        $item['body'] = $content;
-
-                       if (($activity['object_type'] == 'as:Video') && !empty($activity['alternate-url'])) {
-                               $item['body'] .= "\n[video]" . $activity['alternate-url'] . '[/video]';
-                       }
                }
 
                $item['tag'] = self::constructTagString($activity['tags'], $activity['sensitive']);
 
+               self::storeFromBody($item);
+               self::storeTags($item['uri-id'], $activity['tags']);
+
                $item['location'] = $activity['location'];
 
                if (!empty($item['latitude']) && !empty($item['longitude'])) {
@@ -389,6 +421,39 @@ class Processor
                return $item;
        }
 
+       /**
+        * Store hashtags and mentions
+        *
+        * @param array $item
+        */
+       private static function storeFromBody(array $item)
+       {
+               // Make sure to delete all existing tags (can happen when called via the update functionality)
+               DBA::delete('post-tag', ['uri-id' => $item['uri-id']]);
+
+               Tag::storeFromBody($item['uri-id'], $item['body'], '@!');
+       }
+
+       /**
+        * Generate a GUID out of an URL
+        *
+        * @param string $url message URL
+        * @return string with GUID
+        */
+       private static function getGUIDByURL(string $url)
+       {
+               $parsed = parse_url($url);
+
+               $host_hash = hash('crc32', $parsed['host']);
+
+               unset($parsed["scheme"]);
+               unset($parsed["host"]);
+
+               $path = implode("/", $parsed);
+
+               return $host_hash . '-'. hash('fnv164', $path) . '-'. hash('joaat', $path);
+       }
+
        /**
         * Creates an item post
         *
@@ -406,12 +471,30 @@ class Processor
                }
 
                $item['network'] = Protocol::ACTIVITYPUB;
-               $item['private'] = !in_array(0, $activity['receiver']);
                $item['author-link'] = $activity['author'];
                $item['author-id'] = Contact::getIdForURL($activity['author'], 0, true);
                $item['owner-link'] = $activity['actor'];
                $item['owner-id'] = Contact::getIdForURL($activity['actor'], 0, true);
 
+               if (in_array(0, $activity['receiver']) && !empty($activity['unlisted'])) {
+                       $item['private'] = Item::UNLISTED;
+               } elseif (in_array(0, $activity['receiver'])) {
+                       $item['private'] = Item::PUBLIC;
+               } else {
+                       $item['private'] = Item::PRIVATE;
+               }
+
+               if (!empty($activity['raw'])) {
+                       $item['source'] = $activity['raw'];
+                       $item['protocol'] = Conversation::PARCEL_ACTIVITYPUB;
+                       $item['conversation-href'] = $activity['context'] ?? '';
+                       $item['conversation-uri'] = $activity['conversation'] ?? '';
+
+                       if (isset($activity['push'])) {
+                               $item['direction'] = $activity['push'] ? Conversation::PUSH : Conversation::PULL;
+                       }
+               }
+
                $isForum = false;
 
                if (!empty($activity['thread-completion'])) {
@@ -431,7 +514,9 @@ class Processor
 
                $item['created'] = DateTimeFormat::utc($activity['published']);
                $item['edited'] = DateTimeFormat::utc($activity['updated']);
-               $item['guid'] = $activity['diaspora:guid'];
+               $item['guid'] = $activity['diaspora:guid'] ?: $activity['sc:identifier'] ?: self::getGUIDByURL($item['uri']);
+
+               $item['uri-id'] = ItemURI::insert(['uri' => $item['uri'], 'guid' => $item['guid']]);
 
                $item = self::processContent($activity, $item);
                if (empty($item)) {
@@ -445,6 +530,10 @@ class Processor
                $stored = false;
 
                foreach ($activity['receiver'] as $receiver) {
+                       if ($receiver == -1) {
+                               continue;
+                       }
+
                        $item['uid'] = $receiver;
 
                        if ($isForum) {
@@ -462,7 +551,7 @@ class Processor
                                continue;
                        }
 
-                       if (PConfig::get($receiver, 'system', 'accept_only_sharer', false) && ($receiver != 0) && ($item['gravity'] == GRAVITY_PARENT)) {
+                       if (DI::pConfig()->get($receiver, 'system', 'accept_only_sharer', false) && ($receiver != 0) && ($item['gravity'] == GRAVITY_PARENT)) {
                                $skip = !Contact::isSharingByURL($activity['author'], $receiver);
 
                                if ($skip && (($activity['type'] == 'as:Announce') || $isForum)) {
@@ -477,7 +566,7 @@ class Processor
                                Logger::info('Accepting post', ['uid' => $receiver, 'url' => $item['uri']]);
                        }
 
-                       if ($activity['object_type'] == 'as:Event') {
+                       if (($item['gravity'] != GRAVITY_ACTIVITY) && ($activity['object_type'] == 'as:Event')) {
                                self::createEvent($activity, $item);
                        }
 
@@ -494,7 +583,7 @@ class Processor
                }
 
                // Store send a follow request for every reshare - but only when the item had been stored
-               if ($stored && !$item['private'] && ($item['gravity'] == GRAVITY_PARENT) && ($item['author-link'] != $item['owner-link'])) {
+               if ($stored && ($item['private'] != Item::PRIVATE) && ($item['gravity'] == GRAVITY_PARENT) && ($item['author-link'] != $item['owner-link'])) {
                        $author = APContact::getByURL($item['owner-link'], false);
                        // We send automatic follow requests for reshared messages. (We don't need though for forum posts)
                        if ($author['type'] != 'Group') {
@@ -504,6 +593,50 @@ class Processor
                }
        }
 
+       /**
+        * Store tags and mentions into the tag table
+        *
+        * @param integer $uriid
+        * @param array $tags
+        */
+       private static function storeTags(int $uriid, array $tags = null)
+       {
+               foreach ($tags as $tag) {
+                       if (empty($tag['name']) || empty($tag['type']) || !in_array($tag['type'], ['Mention', 'Hashtag'])) {
+                               continue;
+                       }
+
+                       $hash = substr($tag['name'], 0, 1);
+
+                       if ($tag['type'] == 'Mention') {
+                               if (in_array($hash, [Tag::TAG_CHARACTER[Tag::MENTION],
+                                       Tag::TAG_CHARACTER[Tag::EXCLUSIVE_MENTION],
+                                       Tag::TAG_CHARACTER[Tag::IMPLICIT_MENTION]])) {
+                                       $tag['name'] = substr($tag['name'], 1);
+                               }
+                               $type = Tag::IMPLICIT_MENTION;
+
+                               if (!empty($tag['href'])) {
+                                       $apcontact = APContact::getByURL($tag['href']);
+                                       if (!empty($apcontact['name']) || !empty($apcontact['nick'])) {
+                                               $tag['name'] = $apcontact['name'] ?: $apcontact['nick'];
+                                       }
+                               }
+                       } elseif ($tag['type'] == 'Hashtag') {
+                               if ($hash == Tag::TAG_CHARACTER[Tag::HASHTAG]) {
+                                       $tag['name'] = substr($tag['name'], 1);
+                               }
+                               $type = Tag::HASHTAG;
+                       }
+
+                       if (empty($tag['name'])) {
+                               continue;
+                       }
+
+                       Tag::store($uriid, $type, $tag['name'], $tag['href']);
+               }
+       }
+
        /**
         * Creates an mail post
         *
@@ -572,7 +705,7 @@ class Processor
         *
         * @param string $url message URL
         * @param array $child activity array with the child of this message
-        * @return boolean success
+        * @return string fetched message URL
         * @throws \Friendica\Network\HTTPException\InternalServerErrorException
         */
        public static function fetchMissingActivity($url, $child = [])
@@ -586,12 +719,12 @@ class Processor
                $object = ActivityPub::fetchContent($url, $uid);
                if (empty($object)) {
                        Logger::log('Activity ' . $url . ' was not fetchable, aborting.');
-                       return false;
+                       return '';
                }
 
                if (empty($object['id'])) {
                        Logger::log('Activity ' . $url . ' has got not id, aborting. ' . json_encode($object));
-                       return false;
+                       return '';
                }
 
                if (!empty($child['author'])) {
@@ -628,10 +761,11 @@ class Processor
 
                $ldactivity['thread-completion'] = true;
 
-               ActivityPub\Receiver::processActivity($ldactivity);
-               Logger::log('Activity ' . $url . ' had been fetched and processed.');
+               ActivityPub\Receiver::processActivity($ldactivity, json_encode($activity));
+
+               Logger::notice('Activity had been fetched and processed.', ['url' => $url, 'object' => $activity['id']]);
 
-               return true;
+               return $activity['id'];
        }
 
        /**
@@ -813,7 +947,7 @@ class Processor
                        return;
                }
 
-               Item::delete(['uri' => $activity['object_id'], 'author-id' => $author_id, 'gravity' => GRAVITY_ACTIVITY]);
+               Item::markForDeletion(['uri' => $activity['object_id'], 'author-id' => $author_id, 'gravity' => GRAVITY_ACTIVITY]);
        }
 
        /**
@@ -857,13 +991,13 @@ class Processor
         */
        private static function switchContact($cid)
        {
-               $contact = DBA::selectFirst('contact', ['network'], ['id' => $cid, 'network' => Protocol::NATIVE_SUPPORT]);
-               if (!DBA::isResult($contact) || in_array($contact['network'], [Protocol::ACTIVITYPUB, Protocol::DFRN])) {
+               $contact = DBA::selectFirst('contact', ['network', 'url'], ['id' => $cid]);
+               if (!DBA::isResult($contact) || in_array($contact['network'], [Protocol::ACTIVITYPUB, Protocol::DFRN]) || Contact::isLocal($contact['url'])) {
                        return;
                }
 
-               Logger::log('Change existing contact ' . $cid . ' from ' . $contact['network'] . ' to ActivityPub.');
-               Contact::updateFromProbe($cid, Protocol::ACTIVITYPUB);
+               Logger::info('Change existing contact', ['cid' => $cid, 'previous' => $contact['network']]);
+               Contact::updateFromProbe($cid);
        }
 
        /**
@@ -877,11 +1011,11 @@ class Processor
         */
        private static function getImplicitMentionList(array $parent)
        {
-               if (Config::get('system', 'disable_implicit_mentions')) {
+               if (DI::config()->get('system', 'disable_implicit_mentions')) {
                        return [];
                }
 
-               $parent_terms = Term::tagArrayFromItemId($parent['id'], [Term::MENTION, Term::IMPLICIT_MENTION]);
+               $parent_terms = Tag::ArrayFromURIId($parent['uri-id'], [Tag::MENTION, Tag::IMPLICIT_MENTION, Tag::EXCLUSIVE_MENTION]);
 
                $parent_author = Contact::getDetailsByURL($parent['author-link'], 0);
 
@@ -919,7 +1053,7 @@ class Processor
         */
        private static function removeImplicitMentionsFromBody($body, array $potential_mentions)
        {
-               if (Config::get('system', 'disable_implicit_mentions')) {
+               if (DI::config()->get('system', 'disable_implicit_mentions')) {
                        return $body;
                }
 
@@ -942,15 +1076,15 @@ class Processor
 
        private static function convertImplicitMentionsInTags($activity_tags, array $potential_mentions)
        {
-               if (Config::get('system', 'disable_implicit_mentions')) {
+               if (DI::config()->get('system', 'disable_implicit_mentions')) {
                        return $activity_tags;
                }
 
                foreach ($activity_tags as $index => $tag) {
                        if (in_array($tag['href'], $potential_mentions)) {
                                $activity_tags[$index]['name'] = preg_replace(
-                                       '/' . preg_quote(Term::TAG_CHARACTER[Term::MENTION], '/') . '/',
-                                       Term::TAG_CHARACTER[Term::IMPLICIT_MENTION],
+                                       '/' . preg_quote(Tag::TAG_CHARACTER[Tag::MENTION], '/') . '/',
+                                       Tag::TAG_CHARACTER[Tag::IMPLICIT_MENTION],
                                        $activity_tags[$index]['name'],
                                        1
                                );