1
0
Fork 0

Move activity_match() to Protocol\Activity::match()

- With tests
This commit is contained in:
Philipp Holzer 2019-10-23 02:05:11 +02:00
parent 9e94e8b48c
commit 52c42491c4
No known key found for this signature in database
GPG key ID: D8365C3D36B77D90
9 changed files with 135 additions and 37 deletions

View 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);

View 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
*

View 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;
}

View 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

View 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;
}

23
src/Protocol/Activity.php Normal file
View file

@ -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)));
}
}

View 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;

View 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;

View file

@ -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));
}
}