1
0

NetworkTimelineService.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <?php
  2. namespace App\Services;
  3. use Illuminate\Support\Facades\Redis;
  4. use App\{
  5. Profile,
  6. Status,
  7. UserFilter
  8. };
  9. class NetworkTimelineService
  10. {
  11. const CACHE_KEY = 'pf:services:timeline:network';
  12. public static function get($start = 0, $stop = 10)
  13. {
  14. if($stop > 100) {
  15. $stop = 100;
  16. }
  17. return Redis::zrevrange(self::CACHE_KEY, $start, $stop);
  18. }
  19. public static function getRankedMaxId($start = null, $limit = 10)
  20. {
  21. if(!$start) {
  22. return [];
  23. }
  24. return array_keys(Redis::zrevrangebyscore(self::CACHE_KEY, $start, '-inf', [
  25. 'withscores' => true,
  26. 'limit' => [1, $limit]
  27. ]));
  28. }
  29. public static function getRankedMinId($end = null, $limit = 10)
  30. {
  31. if(!$end) {
  32. return [];
  33. }
  34. return array_keys(Redis::zrevrangebyscore(self::CACHE_KEY, '+inf', $end, [
  35. 'withscores' => true,
  36. 'limit' => [0, $limit]
  37. ]));
  38. }
  39. public static function add($val)
  40. {
  41. if(self::count() > config('instance.timeline.network.cache_dropoff')) {
  42. Redis::zpopmin(self::CACHE_KEY);
  43. }
  44. return Redis::zadd(self::CACHE_KEY, $val, $val);
  45. }
  46. public static function rem($val)
  47. {
  48. return Redis::zrem(self::CACHE_KEY, $val);
  49. }
  50. public static function del($val)
  51. {
  52. return self::rem($val);
  53. }
  54. public static function count()
  55. {
  56. return Redis::zcard(self::CACHE_KEY);
  57. }
  58. public static function deleteByProfileId($profileId)
  59. {
  60. $res = Redis::zrange(self::CACHE_KEY, 0, '-1');
  61. if(!$res) {
  62. return;
  63. }
  64. foreach($res as $postId) {
  65. $s = StatusService::get($postId);
  66. if(!$s) {
  67. self::rem($postId);
  68. continue;
  69. }
  70. if($s['account']['id'] == $profileId) {
  71. self::rem($postId);
  72. }
  73. }
  74. return;
  75. }
  76. public static function warmCache($force = false, $limit = 100)
  77. {
  78. if(self::count() == 0 || $force == true) {
  79. $hideNsfw = config('instance.hide_nsfw_on_public_feeds');
  80. Redis::del(self::CACHE_KEY);
  81. $filteredDomains = collect(InstanceService::getBannedDomains())
  82. ->merge(InstanceService::getUnlistedDomains())
  83. ->unique()
  84. ->values()
  85. ->toArray();
  86. $ids = Status::whereNotNull('uri')
  87. ->whereScope('public')
  88. ->when($hideNsfw, function($q, $hideNsfw) {
  89. return $q->where('is_nsfw', false);
  90. })
  91. ->whereNull('in_reply_to_id')
  92. ->whereNull('reblog_of_id')
  93. ->whereIn('type', ['photo', 'photo:album', 'video', 'video:album', 'photo:video:album'])
  94. ->where('created_at', '>', now()->subHours(config('instance.timeline.network.max_hours_old')))
  95. ->orderByDesc('created_at')
  96. ->limit($limit)
  97. ->pluck('uri', 'id');
  98. $ids = $ids->filter(function($k, $v) use($filteredDomains) {
  99. $domain = parse_url($k, PHP_URL_HOST);
  100. return !in_array($domain, $filteredDomains);
  101. })->map(function($k, $v) {
  102. return $v;
  103. })->flatten();
  104. foreach($ids as $id) {
  105. self::add($id);
  106. }
  107. return 1;
  108. }
  109. return 0;
  110. }
  111. }