Realtime_channel.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. <?php
  2. // This file is part of GNU social - https://www.gnu.org/software/social
  3. //
  4. // GNU social is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Affero General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // GNU social is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Affero General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Affero General Public License
  15. // along with GNU social. If not, see <http://www.gnu.org/licenses/>.
  16. /**
  17. * A channel for real-time browser data
  18. *
  19. * For each user currently browsing the site, we want to know which page they're on
  20. * so we can send real-time updates to their browser.
  21. *
  22. * @category Realtime
  23. * @package GNUsocial
  24. * @author Evan Prodromou <evan@status.net>
  25. * @copyright 2011-2019 Free Software Foundation, Inc http://www.fsf.org
  26. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  27. */
  28. defined('GNUSOCIAL') || die();
  29. /**
  30. * A channel for real-time browser data
  31. *
  32. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  33. *
  34. * @see DB_DataObject
  35. */
  36. class Realtime_channel extends Managed_DataObject
  37. {
  38. public $widgetOpts;
  39. public $scoped;
  40. const TIMEOUT = 1800; // 30 minutes
  41. public $__table = 'realtime_channel'; // table name
  42. public $user_id; // int -> user.id, can be null
  43. public $action; // varchar(191) not 255 because utf8mb4 takes more space
  44. public $arg1; // varchar(191) argument not 255 because utf8mb4 takes more space
  45. public $arg2; // varchar(191) usually null not 255 because utf8mb4 takes more space
  46. public $channel_key; // 128-bit shared secret key
  47. public $audience; // listener count
  48. public $created; // created date
  49. public $modified; // modified date
  50. /**
  51. * The One True Thingy that must be defined and declared.
  52. */
  53. public static function schemaDef()
  54. {
  55. return [
  56. 'description' => 'A channel of realtime notice data',
  57. 'fields' => [
  58. 'user_id' => ['type' => 'int',
  59. 'not null' => false,
  60. 'description' => 'user viewing page; can be null'],
  61. 'action' => ['type' => 'varchar',
  62. 'length' => 191,
  63. 'not null' => true,
  64. 'description' => 'page being viewed'],
  65. 'arg1' => ['type' => 'varchar',
  66. 'length' => 191,
  67. 'not null' => false,
  68. 'description' => 'page argument, like username or tag'],
  69. 'arg2' => ['type' => 'varchar',
  70. 'length' => 191,
  71. 'not null' => false,
  72. 'description' => 'second page argument, like tag for showstream'],
  73. 'channel_key' => ['type' => 'varchar',
  74. 'length' => 32,
  75. 'not null' => true,
  76. 'description' => 'shared secret key for this channel'],
  77. 'audience' => ['type' => 'int',
  78. 'not null' => true,
  79. 'default' => 0,
  80. 'description' => 'reference count'],
  81. 'created' => ['type' => 'datetime',
  82. 'not null' => true,
  83. 'description' => 'date this record was created'],
  84. 'modified' => ['type' => 'timestamp',
  85. 'not null' => true,
  86. 'description' => 'date this record was modified'],
  87. ],
  88. 'primary key' => ['channel_key'],
  89. 'unique keys' => [
  90. 'realtime_channel_user_id_action_arg1_arg2_key' => ['user_id', 'action', 'arg1', 'arg2'],
  91. ],
  92. 'foreign keys' => [
  93. 'realtime_channel_user_id_fkey' => ['user', ['user_id' => 'id']],
  94. ],
  95. 'indexes' => [
  96. 'realtime_channel_modified_idx' => ['modified'],
  97. 'realtime_channel_page_idx' => ['action', 'arg1', 'arg2']
  98. ],
  99. ];
  100. }
  101. public static function saveNew(int $user_id, Action $action, $arg1, $arg2): Realtime_channel
  102. {
  103. $channel = new Realtime_channel();
  104. $channel->user_id = $user_id;
  105. $channel->action = $action;
  106. $channel->arg1 = $arg1;
  107. $channel->arg2 = $arg2;
  108. $channel->audience = 1;
  109. $channel->channel_key = common_random_hexstr(16); // 128-bit key, 32 hex chars
  110. $channel->created = common_sql_now();
  111. $channel->modified = $channel->created;
  112. $channel->insert();
  113. return $channel;
  114. }
  115. public static function getChannel(int $user_id, Action $action, $arg1, $arg2): Realtime_channel
  116. {
  117. $channel = self::fetchChannel($user_id, $action, $arg1, $arg2);
  118. // Ignore (and delete!) old channels
  119. if (!empty($channel)) {
  120. $modTime = strtotime($channel->modified);
  121. if ((time() - $modTime) > self::TIMEOUT) {
  122. $channel->delete();
  123. $channel = null;
  124. }
  125. }
  126. if (empty($channel)) {
  127. $channel = self::saveNew($user_id, $action, $arg1, $arg2);
  128. }
  129. return $channel;
  130. }
  131. public static function getAllChannels(Action $action, $arg1, $arg2): array
  132. {
  133. $channel = new Realtime_channel();
  134. $channel->action = $action;
  135. if (is_null($arg1)) {
  136. $channel->whereAdd('arg1 is null');
  137. } else {
  138. $channel->arg1 = $arg1;
  139. }
  140. if (is_null($arg2)) {
  141. $channel->whereAdd('arg2 is null');
  142. } else {
  143. $channel->arg2 = $arg2;
  144. }
  145. $channel->whereAdd(sprintf("modified > TIMESTAMP '%s'", common_sql_date(time() - self::TIMEOUT)));
  146. $channels = [];
  147. if ($channel->find()) {
  148. $channels = $channel->fetchAll();
  149. }
  150. return $channels;
  151. }
  152. public static function fetchChannel(int $user_id, Action $action, $arg1, $arg2): ?Realtime_channel
  153. {
  154. $channel = new Realtime_channel();
  155. if (is_null($user_id)) {
  156. $channel->whereAdd('user_id is null');
  157. } else {
  158. $channel->user_id = $user_id;
  159. }
  160. $channel->action = $action;
  161. if (is_null($arg1)) {
  162. $channel->whereAdd('arg1 is null');
  163. } else {
  164. $channel->arg1 = $arg1;
  165. }
  166. if (is_null($arg2)) {
  167. $channel->whereAdd('arg2 is null');
  168. } else {
  169. $channel->arg2 = $arg2;
  170. }
  171. if ($channel->find(true)) {
  172. $channel->increment();
  173. return $channel;
  174. } else {
  175. return null;
  176. }
  177. }
  178. public function increment(): void
  179. {
  180. // XXX: race
  181. $orig = clone($this);
  182. $this->audience++;
  183. $this->modified = common_sql_now();
  184. $this->update($orig);
  185. }
  186. public function touch(): void
  187. {
  188. // XXX: race
  189. $orig = clone($this);
  190. $this->modified = common_sql_now();
  191. $this->update($orig);
  192. }
  193. public function decrement(): void
  194. {
  195. // XXX: race
  196. if ($this->audience == 1) {
  197. $this->delete();
  198. } else {
  199. $orig = clone($this);
  200. $this->audience--;
  201. $this->modified = common_sql_now();
  202. $this->update($orig);
  203. }
  204. }
  205. }