]> git.mxchange.org Git - friendica.git/commitdiff
Move activity_match() to Protocol\Activity::match()
authorPhilipp Holzer <admin+github@philipp.info>
Wed, 23 Oct 2019 00:05:11 +0000 (02:05 +0200)
committerPhilipp Holzer <admin+github@philipp.info>
Wed, 23 Oct 2019 00:05:11 +0000 (02:05 +0200)
- With tests

include/conversation.php
include/text.php
mod/photos.php
src/Model/Item.php
src/Object/Post.php
src/Protocol/Activity.php [new file with mode: 0644]
src/Protocol/DFRN.php
tests/src/Content/Text/BBCode/VideoTest.php
tests/src/Protocol/ActivityTest.php [new file with mode: 0644]

index b6faa4d2c88e3e97f5f1b06449f9a7a3b0e0de88..c857336e341d5afbd1f9ed19f23e35137a9c3cea 100644 (file)
@@ -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);
index 84efebac2de123fe2738f96e7a79a60b2a1d2e88..775931c6ff82c1578989cf4b05602ca513c3819a 100644 (file)
@@ -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
  *
index 528f78b614f9c61f127318f4f77060e90e19efb0..3a94813e5412b1428cfaab3983d05929cb748429 100644 (file)
@@ -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;
                                        }
 
index 4c98eb50474f0ce62a4007db0ce5c545636f1583..6f84de427acdb792ecaf49cafdd351485dfa7af4 100644 (file)
@@ -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
index 04775bbd0e2f964c3a3a3b6202e7c4d85e85b467..12de457a519590a709b9f5015cb87cc14efca42b 100644 (file)
@@ -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 (file)
index 0000000..64253b0
--- /dev/null
@@ -0,0 +1,23 @@
+<?php
+
+namespace Friendica\Protocol;
+
+/**
+ * Base class for the Activity namespace
+ */
+final class Activity
+{
+       /**
+        * Compare activity uri. Knows about activity namespace.
+        *
+        * @param string $haystack
+        * @param string $needle
+        *
+        * @return boolean
+        */
+       public function match(string $haystack, string $needle) {
+               return (($haystack === $needle) ||
+                       ((basename($needle) === $haystack) &&
+                        strstr($needle, NAMESPACE_ACTIVITY_SCHEMA)));
+       }
+}
index d557af0692fb761db121bb815a59f93bf024aa8b..e1901011e166a74a4e5e99eba8058065170c1e6d 100644 (file)
@@ -12,6 +12,7 @@ use DOMDocument;
 use DOMXPath;
 use Friendica\App;
 use Friendica\App\BaseURL;
+use Friendica\BaseObject;
 use Friendica\Content\OEmbed;
 use Friendica\Content\Text\BBCode;
 use Friendica\Content\Text\HTML;
@@ -2180,24 +2181,27 @@ class DFRN
                        // The functions below are partly used by ostatus.php as well - where we have this variable
                        $contact = Contact::selectFirst([], ['id' => $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;
index dcdbf937a0e6e531579ea389e28470594f2e0d4f..4a176871a1e6d4934492022ec1d66f71124de201 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 
-namespace src\Content\Text\BBCode;
+namespace Friendica\Test\Content\Text\BBCode;
 
 use Friendica\Content\Text\BBCode\Video;
 use Friendica\Test\MockedTest;
diff --git a/tests/src/Protocol/ActivityTest.php b/tests/src/Protocol/ActivityTest.php
new file mode 100644 (file)
index 0000000..b6fcbf3
--- /dev/null
@@ -0,0 +1,57 @@
+<?php
+
+namespace Friendica\Test\Protocol;
+
+use Friendica\Protocol\Activity;
+use Friendica\Test\MockedTest;
+
+class ActivityTest extends MockedTest
+{
+       public function dataMatch()
+       {
+               return [
+                       'empty' => [
+                               '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));
+       }
+}