Realtime_channel.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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' => 'timestamp',
  83. 'not null' => true,
  84. 'description' => 'date this record was modified'],
  85. ],
  86. 'primary key' => ['channel_key'],
  87. 'unique keys' => [
  88. 'realtime_channel_user_id_action_arg1_arg2_key' => ['user_id', 'action', 'arg1', 'arg2'],
  89. ],
  90. 'foreign keys' => [
  91. 'realtime_channel_user_id_fkey' => ['user', ['user_id' => 'id']],
  92. ],
  93. 'indexes' => [
  94. 'realtime_channel_modified_idx' => ['modified'],
  95. 'realtime_channel_page_idx' => ['action', 'arg1', 'arg2']
  96. ],
  97. ];
  98. }
  99. public static function saveNew(int $user_id, Action $action, $arg1, $arg2): Realtime_channel
  100. {
  101. $channel = new Realtime_channel();
  102. $channel->user_id = $user_id;
  103. $channel->action = $action;
  104. $channel->arg1 = $arg1;
  105. $channel->arg2 = $arg2;
  106. $channel->audience = 1;
  107. $channel->channel_key = common_random_hexstr(16); // 128-bit key, 32 hex chars
  108. $channel->created = common_sql_now();
  109. $channel->modified = $channel->created;
  110. $channel->insert();
  111. return $channel;
  112. }
  113. public static function getChannel(int $user_id, Action $action, $arg1, $arg2): Realtime_channel
  114. {
  115. $channel = self::fetchChannel($user_id, $action, $arg1, $arg2);
  116. // Ignore (and delete!) old channels
  117. if (!empty($channel)) {
  118. $modTime = strtotime($channel->modified);
  119. if ((time() - $modTime) > self::TIMEOUT) {
  120. $channel->delete();
  121. $channel = null;
  122. }
  123. }
  124. if (empty($channel)) {
  125. $channel = self::saveNew($user_id, $action, $arg1, $arg2);
  126. }
  127. return $channel;
  128. }
  129. public static function getAllChannels(Action $action, $arg1, $arg2): array
  130. {
  131. $channel = new Realtime_channel();
  132. $channel->action = $action;
  133. if (is_null($arg1)) {
  134. $channel->whereAdd('arg1 is null');
  135. } else {
  136. $channel->arg1 = $arg1;
  137. }
  138. if (is_null($arg2)) {
  139. $channel->whereAdd('arg2 is null');
  140. } else {
  141. $channel->arg2 = $arg2;
  142. }
  143. $channel->whereAdd(sprintf("modified > TIMESTAMP '%s'", common_sql_date(time() - self::TIMEOUT)));
  144. $channels = [];
  145. if ($channel->find()) {
  146. $channels = $channel->fetchAll();
  147. }
  148. return $channels;
  149. }
  150. public static function fetchChannel(int $user_id, Action $action, $arg1, $arg2): ?Realtime_channel
  151. {
  152. $channel = new Realtime_channel();
  153. if (is_null($user_id)) {
  154. $channel->whereAdd('user_id is null');
  155. } else {
  156. $channel->user_id = $user_id;
  157. }
  158. $channel->action = $action;
  159. if (is_null($arg1)) {
  160. $channel->whereAdd('arg1 is null');
  161. } else {
  162. $channel->arg1 = $arg1;
  163. }
  164. if (is_null($arg2)) {
  165. $channel->whereAdd('arg2 is null');
  166. } else {
  167. $channel->arg2 = $arg2;
  168. }
  169. if ($channel->find(true)) {
  170. $channel->increment();
  171. return $channel;
  172. } else {
  173. return null;
  174. }
  175. }
  176. public function increment(): void
  177. {
  178. // XXX: race
  179. $orig = clone($this);
  180. $this->audience++;
  181. $this->modified = common_sql_now();
  182. $this->update($orig);
  183. }
  184. public function touch(): void
  185. {
  186. // XXX: race
  187. $orig = clone($this);
  188. $this->modified = common_sql_now();
  189. $this->update($orig);
  190. }
  191. public function decrement(): void
  192. {
  193. // XXX: race
  194. if ($this->audience == 1) {
  195. $this->delete();
  196. } else {
  197. $orig = clone($this);
  198. $this->audience--;
  199. $this->modified = common_sql_now();
  200. $this->update($orig);
  201. }
  202. }
  203. }