use Friendica\Model\Tag;
use Friendica\Model\User;
use Friendica\Model\Post;
-use Friendica\Moderation\Entity\Report;
use Friendica\Protocol\Activity;
use Friendica\Protocol\ActivityPub;
use Friendica\Protocol\Delivery;
if ($fetch_parents && empty($activity['directmessage']) && ($activity['id'] != $activity['reply-to-id']) && !Post::exists(['uri' => $activity['reply-to-id']])) {
$result = self::fetchParent($activity, !empty($conversation));
- if (!empty($result)) {
- if (($item['thr-parent'] != $result) && Post::exists(['uri' => $result])) {
- $item['thr-parent'] = $result;
- }
- } elseif (empty($conversation)) {
- return [];
+ if (!empty($result) && ($item['thr-parent'] != $result) && Post::exists(['uri' => $result])) {
+ $item['thr-parent'] = $result;
}
}
$item['uri'] = $activity['id'];
if (empty($activity['published']) || empty($activity['updated'])) {
- DI::logger()->notice('published or updated keys are empty for activity', ['activity' => $activity, 'callstack' => System::callstack(10)]);
+ DI::logger()->notice('published or updated keys are empty for activity', ['activity' => $activity]);
}
$item['created'] = DateTimeFormat::utc($activity['published'] ?? 'now');
self::addActivityId($activity['reply-to-id']);
- if (!DI::config()->get('system', 'fetch_by_worker')) {
- $in_background = false;
+ $completion = $activity['completion-mode'] ?? Receiver::COMPLETION_NONE;
+
+ if (DI::config()->get('system', 'decoupled_receiver') && ($completion != Receiver::COMPLETION_MANUAL)) {
+ $in_background = true;
}
$recursion_depth = $activity['recursion-depth'] ?? 0;
if (!$in_background && ($recursion_depth < DI::config()->get('system', 'max_recursion_depth'))) {
- Logger::info('Parent not found. Try to refetch it.', ['parent' => $activity['reply-to-id'], 'recursion-depth' => $recursion_depth]);
+ Logger::info('Parent not found. Try to refetch it.', ['completion' => $completion, 'recursion-depth' => $recursion_depth, 'parent' => $activity['reply-to-id']]);
$result = self::fetchMissingActivity($activity['reply-to-id'], $activity, '', Receiver::COMPLETION_AUTO);
if (empty($result) && self::isActivityGone($activity['reply-to-id'])) {
Logger::notice('The activity is gone, the queue entry will be deleted', ['parent' => $activity['reply-to-id']]);
if (!empty($activity['entry-id'])) {
Queue::deleteById($activity['entry-id']);
}
- return '';
} elseif (!empty($result)) {
- $exists = Post::exists(['uri' => [$result, $activity['reply-to-id']]]);
- if ($exists) {
- Logger::info('The activity has been fetched and created.', ['parent' => $result]);
- return $result;
- } elseif (DI::config()->get('system', 'fetch_by_worker') || DI::config()->get('system', 'decoupled_receiver')) {
- Logger::info('The activity has been fetched and will hopefully be created later.', ['parent' => $result]);
+ $post = Post::selectFirstPost(['uri'], ['uri' => [$result, $activity['reply-to-id']]]);
+ if (!empty($post['uri'])) {
+ Logger::info('The activity has been fetched and created.', ['result' => $result, 'uri' => $post['uri']]);
+ return $post['uri'];
} else {
Logger::notice('The activity exists but has not been created, the queue entry will be deleted.', ['parent' => $result]);
if (!empty($activity['entry-id'])) {
Queue::deleteById($activity['entry-id']);
}
}
- return '';
- }
- if (empty($result) && !DI::config()->get('system', 'fetch_by_worker')) {
- return '';
}
+ return '';
} elseif (self::isActivityGone($activity['reply-to-id'])) {
Logger::notice('The activity is gone. We will not spawn a worker. The queue entry will be deleted', ['parent' => $activity['reply-to-id']]);
if ($in_background) {
Logger::notice('Fetching is done by worker.', ['parent' => $activity['reply-to-id'], 'recursion-depth' => $recursion_depth]);
Fetch::add($activity['reply-to-id']);
$activity['recursion-depth'] = 0;
- $wid = Worker::add(Worker::PRIORITY_HIGH, 'FetchMissingActivity', $activity['reply-to-id'], $activity, '', Receiver::COMPLETION_AUTO);
+ $wid = Worker::add(Worker::PRIORITY_HIGH, 'FetchMissingActivity', $activity['reply-to-id'], $activity, '', Receiver::COMPLETION_ASYNC);
Fetch::setWorkerId($activity['reply-to-id'], $wid);
} else {
Logger::debug('Activity will already be fetched via a worker.', ['url' => $activity['reply-to-id']]);
*/
public static function isActivityGone(string $url): bool
{
+ if (Network::isUrlBlocked($url)) {
+ return true;
+ }
+
try {
$curlResult = HTTPSignature::fetchRaw($url, 0);
} catch (\Exception $exception) {
Logger::notice('Error fetching url', ['url' => $url, 'exception' => $exception]);
return true;
- }
-
- if (Network::isUrlBlocked($url)) {
- return true;
- }
+ }
// @todo To ensure that the remote system is working correctly, we can check if the "Content-Type" contains JSON
if (in_array($curlResult->getReturnCode(), [401, 404])) {
}
if (!empty($parent['uri-id'])) {
- $parent;
+ return $parent;
}
return null;
{
$post = self::getUriIdForFeaturedCollection($activity);
if (empty($post)) {
+ Queue::remove($activity);
return;
}
{
$post = self::getUriIdForFeaturedCollection($activity);
if (empty($post)) {
+ Queue::remove($activity);
return;
}
$content = self::addMentionLinks($content, $activity['tags']);
if (!empty($activity['quote-url'])) {
- $id = Item::fetchByLink($activity['quote-url']);
+ $id = Item::fetchByLink($activity['quote-url'], 0, ActivityPub\Receiver::COMPLETION_ASYNC);
if ($id) {
$shared_item = Post::selectFirst(['uri-id'], ['id' => $id]);
$item['quote-uri-id'] = $shared_item['uri-id'];
}
$tags = array_column(Tag::getByURIId($item['uri-id'], [Tag::HASHTAG]), 'name');
- if (Relay::isSolicitedPost($tags, $item['body'], $item['author-id'], $item['uri'], Protocol::ACTIVITYPUB, $activity['thread-completion'] ?? 0)) {
+ if (Relay::isSolicitedPost($tags, $item['title'] . ' ' . ($item['content-warning'] ?? '') . ' ' . $item['body'], $item['author-id'], $item['uri'], Protocol::ACTIVITYPUB, $activity['thread-completion'] ?? 0)) {
Logger::debug('Post is accepted because of the relay settings', ['uri-id' => $item['uri-id'], 'guid' => $item['guid'], 'url' => $item['uri']]);
return true;
} else {
$item['contact-id'] = Contact::getIdForURL($activity['author']);
}
- if (!empty($activity['directmessage'])) {
- self::postMail($activity, $item);
+ if (!empty($activity['directmessage']) && self::postMail($item)) {
continue;
}
/**
* Creates an mail post
*
- * @param array $activity Activity data
- * @param array $item item array
+ * @param array $item item array
* @return int|bool New mail table row id or false on error
* @throws \Friendica\Network\HTTPException\InternalServerErrorException
*/
- private static function postMail(array $activity, array $item)
+ private static function postMail(array $item): bool
{
if (($item['gravity'] != Item::GRAVITY_PARENT) && !DBA::exists('mail', ['uri' => $item['thr-parent'], 'uid' => $item['uid']])) {
Logger::info('Parent not found, mail will be discarded.', ['uid' => $item['uid'], 'uri' => $item['thr-parent']]);
return false;
}
+ if (!Contact::isFollower($item['contact-id'], $item['uid']) && !Contact::isSharing($item['contact-id'], $item['uid'])) {
+ Logger::info('Contact is not a sharer or follower, mail will be discarded.', ['item' => $item]);
+ return false;
+ }
+
Logger::info('Direct Message', $item);
$msg = [];
if (empty($post['id'])) {
continue;
}
- $id = Item::fetchByLink($post['id']);
+ $id = Item::fetchByLink($post['id'], 0, ActivityPub\Receiver::COMPLETION_ASYNC);
if (!empty($id)) {
$item = Post::selectFirst(['uri-id', 'featured', 'author-id'], ['id' => $id]);
if (!empty($item['uri-id'])) {
return $object;
}
- $object = ActivityPub::fetchContent($url, $uid);
+ $object = HTTPSignature::fetch($url, $uid);
if (empty($object)) {
Logger::notice('Activity was not fetchable, aborting.', ['url' => $url, 'uid' => $uid]);
// We perform negative caching.
* @param string $relay_actor Relay actor
* @param int $completion Completion mode, see Receiver::COMPLETION_*
* @param int $uid User id that is used to fetch the activity
- * @return string fetched message URL
+ * @return string fetched message URL. An empty string indicates a temporary error, null indicates a permament error,
* @throws \Friendica\Network\HTTPException\InternalServerErrorException
* @throws \ImagickException
*/
- public static function fetchMissingActivity(string $url, array $child = [], string $relay_actor = '', int $completion = Receiver::COMPLETION_MANUAL, int $uid = 0): string
+ public static function fetchMissingActivity(string $url, array $child = [], string $relay_actor = '', int $completion = Receiver::COMPLETION_MANUAL, int $uid = 0): ?string
{
- $object = self::fetchCachedActivity($url, $uid);
- if (empty($object)) {
+ if (Network::isUrlBlocked($url)) {
+ return null;
+ }
+
+ try {
+ $curlResult = HTTPSignature::fetchRaw($url, $uid);
+ } catch (\Exception $exception) {
+ Logger::notice('Error fetching url', ['url' => $url, 'exception' => $exception]);
+ return '';
+ }
+
+ if (empty($curlResult)) {
+ return '';
+ }
+
+ $body = $curlResult->getBody();
+ if (!$curlResult->isSuccess() || empty($body)) {
+ if (in_array($curlResult->getReturnCode(), [403, 404, 406, 410])) {
+ return null;
+ }
+ return '';
+ }
+
+ $object = json_decode($body, true);
+ if (empty($object) || !is_array($object)) {
+ $element = explode(';', $curlResult->getContentType());
+ if (!in_array($element[0], ['application/activity+json', 'application/ld+json', 'application/json'])) {
+ Logger::debug('Unexpected content-type', ['url' => $url, 'content-type' => $curlResult->getContentType()]);
+ return null;
+ }
+ Logger::notice('Invalid JSON data', ['url' => $url, 'content-type' => $curlResult->getContentType(), 'body' => $body]);
return '';
}
$actor = $object_actor;
}
- if (!empty($object['published'])) {
- $published = $object['published'];
- } elseif (!empty($child['published'])) {
- $published = $child['published'];
- } else {
- $published = DateTimeFormat::utcNow();
- }
+ $ldobject = JsonLD::compact($object);
- $activity = [];
- $activity['@context'] = $object['@context'] ?? ActivityPub::CONTEXT;
- unset($object['@context']);
- $activity['id'] = $object['id'];
- $activity['to'] = $object['to'] ?? [];
- $activity['cc'] = $object['cc'] ?? [];
- $activity['audience'] = $object['audience'] ?? [];
- $activity['actor'] = $actor;
- $activity['object'] = $object;
- $activity['published'] = $published;
- $activity['type'] = 'Create';
+ $type = JsonLD::fetchElement($ldobject, '@type');
+ $object_id = JsonLD::fetchElement($ldobject, 'as:object', '@id');
- $ldactivity = JsonLD::compact($activity);
+ if (!in_array($type, Receiver::CONTENT_TYPES) && !empty($object_id)) {
+ if (($type == 'as:Announce') && !empty($relay_actor) && ($completion = Receiver::COMPLETION_RELAY)) {
+ if (Item::searchByLink($object_id)) {
+ return $object_id;
+ }
+ Logger::debug('Fetch announced activity', ['type' => $type, 'id' => $object_id, 'actor' => $relay_actor, 'signer' => $signer]);
+ return self::fetchMissingActivity($object_id, $child, $relay_actor, $completion, $uid);
+ }
+ $activity = $object;
+ $ldactivity = $ldobject;
+ } else {
+ $activity = self::getActivityForObject($object, $actor);
+ $ldactivity = JsonLD::compact($activity);
+ $object_id = $object['id'];
+ }
+
$ldactivity['recursion-depth'] = !empty($child['recursion-depth']) ? $child['recursion-depth'] + 1 : 0;
if ($object_actor != $actor) {
if ($completion == Receiver::COMPLETION_RELAY) {
$ldactivity['from-relay'] = $ldactivity['thread-completion'];
- if (!self::acceptIncomingMessage($ldactivity, $object['id'])) {
- return '';
+ if (in_array($type, Receiver::CONTENT_TYPES) && !self::acceptIncomingMessage($ldactivity, $object_id)) {
+ return null;
}
}
return $activity['id'];
}
+ private static function getActivityForObject(array $object, string $actor): array
+ {
+ if (!empty($object['published'])) {
+ $published = $object['published'];
+ } elseif (!empty($child['published'])) {
+ $published = $child['published'];
+ } else {
+ $published = DateTimeFormat::utcNow();
+ }
+
+ $activity = [];
+ $activity['@context'] = $object['@context'] ?? ActivityPub::CONTEXT;
+ unset($object['@context']);
+ $activity['id'] = $object['id'];
+ $activity['to'] = $object['to'] ?? [];
+ $activity['cc'] = $object['cc'] ?? [];
+ $activity['audience'] = $object['audience'] ?? [];
+ $activity['actor'] = $actor;
+ $activity['object'] = $object;
+ $activity['published'] = $published;
+ $activity['type'] = 'Create';
+
+ return $activity;
+ }
+
/**
* Test if incoming relay messages should be accepted
*
$attributed_to = JsonLD::fetchElement($activity['as:object'], 'as:attributedTo', '@id');
$authorid = Contact::getIdForURL($attributed_to);
- $body = HTML::toBBCode(JsonLD::fetchElement($activity['as:object'], 'as:content', '@value') ?? '');
+ $content = JsonLD::fetchElement($activity['as:object'], 'as:name', '@value') ?? '';
+ $content .= ' ' . JsonLD::fetchElement($activity['as:object'], 'as:summary', '@value') ?? '';
+ $content .= ' ' . HTML::toBBCode(JsonLD::fetchElement($activity['as:object'], 'as:content', '@value') ?? '');
+
+ $attachments = JsonLD::fetchElementArray($activity['as:object'], 'as:attachment') ?? [];
+ foreach ($attachments as $media) {
+ if (!empty($media['as:summary'])) {
+ $content .= ' ' . JsonLD::fetchElement($media, 'as:summary', '@value');
+ }
+ if (!empty($media['as:name'])) {
+ $content .= ' ' . JsonLD::fetchElement($media, 'as:name', '@value');
+ }
+ }
$messageTags = [];
$tags = Receiver::processTags(JsonLD::fetchElementArray($activity['as:object'], 'as:tag') ?? []);
if (!empty($tags)) {
foreach ($tags as $tag) {
- if ($tag['type'] != 'Hashtag') {
+ if (($tag['type'] != 'Hashtag') && !strpos($tag['type'], ':Hashtag')) {
continue;
}
$messageTags[] = ltrim(mb_strtolower($tag['name']), '#');
}
}
- return Relay::isSolicitedPost($messageTags, $body, $authorid, $id, Protocol::ACTIVITYPUB, $activity['thread-completion'] ?? 0);
+ $languages = self::getPostLanguages($activity);
+
+ return Relay::isSolicitedPost($messageTags, $content, $authorid, $id, Protocol::ACTIVITYPUB, $activity['thread-completion'] ?? 0, $languages);
+ }
+
+ /**
+ * Fetch the post language from the content
+ *
+ * @param array $activity
+ * @return array
+ */
+ private static function getPostLanguages(array $activity): array
+ {
+ $content = JsonLD::fetchElement($activity['as:object'], 'as:content') ?? '';
+ $languages = JsonLD::fetchElementArray($activity['as:object'], 'as:content', '@language') ?? [];
+ if (empty($languages)) {
+ return [];
+ }
+
+ $iso639 = new \Matriphe\ISO639\ISO639;
+
+ $result = [];
+ foreach ($languages as $language) {
+ if ($language == $content) {
+ continue;
+ }
+ $language = DI::l10n()->toISO6391($language);
+ if (!in_array($language, array_column($iso639->allLanguages(), 0))) {
+ continue;
+ }
+ $result[] = $language;
+ }
+ return $result;
}
/**