123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180 |
- <?php
- if (!defined('GNUSOCIAL')) {
- exit(1);
- }
- class NodeinfoPlugin extends Plugin
- {
- const VERSION = '0.0.1';
- public function onRouterInitialized($m)
- {
- $m->connect(
- '.well-known/nodeinfo',
- array(
- 'action' => 'nodeinfojrd'
- )
- );
- $m->connect(
- 'main/nodeinfo/2.0',
- array(
- 'action' => 'nodeinfo_2_0'
- )
- );
- 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 <diogo@fc.up.pt>
- */
- 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 <diogo@fc.up.pt>
- */
- 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 <diogo@fc.up.pt>
- */
- 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 <diogo@fc.up.pt>
- */
- public function onEndDeleteUser()
- {
- $us = Usage_stats::getKV('type', 'users');
- $us->count -= 1;
- $us->update();
- return true;
- }
- public function onPluginVersion(array &$versions)
- {
- $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.')];
- return true;
- }
- }
|