]> git.mxchange.org Git - friendica.git/blobdiff - src/Model/Nodeinfo.php
Posts per author/server on the community pages (#13764)
[friendica.git] / src / Model / Nodeinfo.php
index 7cde259538c95b26ced1c3f0ffe3b8bc832305bc..825afb4a9a9f14fa0118cfca340471ee733f676d 100644 (file)
@@ -1,10 +1,32 @@
 <?php
+/**
+ * @copyright Copyright (C) 2010-2023, 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\Model;
 
 use Friendica\Core\Addon;
+use Friendica\Core\Config\Capability\IManageConfigValues;
 use Friendica\Database\DBA;
 use Friendica\DI;
+use Friendica\Model\Item;
+use stdClass;
 
 /**
  * Model interaction for the nodeinfo
@@ -24,20 +46,7 @@ class Nodeinfo
                // If the addon 'statistics_json' is enabled then disable it and activate nodeinfo.
                if (Addon::isEnabled('statistics_json')) {
                        $config->set('system', 'nodeinfo', true);
-
-                       $addon = 'statistics_json';
-                       $addons = $config->get('system', 'addon');
-
-                       if ($addons) {
-                               $addons_arr = explode(',', str_replace(' ', '', $addons));
-
-                               $idx = array_search($addon, $addons_arr);
-                               if ($idx !== false) {
-                                       unset($addons_arr[$idx]);
-                                       Addon::uninstall($addon);
-                                       $config->set('system', 'addon', implode(', ', $addons_arr));
-                               }
-                       }
+                       Addon::uninstall('statistics_json');
                }
 
                if (empty($config->get('system', 'nodeinfo'))) {
@@ -46,21 +55,115 @@ class Nodeinfo
 
                $userStats = User::getStatistics();
 
-               $config->set('nodeinfo', 'total_users', $userStats['total_users']);
-               $config->set('nodeinfo', 'active_users_halfyear', $userStats['active_users_halfyear']);
-               $config->set('nodeinfo', 'active_users_monthly', $userStats['active_users_monthly']);
+               DI::keyValue()->set('nodeinfo_total_users', $userStats['total_users']);
+               DI::keyValue()->set('nodeinfo_active_users_halfyear', $userStats['active_users_halfyear']);
+               DI::keyValue()->set('nodeinfo_active_users_monthly', $userStats['active_users_monthly']);
+               DI::keyValue()->set('nodeinfo_active_users_weekly', $userStats['active_users_weekly']);
+
+               $logger->info('user statistics', $userStats);
+
+               $posts = DBA::count('post-thread', ["`uri-id` IN (SELECT `uri-id` FROM `post-user` WHERE NOT `deleted` AND `origin`)"]);
+               $comments = DBA::count('post', ["NOT `deleted` AND `gravity` = ? AND `uri-id` IN (SELECT `uri-id` FROM `post-user` WHERE `origin`)", Item::GRAVITY_COMMENT]);
+               DI::keyValue()->set('nodeinfo_local_posts', $posts);
+               DI::keyValue()->set('nodeinfo_local_comments', $comments);
+
+               $logger->info('User activity', ['posts' => $posts, 'comments' => $comments]);
+       }
+
+       /**
+        * Return the supported services
+        *
+        * @return Object with supported services
+       */
+       public static function getUsage(bool $version2 = false)
+       {
+               $config = DI::config();
+
+               $usage = new stdClass();
+               $usage->users = new \stdClass;
 
-               $logger->debug('user statistics', $userStats);
+               if (!empty($config->get('system', 'nodeinfo'))) {
+                       $usage->users->total = intval(DI::keyValue()->get('nodeinfo_total_users'));
+                       $usage->users->activeHalfyear = intval(DI::keyValue()->get('nodeinfo_active_users_halfyear'));
+                       $usage->users->activeMonth = intval(DI::keyValue()->get('nodeinfo_active_users_monthly'));
+                       $usage->localPosts = intval(DI::keyValue()->get('nodeinfo_local_posts'));
+                       $usage->localComments = intval(DI::keyValue()->get('nodeinfo_local_comments'));
 
-               $items = DBA::p("SELECT COUNT(*) AS `total`, `gravity` FROM `item` WHERE `origin` AND NOT `deleted` AND `uid` != 0 AND `gravity` IN (?, ?) GROUP BY `gravity`",
-                       GRAVITY_PARENT, GRAVITY_COMMENT);
-               while ($item = DBA::fetch($items)) {
-                       if ($item['gravity'] == GRAVITY_PARENT) {
-                               $config->set('nodeinfo', 'local_posts', $item['total']);
-                       } elseif ($item['gravity'] == GRAVITY_COMMENT) {
-                               $config->set('nodeinfo', 'local_comments', $item['total']);
+                       if ($version2) {
+                               $usage->users->activeWeek = intval(DI::keyValue()->get('nodeinfo_active_users_weekly'));
                        }
                }
-               DBA::close($items);
+
+               return $usage;
+       }
+
+       /**
+        * Return the supported services
+        *
+        * @return array with supported services
+       */
+       public static function getServices(): array
+       {
+               $services = [
+                       'inbound'  => [],
+                       'outbound' => [],
+               ];
+
+               if (Addon::isEnabled('bluesky')) {
+                       $services['inbound'][] = 'bluesky';
+                       $services['outbound'][] = 'bluesky';
+               }
+               if (Addon::isEnabled('dwpost')) {
+                       $services['outbound'][] = 'dreamwidth';
+               }
+               if (Addon::isEnabled('statusnet')) {
+                       $services['inbound'][] = 'gnusocial';
+                       $services['outbound'][] = 'gnusocial';
+               }
+               if (Addon::isEnabled('ijpost')) {
+                       $services['outbound'][] = 'insanejournal';
+               }
+               if (Addon::isEnabled('libertree')) {
+                       $services['outbound'][] = 'libertree';
+               }
+               if (Addon::isEnabled('ljpost')) {
+                       $services['outbound'][] = 'livejournal';
+               }
+               if (Addon::isEnabled('pumpio')) {
+                       $services['inbound'][] = 'pumpio';
+                       $services['outbound'][] = 'pumpio';
+               }
+
+               $services['outbound'][] = 'smtp';
+
+               if (Addon::isEnabled('tumblr')) {
+                       $services['outbound'][] = 'tumblr';
+               }
+               if (Addon::isEnabled('twitter')) {
+                       $services['outbound'][] = 'twitter';
+               }
+               if (Addon::isEnabled('wppost')) {
+                       $services['outbound'][] = 'wordpress';
+               }
+
+               return $services;
+       }
+
+       /**
+        * Gathers organization information and returns it as an array
+        *
+        * @param IManageConfigValues $config Configuration instance
+        * @return array Organization information
+        * @throws \Exception
+        */
+       public static function getOrganization(IManageConfigValues $config): array
+       {
+               $administrator = User::getFirstAdmin(['username', 'email', 'nickname']);
+
+               return [
+                       'name'    => $administrator['username'] ?? null,
+                       'contact' => $administrator['email']    ?? null,
+                       'account' => $administrator['nickname'] ?? '' ? DI::baseUrl() . '/profile/' . $administrator['nickname'] : null,
+               ];
        }
 }