1
0
Fork 0

Separate concerns between postItem and createItem in ActivityPub\Processor

- postItem now only posts items created with createItem without altering it
- createItem  now only creates an item array from an activity without posting it
- postItem scope is now public
This commit is contained in:
Hypolite Petovan 2020-07-20 00:37:43 -04:00
parent 664b7c5df2
commit 259a62f9dd
2 changed files with 77 additions and 73 deletions

View file

@ -158,7 +158,8 @@ class Processor
$item = Item::selectFirst(['uri', 'uri-id', 'thr-parent', 'gravity'], ['uri' => $activity['id']]);
if (!DBA::isResult($item)) {
Logger::warning('No existing item, item will be created', ['uri' => $activity['id']]);
self::createItem($activity);
$item = self::createItem($activity);
self::postItem($activity, $item);
return;
}
@ -177,6 +178,7 @@ class Processor
* Prepares data for a message
*
* @param array $activity Activity array
* @return array Internal item
* @throws \Friendica\Network\HTTPException\InternalServerErrorException
* @throws \ImagickException
*/
@ -204,7 +206,71 @@ class Processor
$item['diaspora_signed_text'] = $activity['diaspora:comment'] ?? '';
self::postItem($activity, $item);
/// @todo What to do with $activity['context']?
if (empty($activity['directmessage']) && ($item['gravity'] != GRAVITY_PARENT) && !Item::exists(['uri' => $item['thr-parent']])) {
Logger::info('Parent not found, message will be discarded.', ['thr-parent' => $item['thr-parent']]);
return [];
}
$item['network'] = Protocol::ACTIVITYPUB;
$item['author-link'] = $activity['author'];
$item['author-id'] = Contact::getIdForURL($activity['author'], 0, false);
$item['owner-link'] = $activity['actor'];
$item['owner-id'] = Contact::getIdForURL($activity['actor'], 0, false);
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;
}
}
$item['isForum'] = false;
if (!empty($activity['thread-completion'])) {
// Store the original actor in the "causer" fields to enable the check for ignored or blocked contacts
$item['causer-link'] = $item['owner-link'];
$item['causer-id'] = $item['owner-id'];
Logger::info('Ignoring actor because of thread completion.', ['actor' => $item['owner-link']]);
$item['owner-link'] = $item['author-link'];
$item['owner-id'] = $item['author-id'];
} else {
$actor = APContact::getByURL($item['owner-link'], false);
$item['isForum'] = ($actor['type'] == 'Group');
}
$item['uri'] = $activity['id'];
$item['created'] = DateTimeFormat::utc($activity['published']);
$item['edited'] = DateTimeFormat::utc($activity['updated']);
$guid = $activity['sc:identifier'] ?: self::getGUIDByURL($item['uri']);
$item['guid'] = $activity['diaspora:guid'] ?: $guid;
$item['uri-id'] = ItemURI::insert(['uri' => $item['uri'], 'guid' => $item['guid']]);
$item = self::processContent($activity, $item);
if (empty($item)) {
return [];
}
$item['plink'] = $activity['alternate-url'] ?? $item['uri'];
$item = self::constructAttachList($activity, $item);
return $item;
}
/**
@ -291,7 +357,7 @@ class Processor
*/
public static function createActivity($activity, $verb)
{
$item = [];
$item = self::createItem($activity);
$item['verb'] = $verb;
$item['thr-parent'] = $activity['object_id'];
$item['gravity'] = GRAVITY_ACTIVITY;
@ -434,72 +500,8 @@ class Processor
* @throws \Friendica\Network\HTTPException\InternalServerErrorException
* @throws \ImagickException
*/
private static function postItem($activity, $item)
public static function postItem(array $activity, array $item)
{
/// @todo What to do with $activity['context']?
if (empty($activity['directmessage']) && ($item['gravity'] != GRAVITY_PARENT) && !Item::exists(['uri' => $item['thr-parent']])) {
Logger::info('Parent not found, message will be discarded.', ['thr-parent' => $item['thr-parent']]);
return;
}
$item['network'] = Protocol::ACTIVITYPUB;
$item['author-link'] = $activity['author'];
$item['author-id'] = Contact::getIdForURL($activity['author'], 0, false);
$item['owner-link'] = $activity['actor'];
$item['owner-id'] = Contact::getIdForURL($activity['actor'], 0, false);
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'])) {
// Store the original actor in the "causer" fields to enable the check for ignored or blocked contacts
$item['causer-link'] = $item['owner-link'];
$item['causer-id'] = $item['owner-id'];
Logger::info('Ignoring actor because of thread completion.', ['actor' => $item['owner-link']]);
$item['owner-link'] = $item['author-link'];
$item['owner-id'] = $item['author-id'];
} else {
$actor = APContact::getByURL($item['owner-link'], false);
$isForum = ($actor['type'] == 'Group');
}
$item['uri'] = $activity['id'];
$item['created'] = DateTimeFormat::utc($activity['published']);
$item['edited'] = DateTimeFormat::utc($activity['updated']);
$guid = $activity['sc:identifier'] ?: self::getGUIDByURL($item['uri']);
$item['guid'] = $activity['diaspora:guid'] ?: $guid;
$item['uri-id'] = ItemURI::insert(['uri' => $item['uri'], 'guid' => $item['guid']]);
$item = self::processContent($activity, $item);
if (empty($item)) {
return;
}
$item['plink'] = $activity['alternate-url'] ?? $item['uri'];
$item = self::constructAttachList($activity, $item);
$stored = false;
foreach ($activity['receiver'] as $receiver) {
@ -509,7 +511,7 @@ class Processor
$item['uid'] = $receiver;
if ($isForum) {
if ($item['isForum'] ?? false) {
$item['contact-id'] = Contact::getIdForURL($activity['actor'], $receiver, false);
} else {
$item['contact-id'] = Contact::getIdForURL($activity['author'], $receiver, false);
@ -527,7 +529,7 @@ class Processor
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)) {
if ($skip && (($activity['type'] == 'as:Announce') || ($item['isForum'] ?? false))) {
$skip = !Contact::isSharingByURL($activity['actor'], $receiver);
}

View file

@ -227,6 +227,7 @@ class Receiver
if ($type == 'as:Announce') {
$trust_source = false;
}
$object_data = self::fetchObject($object_id, $activity['as:object'], $trust_source, $uid);
if (empty($object_data)) {
Logger::log("Object data couldn't be processed", Logger::DEBUG);
@ -337,7 +338,6 @@ class Receiver
if (!JsonLD::fetchElement($activity, 'as:actor', '@id')) {
Logger::log('Empty actor', Logger::DEBUG);
return;
}
// Don't trust the source if "actor" differs from "attributedTo". The content could be forged.
@ -374,7 +374,8 @@ class Receiver
switch ($type) {
case 'as:Create':
if (in_array($object_data['object_type'], self::CONTENT_TYPES)) {
ActivityPub\Processor::createItem($object_data);
$item = ActivityPub\Processor::createItem($object_data);
ActivityPub\Processor::postItem($object_data, $item);
}
break;
@ -391,7 +392,8 @@ class Receiver
// If this isn't set, then a single reshare appears on top. This is used for groups.
$object_data['thread-completion'] = ($profile['type'] != 'Group');
ActivityPub\Processor::createItem($object_data);
$item = ActivityPub\Processor::createItem($object_data);
ActivityPub\Processor::postItem($object_data, $item);
// Add the bottom reshare information only for persons
if ($profile['type'] != 'Group') {