PublicTimelineService.php 2.5 KB

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