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