X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=plugins%2FNodeinfo%2FNodeinfoPlugin.php;h=07a86e93f7fb17673d0251af04695ca1bcce21a3;hb=bc030da3208143afcb869ce4611cb39d8d465fa6;hp=5c23ac5e577a75fb1bdaef3fd28047c72d9db96c;hpb=e6667db0cd4dd3706164bb47c6b2ddd0b937708c;p=quix0rs-gnu-social.git diff --git a/plugins/Nodeinfo/NodeinfoPlugin.php b/plugins/Nodeinfo/NodeinfoPlugin.php index 5c23ac5e57..07a86e93f7 100644 --- a/plugins/Nodeinfo/NodeinfoPlugin.php +++ b/plugins/Nodeinfo/NodeinfoPlugin.php @@ -27,13 +27,153 @@ class NodeinfoPlugin extends Plugin return true; } + /** + * Make sure necessary tables are filled out. + * + * @return boolean hook true + */ + public function onCheckSchema() + { + // Ensure schema + $schema = Schema::get(); + $schema->ensureTable('usage_stats', Usage_stats::schemaDef()); + + // Ensure default rows + if (Usage_stats::getKV('type', 'users') == null) { + $us = new Usage_stats(); + $us->type = 'users'; + $us->insert(); + } + + if (Usage_stats::getKV('type', 'posts') == null) { + $us = new Usage_stats(); + $us->type = 'posts'; + $us->insert(); + } + + if (Usage_stats::getKV('type', 'comments') == null) { + $us = new Usage_stats(); + $us->type = 'comments'; + $us->insert(); + } + + return true; + } + + /** + * Increment notices/replies counter + * + * @return boolean hook flag + * @author Diogo Cordeiro + */ + public function onStartNoticeDistribute($notice) + { + assert($notice->id > 0); // Ignore if not a valid notice + + $profile = $notice->getProfile(); + + if (!$profile->isLocal()) { + return true; + } + + // Ignore for activity/non-post-verb notices + if (method_exists('ActivityUtils', 'compareVerbs')) { + $is_post_verb = ActivityUtils::compareVerbs( + $notice->verb, + [ActivityVerb::POST] + ); + } else { + $is_post_verb = ($notice->verb == ActivityVerb::POST ? true : false); + } + if ($notice->source == 'activity' || !$is_post_verb) { + return true; + } + + // Is a reply? + if ($notice->reply_to) { + $us = Usage_stats::getKV('type', 'comments'); + $us->count += 1; + $us->update(); + return true; + } + + // Is an Announce? + if ($notice->isRepeat()) { + return true; + } + + $us = Usage_stats::getKV('type', 'posts'); + $us->count += 1; + $us->update(); + + // That was it + return true; + } + + /** + * Decrement notices/replies counter + * + * @return boolean hook flag + * @author Diogo Cordeiro + */ + public function onStartDeleteOwnNotice($user, $notice) + { + $profile = $user->getProfile(); + + // Only count local notices + if (!$profile->isLocal()) { + return true; + } + + if ($notice->reply_to) { + $us = Usage_stats::getKV('type', 'comments'); + $us->count -= 1; + $us->update(); + return true; + } + + $us = Usage_stats::getKV('type', 'posts'); + $us->count -= 1; + $us->update(); + return true; + } + + /** + * Increment users counter + * + * @return boolean hook flag + * @author Diogo Cordeiro + */ + public function onEndRegistrationTry() + { + $us = Usage_stats::getKV('type', 'users'); + $us->count += 1; + $us->update(); + return true; + } + + /** + * Decrement users counter + * + * @return boolean hook flag + * @author Diogo Cordeiro + */ + public function onEndDeleteUser() + { + $us = Usage_stats::getKV('type', 'users'); + $us->count -= 1; + $us->update(); + return true; + } + + public function onPluginVersion(array &$versions) { - $versions[] = array('name' => 'Nodeinfo', + $versions[] = ['name' => 'Nodeinfo', 'version' => self::VERSION, 'author' => 'chimo', 'homepage' => 'https://github.com/chimo/gs-nodeinfo', - 'description' => _m('Plugin that presents basic instance information using the NodeInfo standard.')); + 'description' => _m('Plugin that presents basic instance information using the NodeInfo standard.')]; return true; } }