]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Nodeinfo/actions/nodeinfo_2_0.php
OStatus and XMPP plugins now inform Nodeinfo plugins about their activity
[quix0rs-gnu-social.git] / plugins / Nodeinfo / actions / nodeinfo_2_0.php
1 <?php
2
3 if (!defined('GNUSOCIAL')) {
4     exit(1);
5 }
6
7 class Nodeinfo_2_0Action extends ApiAction
8 {
9     private $plugins;
10
11     protected function handle()
12     {
13         parent::handle();
14
15         $this->plugins = $this->getActivePluginList();
16
17         $this->showNodeInfo();
18     }
19
20     public function getActivePluginList()
21     {
22         $pluginversions = array();
23         $plugins = array();
24
25         Event::handle('PluginVersion', array(&$pluginversions));
26
27         foreach ($pluginversions as $plugin) {
28             $plugins[strtolower($plugin['name'])] = 1;
29         }
30
31         return $plugins;
32     }
33
34     /*
35      * Technically, the NodeInfo spec defines 'active' as 'signed in at least once',
36      * but GNU social doesn't keep track of when users last logged in, so let's return
37      * the number of users that 'posted at least once', I guess.
38      */
39
40     public function showNodeInfo()
41     {
42         $openRegistrations = $this->getRegistrationsStatus();
43         $userCount = $this->getUserCount();
44         $postCount = $this->getPostCount();
45         $commentCount = $this->getCommentCount();
46
47         $usersActiveHalfyear = $this->getActiveUsers(180);
48         $usersActiveMonth = $this->getActiveUsers(30);
49
50         $protocols = $this->getProtocols();
51         $inboundServices = $this->getInboundServices();
52         $outboundServices = $this->getOutboundServices();
53
54         $json = json_encode([
55             'version' => '2.0',
56
57             'software' => [
58                 'name' => 'gnusocial',
59                 'version' => GNUSOCIAL_VERSION
60             ],
61
62             'protocols' => $protocols,
63
64             // TODO: Have plugins register services
65             'services' => [
66                 'inbound' => $inboundServices,
67                 'outbound' => $outboundServices
68             ],
69
70             'openRegistrations' => $openRegistrations,
71
72             'usage' => [
73                 'users' => [
74                     'total' => $userCount,
75                     'activeHalfyear' => $usersActiveHalfyear,
76                     'activeMonth' => $usersActiveMonth
77                 ],
78                 'localPosts' => $postCount,
79                 'localComments' => $commentCount
80             ],
81
82             'metadata' => new stdClass()
83         ]);
84
85         $this->initDocument('json');
86         print $json;
87         $this->endDocument('json');
88     }
89
90     public function getRegistrationsStatus()
91     {
92         $areRegistrationsClosed = (common_config('site', 'closed')) ? true : false;
93         $isSiteInviteOnly = (common_config('site', 'inviteonly')) ? true : false;
94
95         return !($areRegistrationsClosed || $isSiteInviteOnly);
96     }
97
98     public function getUserCount()
99     {
100         $users = new User();
101         $userCount = $users->count();
102
103         return $userCount;
104     }
105
106     public function getPostCount()
107     {
108         $notices = new Notice();
109         $notices->is_local = Notice::LOCAL_PUBLIC;
110         $notices->whereAdd('reply_to IS NULL');
111         $noticeCount = $notices->count();
112
113         return $noticeCount;
114     }
115
116     public function getCommentCount()
117     {
118         $notices = new Notice();
119         $notices->is_local = Notice::LOCAL_PUBLIC;
120         $notices->whereAdd('reply_to IS NOT NULL');
121         $commentCount = $notices->count();
122
123         return $commentCount;
124     }
125
126     public function getActiveUsers($days)
127     {
128         $notices = new Notice();
129         $notices->joinAdd(array('profile_id', 'user:id'));
130         $notices->whereAdd('notice.created >= NOW() - INTERVAL ' . $days . ' DAY');
131
132         $activeUsersCount = $notices->count('distinct profile_id');
133
134         return $activeUsersCount;
135     }
136
137     public function getProtocols()
138     {
139         $protocols = [];
140
141         Event::handle('NodeInfoProtocols', array(&$protocols));
142
143         return $protocols;
144     }
145
146     public function getInboundServices()
147     {
148         // FIXME: Are those always on?
149         $inboundServices = array('atom1.0', 'rss2.0');
150
151         if (array_key_exists('twitterbridge', $this->plugins) && $config['twitterimport']['enabled']) {
152             $inboundServices[] = 'twitter';
153         }
154
155         if (array_key_exists('ostatus', $this->plugins)) {
156             $inboundServices[] = 'gnusocial';
157         }
158
159         return $inboundServices;
160     }
161
162     public function getOutboundServices()
163     {
164         $xmppEnabled = (array_key_exists('xmpp', $this->plugins) && common_config('xmpp', 'enabled')) ? true : false;
165
166         // FIXME: Are those always on?
167         $outboundServices = array('atom1.0', 'rss2.0');
168
169         if (array_key_exists('twitterbridge', $this->plugins)) {
170             $outboundServices[] = 'twitter';
171         }
172
173         if (array_key_exists('ostatus', $this->plugins)) {
174             $outboundServices[] = 'gnusocial';
175         }
176
177         if ($xmppEnabled) {
178             $outboundServices[] = 'xmpp';
179         }
180
181         return $outboundServices;
182     }
183 }