X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=src%2FProtocol%2FActivityPub%2FTransmitter.php;h=7f5f51c38aedcde640271d4f8bfba59250606102;hb=32f70abf9a7913df1ddb28b9745eb37941d09d80;hp=bd6f67128f3924182bb0a457c594b4b09c1533e6;hpb=a24fca818d459092d04482564d90266fd46668e7;p=friendica.git diff --git a/src/Protocol/ActivityPub/Transmitter.php b/src/Protocol/ActivityPub/Transmitter.php index bd6f67128f..7f5f51c38a 100644 --- a/src/Protocol/ActivityPub/Transmitter.php +++ b/src/Protocol/ActivityPub/Transmitter.php @@ -37,13 +37,14 @@ 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\Protocol\Relay; 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 +62,92 @@ 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; + } + + /** + * Add relay servers to the list of inboxes + * + * @param array $inboxes + * @return array inboxes with added relay servers + */ + public static function addRelayServerInboxesForItem(int $item_id, array $inboxes = []) + { + $relays = Relay::getList($item_id, [], [Protocol::ACTIVITYPUB]); + if (empty($relays)) { + return $inboxes; + } + + foreach ($relays as $relay) { + if (!in_array($relay['batch'], $inboxes)) { + $inboxes[] = $relay['batch']; + } + } + 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 +168,8 @@ class Transmitter 'deleted' => false, 'hidden' => false, 'archive' => false, - 'pending' => false + 'pending' => false, + 'blocked' => false, ]; $condition = DBA::buildCondition($parameters); @@ -141,20 +229,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 +319,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', @@ -303,7 +432,12 @@ class Transmitter $activity = json_decode($conversation['source'], true); $actor = JsonLD::fetchElement($activity, 'actor', 'id'); - $profile = APContact::getByURL($actor); + if (!empty($actor)) { + $permissions['to'][] = $actor; + $profile = APContact::getByURL($actor); + } else { + $profile = []; + } $item_profile = APContact::getByURL($item['author-link']); $exclude[] = $item['author-link']; @@ -312,8 +446,6 @@ class Transmitter $exclude[] = $item['owner-link']; } - $permissions['to'][] = $actor; - foreach (['to', 'cc', 'bto', 'bcc'] as $element) { if (empty($activity[$element])) { continue; @@ -359,7 +491,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 +586,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']; @@ -536,7 +670,7 @@ class Transmitter * * @return boolean "true" if inbox is archived */ - private static function archivedInbox($url) + public static function archivedInbox($url) { return DBA::exists('inbox-status', ['url' => $url, 'archive' => true]); } @@ -555,6 +689,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 +706,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 +722,10 @@ class Transmitter continue; } + if ($isforum && ($contact['network'] == Protocol::DFRN)) { + continue; + } + if (Network::isUrlBlocked($contact['url'])) { continue; } @@ -627,6 +774,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 +792,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 +855,6 @@ class Transmitter $mail['gravity'] = ($mail['reply'] ? GRAVITY_COMMENT: GRAVITY_PARENT); $mail['event-type'] = ''; - $mail['attach'] = ''; $mail['parent'] = 0; @@ -721,6 +873,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 +962,8 @@ class Transmitter $type = 'Follow'; } elseif ($item['verb'] == Activity::TAG) { $type = 'Add'; + } elseif ($item['verb'] == Activity::ANNOUNCE) { + $type = 'Announce'; } else { $type = ''; } @@ -851,23 +1008,31 @@ 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['plink'], $item['created'], $item['guid']) . $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'])) { + $forum_item = Item::selectFirst([], ['uri-id' => $item['uri-id'], 'uid' => $self['uid']]); + if (DBA::isResult($item)) { + $item = $forum_item; + } + } } } + if (empty($item['uri-id'])) { + Logger::warning('Item without uri-id', ['item' => $item]); + return false; + } + if (empty($type)) { $condition = ['item-uri' => $item['uri'], 'protocol' => Conversation::PARCEL_ACTIVITYPUB]; $conversation = DBA::selectFirst('conversation', ['source'], $condition); @@ -879,6 +1044,7 @@ class Transmitter 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'])) { @@ -906,10 +1072,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']; @@ -926,7 +1097,11 @@ class Transmitter } 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') { @@ -947,7 +1122,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; @@ -1009,6 +1187,9 @@ class Transmitter $tags[] = ['type' => 'Hashtag', 'href' => $url, 'name' => '#' . $term['name']]; } else { $contact = Contact::getByURL($term['url'], false, ['addr']); + if (empty($contact)) { + continue; + } if (!empty($contact['addr'])) { $mention = '@' . $contact['addr']; } else { @@ -1072,57 +1253,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; @@ -1141,12 +1287,12 @@ class Transmitter return ''; } - $data = Contact::getByURL($match[1], false, ['url', 'nick']); + $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]'; } /** @@ -1306,16 +1452,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 { @@ -1491,6 +1637,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; @@ -1823,18 +1973,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(); @@ -1853,10 +2004,10 @@ 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 = []; @@ -1866,6 +2017,7 @@ class Transmitter && $profile['contact-type'] != Contact::TYPE_COMMUNITY && !strstr($body, $profile['addr']) && !strstr($body, $tag['url']) + && $tag['url'] !== $authorLink ) { $mentions[] = '@[url=' . $tag['url'] . ']' . $profile['nick'] . '[/url]'; }