use Friendica\Core\Protocol;
use Friendica\Model\Contact;
use Friendica\Model\APContact;
-use Friendica\Model\Conversation;
use Friendica\Model\Item;
use Friendica\Model\User;
use Friendica\Protocol\Activity;
{
const PUBLIC_COLLECTION = 'as:Public';
const ACCOUNT_TYPES = ['as:Person', 'as:Organization', 'as:Service', 'as:Group', 'as:Application'];
- const CONTENT_TYPES = ['as:Note', 'as:Article', 'as:Video', 'as:Image', 'as:Event'];
+ const CONTENT_TYPES = ['as:Note', 'as:Article', 'as:Video', 'as:Image', 'as:Event', 'as:Audio'];
const ACTIVITY_TYPES = ['as:Like', 'as:Dislike', 'as:Accept', 'as:Reject', 'as:TentativeAccept'];
/**
$trust_source = false;
}
- self::processActivity($ldactivity, $body, $uid, $trust_source);
+ self::processActivity($ldactivity, $body, $uid, $trust_source, true);
}
/**
/**
* Prepare the object array
*
- * @param array $activity
- * @param integer $uid User ID
- * @param $trust_source
+ * @param array $activity Array with activity data
+ * @param integer $uid User ID
+ * @param boolean $push Message had been pushed to our system
+ * @param boolean $trust_source Do we trust the source?
*
* @return array with object data
* @throws \Friendica\Network\HTTPException\InternalServerErrorException
* @throws \ImagickException
*/
- private static function prepareObjectData($activity, $uid, &$trust_source)
+ private static function prepareObjectData($activity, $uid, $push, &$trust_source)
{
$actor = JsonLD::fetchElement($activity, 'as:actor', '@id');
if (empty($actor)) {
Logger::log("Object data couldn't be processed", Logger::DEBUG);
return [];
}
+
$object_data['object_id'] = $object_id;
+ if ($type == 'as:Announce') {
+ $object_data['push'] = false;
+ } else {
+ $object_data['push'] = $push;
+ }
+
// Test if it is an answer to a mail
if (DBA::exists('mail', ['uri' => $object_data['reply-to-id']])) {
$object_data['directmessage'] = true;
return 0;
}
- /**
- * Store the unprocessed data into the conversation table
- * This has to be done outside the regular function,
- * since we store everything - not only item posts.
- *
- * @param array $activity Array with activity data
- * @param string $body The raw message
- * @throws \Exception
- */
- private static function storeConversation($activity, $body)
- {
- if (empty($body) || empty($activity['id'])) {
- return;
- }
-
- $conversation = [
- 'protocol' => Conversation::PARCEL_ACTIVITYPUB,
- 'item-uri' => $activity['id'],
- 'reply-to-uri' => $activity['reply-to-id'] ?? '',
- 'conversation-href' => $activity['context'] ?? '',
- 'conversation-uri' => $activity['conversation'] ?? '',
- 'source' => $body,
- 'received' => DateTimeFormat::utcNow()];
-
- DBA::insert('conversation', $conversation, true);
- }
-
/**
* Processes the activity object
*
* @param string $body
* @param integer $uid User ID
* @param boolean $trust_source Do we trust the source?
+ * @param boolean $push Message had been pushed to our system
* @throws \Exception
*/
- public static function processActivity($activity, $body = '', $uid = null, $trust_source = false)
+ public static function processActivity($activity, $body = '', $uid = null, $trust_source = false, $push = false)
{
$type = JsonLD::fetchElement($activity, '@type');
if (!$type) {
}
// $trust_source is called by reference and is set to true if the content was retrieved successfully
- $object_data = self::prepareObjectData($activity, $uid, $trust_source);
+ $object_data = self::prepareObjectData($activity, $uid, $push, $trust_source);
if (empty($object_data)) {
Logger::log('No object data found', Logger::DEBUG);
return;
return;
}
- // Only store content related stuff - and no announces, since they possibly overwrite the original content
- if (in_array($object_data['object_type'], self::CONTENT_TYPES) && ($type != 'as:Announce')) {
- self::storeConversation($object_data, $body);
+ if (!empty($body) && empty($object_data['raw'])) {
+ $object_data['raw'] = $body;
}
// Internal flag for thread completion. See Processor.php
$announce_object_data['author'] = JsonLD::fetchElement($activity, 'as:actor', '@id');
$announce_object_data['object_id'] = $object_data['object_id'];
$announce_object_data['object_type'] = $object_data['object_type'];
+ $announce_object_data['push'] = $push;
+
+ if (!empty($body)) {
+ $announce_object_data['raw'] = $body;
+ }
ActivityPub\Processor::createActivity($announce_object_data, Activity::ANNOUNCE);
}
/**
* Fetch the receiver list from an activity array
*
- * @param array $activity
- * @param string $actor
- * @param array $tags
+ * @param array $activity
+ * @param string $actor
+ * @param array $tags
+ * @param boolean $fetch_unlisted
*
* @return array with receivers (user id)
* @throws \Exception
*/
- private static function getReceivers($activity, $actor, $tags = [])
+ private static function getReceivers($activity, $actor, $tags = [], $fetch_unlisted = false)
{
$receivers = [];
$receivers['uid:0'] = 0;
}
+ // Add receiver "-1" for unlisted posts
+ if ($fetch_unlisted && ($receiver == self::PUBLIC_COLLECTION) && ($element == 'as:cc')) {
+ $receivers['uid:-1'] = -1;
+ }
+
if (($receiver == self::PUBLIC_COLLECTION) && !empty($actor)) {
// This will most likely catch all OStatus connections to Mastodon
$condition = ['alias' => [$actor, Strings::normaliseLink($actor)], 'rel' => [Contact::SHARING, Contact::FRIEND]
}
if (in_array($type, self::CONTENT_TYPES)) {
- return self::processObject($object);
+ $object_data = self::processObject($object);
+
+ if (!empty($data)) {
+ $object_data['raw'] = json_encode($data);
+ }
+ return $object_data;
}
if ($type == 'as:Announce') {
return $object_data;
}
+ /**
+ * Check if the "as:url" element is an array with multiple links
+ * This is the case with audio and video posts.
+ * Then the links are added as attachments
+ *
+ * @param array $object The raw object
+ * @param array $object_data The parsed object data for later processing
+ * @return array the object data
+ */
+ private static function processAttachmentUrls(array $object, array $object_data) {
+ // Check if this is some url with multiple links
+ if (empty($object['as:url'])) {
+ return $object_data;
+ }
+
+ $urls = $object['as:url'];
+ $keys = array_keys($urls);
+ if (!is_numeric(array_pop($keys))) {
+ return $object_data;
+ }
+
+ $attachments = [];
+
+ foreach ($urls as $url) {
+ if (empty($url['@type']) || ($url['@type'] != 'as:Link')) {
+ continue;
+ }
+
+ $href = JsonLD::fetchElement($url, 'as:href', '@id');
+ if (empty($href)) {
+ continue;
+ }
+
+ $mediatype = JsonLD::fetchElement($url, 'as:mediaType');
+ if (empty($mediatype)) {
+ continue;
+ }
+
+ if ($mediatype == 'text/html') {
+ $object_data['alternate-url'] = $href;
+ }
+
+ $filetype = strtolower(substr($mediatype, 0, strpos($mediatype, '/')));
+
+ if ($filetype == 'audio') {
+ $attachments[$filetype] = ['type' => $mediatype, 'url' => $href];
+ } elseif ($filetype == 'video') {
+ $height = (int)JsonLD::fetchElement($url, 'as:height', '@value');
+
+ // We save bandwidth by using a moderate height
+ // Peertube normally uses these heights: 240, 360, 480, 720, 1080
+ if (!empty($attachments[$filetype]['height']) &&
+ (($height > 480) || $height < $attachments[$filetype]['height'])) {
+ continue;
+ }
+
+ $attachments[$filetype] = ['type' => $mediatype, 'url' => $href, 'height' => $height];
+ }
+ }
+
+ foreach ($attachments as $type => $attachment) {
+ $object_data['attachments'][] = ['type' => $type,
+ 'mediaType' => $attachment['type'],
+ 'name' => '',
+ 'url' => $attachment['url']];
+ }
+ return $object_data;
+ }
+
/**
* Fetches data from the object part of an activity
*
$actor = JsonLD::fetchElement($object, 'as:actor', '@id');
}
+ $object_data['sc:identifier'] = JsonLD::fetchElement($object, 'sc:identifier', '@value');
$object_data['diaspora:guid'] = JsonLD::fetchElement($object, 'diaspora:guid', '@value');
$object_data['diaspora:comment'] = JsonLD::fetchElement($object, 'diaspora:comment', '@value');
$object_data['diaspora:like'] = JsonLD::fetchElement($object, 'diaspora:like', '@value');
}
}
- $object_data['receiver'] = self::getReceivers($object, $object_data['actor'], $object_data['tags']);
+ if (in_array($object_data['object_type'], ['as:Audio', 'as:Video'])) {
+ $object_data = self::processAttachmentUrls($object, $object_data);
+ }
+
+ $object_data['receiver'] = self::getReceivers($object, $object_data['actor'], $object_data['tags'], true);
+ $object_data['unlisted'] = in_array(-1, $object_data['receiver']);
+ unset($object_data['receiver']['uid:-1']);
// Common object data: