]> git.mxchange.org Git - friendica.git/blob - src/Model/Nodeinfo.php
Merge pull request #12169 from MrPetovan/bug/7574-notifications-deleted-users
[friendica.git] / src / Model / Nodeinfo.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2022, the Friendica project
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19  *
20  */
21
22 namespace Friendica\Model;
23
24 use Friendica\Core\Addon;
25 use Friendica\Core\Config\Capability\IManageConfigValues;
26 use Friendica\Database\DBA;
27 use Friendica\DI;
28 use Friendica\Model\Item;
29 use stdClass;
30
31 /**
32  * Model interaction for the nodeinfo
33  */
34 class Nodeinfo
35 {
36         /**
37          * Updates the info about the current node
38          *
39          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
40          */
41         public static function update()
42         {
43                 $config = DI::config();
44                 $logger = DI::logger();
45
46                 // If the addon 'statistics_json' is enabled then disable it and activate nodeinfo.
47                 if (Addon::isEnabled('statistics_json')) {
48                         $config->set('system', 'nodeinfo', true);
49                         Addon::uninstall('statistics_json');
50                 }
51
52                 if (empty($config->get('system', 'nodeinfo'))) {
53                         return;
54                 }
55
56                 $userStats = User::getStatistics();
57
58                 $config->set('nodeinfo', 'total_users', $userStats['total_users']);
59                 $config->set('nodeinfo', 'active_users_halfyear', $userStats['active_users_halfyear']);
60                 $config->set('nodeinfo', 'active_users_monthly', $userStats['active_users_monthly']);
61                 $config->set('nodeinfo', 'active_users_weekly', $userStats['active_users_weekly']);
62
63                 $logger->info('user statistics', $userStats);
64
65                 $posts = DBA::count('post-thread', ["`uri-id` IN (SELECT `uri-id` FROM `post-user` WHERE NOT `deleted` AND `origin`)"]);
66                 $comments = DBA::count('post', ["NOT `deleted` AND `gravity` = ? AND `uri-id` IN (SELECT `uri-id` FROM `post-user` WHERE `origin`)", Item::GRAVITY_COMMENT]);
67                 $config->set('nodeinfo', 'local_posts', $posts);
68                 $config->set('nodeinfo', 'local_comments', $comments);
69
70                 $logger->info('User actitivy', ['posts' => $posts, 'comments' => $comments]);
71         }
72
73         /**
74          * Return the supported services
75          *
76          * @return Object with supported services
77         */
78         public static function getUsage(bool $version2 = false)
79         {
80                 $config = DI::config();
81
82                 $usage = new stdClass();
83                 $usage->users = [];
84
85                 if (!empty($config->get('system', 'nodeinfo'))) {
86                         $usage->users = [
87                                 'total'          => intval($config->get('nodeinfo', 'total_users')),
88                                 'activeHalfyear' => intval($config->get('nodeinfo', 'active_users_halfyear')),
89                                 'activeMonth'    => intval($config->get('nodeinfo', 'active_users_monthly'))
90                         ];
91                         $usage->localPosts = intval($config->get('nodeinfo', 'local_posts'));
92                         $usage->localComments = intval($config->get('nodeinfo', 'local_comments'));
93
94                         if ($version2) {
95                                 $usage->users['activeWeek'] = intval($config->get('nodeinfo', 'active_users_weekly'));
96                         }
97                 }
98
99                 return $usage;
100         }
101
102         /**
103          * Return the supported services
104          *
105          * @return array with supported services
106         */
107         public static function getServices(): array
108         {
109                 $services = [
110                         'inbound'  => [],
111                         'outbound' => [],
112                 ];
113
114                 if (Addon::isEnabled('blogger')) {
115                         $services['outbound'][] = 'blogger';
116                 }
117                 if (Addon::isEnabled('dwpost')) {
118                         $services['outbound'][] = 'dreamwidth';
119                 }
120                 if (Addon::isEnabled('statusnet')) {
121                         $services['inbound'][] = 'gnusocial';
122                         $services['outbound'][] = 'gnusocial';
123                 }
124                 if (Addon::isEnabled('ijpost')) {
125                         $services['outbound'][] = 'insanejournal';
126                 }
127                 if (Addon::isEnabled('libertree')) {
128                         $services['outbound'][] = 'libertree';
129                 }
130                 if (Addon::isEnabled('buffer')) {
131                         $services['outbound'][] = 'linkedin';
132                 }
133                 if (Addon::isEnabled('ljpost')) {
134                         $services['outbound'][] = 'livejournal';
135                 }
136                 if (Addon::isEnabled('buffer')) {
137                         $services['outbound'][] = 'pinterest';
138                 }
139                 if (Addon::isEnabled('posterous')) {
140                         $services['outbound'][] = 'posterous';
141                 }
142                 if (Addon::isEnabled('pumpio')) {
143                         $services['inbound'][] = 'pumpio';
144                         $services['outbound'][] = 'pumpio';
145                 }
146
147                 $services['outbound'][] = 'smtp';
148
149                 if (Addon::isEnabled('tumblr')) {
150                         $services['outbound'][] = 'tumblr';
151                 }
152                 if (Addon::isEnabled('twitter') || Addon::isEnabled('buffer')) {
153                         $services['outbound'][] = 'twitter';
154                 }
155                 if (Addon::isEnabled('wppost')) {
156                         $services['outbound'][] = 'wordpress';
157                 }
158
159                 return $services;
160         }
161
162         /**
163          * Gathers organization information and returns it as an array
164          *
165          * @param IManageConfigValues $config Configuration instance
166          * @return array Organization information
167          * @throws \Exception
168          */
169         public static function getOrganization(IManageConfigValues $config): array
170         {
171                 $administrator = User::getFirstAdmin(['username', 'email', 'nickname']);
172
173                 return [
174                         'name'    => $administrator['username'] ?? null,
175                         'contact' => $administrator['email']    ?? null,
176                         'account' => $administrator['nickname'] ?? '' ? DI::baseUrl()->get() . '/profile/' . $administrator['nickname'] : null,
177                 ];
178         }
179 }