AP: Event participation and editing of posts are now supported

This commit is contained in:
Michael 2018-10-27 06:17:17 +00:00
parent f32ea03911
commit f2ddcbe632
3 changed files with 70 additions and 45 deletions

View file

@ -16,6 +16,7 @@ use Friendica\Content\Text\HTML;
use Friendica\Util\JsonLD; use Friendica\Util\JsonLD;
use Friendica\Core\Config; use Friendica\Core\Config;
use Friendica\Protocol\ActivityPub; use Friendica\Protocol\ActivityPub;
use Friendica\Util\DateTimeFormat;
/** /**
* ActivityPub Processor Protocol class * ActivityPub Processor Protocol class
@ -101,6 +102,24 @@ class Processor
return $item; 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']);
$item['body'] = self::convertMentions(HTML::toBBCode($activity['content']));
$item['tag'] = self::constructTagList($activity['tags'], $activity['sensitive']);
Item::update($item, ['uri' => $activity['id']]);
}
/** /**
* Prepares data for a message * Prepares data for a message
* *
@ -128,22 +147,6 @@ class Processor
self::postItem($activity, $item); 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;
self::postItem($activity, $item);
}
/** /**
* Delete items * Delete items
* *
@ -158,14 +161,15 @@ class Processor
} }
/** /**
* Prepare the item array for a "dislike" * Prepare the item array for an activity
* *
* @param array $activity Activity array * @param array $activity Activity array
* @param string $verb Activity verb
*/ */
public static function dislikeItem($activity) public static function createActivity($activity, $verb)
{ {
$item = []; $item = [];
$item['verb'] = ACTIVITY_DISLIKE; $item['verb'] = $verb;
$item['parent-uri'] = $activity['object_id']; $item['parent-uri'] = $activity['object_id'];
$item['gravity'] = GRAVITY_ACTIVITY; $item['gravity'] = GRAVITY_ACTIVITY;
$item['object-type'] = ACTIVITY_OBJ_NOTE; $item['object-type'] = ACTIVITY_OBJ_NOTE;
@ -176,7 +180,8 @@ class Processor
/** /**
* Create an event * Create an event
* *
* @param array $activity Activity array * @param array $activity Activity array
* @param array $item
*/ */
public static function createEvent($activity, $item) public static function createEvent($activity, $item)
{ {

View file

@ -21,14 +21,7 @@ use Friendica\Util\DateTimeFormat;
* @brief ActivityPub Receiver Protocol class * @brief ActivityPub Receiver Protocol class
* *
* To-Do: * To-Do:
* - Update (Image, Video, Article, Note)
* - Undo Announce * - Undo Announce
* - Accept Event
* - Reject Event
* - TentativeAccept Even
* - Undo Accept Event
* - Undo Reject Event
* - Undo TentativeAccept Event
* *
* Check what this is meant to do: * Check what this is meant to do:
* - Add * - Add
@ -36,7 +29,6 @@ use Friendica\Util\DateTimeFormat;
* - Flag * - Flag
* - Remove * - Remove
* - Undo Block * - Undo Block
* - Undo Accept Person
*/ */
class Receiver class Receiver
{ {
@ -125,10 +117,11 @@ class Receiver
*/ */
private static function fetchObjectType($activity, $object_id) private static function fetchObjectType($activity, $object_id)
{ {
if (!empty($activity['as:object'])) {
$object_type = JsonLD::fetchElement($activity['as:object'], '@type'); $object_type = JsonLD::fetchElement($activity['as:object'], '@type');
if (!empty($object_type)) { if (!empty($object_type)) {
return $object_type; return $object_type;
}
} }
if (Item::exists(['uri' => $object_id, 'gravity' => [GRAVITY_PARENT, GRAVITY_COMMENT]])) { if (Item::exists(['uri' => $object_id, 'gravity' => [GRAVITY_PARENT, GRAVITY_COMMENT]])) {
@ -193,7 +186,7 @@ class Receiver
$object_type = self::fetchObjectType($activity, $object_id); $object_type = self::fetchObjectType($activity, $object_id);
// Fetch the content only on activities where this matters // Fetch the content only on activities where this matters
if (in_array($type, ['as:Create', 'as:Announce'])) { if (in_array($type, ['as:Create', 'as:Update', 'as:Announce'])) {
if ($type == 'as:Announce') { if ($type == 'as:Announce') {
$trust_source = false; $trust_source = false;
} }
@ -219,6 +212,11 @@ class Receiver
$object_data['object_actor'] = JsonLD::fetchElement($activity['as:object'], 'as:actor'); $object_data['object_actor'] = JsonLD::fetchElement($activity['as:object'], 'as:actor');
$object_data['object_object'] = JsonLD::fetchElement($activity['as:object'], 'as:object'); $object_data['object_object'] = JsonLD::fetchElement($activity['as:object'], 'as:object');
$object_data['object_type'] = JsonLD::fetchElement($activity['as:object'], '@type'); $object_data['object_type'] = JsonLD::fetchElement($activity['as:object'], '@type');
// An Undo is done on the object of an object, so we need that type as well
if ($type == 'as:Undo') {
$object_data['object_object_type'] = self::fetchObjectType([], $object_data['object_object']);
}
} }
$object_data = self::addActivityFields($object_data, $activity); $object_data = self::addActivityFields($object_data, $activity);
@ -311,20 +309,32 @@ class Receiver
switch ($type) { switch ($type) {
case 'as:Create': case 'as:Create':
case 'as:Announce': case 'as:Announce':
ActivityPub\Processor::createItem($object_data); if (in_array($object_data['object_type'], self::CONTENT_TYPES)) {
ActivityPub\Processor::createItem($object_data);
}
break; break;
case 'as:Like': case 'as:Like':
ActivityPub\Processor::likeItem($object_data); if (in_array($object_data['object_type'], self::CONTENT_TYPES)) {
ActivityPub\Processor::createActivity($object_data, ACTIVITY_LIKE);
}
break; break;
case 'as:Dislike': case 'as:Dislike':
ActivityPub\Processor::dislikeItem($object_data); if (in_array($object_data['object_type'], self::CONTENT_TYPES)) {
ActivityPub\Processor::createActivity($object_data, ACTIVITY_DISLIKE);
}
break;
case 'as:TentativeAccept':
if (in_array($object_data['object_type'], self::CONTENT_TYPES)) {
ActivityPub\Processor::createActivity($object_data, ACTIVITY_ATTENDMAYBE);
}
break; break;
case 'as:Update': case 'as:Update':
if (in_array($object_data['object_type'], self::CONTENT_TYPES)) { if (in_array($object_data['object_type'], self::CONTENT_TYPES)) {
/// @todo ActivityPub\Processor::updateItem($object_data);
} elseif (in_array($object_data['object_type'], self::ACCOUNT_TYPES)) { } elseif (in_array($object_data['object_type'], self::ACCOUNT_TYPES)) {
ActivityPub\Processor::updatePerson($object_data, $body); ActivityPub\Processor::updatePerson($object_data, $body);
} }
@ -339,31 +349,42 @@ class Receiver
break; break;
case 'as:Follow': case 'as:Follow':
ActivityPub\Processor::followUser($object_data); if (in_array($object_data['object_type'], self::ACCOUNT_TYPES)) {
ActivityPub\Processor::followUser($object_data);
}
break; break;
case 'as:Accept': case 'as:Accept':
if ($object_data['object_type'] == 'as:Follow') { if ($object_data['object_type'] == 'as:Follow') {
ActivityPub\Processor::acceptFollowUser($object_data); ActivityPub\Processor::acceptFollowUser($object_data);
} elseif (in_array($object_data['object_type'], self::CONTENT_TYPES)) {
ActivityPub\Processor::createActivity($object_data, ACTIVITY_ATTEND);
} }
break; break;
case 'as:Reject': case 'as:Reject':
if ($object_data['object_type'] == 'as:Follow') { if ($object_data['object_type'] == 'as:Follow') {
ActivityPub\Processor::rejectFollowUser($object_data); ActivityPub\Processor::rejectFollowUser($object_data);
} elseif (in_array($object_data['object_type'], self::CONTENT_TYPES)) {
ActivityPub\Processor::createActivity($object_data, ACTIVITY_ATTENDNO);
} }
break; break;
case 'as:Undo': case 'as:Undo':
if ($object_data['object_type'] == 'as:Follow') { if (($object_data['object_type'] == 'as:Follow') &&
in_array($object_data['object_object_type'], self::ACCOUNT_TYPES)) {
ActivityPub\Processor::undoFollowUser($object_data); ActivityPub\Processor::undoFollowUser($object_data);
} elseif (in_array($object_data['object_type'], self::ACTIVITY_TYPES)) { } elseif (($object_data['object_type'] == 'as:Accept') &&
in_array($object_data['object_object_type'], self::ACCOUNT_TYPES)) {
ActivityPub\Processor::rejectFollowUser($object_data);
} elseif (in_array($object_data['object_type'], self::ACTIVITY_TYPES) &&
in_array($object_data['object_object_type'], self::CONTENT_TYPES)) {
ActivityPub\Processor::undoActivity($object_data); ActivityPub\Processor::undoActivity($object_data);
} }
break; break;
default: default:
logger('Unknown activity: ' . $type, LOGGER_DEBUG); logger('Unknown activity: ' . $type . ' ' . $object_data['object_type'], LOGGER_DEBUG);
break; break;
} }
} }

View file

@ -282,8 +282,7 @@ class Transmitter
foreach ($activity[$element] as $receiver) { foreach ($activity[$element] as $receiver) {
if ($receiver == $profile['followers'] && !empty($item_profile['followers'])) { if ($receiver == $profile['followers'] && !empty($item_profile['followers'])) {
$permissions[$element][] = $item_profile['followers']; $permissions[$element][] = $item_profile['followers'];
} } elseif (!in_array($receiver, $exclude)) {
if (!in_array($receiver, $exclude)) {
$permissions[$element][] = $receiver; $permissions[$element][] = $receiver;
} }
} }
@ -309,13 +308,13 @@ class Transmitter
$data = ['to' => [], 'cc' => [], 'bcc' => []]; $data = ['to' => [], 'cc' => [], 'bcc' => []];
$data = array_merge($data, self::fetchPermissionBlockFromConversation($item));
$actor_profile = APContact::getByURL($item['author-link']); $actor_profile = APContact::getByURL($item['author-link']);
$terms = Term::tagArrayFromItemId($item['id'], TERM_MENTION); $terms = Term::tagArrayFromItemId($item['id'], TERM_MENTION);
if (!$item['private']) { if (!$item['private']) {
$data = array_merge($data, self::fetchPermissionBlockFromConversation($item));
$data['to'][] = ActivityPub::PUBLIC_COLLECTION; $data['to'][] = ActivityPub::PUBLIC_COLLECTION;
if (!empty($actor_profile['followers'])) { if (!empty($actor_profile['followers'])) {
$data['cc'][] = $actor_profile['followers']; $data['cc'][] = $actor_profile['followers'];