namespace Friendica\Protocol\ActivityPub;
+use Friendica\App;
use Friendica\Content\Feature;
use Friendica\Content\Text\BBCode;
use Friendica\Core\Cache\Enum\Duration;
use Friendica\DI;
use Friendica\Model\APContact;
use Friendica\Model\Contact;
-use Friendica\Model\Conversation;
use Friendica\Model\GServer;
use Friendica\Model\Item;
use Friendica\Model\Photo;
use Friendica\Network\HTTPException;
use Friendica\Protocol\Activity;
use Friendica\Protocol\ActivityPub;
+use Friendica\Protocol\Diaspora;
use Friendica\Protocol\Relay;
use Friendica\Util\DateTimeFormat;
use Friendica\Util\HTTPSignature;
{
const CACHEKEY_FEATURED = 'transmitter:getFeatured:';
const CACHEKEY_CONTACTS = 'transmitter:getContacts:';
- const CACHEKEY_OUTBOX = 'transmitter:getOutbox:';
/**
* Add relay servers to the list of inboxes
*/
public static function getOutbox(array $owner, int $page = null, string $requester = '', bool $nocache = false): array
{
- if (empty($page)) {
- $cachekey = self::CACHEKEY_OUTBOX . $owner['uid'];
- $result = DI::cache()->get($cachekey);
- if (!$nocache && !is_null($result)) {
- return $result;
- }
- }
-
$condition = ['private' => [Item::PUBLIC, Item::UNLISTED]];
if (!empty($requester)) {
$condition = array_merge($condition, [
'uid' => $owner['uid'],
'author-id' => Contact::getIdForURL($owner['url'], 0, false),
- 'gravity' => [GRAVITY_PARENT, GRAVITY_COMMENT],
+ 'gravity' => [Item::GRAVITY_PARENT, Item::GRAVITY_COMMENT],
'network' => Protocol::FEDERATED,
'parent-network' => Protocol::FEDERATED,
'origin' => true,
'visible' => true
]);
- $count = Post::count($condition);
+ $apcontact = APContact::getByURL($owner['url']);
$data = ['@context' => ActivityPub::CONTEXT];
$data['id'] = DI::baseUrl() . '/outbox/' . $owner['nickname'];
$data['type'] = 'OrderedCollection';
- $data['totalItems'] = $count;
+ $data['totalItems'] = $apcontact['statuses_count'] ?? 0;
if (!empty($page)) {
$data['id'] .= '?' . http_build_query(['page' => $page]);
$data['next'] = DI::baseUrl() . '/outbox/' . $owner['nickname'] . '?page=' . ($page + 1);
}
+ // Fix the cached total item count when it is lower than the real count
+ $total = (($page - 1) * 20) + $data['totalItems'];
+ if ($total > $data['totalItems']) {
+ $data['totalItems'] = $total;
+ }
+
$data['partOf'] = DI::baseUrl() . '/outbox/' . $owner['nickname'];
$data['orderedItems'] = $list;
}
- if (!empty($cachekey)) {
- DI::cache()->set($cachekey, $data, Duration::DAY);
- }
-
return $data;
}
'uid' => $owner['uid'],
'author-id' => $owner_cid,
'private' => [Item::PUBLIC, Item::UNLISTED],
- 'gravity' => [GRAVITY_PARENT, GRAVITY_COMMENT],
+ 'gravity' => [Item::GRAVITY_PARENT, Item::GRAVITY_COMMENT],
'network' => Protocol::FEDERATED,
'parent-network' => Protocol::FEDERATED,
'origin' => true,
{
return [
'type' => 'Service',
- 'name' => FRIENDICA_PLATFORM . " '" . FRIENDICA_CODENAME . "' " . FRIENDICA_VERSION . '-' . DB_UPDATE_VERSION,
+ 'name' => App::PLATFORM . " '" . App::CODENAME . "' " . App::VERSION . '-' . DB_UPDATE_VERSION,
'url' => DI::baseUrl()->get()
];
}
$item_profile = APContact::getByURL($item['author-link']);
$exclude[] = $item['author-link'];
- if ($item['gravity'] == GRAVITY_PARENT) {
+ if ($item['gravity'] == Item::GRAVITY_PARENT) {
$exclude[] = $item['owner-link'];
}
}
$parent = Post::selectFirst(['causer-link', 'post-reason'], ['id' => $item['parent']]);
- if (($parent['post-reason'] == Item::PR_ANNOUNCEMENT) && !empty($parent['causer-link'])) {
+ if (!empty($parent) && ($parent['post-reason'] == Item::PR_ANNOUNCEMENT) && !empty($parent['causer-link'])) {
$profile = APContact::getByURL($parent['causer-link'], false);
$is_forum_thread = isset($profile['type']) && $profile['type'] == 'Group';
} else {
$data = ['to' => [], 'cc' => [], 'bcc' => []];
- if ($item['gravity'] == GRAVITY_PARENT) {
+ if ($item['gravity'] == Item::GRAVITY_PARENT) {
$actor_profile = APContact::getByURL($item['owner-link']);
} else {
$actor_profile = APContact::getByURL($item['author-link']);
if (!empty($item['parent'])) {
$parents = Post::select(['id', 'author-link', 'owner-link', 'gravity', 'uri'], ['parent' => $item['parent']], ['order' => ['id']]);
while ($parent = Post::fetch($parents)) {
- if ($parent['gravity'] == GRAVITY_PARENT) {
+ if ($parent['gravity'] == Item::GRAVITY_PARENT) {
$profile = APContact::getByURL($parent['owner-link'], false);
if (!empty($profile)) {
- if ($item['gravity'] != GRAVITY_PARENT) {
+ if ($item['gravity'] != Item::GRAVITY_PARENT) {
// Comments to forums are directed to the forum
// But comments to forums aren't directed to the followers collection
// This rule is only valid when the actor isn't the forum.
$data['to'][] = $profile['url'];
} else {
$data['cc'][] = $profile['url'];
- if (($item['private'] != Item::PRIVATE) && !empty($actor_profile['followers'])&& !$is_forum_thread) {
+ if (($item['private'] != Item::PRIVATE) && !empty($actor_profile['followers']) && (!$exclusive || !$is_forum_thread)) {
$data['cc'][] = $actor_profile['followers'];
}
}
$networks = [Protocol::ACTIVITYPUB, Protocol::OSTATUS];
}
- $condition = ['uid' => $uid, 'archive' => false, 'pending' => false, 'blocked' => false, 'network' => Protocol::FEDERATED];
+ $condition = [
+ 'uid' => $uid,
+ 'archive' => false,
+ 'pending' => false,
+ 'blocked' => false,
+ 'network' => Protocol::FEDERATED,
+ ];
if (!empty($uid)) {
$condition['rel'] = [Contact::FOLLOWER, Contact::FRIEND];
$inboxes = [];
- if ($item['gravity'] == GRAVITY_ACTIVITY) {
+ if ($item['gravity'] == Item::GRAVITY_ACTIVITY) {
$item_profile = APContact::getByURL($item['author-link'], false);
} else {
$item_profile = APContact::getByURL($item['owner-link'], false);
$mail['parent-uri'] = $reply['uri'];
$mail['parent-uri-id'] = $reply['uri-id'];
$mail['parent-author-id'] = Contact::getIdForURL($reply['from-url'], 0, false);
- $mail['gravity'] = ($mail['reply'] ? GRAVITY_COMMENT: GRAVITY_PARENT);
+ $mail['gravity'] = ($mail['reply'] ? Item::GRAVITY_COMMENT: Item::GRAVITY_PARENT);
$mail['event-type'] = '';
$mail['language'] = '';
$mail['parent'] = 0;
*
* @param integer $item_id Item id
* @param boolean $force Force new cache entry
- * @return array with the activity
+ * @return array|false activity or false on failure
* @throws \Exception
*/
- public static function createCachedActivityFromItem(int $item_id, bool $force = false): array
+ public static function createCachedActivityFromItem(int $item_id, bool $force = false, bool $object_mode = false)
{
- $cachekey = 'APDelivery:createActivity:' . $item_id;
+ $cachekey = 'APDelivery:createActivity:' . $item_id . ':' . (int)$object_mode;
if (!$force) {
$data = DI::cache()->get($cachekey);
}
}
- $data = self::createActivityFromItem($item_id);
+ $data = self::createActivityFromItem($item_id, $object_mode);
DI::cache()->set($cachekey, $data, Duration::QUARTER_HOUR);
return $data;
}
if (!$item['deleted']) {
- $condition = ['item-uri' => $item['uri'], 'protocol' => Conversation::PARCEL_ACTIVITYPUB];
- $conversation = DBA::selectFirst('conversation', ['source'], $condition);
- if (!$item['origin'] && DBA::isResult($conversation)) {
- $data = json_decode($conversation['source'], true);
- if (!empty($data['type'])) {
- if (in_array($data['type'], ['Create', 'Update'])) {
- if ($object_mode) {
- unset($data['@context']);
- unset($data['signature']);
- }
- Logger::info('Return stored conversation', ['item' => $item_id]);
- return $data;
- } elseif (in_array('as:' . $data['type'], Receiver::CONTENT_TYPES)) {
- if (!empty($data['@context'])) {
- $context = $data['@context'];
- unset($data['@context']);
- }
- unset($data['actor']);
- $object = $data;
- }
+ $data = Post\Activity::getByURIId($item['uri-id']);
+ if (!$item['origin'] && !empty($data)) {
+ if ($object_mode) {
+ unset($data['@context']);
+ unset($data['signature']);
}
+ Logger::info('Return stored conversation', ['item' => $item_id]);
+ return $data;
}
}
+ if (!$item['origin'] && empty($object)) {
+ Logger::debug('Post is not ours and is not stored', ['id' => $item_id, 'uri-id' => $item['uri-id']]);
+ return false;
+ }
+
$type = self::getTypeOfItem($item);
if (!$object_mode) {
$data = ['@context' => $context ?? ActivityPub::CONTEXT];
- if ($item['deleted'] && ($item['gravity'] == GRAVITY_ACTIVITY)) {
+ if ($item['deleted'] && ($item['gravity'] == Item::GRAVITY_ACTIVITY)) {
$type = 'Undo';
} elseif ($item['deleted']) {
$type = 'Delete';
}
if ($type == 'Delete') {
- $data['id'] = Item::newURI($item['uid'], $item['guid']) . '/' . $type;;
- } elseif (($item['gravity'] == GRAVITY_ACTIVITY) && ($type != 'Undo')) {
+ $data['id'] = Item::newURI($item['guid']) . '/' . $type;;
+ } elseif (($item['gravity'] == Item::GRAVITY_ACTIVITY) && ($type != 'Undo')) {
$data['id'] = $item['uri'];
} else {
$data['id'] = $item['uri'] . '/' . $type;
$data['type'] = $type;
- if (($type != 'Announce') || ($item['gravity'] != GRAVITY_PARENT)) {
+ if (($type != 'Announce') || ($item['gravity'] != Item::GRAVITY_PARENT)) {
$data['actor'] = $item['author-link'];
} else {
$data['actor'] = $item['owner-link'];
/**
* Returns a tag array for a given item array
*
- * @param array $item Item array
+ * @param array $item Item array
+ * @param string $quote_url Url of the attached quote link
* @return array of tags
* @throws \Friendica\Network\HTTPException\InternalServerErrorException
*/
- private static function createTagList(array $item): array
+ private static function createTagList(array $item, string $quote_url): array
{
$tags = [];
$tags[] = ['type' => 'Mention', 'href' => $announce['actor']['url'], 'name' => '@' . $announce['actor']['addr']];
}
+ // @see https://codeberg.org/fediverse/fep/src/branch/main/feps/fep-e232.md
+ if (!empty($quote_url)) {
+ // Currently deactivated because of compatibility issues with Pleroma
+ //$tags[] = [
+ // 'type' => 'Link',
+ // 'mediaType' => 'application/ld+json; profile="https://www.w3.org/ns/activitystreams"',
+ // 'href' => $quote_url,
+ // 'name' => '♲ ' . BBCode::convertForUriId($item['uri-id'], $quote_url, BBCode::ACTIVITYPUB)
+ //];
+ }
+
return $tags;
}
* Adds attachment data to the JSON document
*
* @param array $item Data of the item that is to be posted
- * @param string $type Object type
*
* @return array with attachment data
* @throws \Friendica\Network\HTTPException\InternalServerErrorException
*/
- private static function createAttachmentList(array $item, string $type): array
+ private static function createAttachmentList(array $item): array
{
$attachments = [];
- $uriids = [$item['uri-id']];
- $shared = BBCode::fetchShareAttributes($item['body']);
- if (!empty($shared['guid'])) {
- $shared_item = Post::selectFirst(['uri-id'], ['guid' => $shared['guid']]);
- if (!empty($shared_item['uri-id'])) {
- $uriids[] = $shared_item['uri-id'];
- }
- }
-
$urls = [];
- foreach ($uriids as $uriid) {
- foreach (Post\Media::getByURIId($uriid, [Post\Media::AUDIO, Post\Media::IMAGE, Post\Media::VIDEO, Post\Media::DOCUMENT, Post\Media::TORRENT]) as $attachment) {
- if (in_array($attachment['url'], $urls)) {
- continue;
- }
- $urls[] = $attachment['url'];
-
- $attach = ['type' => 'Document',
- 'mediaType' => $attachment['mimetype'],
- 'url' => $attachment['url'],
- 'name' => $attachment['description']];
+ foreach (Post\Media::getByURIId($item['uri-id'], [Post\Media::AUDIO, Post\Media::IMAGE, Post\Media::VIDEO, Post\Media::DOCUMENT, Post\Media::TORRENT]) as $attachment) {
+ if (in_array($attachment['url'], $urls)) {
+ continue;
+ }
+ $urls[] = $attachment['url'];
- if (!empty($attachment['height'])) {
- $attach['height'] = $attachment['height'];
- }
+ $attach = ['type' => 'Document',
+ 'mediaType' => $attachment['mimetype'],
+ 'url' => $attachment['url'],
+ 'name' => $attachment['description']];
- if (!empty($attachment['width'])) {
- $attach['width'] = $attachment['width'];
- }
+ if (!empty($attachment['height'])) {
+ $attach['height'] = $attachment['height'];
+ }
- if (!empty($attachment['preview'])) {
- $attach['image'] = $attachment['preview'];
- }
+ if (!empty($attachment['width'])) {
+ $attach['width'] = $attachment['width'];
+ }
- $attachments[] = $attach;
+ if (!empty($attachment['preview'])) {
+ $attach['image'] = $attachment['preview'];
}
+
+ $attachments[] = $attach;
}
return $attachments;
return $body;
}
- /**
- * Fetches the "context" value for a givem item array from the "conversation" table
- *
- * @param array $item Item array
- * @return string with context url
- * @throws \Exception
- */
- private static function fetchContextURLForItem(array $item): string
- {
- $conversation = DBA::selectFirst('conversation', ['conversation-href', 'conversation-uri'], ['item-uri' => $item['parent-uri']]);
- if (DBA::isResult($conversation) && !empty($conversation['conversation-href'])) {
- $context_uri = $conversation['conversation-href'];
- } elseif (DBA::isResult($conversation) && !empty($conversation['conversation-uri'])) {
- $context_uri = $conversation['conversation-uri'];
- } else {
- $context_uri = $item['parent-uri'] . '#context';
- }
- return $context_uri;
- }
-
/**
* Returns if the post contains sensitive content ("nsfw")
*
// We are treating posts differently when they are directed to a community.
// This is done to better support Lemmy. Most of the changes should work with other systems as well.
// But to not risk compatibility issues we currently perform the changes only for communities.
- if ($item['gravity'] == GRAVITY_PARENT) {
+ if ($item['gravity'] == Item::GRAVITY_PARENT) {
$isCommunityPost = !empty(Tag::getByURIId($item['uri-id'], [Tag::EXCLUSIVE_MENTION]));
$links = Post\Media::getByURIId($item['uri-id'], [Post\Media::HTML]);
if ($isCommunityPost && (count($links) == 1)) {
$data['url'] = $link ?? $item['plink'];
$data['attributedTo'] = $item['author-link'];
$data['sensitive'] = self::isSensitive($item['uri-id']);
- $data['context'] = self::fetchContextURLForItem($item);
+
+ if (!empty($item['conversation']) && ($item['conversation'] != './')) {
+ $data['conversation'] = $data['context'] = $item['conversation'];
+ }
if (!empty($item['title'])) {
$data['name'] = BBCode::toPlaintext($item['title'], false);
$permission_block = self::createPermissionBlockForItem($item, false);
+ $real_quote = false;
+
$body = $item['body'];
if ($type == 'Note') {
$body = BBCode::setMentionsToNicknames($body);
+ if (!empty($item['quote-uri-id'])) {
+ $body = BBCode::removeSharedData($body);
+ if (Post::exists(['uri-id' => $item['quote-uri-id'], 'network' => [Protocol::ACTIVITYPUB, Protocol::DFRN]])) {
+ $real_quote = true;
+ $data['quoteUrl'] = $item['quote-uri'];
+ $body = DI::contentItem()->addShareLink($body, $item['quote-uri-id']);
+ } else {
+ $body = DI::contentItem()->addSharedPost($item, $body);
+ }
+ }
+
$data['content'] = BBCode::convertForUriId($item['uri-id'], $body, BBCode::ACTIVITYPUB);
}
$language = self::getLanguage($item);
if (!empty($language)) {
$richbody = BBCode::setMentionsToNicknames($item['body'] ?? '');
+ if (!empty($item['quote-uri-id'])) {
+ $richbody = BBCode::removeSharedData($richbody);
+ if ($real_quote) {
+ $richbody = DI::contentItem()->addShareLink($richbody, $item['quote-uri-id']);
+ } else {
+ $richbody = DI::contentItem()->addSharedPost($item, $richbody);
+ }
+ }
$richbody = BBCode::removeAttachment($richbody);
$data['contentMap'][$language] = BBCode::convertForUriId($item['uri-id'], $richbody, BBCode::EXTERNAL);
$data['diaspora:comment'] = $item['signed_text'];
}
- $data['attachment'] = self::createAttachmentList($item, $type);
- $data['tag'] = self::createTagList($item);
+ $data['attachment'] = self::createAttachmentList($item);
+ $data['tag'] = self::createTagList($item, $data['quoteUrl'] ?? '');
if (empty($data['location']) && (!empty($item['coord']) || !empty($item['location']))) {
$data['location'] = self::createLocation($item);
* @param array $item
* @return array Announcement array
*/
- public static function getAnnounceArray(array $item): array
+ private static function getAnnounceArray(array $item): array
{
- $reshared = Item::getShareArray($item);
- if (empty($reshared['guid'])) {
- return [];
- }
-
- $reshared_item = Post::selectFirst(Item::DELIVER_FIELDLIST, ['guid' => $reshared['guid']]);
- if (!DBA::isResult($reshared_item)) {
+ $reshared = DI::contentItem()->getSharedPost($item, Item::DELIVER_FIELDLIST);
+ if (empty($reshared)) {
return [];
}
- if (!in_array($reshared_item['network'], [Protocol::ACTIVITYPUB, Protocol::DFRN])) {
+ if (!in_array($reshared['post']['network'], [Protocol::ACTIVITYPUB, Protocol::DFRN])) {
return [];
}
- $profile = APContact::getByURL($reshared_item['author-link'], false);
+ $profile = APContact::getByURL($reshared['post']['author-link'], false);
if (empty($profile)) {
return [];
}
- return ['object' => $reshared_item, 'actor' => $profile, 'comment' => $reshared['comment']];
+ return ['object' => $reshared['post'], 'actor' => $profile, 'comment' => $reshared['comment']];
}
/**
/**
* Reject a contact request or terminates the contact relation
*
- * @param string $target Target profile
- * @param integer $id Object id
- * @param integer $uid User ID
+ * @param string $target Target profile
+ * @param string $objectId Object id
+ * @param int $uid User ID
* @return bool Operation success
* @throws HTTPException\InternalServerErrorException
* @throws \ImagickException
*/
- public static function sendContactReject(string $target, int $id, int $uid): bool
+ public static function sendContactReject(string $target, string $objectId, int $uid): bool
{
$profile = APContact::getByURL($target);
if (empty($profile['inbox'])) {
'@context' => ActivityPub::CONTEXT,
'id' => DI::baseUrl() . '/activity/' . System::createGUID(),
'type' => 'Reject',
- 'actor' => $owner['url'],
+ 'actor' => $owner['url'],
'object' => [
- 'id' => (string)$id,
+ 'id' => $objectId,
'type' => 'Follow',
'actor' => $profile['url'],
'object' => $owner['url']
'to' => [$profile['url']],
];
- Logger::debug('Sending reject to ' . $target . ' for user ' . $uid . ' with id ' . $id);
+ Logger::debug('Sending reject to ' . $target . ' for user ' . $uid . ' with id ' . $objectId);
$signed = LDSignature::sign($data, $owner);
return HTTPSignature::transmit($signed, $profile['inbox'], $uid);
return false;
}
- $id = DI::baseUrl() . '/activity/' . System::createGUID();
+ $objectId = DI::baseUrl() . '/activity/' . System::createGUID();
$owner = User::getOwnerDataById($uid);
$data = [
'@context' => ActivityPub::CONTEXT,
- 'id' => $id,
+ 'id' => $objectId,
'type' => 'Undo',
'actor' => $owner['url'],
'object' => [
'to' => [$profile['url']],
];
- Logger::info('Sending undo to ' . $target . ' for user ' . $uid . ' with id ' . $id);
+ Logger::info('Sending undo to ' . $target . ' for user ' . $uid . ' with id ' . $objectId);
$signed = LDSignature::sign($data, $owner);
return HTTPSignature::transmit($signed, $profile['inbox'], $uid);