QvitterBlocked.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php
  2. /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  3. · ·
  4. · Extends GNU social's Profile_block class with useful functions ·
  5. · ·
  6. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  7. · ·
  8. · ·
  9. · Q V I T T E R ·
  10. · ·
  11. · https://git.gnu.io/h2p/Qvitter ·
  12. · ·
  13. · ·
  14. · <o) ·
  15. · /_//// ·
  16. · (____/ ·
  17. · (o< ·
  18. · o> \\\\_\ ·
  19. · \\) \____) ·
  20. · ·
  21. · ·
  22. · ·
  23. · Qvitter is free software: you can redistribute it and / or modify it ·
  24. · under the terms of the GNU Affero General Public License as published by ·
  25. · the Free Software Foundation, either version three of the License or (at ·
  26. · your option) any later version. ·
  27. · ·
  28. · Qvitter is distributed in hope that it will be useful but WITHOUT ANY ·
  29. · WARRANTY; without even the implied warranty of MERCHANTABILTY or FITNESS ·
  30. · FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for ·
  31. · more details. ·
  32. · ·
  33. · You should have received a copy of the GNU Affero General Public License ·
  34. · along with Qvitter. If not, see <http://www.gnu.org/licenses/>. ·
  35. · ·
  36. · Contact h@nnesmannerhe.im if you have any questions. ·
  37. · ·
  38. · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · */
  39. if (!defined('GNUSOCIAL')) { exit(1); }
  40. class QvitterBlocked extends Profile_block
  41. {
  42. const CACHE_WINDOW = 201;
  43. public static function getBlocked($profile_id, $offset = 0, $limit = PROFILES_PER_PAGE)
  44. {
  45. $ids = self::getBlockedIDs($profile_id, $offset, $limit);
  46. try {
  47. $blocked = Profile_block::listFind('blocked', $ids);
  48. return $blocked;
  49. } catch(NoResultException $e) {
  50. return false;
  51. }
  52. }
  53. public static function getBlockedIDs($profile_id, $offset, $limit)
  54. {
  55. $cacheKey = 'qvitterblocked:'.$profile_id;
  56. $queryoffset = $offset;
  57. $querylimit = $limit;
  58. if ($offset + $limit <= self::CACHE_WINDOW) {
  59. // Oh, it seems it should be cached
  60. $ids = self::cacheGet($cacheKey);
  61. if (is_array($ids)) {
  62. return array_slice($ids, $offset, $limit);
  63. }
  64. // Being here indicates we didn't find anything cached
  65. // so we'll have to fill it up simultaneously
  66. $queryoffset = 0;
  67. $querylimit = self::CACHE_WINDOW;
  68. }
  69. $blocks = new Profile_block();
  70. $blocks->blocker = $profile_id;
  71. $blocks->selectAdd('blocked');
  72. $blocks->whereAdd("blocked != {$profile_id}");
  73. $blocks->orderBy('modified DESC');
  74. $blocks->limit($queryoffset, $querylimit);
  75. if (!$blocks->find()) {
  76. return array();
  77. }
  78. $ids = $blocks->fetchAll('blocked');
  79. // If we're simultaneously filling up cache, remember to slice
  80. if ($queryoffset === 0 && $querylimit === self::CACHE_WINDOW) {
  81. self::cacheSet($cacheKey, $ids);
  82. return array_slice($ids, $offset, $limit);
  83. }
  84. return $ids;
  85. }
  86. /**
  87. * Flush cached blocks when blocks are updated
  88. *
  89. * @param mixed $dataObject Original version of object
  90. *
  91. * @return boolean success flag.
  92. */
  93. function update($dataObject=false)
  94. {
  95. self::blow('qvitterblocked:'.$this->blocker);
  96. self::blow('qvitterblocked:'.$this->blocked);
  97. return parent::update($dataObject);
  98. }
  99. }