CollectionService.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <?php
  2. namespace App\Services;
  3. use App\Collection;
  4. use App\CollectionItem;
  5. use Illuminate\Support\Facades\Cache;
  6. use Illuminate\Support\Facades\Redis;
  7. class CollectionService
  8. {
  9. const CACHE_KEY = 'pf:services:collections-v1:';
  10. public static function getItems($id, $start = 0, $stop = 10)
  11. {
  12. if(self::count($id)) {
  13. return Redis::zrangebyscore(self::CACHE_KEY . 'items:' . $id, $start, $stop);
  14. }
  15. return self::coldBootItems($id);
  16. }
  17. public static function addItem($id, $sid, $score)
  18. {
  19. Redis::zadd(self::CACHE_KEY . 'items:' . $id, $score, $sid);
  20. }
  21. public static function removeItem($id, $sid)
  22. {
  23. Redis::zrem(self::CACHE_KEY . 'items:' . $id, $sid);
  24. }
  25. public static function clearItems($id)
  26. {
  27. Redis::del(self::CACHE_KEY . 'items:' . $id);
  28. }
  29. public static function coldBootItems($id)
  30. {
  31. return Cache::remember(self::CACHE_KEY . 'items:' . $id, 86400, function() use($id) {
  32. return CollectionItem::whereCollectionId($id)
  33. ->orderBy('order')
  34. ->get()
  35. ->filter(function($item) use($id) {
  36. return StatusService::get($item->object_id) != null;
  37. })
  38. ->each(function($item) use ($id) {
  39. self::addItem($id, $item->object_id, $item->order);
  40. })
  41. ->map(function($item) {
  42. return (string) $item->object_id;
  43. })
  44. ->values()
  45. ->toArray();
  46. });
  47. }
  48. public static function count($id)
  49. {
  50. $count = Redis::zcard(self::CACHE_KEY . 'items:' . $id);
  51. if(!$count) {
  52. self::coldBootItems($id);
  53. $count = Redis::zcard(self::CACHE_KEY . 'items:' . $id);
  54. }
  55. return $count;
  56. }
  57. public static function getCollection($id)
  58. {
  59. $collection = Cache::remember(self::CACHE_KEY . 'get:' . $id, 86400, function() use($id) {
  60. $collection = Collection::find($id);
  61. if(!$collection) {
  62. return false;
  63. }
  64. $account = AccountService::get($collection->profile_id);
  65. if(!$account) {
  66. return false;
  67. }
  68. return [
  69. 'id' => (string) $collection->id,
  70. 'pid' => (string) $collection->profile_id,
  71. 'visibility' => $collection->visibility,
  72. 'title' => $collection->title,
  73. 'description' => $collection->description,
  74. 'thumb' => url('/storage/no-preview.png'),
  75. 'url' => $collection->url(),
  76. 'updated_at' => $collection->updated_at,
  77. 'published_at' => $collection->published_at,
  78. ];
  79. });
  80. if($collection) {
  81. $account = AccountService::get($collection['pid']);
  82. if(!$account) {
  83. return false;
  84. }
  85. $collection['avatar'] = $account['avatar'];
  86. $collection['username'] = $account['username'];
  87. $collection['thumb'] = self::getThumb($id);
  88. $collection['post_count'] = self::count($id);
  89. }
  90. return $collection;
  91. }
  92. public static function setCollection($id, $collection)
  93. {
  94. $account = AccountService::get($collection->profile_id);
  95. if(!$account) {
  96. return false;
  97. }
  98. $res = [
  99. 'id' => (string) $collection->id,
  100. 'pid' => (string) $collection->profile_id,
  101. 'visibility' => $collection->visibility,
  102. 'title' => $collection->title,
  103. 'description' => $collection->description,
  104. 'thumb' => self::getThumb($id),
  105. 'url' => $collection->url(),
  106. 'updated_at' => $collection->updated_at,
  107. 'published_at' => $collection->published_at
  108. ];
  109. Cache::put(self::CACHE_KEY . 'get:' . $id, $res, 86400);
  110. $res['post_count'] = self::count($id);
  111. return $res;
  112. }
  113. public static function deleteCollection($id)
  114. {
  115. Redis::del(self::CACHE_KEY . 'items:' . $id);
  116. Cache::forget(self::CACHE_KEY . 'get:' . $id);
  117. }
  118. public static function getThumb($id)
  119. {
  120. $item = self::getItems($id, 0, 1);
  121. if(!$item || empty($item)) {
  122. return url('/storage/no-preview.png');
  123. }
  124. $status = StatusService::get($item[0]);
  125. if(!$status) {
  126. return url('/storage/no-preview.png');
  127. }
  128. if(!isset($status['media_attachments']) || empty($status['media_attachments'])) {
  129. return url('/storage/no-preview.png');
  130. }
  131. return $status['media_attachments'][0]['url'];
  132. }
  133. }