NodeinfoPlugin.php 4.2 KB

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