Skip to content

Commit

Permalink
Merge pull request friendica#9772 from annando/deliver-via-ap
Browse files Browse the repository at this point in the history
Deliver to Friendica via AP if possible
  • Loading branch information
MrPetovan committed Jan 11, 2021
2 parents 251bd30 + f40ab2b commit 56fbd00
Show file tree
Hide file tree
Showing 2 changed files with 116 additions and 164 deletions.
32 changes: 25 additions & 7 deletions src/Protocol/ActivityPub/Transmitter.php
Expand Up @@ -33,6 +33,7 @@
use Friendica\Model\APContact;
use Friendica\Model\Contact;
use Friendica\Model\Conversation;
use Friendica\Model\GServer;
use Friendica\Model\Item;
use Friendica\Model\ItemURI;
use Friendica\Model\Profile;
Expand Down Expand Up @@ -564,8 +565,8 @@ private static function createPermissionBlockForItem($item, $blindcopy, $last_id
foreach ($terms as $term) {
$cid = Contact::getIdForURL($term['url'], $item['uid']);
if (!empty($cid) && in_array($cid, $receiver_list)) {
$contact = DBA::selectFirst('contact', ['url', 'network', 'protocol'], ['id' => $cid]);
if (!DBA::isResult($contact) || (!in_array($contact['network'], $networks) && ($contact['protocol'] != Protocol::ACTIVITYPUB))) {
$contact = DBA::selectFirst('contact', ['url', 'network', 'protocol', 'gsid'], ['id' => $cid, 'network' => Protocol::FEDERATED]);
if (!DBA::isResult($contact) || !self::isAPContact($contact, $networks)) {
continue;
}

Expand All @@ -576,8 +577,8 @@ private static function createPermissionBlockForItem($item, $blindcopy, $last_id
}

foreach ($receiver_list as $receiver) {
$contact = DBA::selectFirst('contact', ['url', 'hidden', 'network', 'protocol'], ['id' => $receiver]);
if (!DBA::isResult($contact) || (!in_array($contact['network'], $networks) && ($contact['protocol'] != Protocol::ACTIVITYPUB))) {
$contact = DBA::selectFirst('contact', ['url', 'hidden', 'network', 'protocol', 'gsid'], ['id' => $receiver, 'network' => Protocol::FEDERATED]);
if (!DBA::isResult($contact) || !self::isAPContact($contact, $networks)) {
continue;
}

Expand Down Expand Up @@ -689,6 +690,23 @@ public static function archivedInbox($url)
return DBA::exists('inbox-status', ['url' => $url, 'archive' => true]);
}

/**
* Check if a given contact should be delivered via AP
*
* @param array $contact
* @param array $networks
* @return bool
* @throws Exception
*/
private static function isAPContact(array $contact, array $networks)
{
if (in_array($contact['network'], $networks) || ($contact['protocol'] == Protocol::ACTIVITYPUB)) {
return true;
}

return GServer::getProtocol($contact['gsid'] ?? 0) == Post\DeliveryData::ACTIVITYPUB;
}

/**
* Fetches a list of inboxes of followers of a given user
*
Expand Down Expand Up @@ -721,19 +739,19 @@ public static function fetchTargetInboxesforUser($uid, $personal = false, bool $
$networks = [Protocol::ACTIVITYPUB, Protocol::OSTATUS];
}

$condition = ['uid' => $uid, 'archive' => false, 'pending' => false, 'blocked' => false];
$condition = ['uid' => $uid, 'archive' => false, 'pending' => false, 'blocked' => false, 'network' => Protocol::FEDERATED];

if (!empty($uid)) {
$condition['rel'] = [Contact::FOLLOWER, Contact::FRIEND];
}

$contacts = DBA::select('contact', ['id', 'url', 'network', 'protocol'], $condition);
$contacts = DBA::select('contact', ['id', 'url', 'network', 'protocol', 'gsid'], $condition);
while ($contact = DBA::fetch($contacts)) {
if (Contact::isLocal($contact['url'])) {
continue;
}

if (!in_array($contact['network'], $networks) && ($contact['protocol'] != Protocol::ACTIVITYPUB)) {
if (!self::isAPContact($contact, $networks)) {
continue;
}

Expand Down

0 comments on commit 56fbd00

Please sign in to comment.