Realtime_channel.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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. const TIMEOUT = 1800; // 30 minutes
  39. public $__table = 'realtime_channel'; // table name
  40. public $user_id; // int -> user.id, can be null
  41. public $action; // varchar(191) not 255 because utf8mb4 takes more space
  42. public $arg1; // varchar(191) argument not 255 because utf8mb4 takes more space
  43. public $arg2; // varchar(191) usually null not 255 because utf8mb4 takes more space
  44. public $channel_key; // 128-bit shared secret key
  45. public $audience; // listener count
  46. public $created; // created date
  47. public $modified; // modified date
  48. /**
  49. * The One True Thingy that must be defined and declared.
  50. */
  51. public static function schemaDef()
  52. {
  53. return [
  54. 'description' => 'A channel of realtime notice data',
  55. 'fields' => [
  56. 'user_id' => ['type' => 'int',
  57. 'not null' => false,
  58. 'description' => 'user viewing page; can be null'],
  59. 'action' => ['type' => 'varchar',
  60. 'length' => 191,
  61. 'not null' => true,
  62. 'description' => 'page being viewed'],
  63. 'arg1' => ['type' => 'varchar',
  64. 'length' => 191,
  65. 'not null' => false,
  66. 'description' => 'page argument, like username or tag'],
  67. 'arg2' => ['type' => 'varchar',
  68. 'length' => 191,
  69. 'not null' => false,
  70. 'description' => 'second page argument, like tag for showstream'],
  71. 'channel_key' => ['type' => 'varchar',
  72. 'length' => 32,
  73. 'not null' => true,
  74. 'description' => 'shared secret key for this channel'],
  75. 'audience' => ['type' => 'int',
  76. 'not null' => true,
  77. 'default' => 0,
  78. 'description' => 'reference count'],
  79. 'created' => ['type' => 'datetime',
  80. 'not null' => true,
  81. 'description' => 'date this record was created'],
  82. 'modified' => ['type' => 'datetime',
  83. 'not null' => true,
  84. 'description' => 'date this record was modified'],
  85. ],
  86. 'primary key' => ['channel_key'],
  87. 'unique keys' => ['realtime_channel_user_page_idx' => ['user_id', 'action', 'arg1', 'arg2']],
  88. 'foreign keys' => [
  89. 'realtime_channel_user_id_fkey' => ['user', ['user_id' => 'id']],
  90. ],
  91. 'indexes' => [
  92. 'realtime_channel_modified_idx' => ['modified'],
  93. 'realtime_channel_page_idx' => ['action', 'arg1', 'arg2']
  94. ],
  95. ];
  96. }
  97. public static function saveNew(int $user_id, Action $action, $arg1, $arg2): Realtime_channel
  98. {
  99. $channel = new Realtime_channel();
  100. $channel->user_id = $user_id;
  101. $channel->action = $action;
  102. $channel->arg1 = $arg1;
  103. $channel->arg2 = $arg2;
  104. $channel->audience = 1;
  105. $channel->channel_key = common_random_hexstr(16); // 128-bit key, 32 hex chars
  106. $channel->created = common_sql_now();
  107. $channel->modified = $channel->created;
  108. $channel->insert();
  109. return $channel;
  110. }
  111. public static function getChannel(int $user_id, Action $action, $arg1, $arg2): Realtime_channel
  112. {
  113. $channel = self::fetchChannel($user_id, $action, $arg1, $arg2);
  114. // Ignore (and delete!) old channels
  115. if (!empty($channel)) {
  116. $modTime = strtotime($channel->modified);
  117. if ((time() - $modTime) > self::TIMEOUT) {
  118. $channel->delete();
  119. $channel = null;
  120. }
  121. }
  122. if (empty($channel)) {
  123. $channel = self::saveNew($user_id, $action, $arg1, $arg2);
  124. }
  125. return $channel;
  126. }
  127. public static function getAllChannels(Action $action, $arg1, $arg2): array
  128. {
  129. $channel = new Realtime_channel();
  130. $channel->action = $action;
  131. if (is_null($arg1)) {
  132. $channel->whereAdd('arg1 is null');
  133. } else {
  134. $channel->arg1 = $arg1;
  135. }
  136. if (is_null($arg2)) {
  137. $channel->whereAdd('arg2 is null');
  138. } else {
  139. $channel->arg2 = $arg2;
  140. }
  141. $channel->whereAdd(sprintf("modified > TIMESTAMP '%s'", common_sql_date(time() - self::TIMEOUT)));
  142. $channels = [];
  143. if ($channel->find()) {
  144. $channels = $channel->fetchAll();
  145. }
  146. return $channels;
  147. }
  148. public static function fetchChannel(int $user_id, Action $action, $arg1, $arg2): ?Realtime_channel
  149. {
  150. $channel = new Realtime_channel();
  151. if (is_null($user_id)) {
  152. $channel->whereAdd('user_id is null');
  153. } else {
  154. $channel->user_id = $user_id;
  155. }
  156. $channel->action = $action;
  157. if (is_null($arg1)) {
  158. $channel->whereAdd('arg1 is null');
  159. } else {
  160. $channel->arg1 = $arg1;
  161. }
  162. if (is_null($arg2)) {
  163. $channel->whereAdd('arg2 is null');
  164. } else {
  165. $channel->arg2 = $arg2;
  166. }
  167. if ($channel->find(true)) {
  168. $channel->increment();
  169. return $channel;
  170. } else {
  171. return null;
  172. }
  173. }
  174. public function increment(): void
  175. {
  176. // XXX: race
  177. $orig = clone($this);
  178. $this->audience++;
  179. $this->modified = common_sql_now();
  180. $this->update($orig);
  181. }
  182. public function touch(): void
  183. {
  184. // XXX: race
  185. $orig = clone($this);
  186. $this->modified = common_sql_now();
  187. $this->update($orig);
  188. }
  189. public function decrement(): void
  190. {
  191. // XXX: race
  192. if ($this->audience == 1) {
  193. $this->delete();
  194. } else {
  195. $orig = clone($this);
  196. $this->audience--;
  197. $this->modified = common_sql_now();
  198. $this->update($orig);
  199. }
  200. }
  201. }