]> git.mxchange.org Git - friendica.git/commitdiff
We now offer an endpoint for featured posts
authorMichael <heluecht@pirati.ca>
Fri, 8 Apr 2022 21:25:31 +0000 (21:25 +0000)
committerMichael <heluecht@pirati.ca>
Fri, 8 Apr 2022 21:25:31 +0000 (21:25 +0000)
src/Model/Contact.php
src/Module/ActivityPub/Featured.php [new file with mode: 0644]
src/Protocol/ActivityPub.php
src/Protocol/ActivityPub/Transmitter.php
static/routes.config.php

index e23fa316991a2e623279ab6d46dba9182ceb3857..acb4bc10ed2e95bce7be8030b6813d8727b757d6 100644 (file)
@@ -1474,7 +1474,8 @@ class Contact
                        if ($pager->getStart() == 0) {
                                $cdata = Contact::getPublicAndUserContactID($cid, local_user());
                                if (!empty($cdata['public'])) {
-                                       $condition = ["`uri-id` IN (SELECT `uri-id` FROM `collection-view` WHERE `cid` = ?)", $cdata['public']];
+                                       $condition = ["`uri-id` IN (SELECT `uri-id` FROM `collection-view` WHERE `cid` = ? AND `type` = ?)",
+                                               $cdata['public'], Post\Collection::FEATURED];
                                        $pinned = Post::toArray(Post::selectForUser(local_user(), $fields, $condition, $params));
                                        $items = array_merge($pinned, $items);
                                }
diff --git a/src/Module/ActivityPub/Featured.php b/src/Module/ActivityPub/Featured.php
new file mode 100644 (file)
index 0000000..33aabf3
--- /dev/null
@@ -0,0 +1,51 @@
+<?php
+/**
+ * @copyright Copyright (C) 2010-2022, the Friendica project
+ *
+ * @license GNU AGPL version 3 or any later version
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program.  If not, see <https://www.gnu.org/licenses/>.
+ *
+ */
+
+namespace Friendica\Module\ActivityPub;
+
+use Friendica\BaseModule;
+use Friendica\Model\User;
+use Friendica\Protocol\ActivityPub;
+
+/**
+ * ActivityPub featured posts
+ */
+class Featured extends BaseModule
+{
+       protected function rawContent(array $request = [])
+       {
+               if (empty($this->parameters['nickname'])) {
+                       throw new \Friendica\Network\HTTPException\NotFoundException();
+               }
+
+               $owner = User::getOwnerDataByNick($this->parameters['nickname']);
+               if (empty($owner)) {
+                       throw new \Friendica\Network\HTTPException\NotFoundException();
+               }
+
+               $page = $_REQUEST['page'] ?? null;
+
+               $outbox = ActivityPub\Transmitter::getFeatured($owner, $page);
+               header('Content-Type: application/activity+json');
+               echo json_encode($outbox);
+               exit();
+       }
+}
index 9ab2a3c8851215783c3572a7680b13740b41a295..78496e2433de2e87fb8db7378f8251db9d536cf5 100644 (file)
@@ -65,6 +65,10 @@ class ActivityPub
                'diaspora' => 'https://diasporafoundation.org/ns/',
                'litepub' => 'http://litepub.social/ns#',
                'toot' => 'http://joinmastodon.org/ns#',
+               'featured' => [
+                       "@id" => "toot:featured",
+                       "@type" => "@id",
+               ],
                'schema' => 'http://schema.org#',
                'manuallyApprovesFollowers' => 'as:manuallyApprovesFollowers',
                'sensitive' => 'as:sensitive', 'Hashtag' => 'as:Hashtag',
index 553993edca300717b225bcf6a232e2c9ba7f5063..a39a4c502a6c453834119e04e2cbb95c4f830146 100644 (file)
@@ -288,6 +288,69 @@ class Transmitter
                return $data;
        }
 
