]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Nodeinfo/NodeinfoPlugin.php
[ROUTES] Allow accept-header specification during router creation
[quix0rs-gnu-social.git] / plugins / Nodeinfo / NodeinfoPlugin.php
1 <?php
2
3 if (!defined('GNUSOCIAL')) {
4     exit(1);
5 }
6
7 class NodeinfoPlugin extends Plugin
8 {
9     const PLUGIN_VERSION = '1.0.2';
10
11     public function onRouterInitialized($m)
12     {
13         $m->connect('.well-known/nodeinfo',
14                     ['action' => 'nodeinfojrd']);
15
16         $m->connect('main/nodeinfo/2.0',
17                     ['action' => 'nodeinfo_2_0']);
18
19         return true;
20     }
21
22     /**
23      * Make sure necessary tables are filled out.
24      *
25      * @return boolean hook true
26      */
27     public function onCheckSchema()
28     {
29         // Ensure schema
30         $schema = Schema::get();
31         $schema->ensureTable('usage_stats', Usage_stats::schemaDef());
32
33         // Ensure default rows
34         if (Usage_stats::getKV('type', 'users') == null) {
35             $us = new Usage_stats();
36             $us->type = 'users';
37             $us->insert();
38         }
39
40         if (Usage_stats::getKV('type', 'posts') == null) {
41             $us = new Usage_stats();
42             $us->type = 'posts';
43             $us->insert();
44         }
45
46         if (Usage_stats::getKV('type', 'comments') == null) {
47             $us = new Usage_stats();
48             $us->type = 'comments';
49             $us->insert();
50         }
51
52         return true;
53     }
54
55     /**
56      * Increment notices/replies counter
57      *
58      * @return boolean hook flag
59      * @author Diogo Cordeiro <diogo@fc.up.pt>
60      */
61     public function onStartNoticeDistribute($notice)
62     {
63         assert($notice->id > 0);        // Ignore if not a valid notice
64
65         $profile = $notice->getProfile();
66
67         if (!$profile->isLocal()) {
68             return true;
69         }
70
71         // Ignore for activity/non-post-verb notices
72         if (method_exists('ActivityUtils', 'compareVerbs')) {
73             $is_post_verb = ActivityUtils::compareVerbs(
74                 $notice->verb,
75                 [ActivityVerb::POST]
76             );
77         } else {
78             $is_post_verb = ($notice->verb == ActivityVerb::POST ? true : false);
79         }
80         if ($notice->source == 'activity' || !$is_post_verb) {
81             return true;
82         }
83
84         // Is a reply?
85         if ($notice->reply_to) {
86             $us = Usage_stats::getKV('type', 'comments');
87             $us->count += 1;
88             $us->update();
89             return true;
90         }
91
92         // Is an Announce?
93         if ($notice->isRepeat()) {
94             return true;
95         }
96
97         $us = Usage_stats::getKV('type', 'posts');
98         $us->count += 1;
99         $us->update();
100
101         // That was it
102         return true;
103     }
104
105     /**
106      * Decrement notices/replies counter
107      *
108      * @return boolean hook flag
109      * @author Diogo Cordeiro <diogo@fc.up.pt>
110      */
111     public function onStartDeleteOwnNotice($user, $notice)
112     {
113         $profile = $user->getProfile();
114
115         // Only count local notices
116         if (!$profile->isLocal()) {
117             return true;
118         }
119
120         if ($notice->reply_to) {
121             $us = Usage_stats::getKV('type', 'comments');
122             $us->count -= 1;
123             $us->update();
124             return true;
125         }
126
127         $us = Usage_stats::getKV('type', 'posts');
128         $us->count -= 1;
129         $us->update();
130         return true;
131     }
132
133     /**
134      * Increment users counter
135      *
136      * @return boolean hook flag
137      * @author Diogo Cordeiro <diogo@fc.up.pt>
138      */
139     public function onEndRegistrationTry()
140     {
141         $us = Usage_stats::getKV('type', 'users');
142         $us->count += 1;
143         $us->update();
144         return true;
145     }
146
147     /**
148      * Decrement users counter
149      *
150      * @return boolean hook flag
151      * @author Diogo Cordeiro <diogo@fc.up.pt>
152      */
153     public function onEndDeleteUser()
154     {
155         $us = Usage_stats::getKV('type', 'users');
156         $us->count -= 1;
157         $us->update();
158         return true;
159     }
160
161
162     public function onPluginVersion(array &$versions)
163     {
164         $versions[] = ['name' => 'Nodeinfo',
165             'version' => self::PLUGIN_VERSION,
166             'author' => 'chimo',
167             'homepage' => 'https://github.com/chimo/gs-nodeinfo',
168             'description' => _m('Plugin that presents basic instance information using the NodeInfo standard.')];
169         return true;
170     }
171
172     public function onEndUpgrade()
173     {
174         $users = new Usage_stats();
175         if ($users->getUserCount() == 0) {
176             define('NODEINFO_UPGRADE', true);
177             require_once __DIR__ . DIRECTORY_SEPARATOR . 'scripts' . DIRECTORY_SEPARATOR . 'fix_stats.php';
178         }
179     }
180 }