HomeTimelineService.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. namespace App\Services;
  3. use Illuminate\Support\Facades\Cache;
  4. use Illuminate\Support\Facades\Redis;
  5. use App\Follower;
  6. use App\Status;
  7. use App\Models\UserDomainBlock;
  8. class HomeTimelineService
  9. {
  10. const CACHE_KEY = 'pf:services:timeline:home:';
  11. const FOLLOWER_FEED_POST_LIMIT = 10;
  12. public static function get($id, $start = 0, $stop = 10)
  13. {
  14. if($stop > 100) {
  15. $stop = 100;
  16. }
  17. return Redis::zrevrange(self::CACHE_KEY . $id, $start, $stop);
  18. }
  19. public static function getRankedMaxId($id, $start = null, $limit = 10)
  20. {
  21. if(!$start) {
  22. return [];
  23. }
  24. return array_keys(Redis::zrevrangebyscore(self::CACHE_KEY . $id, $start, '-inf', [
  25. 'withscores' => true,
  26. 'limit' => [1, $limit - 1]
  27. ]));
  28. }
  29. public static function getRankedMinId($id, $end = null, $limit = 10)
  30. {
  31. if(!$end) {
  32. return [];
  33. }
  34. return array_keys(Redis::zrevrangebyscore(self::CACHE_KEY . $id, '+inf', $end, [
  35. 'withscores' => true,
  36. 'limit' => [0, $limit]
  37. ]));
  38. }
  39. public static function add($id, $val)
  40. {
  41. if(self::count($id) >= 400) {
  42. Redis::zpopmin(self::CACHE_KEY . $id);
  43. }
  44. return Redis::zadd(self::CACHE_KEY .$id, $val, $val);
  45. }
  46. public static function rem($id, $val)
  47. {
  48. return Redis::zrem(self::CACHE_KEY . $id, $val);
  49. }
  50. public static function count($id)
  51. {
  52. return Redis::zcard(self::CACHE_KEY . $id);
  53. }
  54. public static function warmCache($id, $force = false, $limit = 100, $returnIds = false)
  55. {
  56. if(self::count($id) == 0 || $force == true) {
  57. Redis::del(self::CACHE_KEY . $id);
  58. $following = Cache::remember('profile:following:'.$id, 1209600, function() use($id) {
  59. $following = Follower::whereProfileId($id)->pluck('following_id');
  60. return $following->push($id)->toArray();
  61. });
  62. $minId = SnowflakeService::byDate(now()->subMonths(6));
  63. $filters = UserFilterService::filters($id);
  64. if($filters && count($filters)) {
  65. $following = array_diff($following, $filters);
  66. }
  67. $domainBlocks = UserDomainBlock::whereProfileId($id)->pluck('domain')->toArray();
  68. $ids = Status::where('id', '>', $minId)
  69. ->whereIn('profile_id', $following)
  70. ->whereNull(['in_reply_to_id', 'reblog_of_id'])
  71. ->whereIn('type', ['photo', 'photo:album', 'video', 'video:album', 'photo:video:album'])
  72. ->whereIn('visibility',['public', 'unlisted', 'private'])
  73. ->orderByDesc('id')
  74. ->limit($limit)
  75. ->pluck('id');
  76. foreach($ids as $pid) {
  77. $status = StatusService::get($pid, false);
  78. if(!$status || !isset($status['account'], $status['url'])) {
  79. continue;
  80. }
  81. if($domainBlocks && count($domainBlocks)) {
  82. $domain = strtolower(parse_url($status['url'], PHP_URL_HOST));
  83. if(in_array($domain, $domainBlocks)) {
  84. continue;
  85. }
  86. }
  87. self::add($id, $pid);
  88. }
  89. return $returnIds ? $ids : 1;
  90. }
  91. return 0;
  92. }
  93. }