From cd0d6cb626b5b9deb1605259e942a4a95369d264 Mon Sep 17 00:00:00 2001 From: Michael Date: Thu, 14 Mar 2019 18:44:41 +0000 Subject: [PATCH] Forums now are working with AP as well --- src/Model/Contact.php | 27 +++++++++++++++++ src/Model/Item.php | 27 +++++++++++++++++ src/Protocol/ActivityPub/Transmitter.php | 34 ++++++++++++--------- src/Worker/Notifier.php | 38 +++++------------------- 4 files changed, 82 insertions(+), 44 deletions(-) diff --git a/src/Model/Contact.php b/src/Model/Contact.php index 9afaf77fc0..d938ad29c2 100644 --- a/src/Model/Contact.php +++ b/src/Model/Contact.php @@ -2226,8 +2226,35 @@ class Contact extends BaseObject return $redirect; } + /** + * Remove a contact from all groups + * + * @param integer $contact_id + * + * @return boolean Success + */ public static function removeFromGroups($contact_id) { return DBA::delete('group_member', ['contact-id' => $contact_id]); } + + /** + * Is the contact a forum? + * + * @param integer $contactid ID of the contact + * + * @return boolean "true" if it is a forum + */ + public static function isForum($contactid) + { + $fields = ['forum', 'prv']; + $condition = ['id' => $contactid]; + $contact = DBA::selectFirst('contact', $fields, $condition); + if (!DBA::isResult($contact)) { + return false; + } + + // Is it a forum? + return ($contact['forum'] || $contact['prv']); + } } diff --git a/src/Model/Item.php b/src/Model/Item.php index 964e94ddb9..4808b2d035 100644 --- a/src/Model/Item.php +++ b/src/Model/Item.php @@ -3592,4 +3592,31 @@ class Item extends BaseObject return $ret; } + + /** + * Is the given item array a post that is sent as starting post to a forum? + * + * @param array $item + * @param array $owner + * + * @return boolean "true" when it is a forum post + */ + public static function isForumPost(array $item, array $owner = []) + { + if (empty($owner)) { + $owner = User::getOwnerDataById($item['uid']); + if (empty($owner)) { + return false; + } + } + + if (($item['author-id'] == $item['owner-id']) || + ($owner['id'] == $item['contact-id']) || + ($item['uri'] != $item['parent-uri']) || + $item['origin']) { + return false; + } + + return Contact::isForum($item['contact-id']); + } } diff --git a/src/Protocol/ActivityPub/Transmitter.php b/src/Protocol/ActivityPub/Transmitter.php index f2df43e854..94012e4d63 100644 --- a/src/Protocol/ActivityPub/Transmitter.php +++ b/src/Protocol/ActivityPub/Transmitter.php @@ -303,15 +303,16 @@ class Transmitter /** * Creates an array of permissions from an item thread * - * @param array $item - * @param boolean $blindcopy - * @param boolean $last_id + * @param array $item Item array + * @param boolean $blindcopy addressing via "bcc" or "cc"? + * @param integer $last_id Last item id for adding receivers + * @param boolean $forum_mode "true" means that we are sending content to a forum * * @return array with permission data * @throws \Friendica\Network\HTTPException\InternalServerErrorException * @throws \ImagickException */ - private static function createPermissionBlockForItem($item, $blindcopy, $last_id = 0) + private static function createPermissionBlockForItem($item, $blindcopy, $last_id = 0, $forum_mode = false) { if ($last_id == 0) { $last_id = $item['id']; @@ -399,7 +400,7 @@ class Transmitter } } else { // Public thread parent post always are directed to the followes - if (!$item['private']) { + if (!$item['private'] && !$forum_mode) { $data['cc'][] = $actor_profile['followers']; } } @@ -515,18 +516,18 @@ class Transmitter /** * Fetches an array of inboxes for the given item and user * - * @param array $item - * @param integer $uid User ID - * @param boolean $personal fetch personal inboxes - * @param integer $last_id Last item id for adding receivers - * + * @param array $item Item array + * @param integer $uid User ID + * @param boolean $personal fetch personal inboxes + * @param integer $last_id Last item id for adding receivers + * @param boolean $forum_mode "true" means that we are sending content to a forum * @return array with inboxes * @throws \Friendica\Network\HTTPException\InternalServerErrorException * @throws \ImagickException */ - public static function fetchTargetInboxes($item, $uid, $personal = false, $last_id = 0) + public static function fetchTargetInboxes($item, $uid, $personal = false, $last_id = 0, $forum_mode = false) { - $permissions = self::createPermissionBlockForItem($item, true, $last_id); + $permissions = self::createPermissionBlockForItem($item, true, $last_id, $forum_mode); if (empty($permissions)) { return []; } @@ -659,7 +660,7 @@ class Transmitter return false; } - if ($item['wall']) { + 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'; @@ -697,7 +698,12 @@ class Transmitter $data['id'] = $item['uri'] . '#' . $type; $data['type'] = $type; - $data['actor'] = $item['owner-link']; + + if (Item::isForumPost($item) && ($type != 'Announce')) { + $data['actor'] = $item['author-link']; + } else { + $data['actor'] = $item['owner-link']; + } $data['published'] = DateTimeFormat::utc($item['created'] . '+00:00', DateTimeFormat::ATOM); diff --git a/src/Worker/Notifier.php b/src/Worker/Notifier.php index 4c78d3c6ad..baae33f7e0 100644 --- a/src/Worker/Notifier.php +++ b/src/Worker/Notifier.php @@ -154,7 +154,7 @@ class Notifier $parent = $items[0]; if (!self::isRemovalActivity($cmd, $owner, Protocol::ACTIVITYPUB)) { - $delivery_queue_count += self::activityPubDelivery($cmd, $target_item, $parent, $a->queue['priority'], $a->queue['created']); + $delivery_queue_count += self::activityPubDelivery($cmd, $target_item, $parent, $a->queue['priority'], $a->queue['created'], $owner); } $fields = ['network', 'author-id', 'owner-id']; @@ -207,13 +207,13 @@ class Notifier } // Special treatment for forum posts - if (self::isForumPost($target_item, $owner)) { + if (Item::isForumPost($target_item, $owner)) { $relay_to_owner = true; $direct_forum_delivery = true; } // Avoid that comments in a forum thread are sent to OStatus - if (self::isForumPost($parent, $owner)) { + if (Item::isForumPost($parent, $owner)) { $direct_forum_delivery = true; } @@ -413,7 +413,7 @@ class Notifier // Add the relay to the list, avoid duplicates. // Don't send community posts to the relay. Forum posts via the Diaspora protocol are looking ugly. - if (!$followup && !self::isForumPost($target_item, $owner)) { + if (!$followup && !Item::isForumPost($target_item, $owner)) { $relay_list = Diaspora::relayList($target_id, $relay_list); } } @@ -598,7 +598,7 @@ class Notifier * @throws \Friendica\Network\HTTPException\InternalServerErrorException * @throws \ImagickException */ - private static function activityPubDelivery($cmd, array $target_item, array $parent, $priority, $created) + private static function activityPubDelivery($cmd, array $target_item, array $parent, $priority, $created, $owner) { $inboxes = []; @@ -607,6 +607,9 @@ class Notifier if ($target_item['origin']) { $inboxes = ActivityPub\Transmitter::fetchTargetInboxes($target_item, $uid); Logger::log('Origin item ' . $target_item['id'] . ' with URL ' . $target_item['uri'] . ' will be distributed.', Logger::DEBUG); + } elseif (Item::isForumPost($target_item, $owner)) { + $inboxes = ActivityPub\Transmitter::fetchTargetInboxes($target_item, $uid, false, 0, true); + Logger::log('Forum item ' . $target_item['id'] . ' with URL ' . $target_item['uri'] . ' will be distributed.', Logger::DEBUG); } elseif (!DBA::exists('conversation', ['item-uri' => $target_item['uri'], 'protocol' => Conversation::PARCEL_ACTIVITYPUB])) { Logger::log('Remote item ' . $target_item['id'] . ' with URL ' . $target_item['uri'] . ' is no AP post. It will not be distributed.', Logger::DEBUG); return 0; @@ -634,29 +637,4 @@ class Notifier return count($inboxes); } - - private static function isForumPost(array $item, array $owner) - { - if (($item['author-id'] == $item['owner-id']) || - ($owner['id'] == $item['contact-id']) || - ($item['uri'] != $item['parent-uri'])) { - return false; - } - - return self::isForum($item['contact-id']); - } - - private static function isForum($contactid) - { - $fields = ['forum', 'prv']; - $condition = ['id' => $contactid]; - $contact = DBA::selectFirst('contact', $fields, $condition); - if (!DBA::isResult($contact)) { - // Should never happen - return false; - } - - // Is it a forum? - return ($contact['forum'] || $contact['prv']); - } } -- 2.39.5