LikeService.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <?php
  2. namespace App\Services;
  3. use App\Util\ActivityPub\Helpers;
  4. use Illuminate\Support\Facades\Cache;
  5. use Illuminate\Support\Facades\Redis;
  6. use App\Like;
  7. class LikeService {
  8. const CACHE_KEY = 'pf:services:likes:ids:';
  9. const CACHE_SET_KEY = 'pf:services:likes:set:';
  10. public static function add($profileId, $statusId)
  11. {
  12. $key = self::CACHE_KEY . $profileId . ':' . $statusId;
  13. Cache::increment('pf:services:likes:count:'.$statusId);
  14. Cache::forget('pf:services:likes:liked_by:'.$statusId);
  15. self::setAdd($profileId, $statusId);
  16. return Cache::put($key, true, 86400);
  17. }
  18. public static function setAdd($profileId, $statusId)
  19. {
  20. if(self::setCount($profileId) > 400) {
  21. Redis::zpopmin(self::CACHE_SET_KEY . $profileId);
  22. }
  23. return Redis::zadd(self::CACHE_SET_KEY . $profileId, $statusId, $statusId);
  24. }
  25. public static function setCount($id)
  26. {
  27. return Redis::zcard(self::CACHE_SET_KEY . $id);
  28. }
  29. public static function setRem($profileId, $val)
  30. {
  31. return Redis::zrem(self::CACHE_SET_KEY . $profileId, $val);
  32. }
  33. public static function get($profileId, $start = 0, $stop = 10)
  34. {
  35. if($stop > 100) {
  36. $stop = 100;
  37. }
  38. return Redis::zrevrange(self::CACHE_SET_KEY . $profileId, $start, $stop);
  39. }
  40. public static function remove($profileId, $statusId)
  41. {
  42. $key = self::CACHE_KEY . $profileId . ':' . $statusId;
  43. Cache::decrement('pf:services:likes:count:'.$statusId);
  44. Cache::forget('pf:services:likes:liked_by:'.$statusId);
  45. self::setRem($profileId, $statusId);
  46. return Cache::put($key, false, 86400);
  47. }
  48. public static function liked($profileId, $statusId)
  49. {
  50. $key = self::CACHE_KEY . $profileId . ':' . $statusId;
  51. return Cache::remember($key, 86400, function() use($profileId, $statusId) {
  52. return Like::whereProfileId($profileId)->whereStatusId($statusId)->exists();
  53. });
  54. }
  55. public static function likedBy($status)
  56. {
  57. $empty = [
  58. 'username' => null,
  59. 'others' => false
  60. ];
  61. if(!$status) {
  62. return $empty;
  63. }
  64. $res = Cache::remember('pf:services:likes:liked_by:' . $status->id, 86400, function() use($status, $empty) {
  65. $like = Like::whereStatusId($status->id)->first();
  66. if(!$like) {
  67. return $empty;
  68. }
  69. $id = $like->profile_id;
  70. $profile = ProfileService::get($id, true);
  71. if(!$profile) {
  72. return [];
  73. }
  74. $profileUrl = "/i/web/profile/{$profile['id']}";
  75. $res = [
  76. 'id' => (string) $profile['id'],
  77. 'username' => $profile['username'],
  78. 'url' => $profileUrl,
  79. 'others' => $status->likes_count >= 3,
  80. ];
  81. return $res;
  82. });
  83. if(!isset($res['id']) || !isset($res['url'])) {
  84. return $empty;
  85. }
  86. $res['total_count'] = ($status->likes_count - 1);
  87. $res['total_count_pretty'] = number_format($res['total_count']);
  88. return $res;
  89. }
  90. public static function count($id)
  91. {
  92. return Cache::get('pf:services:likes:count:'.$id, 0);
  93. }
  94. }