From: Philipp Holzer Date: Wed, 23 Oct 2019 00:05:11 +0000 (+0200) Subject: Move activity_match() to Protocol\Activity::match() X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=52c42491c4e1c42a386ffa78698052a82007b63e;p=friendica.git Move activity_match() to Protocol\Activity::match() - With tests --- diff --git a/include/conversation.php b/include/conversation.php index b6faa4d2c8..c857336e34 100644 --- a/include/conversation.php +++ b/include/conversation.php @@ -4,6 +4,7 @@ */ use Friendica\App; +use Friendica\BaseObject; use Friendica\Content\ContactSelector; use Friendica\Content\Feature; use Friendica\Content\Pager; @@ -24,6 +25,7 @@ use Friendica\Model\Profile; use Friendica\Model\Term; use Friendica\Object\Post; use Friendica\Object\Thread; +use Friendica\Protocol\Activity; use Friendica\Util\DateTimeFormat; use Friendica\Util\Proxy as ProxyUtils; use Friendica\Util\Temporal; @@ -138,12 +140,15 @@ function localize_item(&$item) During the further steps of the database restructuring I would like to address this issue. */ + /** @var Activity $activity */ + $activity = BaseObject::getClass(Activity::class); + $xmlhead = "<" . "?xml version='1.0' encoding='UTF-8' ?" . ">"; - if (activity_match($item['verb'], ACTIVITY_LIKE) - || activity_match($item['verb'], ACTIVITY_DISLIKE) - || activity_match($item['verb'], ACTIVITY_ATTEND) - || activity_match($item['verb'], ACTIVITY_ATTENDNO) - || activity_match($item['verb'], ACTIVITY_ATTENDMAYBE)) { + if ($activity->match($item['verb'], ACTIVITY_LIKE) + || $activity->match($item['verb'], ACTIVITY_DISLIKE) + || $activity->match($item['verb'], ACTIVITY_ATTEND) + || $activity->match($item['verb'], ACTIVITY_ATTENDNO) + || $activity->match($item['verb'], ACTIVITY_ATTENDMAYBE)) { $fields = ['author-link', 'author-name', 'verb', 'object-type', 'resource-id', 'body', 'plink']; $obj = Item::selectFirst($fields, ['uri' => $item['parent-uri']]); @@ -178,22 +183,22 @@ function localize_item(&$item) $plink = '[url=' . $obj['plink'] . ']' . $post_type . '[/url]'; $bodyverb = ''; - if (activity_match($item['verb'], ACTIVITY_LIKE)) { + if ($activity->match($item['verb'], ACTIVITY_LIKE)) { $bodyverb = L10n::t('%1$s likes %2$s\'s %3$s'); - } elseif (activity_match($item['verb'], ACTIVITY_DISLIKE)) { + } elseif ($activity->match($item['verb'], ACTIVITY_DISLIKE)) { $bodyverb = L10n::t('%1$s doesn\'t like %2$s\'s %3$s'); - } elseif (activity_match($item['verb'], ACTIVITY_ATTEND)) { + } elseif ($activity->match($item['verb'], ACTIVITY_ATTEND)) { $bodyverb = L10n::t('%1$s attends %2$s\'s %3$s'); - } elseif (activity_match($item['verb'], ACTIVITY_ATTENDNO)) { + } elseif ($activity->match($item['verb'], ACTIVITY_ATTENDNO)) { $bodyverb = L10n::t('%1$s doesn\'t attend %2$s\'s %3$s'); - } elseif (activity_match($item['verb'], ACTIVITY_ATTENDMAYBE)) { + } elseif ($activity->match($item['verb'], ACTIVITY_ATTENDMAYBE)) { $bodyverb = L10n::t('%1$s attends maybe %2$s\'s %3$s'); } $item['body'] = sprintf($bodyverb, $author, $objauthor, $plink); } - if (activity_match($item['verb'], ACTIVITY_FRIEND)) { + if ($activity->match($item['verb'], ACTIVITY_FRIEND)) { if ($item['object-type']=="" || $item['object-type']!== ACTIVITY_OBJ_PERSON) return; @@ -275,7 +280,7 @@ function localize_item(&$item) } - if (activity_match($item['verb'], ACTIVITY_TAG)) { + if ($activity->match($item['verb'], ACTIVITY_TAG)) { $fields = ['author-id', 'author-link', 'author-name', 'author-network', 'verb', 'object-type', 'resource-id', 'body', 'plink']; $obj = Item::selectFirst($fields, ['uri' => $item['parent-uri']]); @@ -320,7 +325,7 @@ function localize_item(&$item) $item['body'] = L10n::t('%1$s tagged %2$s\'s %3$s with %4$s', $author, $objauthor, $plink, $tag); } - if (activity_match($item['verb'], ACTIVITY_FAVORITE)) { + if ($activity->match($item['verb'], ACTIVITY_FAVORITE)) { if ($item['object-type'] == "") { return; } @@ -393,19 +398,22 @@ function count_descendants($item) { function visible_activity($item) { + /** @var Activity $activity */ + $activity = BaseObject::getClass(Activity::class); + /* * likes (etc.) can apply to other things besides posts. Check if they are post children, * in which case we handle them specially */ $hidden_activities = [ACTIVITY_LIKE, ACTIVITY_DISLIKE, ACTIVITY_ATTEND, ACTIVITY_ATTENDNO, ACTIVITY_ATTENDMAYBE, ACTIVITY_FOLLOW, ACTIVITY2_ANNOUNCE]; foreach ($hidden_activities as $act) { - if (activity_match($item['verb'], $act)) { + if ($activity->match($item['verb'], $act)) { return false; } } // @TODO below if() block can be rewritten to a single line: $isVisible = allConditionsHere; - if (activity_match($item['verb'], ACTIVITY_FOLLOW) && $item['object-type'] === ACTIVITY_OBJ_NOTE && empty($item['self']) && $item['uid'] == local_user()) { + if ($activity->match($item['verb'], ACTIVITY_FOLLOW) && $item['object-type'] === ACTIVITY_OBJ_NOTE && empty($item['self']) && $item['uid'] == local_user()) { return false; } @@ -1017,7 +1025,10 @@ function builtin_activity_puller($item, &$conv_responses) { return; } - if (activity_match($item['verb'], $verb) && ($item['id'] != $item['parent'])) { + /** @var Activity $activity */ + $activity = BaseObject::getClass(Activity::class); + + if ($activity->match($item['verb'], $verb) && ($item['id'] != $item['parent'])) { $author = ['uid' => 0, 'id' => $item['author-id'], 'network' => $item['author-network'], 'url' => $item['author-link']]; $url = Contact::magicLinkByContact($author); diff --git a/include/text.php b/include/text.php index 84efebac2d..775931c6ff 100644 --- a/include/text.php +++ b/include/text.php @@ -3,20 +3,8 @@ * @file include/text.php */ -use Friendica\Content\Text\BBCode; use Friendica\Model\FileTag; -/** - * Compare activity uri. Knows about activity namespace. - * - * @param string $haystack - * @param string $needle - * @return boolean - */ -function activity_match($haystack,$needle) { - return (($haystack === $needle) || ((basename($needle) === $haystack) && strstr($needle, NAMESPACE_ACTIVITY_SCHEMA))); -} - /** * return array with details for categories and folders for an item * diff --git a/mod/photos.php b/mod/photos.php index 528f78b614..3a94813e54 100644 --- a/mod/photos.php +++ b/mod/photos.php @@ -1446,7 +1446,12 @@ function photos_content(App $a) $template = $tpl; $sparkle = ''; - if ((activity_match($item['verb'], ACTIVITY_LIKE) || activity_match($item['verb'], ACTIVITY_DISLIKE)) && ($item['id'] != $item['parent'])) { + /** @var \Friendica\Protocol\Activity $activity */ + $activity = BaseObject::getClass(\Friendica\Protocol\Activity::class); + + if (($activity->match($item['verb'], ACTIVITY_LIKE) || + $activity->match($item['verb'], ACTIVITY_DISLIKE)) && + ($item['id'] != $item['parent'])) { continue; } diff --git a/src/Model/Item.php b/src/Model/Item.php index 4c98eb5047..6f84de427a 100644 --- a/src/Model/Item.php +++ b/src/Model/Item.php @@ -21,6 +21,7 @@ use Friendica\Core\System; use Friendica\Core\Session; use Friendica\Core\Worker; use Friendica\Database\DBA; +use Friendica\Protocol\Activity; use Friendica\Protocol\ActivityPub; use Friendica\Protocol\Diaspora; use Friendica\Protocol\OStatus; @@ -1358,13 +1359,16 @@ class Item extends BaseObject $item['parent-uri'] = $item['thr-parent']; } + /** @var Activity $activity */ + $activity = self::getClass(Activity::class); + if (isset($item['gravity'])) { $item['gravity'] = intval($item['gravity']); } elseif ($item['parent-uri'] === $item['uri']) { $item['gravity'] = GRAVITY_PARENT; - } elseif (activity_match($item['verb'], ACTIVITY_POST)) { + } elseif ($activity->match($item['verb'], ACTIVITY_POST)) { $item['gravity'] = GRAVITY_COMMENT; - } elseif (activity_match($item['verb'], ACTIVITY_FOLLOW)) { + } elseif ($activity->match($item['verb'], ACTIVITY_FOLLOW)) { $item['gravity'] = GRAVITY_ACTIVITY; } else { $item['gravity'] = GRAVITY_UNKNOWN; // Should not happen diff --git a/src/Object/Post.php b/src/Object/Post.php index 04775bbd0e..12de457a51 100644 --- a/src/Object/Post.php +++ b/src/Object/Post.php @@ -21,6 +21,7 @@ use Friendica\Model\Contact; use Friendica\Model\Item; use Friendica\Model\Term; use Friendica\Model\User; +use Friendica\Protocol\Activity; use Friendica\Util\Crypto; use Friendica\Util\DateTimeFormat; use Friendica\Util\Proxy as ProxyUtils; @@ -517,12 +518,17 @@ class Post extends BaseObject Logger::log('[WARN] Post::addChild : Item already exists (' . $item->getId() . ').', Logger::DEBUG); return false; } + + /** @var Activity $activity */ + $activity = self::getClass(Activity::class); + /* * Only add what will be displayed */ if ($item->getDataValue('network') === Protocol::MAIL && local_user() != $item->getDataValue('uid')) { return false; - } elseif (activity_match($item->getDataValue('verb'), ACTIVITY_LIKE) || activity_match($item->getDataValue('verb'), ACTIVITY_DISLIKE)) { + } elseif ($activity->match($item->getDataValue('verb'), ACTIVITY_LIKE) || + $activity->match($item->getDataValue('verb'), ACTIVITY_DISLIKE)) { return false; } diff --git a/src/Protocol/Activity.php b/src/Protocol/Activity.php new file mode 100644 index 0000000000..64253b0652 --- /dev/null +++ b/src/Protocol/Activity.php @@ -0,0 +1,23 @@ + $importer['id']]); + /** @var Activity $activity */ + $activity = BaseObject::getClass(Activity::class); + // Big question: Do we need these functions? They were part of the "consume_feed" function. // This function once was responsible for DFRN and OStatus. - if (activity_match($item["verb"], ACTIVITY_FOLLOW)) { + if ($activity->match($item["verb"], ACTIVITY_FOLLOW)) { Logger::log("New follower"); Contact::addRelationship($importer, $contact, $item); return false; } - if (activity_match($item["verb"], ACTIVITY_UNFOLLOW)) { + if ($activity->match($item["verb"], ACTIVITY_UNFOLLOW)) { Logger::log("Lost follower"); Contact::removeFollower($importer, $contact, $item); return false; } - if (activity_match($item["verb"], ACTIVITY_REQ_FRIEND)) { + if ($activity->match($item["verb"], ACTIVITY_REQ_FRIEND)) { Logger::log("New friend request"); Contact::addRelationship($importer, $contact, $item, true); return false; } - if (activity_match($item["verb"], ACTIVITY_UNFRIEND)) { + if ($activity->match($item["verb"], ACTIVITY_UNFRIEND)) { Logger::log("Lost sharer"); Contact::removeSharer($importer, $contact, $item); return false; diff --git a/tests/src/Content/Text/BBCode/VideoTest.php b/tests/src/Content/Text/BBCode/VideoTest.php index dcdbf937a0..4a176871a1 100644 --- a/tests/src/Content/Text/BBCode/VideoTest.php +++ b/tests/src/Content/Text/BBCode/VideoTest.php @@ -1,6 +1,6 @@ [ + 'haystack' => '', + 'needle' => '', + 'assert' => true, + ], + 'simple' => [ + 'haystack' => ACTIVITY_OBJ_TAGTERM, + 'needle' => ACTIVITY_OBJ_TAGTERM, + 'assert' => true, + ], + 'withNamespace' => [ + 'haystack' => 'tagterm', + 'needle' => NAMESPACE_ACTIVITY_SCHEMA . ACTIVITY_OBJ_TAGTERM, + 'assert' => true, + ], + 'invalidSimple' => [ + 'haystack' => 'tagterm', + 'needle' => '', + 'assert' => false, + ], + 'invalidWithOutNamespace' => [ + 'haystack' => 'tagterm', + 'needle' => ACTIVITY_OBJ_TAGTERM, + 'assert' => false, + ], + 'withSubPath' => [ + 'haystack' => 'tagterm', + 'needle' => NAMESPACE_ACTIVITY_SCHEMA . '/bla/' . ACTIVITY_OBJ_TAGTERM, + 'assert' => true, + ], + ]; + } + + /** + * Test the different, possible matchings + * + * @dataProvider dataMatch + */ + public function testMatch(string $haystack, string $needle, bool $assert) + { + $activity = new Activity(); + + $this->assertEquals($assert, $activity->match($haystack, $needle)); + } +}