+       /**
+        * Public posts for the given owner
+        *
+        * @param array   $owner Owner array
+        * @param integer $page  Page number
+        *
+        * @return array of posts
+        * @throws \Friendica\Network\HTTPException\InternalServerErrorException
+        * @throws \ImagickException
+        */
+       public static function getFeatured($owner, $page = null)
+       {
+               $condition = ["`uri-id` IN (SELECT `uri-id` FROM `collection-view` WHERE `cid` = ? AND `type` = ?)",
+                       Contact::getIdForURL($owner['url'], 0, false), Post\Collection::FEATURED];
+
+               $condition = DBA::mergeConditions($condition,
+                       ['uid'           => $owner['uid'],
+                       'author-id'      => Contact::getIdForURL($owner['url'], 0, false),
+                       'private'        => [Item::PUBLIC, Item::UNLISTED],
+                       'gravity'        => [GRAVITY_PARENT, GRAVITY_COMMENT],
+                       'network'        => Protocol::FEDERATED,
+                       'parent-network' => Protocol::FEDERATED,
+                       'origin'         => true,
+                       'deleted'        => false,
+                       'visible'        => true]);
+
+               $count = Post::count($condition);
+
+               $data = ['@context' => ActivityPub::CONTEXT];
+               $data['id'] = DI::baseUrl() . '/featured/' . $owner['nickname'];
+               $data['type'] = 'OrderedCollection';
+               $data['totalItems'] = $count;
+
+               if (empty($page)) {
+                       $data['first'] = DI::baseUrl() . '/featured/' . $owner['nickname'] . '?page=1';
+               } else {
+                       $data['type'] = 'OrderedCollectionPage';
+                       $list = [];
+
+                       $items = Post::select(['id'], $condition, ['limit' => [($page - 1) * 20, 20], 'order' => ['created' => true]]);
+                       while ($item = Post::fetch($items)) {
+                               $activity = self::createActivityFromItem($item['id'], true);
+                               $activity['type'] = $activity['type'] == 'Update' ? 'Create' : $activity['type'];
+
+                               // Only list "Create" activity objects here, no reshares
+                               if (!empty($activity['object']) && ($activity['type'] == 'Create')) {
+                                       $list[] = $activity['object'];
+                               }
+                       }
+                       DBA::close($items);
+
+                       if (!empty($list)) {
+                               $data['next'] = DI::baseUrl() . '/featured/' . $owner['nickname'] . '?page=' . ($page + 1);
+                       }
+
+                       $data['partOf'] = DI::baseUrl() . '/featured/' . $owner['nickname'];
+
+                       $data['orderedItems'] = $list;
+               }
+
+               return $data;
+       }
+
        /**
         * Return the service array containing information the used software and it's url
         *
@@ -328,8 +391,9 @@ class Transmitter
                if ($uid != 0) {
                        $data['following'] = DI::baseUrl() . '/following/' . $owner['nick'];
                        $data['followers'] = DI::baseUrl() . '/followers/' . $owner['nick'];
-                       $data['inbox'] = DI::baseUrl() . '/inbox/' . $owner['nick'];
-                       $data['outbox'] = DI::baseUrl() . '/outbox/' . $owner['nick'];
+                       $data['inbox']     = DI::baseUrl() . '/inbox/' . $owner['nick'];
+                       $data['outbox']    = DI::baseUrl() . '/outbox/' . $owner['nick'];
+                       $data['featured']  = DI::baseUrl() . '/featured/' . $owner['nick'];
                } else {
                        $data['inbox'] = DI::baseUrl() . '/friendica/inbox';
                }
index 21afd2307bc9305d41b53955623c37708d4e2cc2..ddd678d27797f4193d661f1c2e1433c1ca0d9dba 100644 (file)
@@ -361,7 +361,9 @@ return [
        '/dirfind'                  => [Module\Search\Directory::class, [R::GET]],
        '/directory'                => [Module\Directory::class,        [R::GET]],
 
-       '/events/json'                => [Module\Events\Json::class,        [R::GET]],
+       '/events/json'              => [Module\Events\Json::class,      [R::GET]],
+
+       '/featured/{nickname}'      => [Module\ActivityPub\Featured::class, [R::GET]],
 
        '/feed'     => [
                '/{nickname}'          => [Module\Feed::class, [R::GET]],