X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=src%2FProtocol%2FActivityPub%2FTransmitter.php;h=2291b68e9a2c9acc116ce0130813168938b72478;hb=4392858662214959f649b68f6b2eabe041c98d81;hp=b261e6060c0ab8e96a7f60bc95ee231da29dfe24;hpb=aab4fa0c3121a777bd58c3350a13ebaaf1fd5457;p=friendica.git diff --git a/src/Protocol/ActivityPub/Transmitter.php b/src/Protocol/ActivityPub/Transmitter.php index b261e6060c..2291b68e9a 100644 --- a/src/Protocol/ActivityPub/Transmitter.php +++ b/src/Protocol/ActivityPub/Transmitter.php @@ -151,8 +151,8 @@ class Transmitter { $public_contact = Contact::getIdForURL($owner['url'], 0, true); - $condition = ['uid' => $owner['uid'], 'contact-id' => $owner['id'], 'author-id' => $public_contact, - 'wall' => true, 'private' => false, 'gravity' => [GRAVITY_PARENT, GRAVITY_COMMENT], + $condition = ['uid' => 0, 'contact-id' => $public_contact, 'author-id' => $public_contact, + 'private' => false, 'gravity' => [GRAVITY_PARENT, GRAVITY_COMMENT], 'deleted' => false, 'visible' => true]; $count = DBA::count('item', $condition); @@ -308,6 +308,12 @@ class Transmitter */ private static function createPermissionBlockForItem($item) { + // Will be activated in a later step + // $networks = [Protocol::ACTIVITYPUB, Protocol::DFRN, Protocol::DIASPORA, Protocol::OSTATUS]; + + // For now only send to these contacts: + $networks = [Protocol::ACTIVITYPUB, Protocol::OSTATUS]; + $data = ['to' => [], 'cc' => []]; $data = array_merge($data, self::fetchPermissionBlockFromConversation($item)); @@ -316,8 +322,6 @@ class Transmitter $terms = Term::tagArrayFromItemId($item['id'], TERM_MENTION); - $contacts[$item['author-link']] = $item['author-link']; - if (!$item['private']) { $data['to'][] = ActivityPub::PUBLIC_COLLECTION; if (!empty($actor_profile['followers'])) { @@ -326,9 +330,8 @@ class Transmitter foreach ($terms as $term) { $profile = APContact::getByURL($term['url'], false); - if (!empty($profile) && empty($contacts[$profile['url']])) { + if (!empty($profile)) { $data['to'][] = $profile['url']; - $contacts[$profile['url']] = $profile['url']; } } } else { @@ -339,22 +342,22 @@ class Transmitter foreach ($terms as $term) { $cid = Contact::getIdForURL($term['url'], $item['uid']); if (!empty($cid) && in_array($cid, $receiver_list)) { - $contact = DBA::selectFirst('contact', ['url'], ['id' => $cid, 'network' => Protocol::ACTIVITYPUB]); - $data['to'][] = $contact['url']; - $contacts[$contact['url']] = $contact['url']; + $contact = DBA::selectFirst('contact', ['url'], ['id' => $cid, 'network' => $networks]); + if (DBA::isResult($contact) && !empty($profile = APContact::getByURL($contact['url'], false))) { + $data['to'][] = $profile['url']; + } } } foreach ($receiver_list as $receiver) { - $contact = DBA::selectFirst('contact', ['url'], ['id' => $receiver, 'network' => Protocol::ACTIVITYPUB]); - if (empty($contacts[$contact['url']])) { - $data['cc'][] = $contact['url']; - $contacts[$contact['url']] = $contact['url']; + $contact = DBA::selectFirst('contact', ['url'], ['id' => $receiver, 'network' => $networks]); + if (DBA::isResult($contact) && !empty($profile = APContact::getByURL($contact['url'], false))) { + $data['cc'][] = $profile['url']; } } } - $parents = Item::select(['id', 'author-link', 'owner-link', 'gravity'], ['parent' => $item['parent']]); + $parents = Item::select(['id', 'author-link', 'owner-link', 'gravity', 'uri'], ['parent' => $item['parent']]); while ($parent = Item::fetch($parents)) { // Don't include data from future posts if ($parent['id'] >= $item['id']) { @@ -362,9 +365,12 @@ class Transmitter } $profile = APContact::getByURL($parent['author-link'], false); - if (!empty($profile) && empty($contacts[$profile['url']])) { - $data['cc'][] = $profile['url']; - $contacts[$profile['url']] = $profile['url']; + if (!empty($profile)) { + if ($parent['uri'] == $item['thr-parent']) { + $data['to'][] = $profile['url']; + } else { + $data['cc'][] = $profile['url']; + } } if ($item['gravity'] != GRAVITY_PARENT) { @@ -372,37 +378,67 @@ class Transmitter } $profile = APContact::getByURL($parent['owner-link'], false); - if (!empty($profile) && empty($contacts[$profile['url']])) { + if (!empty($profile)) { $data['cc'][] = $profile['url']; - $contacts[$profile['url']] = $profile['url']; } } DBA::close($parents); - return $data; + $data['to'] = array_unique($data['to']); + $data['cc'] = array_unique($data['cc']); + + if (($key = array_search($item['author-link'], $data['to'])) !== false) { + unset($data['to'][$key]); + } + + if (($key = array_search($item['author-link'], $data['cc'])) !== false) { + unset($data['cc'][$key]); + } + + foreach ($data['to'] as $to) { + if (($key = array_search($to, $data['cc'])) !== false) { + unset($data['cc'][$key]); + } + } + + return ['to' => array_values($data['to']), 'cc' => array_values($data['cc'])]; } /** * Fetches a list of inboxes of followers of a given user * * @param integer $uid User ID + * @param boolean $personal fetch personal inboxes * * @return array of follower inboxes */ - public static function fetchTargetInboxesforUser($uid) + public static function fetchTargetInboxesforUser($uid, $personal = false) { $inboxes = []; - $condition = ['uid' => $uid, 'network' => Protocol::ACTIVITYPUB, 'archive' => false, 'pending' => false]; + // Will be activated in a later step + // $networks = [Protocol::ACTIVITYPUB, Protocol::DFRN, Protocol::DIASPORA, Protocol::OSTATUS]; + + // For now only send to these contacts: + $networks = [Protocol::ACTIVITYPUB, Protocol::OSTATUS]; + + $condition = ['uid' => $uid, 'network' => $networks, 'archive' => false, 'pending' => false]; if (!empty($uid)) { $condition['rel'] = [Contact::FOLLOWER, Contact::FRIEND]; } - $contacts = DBA::select('contact', ['notify', 'batch'], $condition); + $contacts = DBA::select('contact', ['url'], $condition); while ($contact = DBA::fetch($contacts)) { - $contact = defaults($contact, 'batch', $contact['notify']); - $inboxes[$contact] = $contact; + $profile = APContact::getByURL($contact['url'], false); + if (!empty($profile)) { + if (empty($profile['sharedinbox']) || $personal) { + $target = $profile['inbox']; + } else { + $target = $profile['sharedinbox']; + } + $inboxes[$target] = $target; + } } DBA::close($contacts); @@ -414,10 +450,11 @@ class Transmitter * * @param array $item * @param integer $uid User ID + * @param boolean $personal fetch personal inboxes * * @return array with inboxes */ - public static function fetchTargetInboxes($item, $uid) + public static function fetchTargetInboxes($item, $uid, $personal = false) { $permissions = self::createPermissionBlockForItem($item); if (empty($permissions)) { @@ -427,9 +464,9 @@ class Transmitter $inboxes = []; if ($item['gravity'] == GRAVITY_ACTIVITY) { - $item_profile = APContact::getByURL($item['author-link']); + $item_profile = APContact::getByURL($item['author-link'], false); } else { - $item_profile = APContact::getByURL($item['owner-link']); + $item_profile = APContact::getByURL($item['owner-link'], false); } foreach (['to', 'cc', 'bto', 'bcc'] as $element) { @@ -439,11 +476,15 @@ class Transmitter foreach ($permissions[$element] as $receiver) { if ($receiver == $item_profile['followers']) { - $inboxes = self::fetchTargetInboxesforUser($uid); + $inboxes = self::fetchTargetInboxesforUser($uid, $personal); } else { - $profile = APContact::getByURL($receiver); + $profile = APContact::getByURL($receiver, false); if (!empty($profile)) { - $target = defaults($profile, 'sharedinbox', $profile['inbox']); + if (empty($profile['sharedinbox']) || $personal) { + $target = $profile['inbox']; + } else { + $target = $profile['sharedinbox']; + } $inboxes[$target] = $target; } } @@ -504,7 +545,7 @@ class Transmitter $data = ActivityPub\Transmitter::createActivityFromItem($item_id); - Cache::set($cachekey, $data, CACHE_QUARTER_HOUR); + Cache::set($cachekey, $data, Cache::QUARTER_HOUR); return $data; } @@ -718,6 +759,26 @@ class Transmitter return $attachments; } + /** + * @brief Callback function to replace a Friendica style mention in a mention that is used on AP + * + * @param array $match Matching values for the callback + * @return string Replaced mention + */ + private static function mentionCallback($match) + { + if (empty($match[1])) { + return; + } + + $data = Contact::getDetailsByURL($match[1]); + if (empty($data) || empty($data['nick'])) { + return; + } + + return '@[url=' . $data['url'] . ']' . $data['nick'] . '[/url]'; + } + /** * Remove image elements and replaces them with links to the image * @@ -826,6 +887,9 @@ class Transmitter $body = self::removePictures($body); } + $regexp = "/[@!]\[url\=([^\[\]]*)\].*?\[\/url\]/ism"; + $body = preg_replace_callback($regexp, ['self', 'mentionCallback'], $body); + $data['content'] = BBCode::convert($body, false, 7); $data['source'] = ['content' => $item['body'], 'mediaType' => "text/bbcode"];