Realtime_channel.php 7.7 KB

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