CollectionService.php 3.7 KB

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