]> git.mxchange.org Git - friendica.git/commitdiff
Forums now are working with AP as well
authorMichael <heluecht@pirati.ca>
Thu, 14 Mar 2019 18:44:41 +0000 (18:44 +0000)
committerMichael <heluecht@pirati.ca>
Thu, 14 Mar 2019 18:44:41 +0000 (18:44 +0000)
src/Model/Contact.php
src/Model/Item.php
src/Protocol/ActivityPub/Transmitter.php
src/Worker/Notifier.php

index 9afaf77fc0b8d4994cacd961b2594c8d79683b73..d938ad29c284dcb3fd21335c05ff2220e7d78c0e 100644 (file)
@@ -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']);
+       }
 }
index 964e94ddb9afb93d84d6a0bb47c10ef5bd95cdfe..4808b2d03561f61996104d54a801a0be9b6bd3d8 100644 (file)
@@ -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']);
+       }
 }
index f2df43e854ffef5af74e993045f8257309b0f986..94012e4d639be82e91b618e1a0f59c92c74a60a1 100644 (file)
@@ -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);
 
index 4c78d3c6adc577641122e1eb9d1d174d07965872..baae33f7e0f3d8f2015e7756e582957005a00660 100644 (file)
@@ -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']);
-       }
 }