<?php
/**
- * @file src/Protocol/ActivityPub/Processor.php
+ * @copyright Copyright (C) 2020, Friendica
+ *
+ * @license GNU AGPL version 3 or any later version
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
+ *
*/
+
namespace Friendica\Protocol\ActivityPub;
-use Friendica\Database\DBA;
use Friendica\Content\Text\BBCode;
use Friendica\Content\Text\HTML;
-use Friendica\Core\Config;
-use Friendica\Core\PConfig;
use Friendica\Core\Logger;
use Friendica\Core\Protocol;
-use Friendica\Model\Contact;
+use Friendica\Database\DBA;
+use Friendica\DI;
use Friendica\Model\APContact;
-use Friendica\Model\Item;
+use Friendica\Model\Contact;
+use Friendica\Model\Conversation;
use Friendica\Model\Event;
+use Friendica\Model\Item;
+use Friendica\Model\Mail;
use Friendica\Model\Term;
use Friendica\Model\User;
-use Friendica\Model\Mail;
+use Friendica\Protocol\Activity;
use Friendica\Protocol\ActivityPub;
use Friendica\Util\DateTimeFormat;
use Friendica\Util\JsonLD;
$tag_text = '';
foreach ($tags as $tag) {
- if (in_array(defaults($tag, 'type', ''), ['Mention', 'Hashtag'])) {
+ if (in_array($tag['type'] ?? '', ['Mention', 'Hashtag'])) {
if (!empty($tag_text)) {
$tag_text .= ',';
}
/**
* Add attachment data to the item array
*
- * @param array $attachments
+ * @param array $activity
* @param array $item
- * @param boolean $no_images
*
* @return array array
*/
- private static function constructAttachList($attachments, $item, $no_images)
+ private static function constructAttachList($activity, $item)
{
- if (empty($attachments)) {
+ if (empty($activity['attachments'])) {
return $item;
}
- foreach ($attachments as $attach) {
+ foreach ($activity['attachments'] as $attach) {
$filetype = strtolower(substr($attach['mediaType'], 0, strpos($attach['mediaType'], '/')));
if ($filetype == 'image') {
- if ($no_images) {
+ if (!empty($activity['source']) && strpos($activity['source'], $attach['url'])) {
continue;
}
if (!isset($attach['length'])) {
$attach['length'] = "0";
}
- $item["attach"] .= '[attach]href="'.$attach['url'].'" length="'.$attach['length'].'" type="'.$attach['mediaType'].'" title="'.defaults($attach, 'name', '').'"[/attach]';
+ $item["attach"] .= '[attach]href="'.$attach['url'].'" length="'.$attach['length'].'" type="'.$attach['mediaType'].'" title="'.($attach['name'] ?? '') .'"[/attach]';
}
}
public static function createItem($activity)
{
$item = [];
- $item['verb'] = ACTIVITY_POST;
+ $item['verb'] = Activity::POST;
$item['thr-parent'] = $activity['reply-to-id'];
if ($activity['reply-to-id'] == $activity['id']) {
$item['gravity'] = GRAVITY_PARENT;
- $item['object-type'] = ACTIVITY_OBJ_NOTE;
+ $item['object-type'] = Activity\ObjectType::NOTE;
} else {
$item['gravity'] = GRAVITY_COMMENT;
- $item['object-type'] = ACTIVITY_OBJ_COMMENT;
+ $item['object-type'] = Activity\ObjectType::COMMENT;
+
+ // Ensure that the comment reaches all receivers of the referring post
+ $activity['receiver'] = self::addReceivers($activity);
}
if (empty($activity['directmessage']) && ($activity['id'] != $activity['reply-to-id']) && !Item::exists(['uri' => $activity['reply-to-id']])) {
- Logger::log('Parent ' . $activity['reply-to-id'] . ' not found. Try to refetch it.');
+ Logger::notice('Parent not found. Try to refetch it.', ['parent' => $activity['reply-to-id']]);
self::fetchMissingActivity($activity['reply-to-id'], $activity);
}
- $item['diaspora_signed_text'] = defaults($activity, 'diaspora:comment', '');
+ $item['diaspora_signed_text'] = $activity['diaspora:comment'] ?? '';
self::postItem($activity, $item);
}
}
}
+ /**
+ * Add users to the receiver list of the given public activity.
+ * This is used to ensure that the activity will be stored in every thread.
+ *
+ * @param array $activity Activity array
+ * @return array Modified receiver list
+ */
+ private static function addReceivers(array $activity)
+ {
+ if (!in_array(0, $activity['receiver'])) {
+ // Private activities will not be modified
+ return $activity['receiver'];
+ }
+
+ // Add all owners of the referring item to the receivers
+ $original = $receivers = $activity['receiver'];
+ $items = Item::select(['uid'], ['uri' => $activity['object_id']]);
+ while ($item = DBA::fetch($items)) {
+ $receivers['uid:' . $item['uid']] = $item['uid'];
+ }
+ DBA::close($items);
+
+ if (count($original) != count($receivers)) {
+ Logger::info('Improved data', ['id' => $activity['id'], 'object' => $activity['object_id'], 'original' => $original, 'improved' => $receivers]);
+ }
+
+ return $receivers;
+ }
+
/**
* Prepare the item array for an activity
*
$item['verb'] = $verb;
$item['thr-parent'] = $activity['object_id'];
$item['gravity'] = GRAVITY_ACTIVITY;
- $item['object-type'] = ACTIVITY_OBJ_NOTE;
+ $item['object-type'] = Activity\ObjectType::NOTE;
+
+ $item['diaspora_signed_text'] = $activity['diaspora:like'] ?? '';
- $item['diaspora_signed_text'] = defaults($activity, 'diaspora:like', '');
+ $activity['receiver'] = self::addReceivers($activity);
self::postItem($activity, $item);
}
Logger::warning('Unknown parent item.', ['uri' => $item['thr-parent']]);
return false;
}
- if ($item_private && !$parent['private']) {
+ if ($item_private && ($parent['private'] == Item::PRIVATE)) {
Logger::warning('Item is private but the parent is not. Dropping.', ['item-uri' => $item['uri'], 'thr-parent' => $item['thr-parent']]);
return false;
}
return $item;
}
+ /**
+ * Generate a GUID out of an URL
+ *
+ * @param string $url message URL
+ * @return string with GUID
+ */
+ private static function getGUIDByURL(string $url)
+ {
+ $parsed = parse_url($url);
+
+ $host_hash = hash('crc32', $parsed['host']);
+
+ unset($parsed["scheme"]);
+ unset($parsed["host"]);
+
+ $path = implode("/", $parsed);
+
+ return $host_hash . '-'. hash('fnv164', $path) . '-'. hash('joaat', $path);
+ }
+
/**
* Creates an item post
*
}
$item['network'] = Protocol::ACTIVITYPUB;
- $item['private'] = !in_array(0, $activity['receiver']);
$item['author-link'] = $activity['author'];
$item['author-id'] = Contact::getIdForURL($activity['author'], 0, true);
$item['owner-link'] = $activity['actor'];
$item['owner-id'] = Contact::getIdForURL($activity['actor'], 0, true);
+ 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'])) {
$item['created'] = DateTimeFormat::utc($activity['published']);
$item['edited'] = DateTimeFormat::utc($activity['updated']);
- $item['guid'] = $activity['diaspora:guid'];
+ $item['guid'] = $activity['diaspora:guid'] ?: self::getGUIDByURL($item['uri']);
$item = self::processContent($activity, $item);
if (empty($item)) {
return;
}
- $item['plink'] = defaults($activity, 'alternate-url', $item['uri']);
+ $item['plink'] = $activity['alternate-url'] ?? $item['uri'];
- $item = self::constructAttachList($activity['attachments'], $item, !empty($activity['source']));
+ $item = self::constructAttachList($activity, $item);
$stored = false;
foreach ($activity['receiver'] as $receiver) {
+ if ($receiver == -1) {
+ continue;
+ }
+
$item['uid'] = $receiver;
if ($isForum) {
continue;
}
- if (PConfig::get($receiver, 'system', 'accept_only_sharer', false) && ($receiver != 0) && ($item['gravity'] == GRAVITY_PARENT)) {
+ 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)) {
}
// Store send a follow request for every reshare - but only when the item had been stored
- if ($stored && !$item['private'] && ($item['gravity'] == GRAVITY_PARENT) && ($item['author-link'] != $item['owner-link'])) {
+ if ($stored && ($item['private'] != Item::PRIVATE) && ($item['gravity'] == GRAVITY_PARENT) && ($item['author-link'] != $item['owner-link'])) {
$author = APContact::getByURL($item['owner-link'], false);
// We send automatic follow requests for reshared messages. (We don't need though for forum posts)
if ($author['type'] != 'Group') {
*
* @param string $url message URL
* @param array $child activity array with the child of this message
- * @return boolean success
+ * @return string fetched message URL
* @throws \Friendica\Network\HTTPException\InternalServerErrorException
*/
public static function fetchMissingActivity($url, $child = [])
$object = ActivityPub::fetchContent($url, $uid);
if (empty($object)) {
Logger::log('Activity ' . $url . ' was not fetchable, aborting.');
- return false;
+ return '';
}
if (empty($object['id'])) {
Logger::log('Activity ' . $url . ' has got not id, aborting. ' . json_encode($object));
- return false;
+ return '';
}
if (!empty($child['author'])) {
$activity['@context'] = $object['@context'];
unset($object['@context']);
$activity['id'] = $object['id'];
- $activity['to'] = defaults($object, 'to', []);
- $activity['cc'] = defaults($object, 'cc', []);
+ $activity['to'] = $object['to'] ?? [];
+ $activity['cc'] = $object['cc'] ?? [];
$activity['actor'] = $actor;
$activity['object'] = $object;
$activity['published'] = $published;
$ldactivity['thread-completion'] = true;
- ActivityPub\Receiver::processActivity($ldactivity);
- Logger::log('Activity ' . $url . ' had been fetched and processed.');
+ ActivityPub\Receiver::processActivity($ldactivity, json_encode($activity));
+
+ Logger::notice('Activity had been fetched and processed.', ['url' => $url, 'object' => $activity['id']]);
- return true;
+ return $activity['id'];
}
/**
$item = ['author-id' => Contact::getIdForURL($activity['actor']),
'author-link' => $activity['actor']];
- $note = Strings::escapeTags(trim(defaults($activity, 'content', '')));
+ $note = Strings::escapeTags(trim($activity['content'] ?? ''));
// Ensure that the contact has got the right network type
self::switchContact($item['author-id']);
*/
private static function switchContact($cid)
{
- $contact = DBA::selectFirst('contact', ['network'], ['id' => $cid, 'network' => Protocol::NATIVE_SUPPORT]);
- if (!DBA::isResult($contact) || in_array($contact['network'], [Protocol::ACTIVITYPUB, Protocol::DFRN])) {
+ $contact = DBA::selectFirst('contact', ['network', 'url'], ['id' => $cid]);
+ if (!DBA::isResult($contact) || in_array($contact['network'], [Protocol::ACTIVITYPUB, Protocol::DFRN]) || Contact::isLocal($contact['url'])) {
return;
}
- Logger::log('Change existing contact ' . $cid . ' from ' . $contact['network'] . ' to ActivityPub.');
- Contact::updateFromProbe($cid, Protocol::ACTIVITYPUB);
+ Logger::info('Change existing contact', ['cid' => $cid, 'previous' => $contact['network']]);
+ Contact::updateFromProbe($cid);
}
/**
*/
private static function getImplicitMentionList(array $parent)
{
- if (Config::get('system', 'disable_implicit_mentions')) {
+ if (DI::config()->get('system', 'disable_implicit_mentions')) {
return [];
}
*/
private static function removeImplicitMentionsFromBody($body, array $potential_mentions)
{
- if (Config::get('system', 'disable_implicit_mentions')) {
+ if (DI::config()->get('system', 'disable_implicit_mentions')) {
return $body;
}
// Extract one prepended mention at a time from the body
while(preg_match('#^(@\[url=([^\]]+)].*?\[\/url]\s)(.*)#is', $body, $matches)) {
- if (!in_array($matches[2], $potential_mentions) ) {
+ if (!in_array($matches[2], $potential_mentions)) {
$kept_mentions[] = $matches[1];
}
private static function convertImplicitMentionsInTags($activity_tags, array $potential_mentions)
{
- if (Config::get('system', 'disable_implicit_mentions')) {
+ if (DI::config()->get('system', 'disable_implicit_mentions')) {
return $activity_tags;
}