UserFilterService.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php
  2. namespace App\Services;
  3. use Cache;
  4. use Illuminate\Support\Facades\Redis;
  5. use App\{
  6. Follower,
  7. Profile,
  8. UserFilter
  9. };
  10. class UserFilterService {
  11. const USER_MUTES_KEY = 'pf:services:mutes:ids:';
  12. const USER_BLOCKS_KEY = 'pf:services:blocks:ids:';
  13. public static function mutes(int $profile_id) : array
  14. {
  15. $key = self::USER_MUTES_KEY . $profile_id;
  16. $cached = Redis::zrevrange($key, 0, -1);
  17. if($cached) {
  18. return $cached;
  19. } else {
  20. $ids = UserFilter::whereFilterType('mute')
  21. ->whereUserId($profile_id)
  22. ->pluck('filterable_id')
  23. ->toArray();
  24. foreach ($ids as $muted_id) {
  25. Redis::zadd($key, (int) $muted_id, (int) $muted_id);
  26. }
  27. return $ids;
  28. }
  29. }
  30. public static function blocks(int $profile_id) : array
  31. {
  32. $key = self::USER_BLOCKS_KEY . $profile_id;
  33. $cached = Redis::zrevrange($key, 0, -1);
  34. if($cached) {
  35. return $cached;
  36. } else {
  37. $ids = UserFilter::whereFilterType('block')
  38. ->whereUserId($profile_id)
  39. ->pluck('filterable_id')
  40. ->toArray();
  41. foreach ($ids as $blocked_id) {
  42. Redis::zadd($key, $blocked_id, $blocked_id);
  43. }
  44. return $ids;
  45. }
  46. }
  47. public static function filters(int $profile_id) : array
  48. {
  49. return array_unique(array_merge(self::mutes($profile_id), self::blocks($profile_id)));
  50. }
  51. public static function mute(int $profile_id, int $muted_id)
  52. {
  53. $key = self::USER_MUTES_KEY . $profile_id;
  54. $mutes = self::mutes($profile_id);
  55. $exists = in_array($muted_id, $mutes);
  56. if(!$exists) {
  57. Redis::zadd($key, $muted_id, $muted_id);
  58. }
  59. return true;
  60. }
  61. public static function unmute(int $profile_id, string $muted_id)
  62. {
  63. $key = self::USER_MUTES_KEY . $profile_id;
  64. $mutes = self::mutes($profile_id);
  65. $exists = in_array($muted_id, $mutes);
  66. if($exists) {
  67. Redis::zrem($key, $muted_id);
  68. }
  69. return true;
  70. }
  71. public static function block(int $profile_id, int $blocked_id)
  72. {
  73. $key = self::USER_BLOCKS_KEY . $profile_id;
  74. $exists = in_array($blocked_id, self::blocks($profile_id));
  75. if(!$exists) {
  76. Redis::zadd($key, $blocked_id, $blocked_id);
  77. }
  78. return true;
  79. }
  80. public static function unblock(int $profile_id, string $blocked_id)
  81. {
  82. $key = self::USER_BLOCKS_KEY . $profile_id;
  83. $exists = in_array($blocked_id, self::blocks($profile_id));
  84. if($exists) {
  85. Redis::zrem($key, $blocked_id);
  86. }
  87. return $exists;
  88. }
  89. public static function blockCount(int $profile_id)
  90. {
  91. return Redis::zcard(self::USER_BLOCKS_KEY . $profile_id);
  92. }
  93. public static function muteCount(int $profile_id)
  94. {
  95. return Redis::zcard(self::USER_MUTES_KEY . $profile_id);
  96. }
  97. }