SearchSub.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. * Data class to store local search subscriptions
  18. *
  19. * @category Plugin
  20. * @package SearchSubPlugin
  21. * @author Brion Vibber <brion@status.net>
  22. * @copyright 2011-2019 Free Software Foundation, Inc http://www.fsf.org
  23. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  24. */
  25. defined('GNUSOCIAL') || die();
  26. /**
  27. * For storing the search subscriptions
  28. *
  29. * @author Brion Vibber <brion@status.net>
  30. * @copyright 2011-2019 Free Software Foundation, Inc http://www.fsf.org
  31. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  32. *
  33. * @see DB_DataObject
  34. */
  35. class SearchSub extends Managed_DataObject
  36. {
  37. public $__table = 'searchsub'; // table name
  38. public $search; // text
  39. public $profile_id; // int -> profile.id
  40. public $created; // datetime
  41. /**
  42. * The One True Thingy that must be defined and declared.
  43. */
  44. public static function schemaDef()
  45. {
  46. return array(
  47. 'description' => 'SearchSubPlugin search subscription records',
  48. 'fields' => array(
  49. 'search' => array('type' => 'varchar', 'length' => 64, 'not null' => true, 'description' => 'hash search associated with this subscription'),
  50. 'profile_id' => array('type' => 'int', 'not null' => true, 'description' => 'profile ID of subscribing user'),
  51. 'created' => array('type' => 'datetime', 'not null' => true, 'description' => 'date this record was created'),
  52. ),
  53. 'primary key' => array('search', 'profile_id'),
  54. 'foreign keys' => array(
  55. 'searchsub_profile_id_fkey' => array('profile', array('profile_id' => 'id')),
  56. ),
  57. 'indexes' => array(
  58. 'searchsub_created_idx' => array('created'),
  59. 'searchsub_profile_id_search_idx' => array('profile_id', 'search'),
  60. ),
  61. );
  62. }
  63. /**
  64. * Start a search subscription!
  65. *
  66. * @param profile $profile subscriber
  67. * @param string $search subscribee
  68. * @return SearchSub
  69. */
  70. public static function start(Profile $profile, $search)
  71. {
  72. $ts = new SearchSub();
  73. $ts->search = $search;
  74. $ts->profile_id = $profile->id;
  75. $ts->created = common_sql_now();
  76. $ts->insert();
  77. self::blow('searchsub:by_profile:%d', $profile->id);
  78. return $ts;
  79. }
  80. /**
  81. * End a search subscription!
  82. *
  83. * @param profile $profile subscriber
  84. * @param string $search subscribee
  85. */
  86. public static function cancel(Profile $profile, $search)
  87. {
  88. $ts = SearchSub::pkeyGet(array('search' => $search,
  89. 'profile_id' => $profile->id));
  90. if ($ts) {
  91. $ts->delete();
  92. self::blow('searchsub:by_profile:%d', $profile->id);
  93. }
  94. }
  95. public static function forProfile(Profile $profile)
  96. {
  97. $searches = array();
  98. $keypart = sprintf('searchsub:by_profile:%d', $profile->id);
  99. $searchstring = self::cacheGet($keypart);
  100. if ($searchstring !== false) {
  101. if (!empty($searchstring)) {
  102. $searches = explode(',', $searchstring);
  103. }
  104. } else {
  105. $searchsub = new SearchSub();
  106. $searchsub->profile_id = $profile->id;
  107. $searchsub->selectAdd();
  108. $searchsub->selectAdd('search');
  109. if ($searchsub->find()) {
  110. $searches = $searchsub->fetchAll('search');
  111. }
  112. self::cacheSet($keypart, implode(',', $searches));
  113. }
  114. return $searches;
  115. }
  116. }