X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=src%2FProtocol%2FActivityPub%2FTransmitter.php;h=6ac55dc12317946ca22392cc9a9c3941634cb18c;hb=acae3df0a22e3a58cae1a64e67d1d1598463bdca;hp=dfe7c77df7c6270acd4c43b90cbc9b98de7afc81;hpb=87f2efff4bce8fe39be1dd07d851e2081753cd33;p=friendica.git diff --git a/src/Protocol/ActivityPub/Transmitter.php b/src/Protocol/ActivityPub/Transmitter.php index dfe7c77df7..6ac55dc123 100644 --- a/src/Protocol/ActivityPub/Transmitter.php +++ b/src/Protocol/ActivityPub/Transmitter.php @@ -37,13 +37,13 @@ use Friendica\Model\Item; use Friendica\Model\ItemURI; use Friendica\Model\Profile; use Friendica\Model\Photo; +use Friendica\Model\Post; use Friendica\Model\Tag; use Friendica\Model\User; use Friendica\Protocol\Activity; use Friendica\Protocol\ActivityPub; use Friendica\Util\DateTimeFormat; use Friendica\Util\HTTPSignature; -use Friendica\Util\Images; use Friendica\Util\JsonLD; use Friendica\Util\LDSignature; use Friendica\Util\Map; @@ -61,6 +61,71 @@ require_once 'mod/share.php'; */ class Transmitter { + /** + * Add relay servers to the list of inboxes + * + * @param array $inboxes + * @return array inboxes with added relay servers + */ + public static function addRelayServerInboxes(array $inboxes = []) + { + $contacts = DBA::select('apcontact', ['inbox'], + ["`type` = ? AND `url` IN (SELECT `url` FROM `contact` WHERE `uid` = ? AND `rel` IN (?, ?))", + 'Application', 0, Contact::FOLLOWER, Contact::FRIEND]); + while ($contact = DBA::fetch($contacts)) { + $inboxes[] = $contact['inbox']; + } + DBA::close($contacts); + + return $inboxes; + } + + /** + * Subscribe to a relay + * + * @param string $url Subscribe actor url + * @return bool success + */ + public static function sendRelayFollow(string $url) + { + $contact = Contact::getByURL($url); + if (empty($contact)) { + return false; + } + + $activity_id = ActivityPub\Transmitter::activityIDFromContact($contact['id']); + $success = ActivityPub\Transmitter::sendActivity('Follow', $url, 0, $activity_id); + if ($success) { + $rel = $contact['rel'] == Contact::SHARING ? Contact::FRIEND : Contact::FOLLOWER; + DBA::update('contact', ['rel' => $rel], ['id' => $contact['id']]); + } + + return $success; + } + + /** + * Unsubscribe from a relay + * + * @param string $url Subscribe actor url + * @param bool $force Set the relay status as non follower even if unsubscribe hadn't worked + * @return bool success + */ + public static function sendRelayUndoFollow(string $url, bool $force = false) + { + $contact = Contact::getByURL($url); + if (empty($contact)) { + return false; + } + + $success = self::sendContactUndo($url, $contact['id'], 0); + if ($success || $force) { + $rel = $contact['rel'] == Contact::FRIEND ? Contact::SHARING : Contact::NOTHING; + DBA::update('contact', ['rel' => $rel], ['id' => $contact['id']]); + } + + return $success; + } + /** * Collects a list of contacts of the given owner * @@ -81,7 +146,8 @@ class Transmitter 'deleted' => false, 'hidden' => false, 'archive' => false, - 'pending' => false + 'pending' => false, + 'blocked' => false, ]; $condition = DBA::buildCondition($parameters); @@ -141,20 +207,37 @@ class Transmitter /** * Public posts for the given owner * - * @param array $owner Owner array - * @param integer $page Page numbe + * @param array $owner Owner array + * @param integer $page Page number + * @param string $requester URL of requesting account * * @return array of posts * @throws \Friendica\Network\HTTPException\InternalServerErrorException * @throws \ImagickException */ - public static function getOutbox($owner, $page = null) + public static function getOutbox($owner, $page = null, $requester = '') { - $public_contact = Contact::getIdForURL($owner['url'], 0, true); + $public_contact = Contact::getIdForURL($owner['url']); + $condition = ['uid' => 0, 'contact-id' => $public_contact, + 'private' => [Item::PUBLIC, Item::UNLISTED]]; + + if (!empty($requester)) { + $requester_id = Contact::getIdForURL($requester, $owner['uid']); + if (!empty($requester_id)) { + $permissionSets = DI::permissionSet()->selectByContactId($requester_id, $owner['uid']); + if (!empty($permissionSets)) { + $condition = ['uid' => $owner['uid'], 'origin' => true, + 'psid' => array_merge($permissionSets->column('id'), + [DI::permissionSet()->getIdFromACL($owner['uid'], '', '', '', '')])]; + } + } + } + + $condition = array_merge($condition, + ['author-id' => $public_contact, + 'gravity' => [GRAVITY_PARENT, GRAVITY_COMMENT], + 'deleted' => false, 'visible' => true, 'moderated' => false]); - $condition = ['uid' => 0, 'contact-id' => $public_contact, 'author-id' => $public_contact, - 'private' => [Item::PUBLIC, Item::UNLISTED], 'gravity' => [GRAVITY_PARENT, GRAVITY_COMMENT], - 'deleted' => false, 'visible' => true, 'moderated' => false]; $count = DBA::count('item', $condition); $data = ['@context' => ActivityPub::CONTEXT]; @@ -214,39 +297,63 @@ class Transmitter */ public static function getProfile($uid) { - $condition = ['uid' => $uid, 'blocked' => false, 'account_expired' => false, - 'account_removed' => false, 'verified' => true]; - $fields = ['guid', 'nickname', 'pubkey', 'account-type', 'page-flags']; - $user = DBA::selectFirst('user', $fields, $condition); - if (!DBA::isResult($user)) { - return []; - } + if ($uid != 0) { + $condition = ['uid' => $uid, 'blocked' => false, 'account_expired' => false, + 'account_removed' => false, 'verified' => true]; + $fields = ['guid', 'nickname', 'pubkey', 'account-type', 'page-flags']; + $user = DBA::selectFirst('user', $fields, $condition); + if (!DBA::isResult($user)) { + return []; + } - $fields = ['locality', 'region', 'country-name']; - $profile = DBA::selectFirst('profile', $fields, ['uid' => $uid]); - if (!DBA::isResult($profile)) { - return []; - } + $fields = ['locality', 'region', 'country-name']; + $profile = DBA::selectFirst('profile', $fields, ['uid' => $uid]); + if (!DBA::isResult($profile)) { + return []; + } - $fields = ['name', 'url', 'location', 'about', 'avatar', 'photo']; - $contact = DBA::selectFirst('contact', $fields, ['uid' => $uid, 'self' => true]); - if (!DBA::isResult($contact)) { - return []; + $fields = ['name', 'url', 'location', 'about', 'avatar', 'photo']; + $contact = DBA::selectFirst('contact', $fields, ['uid' => $uid, 'self' => true]); + if (!DBA::isResult($contact)) { + return []; + } + } else { + $contact = User::getSystemAccount(); + $user = ['guid' => '', 'nickname' => $contact['nick'], 'pubkey' => $contact['pubkey'], + 'account-type' => $contact['contact-type'], 'page-flags' => User::PAGE_FLAGS_NORMAL]; + $profile = ['locality' => '', 'region' => '', 'country-name' => '']; } $data = ['@context' => ActivityPub::CONTEXT]; $data['id'] = $contact['url']; - $data['diaspora:guid'] = $user['guid']; + + if (!empty($user['guid'])) { + $data['diaspora:guid'] = $user['guid']; + } + $data['type'] = ActivityPub::ACCOUNT_TYPES[$user['account-type']]; - $data['following'] = DI::baseUrl() . '/following/' . $user['nickname']; - $data['followers'] = DI::baseUrl() . '/followers/' . $user['nickname']; - $data['inbox'] = DI::baseUrl() . '/inbox/' . $user['nickname']; - $data['outbox'] = DI::baseUrl() . '/outbox/' . $user['nickname']; + + if ($uid != 0) { + $data['following'] = DI::baseUrl() . '/following/' . $user['nickname']; + $data['followers'] = DI::baseUrl() . '/followers/' . $user['nickname']; + $data['inbox'] = DI::baseUrl() . '/inbox/' . $user['nickname']; + $data['outbox'] = DI::baseUrl() . '/outbox/' . $user['nickname']; + } else { + $data['inbox'] = DI::baseUrl() . '/friendica/inbox'; + } + $data['preferredUsername'] = $user['nickname']; $data['name'] = $contact['name']; - $data['vcard:hasAddress'] = ['@type' => 'vcard:Home', 'vcard:country-name' => $profile['country-name'], - 'vcard:region' => $profile['region'], 'vcard:locality' => $profile['locality']]; - $data['summary'] = BBCode::convert($contact['about'], false); + + if (!empty($profile['country-name'] . $profile['region'] . $profile['locality'])) { + $data['vcard:hasAddress'] = ['@type' => 'vcard:Home', 'vcard:country-name' => $profile['country-name'], + 'vcard:region' => $profile['region'], 'vcard:locality' => $profile['locality']]; + } + + if (!empty($contact['about'])) { + $data['summary'] = BBCode::convert($contact['about'], false); + } + $data['url'] = $contact['url']; $data['manuallyApprovesFollowers'] = in_array($user['page-flags'], [User::PAGE_FLAGS_NORMAL, User::PAGE_FLAGS_PRVGROUP]); $data['publicKey'] = ['id' => $contact['url'] . '#main-key', @@ -359,7 +466,7 @@ class Transmitter // Check if we should always deliver our stuff via BCC if (!empty($item['uid'])) { - $profile = Profile::getByUID($item['uid']); + $profile = User::getOwnerDataById($item['uid']); if (!empty($profile)) { $always_bcc = $profile['hide-friends']; } @@ -454,7 +561,9 @@ class Transmitter if ($item['gravity'] != GRAVITY_PARENT) { // Comments to forums are directed to the forum // But comments to forums aren't directed to the followers collection - if ($profile['type'] == 'Group') { + // This rule is only valid when the actor isn't the forum. + // The forum needs to transmit their content to their followers. + if (($profile['type'] == 'Group') && ($profile['url'] != $actor_profile['url'])) { $data['to'][] = $profile['url']; } else { $data['cc'][] = $profile['url']; @@ -555,6 +664,15 @@ class Transmitter { $inboxes = []; + $isforum = false; + + if (!empty($item['uid'])) { + $profile = User::getOwnerDataById($item['uid']); + if (!empty($profile)) { + $isforum = $profile['account-type'] == User::ACCOUNT_TYPE_COMMUNITY; + } + } + if (DI::config()->get('debug', 'total_ap_delivery')) { // Will be activated in a later step $networks = Protocol::FEDERATED; @@ -563,7 +681,7 @@ class Transmitter $networks = [Protocol::ACTIVITYPUB, Protocol::OSTATUS]; } - $condition = ['uid' => $uid, 'archive' => false, 'pending' => false]; + $condition = ['uid' => $uid, 'archive' => false, 'pending' => false, 'blocked' => false]; if (!empty($uid)) { $condition['rel'] = [Contact::FOLLOWER, Contact::FRIEND]; @@ -579,6 +697,10 @@ class Transmitter continue; } + if ($isforum && ($contact['dfrn'] == Protocol::DFRN)) { + continue; + } + if (Network::isUrlBlocked($contact['url'])) { continue; } @@ -627,6 +749,12 @@ class Transmitter $item_profile = APContact::getByURL($item['owner-link'], false); } + if (empty($item_profile)) { + return []; + } + + $profile_uid = User::getIdForURL($item_profile['url']); + foreach (['to', 'cc', 'bto', 'bcc'] as $element) { if (empty($permissions[$element])) { continue; @@ -639,7 +767,7 @@ class Transmitter continue; } - if ($item_profile && $receiver == $item_profile['followers']) { + if ($item_profile && ($receiver == $item_profile['followers']) && ($uid == $profile_uid)) { $inboxes = array_merge($inboxes, self::fetchTargetInboxesforUser($uid, $personal)); } else { if (Contact::isLocal($receiver)) { @@ -702,7 +830,6 @@ class Transmitter $mail['gravity'] = ($mail['reply'] ? GRAVITY_COMMENT: GRAVITY_PARENT); $mail['event-type'] = ''; - $mail['attach'] = ''; $mail['parent'] = 0; @@ -721,6 +848,9 @@ class Transmitter public static function createActivityFromMail($mail_id, $object_mode = false) { $mail = self::ItemArrayFromMail($mail_id); + if (empty($mail)) { + return []; + } $object = self::createNote($mail); if (!$object_mode) { @@ -807,6 +937,8 @@ class Transmitter $type = 'Follow'; } elseif ($item['verb'] == Activity::TAG) { $type = 'Add'; + } elseif ($item['verb'] == Activity::ANNOUNCE) { + $type = 'Announce'; } else { $type = ''; } @@ -834,7 +966,7 @@ class Transmitter } } - $data = ActivityPub\Transmitter::createActivityFromItem($item_id); + $data = self::createActivityFromItem($item_id); DI::cache()->set($cachekey, $data, Duration::QUARTER_HOUR); return $data; @@ -851,20 +983,20 @@ class Transmitter */ public static function createActivityFromItem($item_id, $object_mode = false) { + Logger::info('Fetching activity', ['item' => $item_id]); $item = Item::selectFirst([], ['id' => $item_id, 'parent-network' => Protocol::NATIVE_SUPPORT]); - if (!DBA::isResult($item)) { return false; } - if ($item['wall'] && ($item['uri'] == $item['parent-uri'])) { - $owner = User::getOwnerDataById($item['uid']); - if (($owner['account-type'] == User::ACCOUNT_TYPE_COMMUNITY) && ($item['author-link'] != $owner['url'])) { - $type = 'Announce'; - - // Disguise forum posts as reshares. Will later be converted to a real announce - $item['body'] = BBCode::getShareOpeningTag($item['author-name'], $item['author-link'], $item['author-avatar'], - $item['guid'], $item['created'], $item['plink']) . $item['body'] . '[/share]'; + // In case of a forum post ensure to return the original post if author and forum are on the same machine + if (!empty($item['forum_mode'])) { + $author = Contact::getById($item['author-id'], ['nurl']); + if (!empty($author['nurl'])) { + $self = Contact::selectFirst(['uid'], ['nurl' => $author['nurl'], 'self' => true]); + if (!empty($self['uid'])) { + $item = Item::selectFirst([], ['uri-id' => $item['uri-id'], 'uid' => $self['uid']]); + } } } @@ -873,8 +1005,22 @@ class Transmitter $conversation = DBA::selectFirst('conversation', ['source'], $condition); if (DBA::isResult($conversation)) { $data = json_decode($conversation['source'], true); - if (!empty($data)) { - return $data; + 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; + } } } @@ -882,7 +1028,7 @@ class Transmitter } if (!$object_mode) { - $data = ['@context' => ActivityPub::CONTEXT]; + $data = ['@context' => $context ?? ActivityPub::CONTEXT]; if ($item['deleted'] && ($item['gravity'] == GRAVITY_ACTIVITY)) { $type = 'Undo'; @@ -893,10 +1039,15 @@ class Transmitter $data = []; } - $data['id'] = $item['uri'] . '/' . $type; + if (($item['gravity'] == GRAVITY_ACTIVITY) && ($type != 'Undo')) { + $data['id'] = $item['uri']; + } else { + $data['id'] = $item['uri'] . '/' . $type; + } + $data['type'] = $type; - if (Item::isForumPost($item) && ($type != 'Announce')) { + if (($type != 'Announce') || ($item['gravity'] != GRAVITY_PARENT)) { $data['actor'] = $item['author-link']; } else { $data['actor'] = $item['owner-link']; @@ -909,11 +1060,15 @@ class Transmitter $data = array_merge($data, self::createPermissionBlockForItem($item, false)); if (in_array($data['type'], ['Create', 'Update', 'Delete'])) { - $data['object'] = self::createNote($item); + $data['object'] = $object ?? self::createNote($item); } elseif ($data['type'] == 'Add') { $data = self::createAddTag($item, $data); } elseif ($data['type'] == 'Announce') { - $data = self::createAnnounce($item, $data); + if ($item['verb'] == ACTIVITY::ANNOUNCE) { + $data['object'] = $item['thr-parent']; + } else { + $data = self::createAnnounce($item, $data); + } } elseif ($data['type'] == 'Follow') { $data['object'] = $item['parent-uri']; } elseif ($data['type'] == 'Undo') { @@ -934,7 +1089,10 @@ class Transmitter $owner = User::getOwnerDataById($uid); - if (!$object_mode && !empty($owner)) { + Logger::info('Fetched activity', ['item' => $item_id, 'uid' => $uid]); + + // We don't sign if we aren't the actor. This is important for relaying content especially for forums + if (!$object_mode && !empty($owner) && ($data['actor'] == $owner['url'])) { return LDSignature::sign($data, $owner); } else { return $data; @@ -995,7 +1153,10 @@ class Transmitter $url = DI::baseUrl() . '/search?tag=' . urlencode($term['name']); $tags[] = ['type' => 'Hashtag', 'href' => $url, 'name' => '#' . $term['name']]; } else { - $contact = Contact::getDetailsByURL($term['url']); + $contact = Contact::getByURL($term['url'], false, ['addr']); + if (empty($contact)) { + continue; + } if (!empty($contact['addr'])) { $mention = '@' . $contact['addr']; } else { @@ -1059,57 +1220,22 @@ class Transmitter $attachments[] = $attachment; } */ - $arr = explode('[/attach],', $item['attach']); - if (count($arr)) { - foreach ($arr as $r) { - $matches = false; - $cnt = preg_match('|\[attach\]href=\"(.*?)\" length=\"(.*?)\" type=\"(.*?)\" title=\"(.*?)\"|', $r, $matches); - if ($cnt) { - $attributes = ['type' => 'Document', - 'mediaType' => $matches[3], - 'url' => $matches[1], - 'name' => null]; - - if (trim($matches[4]) != '') { - $attributes['name'] = trim($matches[4]); - } - - $attachments[] = $attributes; - } - } + foreach (Post\Media::getByURIId($item['uri-id'], [Post\Media::DOCUMENT, Post\Media::TORRENT, Post\Media::UNKNOWN]) as $attachment) { + $attachments[] = ['type' => 'Document', + 'mediaType' => $attachment['mimetype'], + 'url' => $attachment['url'], + 'name' => $attachment['description']]; } if ($type != 'Note') { return $attachments; } - // Simplify image codes - $body = preg_replace("/\[img\=([0-9]*)x([0-9]*)\](.*?)\[\/img\]/ism", '[img]$3[/img]', $item['body']); - - // Grab all pictures without alternative descriptions and create attachments out of them - if (preg_match_all("/\[img\]([^\[\]]*)\[\/img\]/Usi", $body, $pictures)) { - foreach ($pictures[1] as $picture) { - $imgdata = Images::getInfoFromURLCached($picture); - if ($imgdata) { - $attachments[] = ['type' => 'Document', - 'mediaType' => $imgdata['mime'], - 'url' => $picture, - 'name' => null]; - } - } - } - - // Grab all pictures with alternative description and create attachments out of them - if (preg_match_all("/\[img=([^\[\]]*)\]([^\[\]]*)\[\/img\]/Usi", $body, $pictures, PREG_SET_ORDER)) { - foreach ($pictures as $picture) { - $imgdata = Images::getInfoFromURLCached($picture[1]); - if ($imgdata) { - $attachments[] = ['type' => 'Document', - 'mediaType' => $imgdata['mime'], - 'url' => $picture[1], - 'name' => $picture[2]]; - } - } + foreach (Post\Media::getByURIId($item['uri-id'], [Post\Media::AUDIO, Post\Media::IMAGE, Post\Media::VIDEO]) as $attachment) { + $attachments[] = ['type' => 'Document', + 'mediaType' => $attachment['mimetype'], + 'url' => $attachment['url'], + 'name' => $attachment['description']]; } return $attachments; @@ -1128,12 +1254,12 @@ class Transmitter return ''; } - $data = Contact::getDetailsByURL($match[1]); + $data = Contact::getByURL($match[1], false, ['url', 'alias', 'nick']); if (empty($data['nick'])) { return $match[0]; } - return '@[url=' . $data['url'] . ']' . $data['nick'] . '[/url]'; + return '[url=' . ($data['alias'] ?: $data['url']) . ']@' . $data['nick'] . '[/url]'; } /** @@ -1293,16 +1419,16 @@ class Transmitter $body = $item['body']; - if (empty($item['uid']) || !Feature::isEnabled($item['uid'], 'explicit_mentions')) { - $body = self::prependMentions($body, $item['uri-id']); - } - if ($type == 'Note') { - $body = self::removePictures($body); + $body = $item['raw-body'] ?? self::removePictures($body); } elseif (($type == 'Article') && empty($data['summary'])) { $data['summary'] = BBCode::toPlaintext(Plaintext::shorten(self::removePictures($body), 1000)); } + if (empty($item['uid']) || !Feature::isEnabled($item['uid'], 'explicit_mentions')) { + $body = self::prependMentions($body, $item['uri-id'], $item['author-link']); + } + if ($type == 'Event') { $data = array_merge($data, self::createEvent($item)); } else { @@ -1478,6 +1604,10 @@ class Transmitter */ public static function isAnnounce($item) { + if (!empty($item['verb']) && ($item['verb'] == Activity::ANNOUNCE)) { + return true; + } + $announce = self::getAnnounceArray($item); if (empty($announce)) { return false; @@ -1810,18 +1940,19 @@ class Transmitter * @throws \Friendica\Network\HTTPException\InternalServerErrorException * @throws \ImagickException * @throws \Exception + * @return bool success */ public static function sendContactUndo($target, $cid, $uid) { $profile = APContact::getByURL($target); if (empty($profile['inbox'])) { Logger::warning('No inbox found for target', ['target' => $target, 'profile' => $profile]); - return; + return false; } $object_id = self::activityIDFromContact($cid); if (empty($object_id)) { - return; + return false; } $id = DI::baseUrl() . '/activity/' . System::createGUID(); @@ -1840,19 +1971,20 @@ class Transmitter Logger::log('Sending undo to ' . $target . ' for user ' . $uid . ' with id ' . $id, Logger::DEBUG); $signed = LDSignature::sign($data, $owner); - HTTPSignature::transmit($signed, $profile['inbox'], $uid); + return HTTPSignature::transmit($signed, $profile['inbox'], $uid); } - private static function prependMentions($body, int $uriid) + private static function prependMentions($body, int $uriid, string $authorLink) { $mentions = []; foreach (Tag::getByURIId($uriid, [Tag::IMPLICIT_MENTION]) as $tag) { - $profile = Contact::getDetailsByURL($tag['url']); + $profile = Contact::getByURL($tag['url'], false, ['addr', 'contact-type', 'nick']); if (!empty($profile['addr']) && $profile['contact-type'] != Contact::TYPE_COMMUNITY && !strstr($body, $profile['addr']) && !strstr($body, $tag['url']) + && $tag['url'] !== $authorLink ) { $mentions[] = '@[url=' . $tag['url'] . ']' . $profile['nick'] . '[/url]'; }