UserFilterService.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. <?php
  2. namespace App\Services;
  3. use Cache;
  4. use App\UserFilter;
  5. use App\Models\UserDomainBlock;
  6. use Illuminate\Support\Facades\Redis;
  7. class UserFilterService
  8. {
  9. const USER_MUTES_KEY = 'pf:services:mutes:ids:';
  10. const USER_BLOCKS_KEY = 'pf:services:blocks:ids:';
  11. const USER_DOMAIN_KEY = 'pf:services:domain-blocks:ids:';
  12. public static function mutes(int $profile_id)
  13. {
  14. $key = self::USER_MUTES_KEY . $profile_id;
  15. $warm = Cache::has($key . ':cached-v0');
  16. if($warm) {
  17. return Redis::zrevrange($key, 0, -1) ?? [];
  18. } else {
  19. if(Redis::zrevrange($key, 0, -1)) {
  20. return Redis::zrevrange($key, 0, -1);
  21. }
  22. $ids = UserFilter::whereFilterType('mute')
  23. ->whereUserId($profile_id)
  24. ->pluck('filterable_id')
  25. ->map(function($id) {
  26. $acct = AccountService::get($id, true);
  27. if(!$acct) {
  28. return false;
  29. }
  30. return $acct['id'];
  31. })
  32. ->filter(function($res) {
  33. return $res;
  34. })
  35. ->values()
  36. ->toArray();
  37. foreach ($ids as $muted_id) {
  38. Redis::zadd($key, (int) $muted_id, (int) $muted_id);
  39. }
  40. Cache::set($key . ':cached-v0', 1, 7776000);
  41. return $ids;
  42. }
  43. }
  44. public static function blocks(int $profile_id)
  45. {
  46. $key = self::USER_BLOCKS_KEY . $profile_id;
  47. $warm = Cache::has($key . ':cached-v0');
  48. if($warm) {
  49. return Redis::zrevrange($key, 0, -1) ?? [];
  50. } else {
  51. if(Redis::zrevrange($key, 0, -1)) {
  52. return Redis::zrevrange($key, 0, -1);
  53. }
  54. $ids = UserFilter::whereFilterType('block')
  55. ->whereUserId($profile_id)
  56. ->pluck('filterable_id')
  57. ->map(function($id) {
  58. $acct = AccountService::get($id, true);
  59. if(!$acct) {
  60. return false;
  61. }
  62. return $acct['id'];
  63. })
  64. ->filter(function($res) {
  65. return $res;
  66. })
  67. ->values()
  68. ->toArray();
  69. foreach ($ids as $blocked_id) {
  70. Redis::zadd($key, (int) $blocked_id, (int) $blocked_id);
  71. }
  72. Cache::set($key . ':cached-v0', 1, 7776000);
  73. return $ids;
  74. }
  75. }
  76. public static function filters(int $profile_id)
  77. {
  78. return array_unique(array_merge(self::mutes($profile_id), self::blocks($profile_id)));
  79. }
  80. public static function mute(int $profile_id, int $muted_id)
  81. {
  82. if($profile_id == $muted_id) {
  83. return false;
  84. }
  85. $key = self::USER_MUTES_KEY . $profile_id;
  86. $mutes = self::mutes($profile_id);
  87. $exists = in_array($muted_id, $mutes);
  88. if(!$exists) {
  89. Redis::zadd($key, $muted_id, $muted_id);
  90. }
  91. return true;
  92. }
  93. public static function unmute(int $profile_id, string $muted_id)
  94. {
  95. if($profile_id == $muted_id) {
  96. return false;
  97. }
  98. $key = self::USER_MUTES_KEY . $profile_id;
  99. $mutes = self::mutes($profile_id);
  100. $exists = in_array($muted_id, $mutes);
  101. if($exists) {
  102. Redis::zrem($key, $muted_id);
  103. }
  104. return true;
  105. }
  106. public static function block(int $profile_id, int $blocked_id)
  107. {
  108. if($profile_id == $blocked_id) {
  109. return false;
  110. }
  111. $key = self::USER_BLOCKS_KEY . $profile_id;
  112. $exists = in_array($blocked_id, self::blocks($profile_id));
  113. if(!$exists) {
  114. Redis::zadd($key, $blocked_id, $blocked_id);
  115. }
  116. return true;
  117. }
  118. public static function unblock(int $profile_id, string $blocked_id)
  119. {
  120. if($profile_id == $blocked_id) {
  121. return false;
  122. }
  123. $key = self::USER_BLOCKS_KEY . $profile_id;
  124. $exists = in_array($blocked_id, self::blocks($profile_id));
  125. if($exists) {
  126. Redis::zrem($key, $blocked_id);
  127. }
  128. return $exists;
  129. }
  130. public static function blockCount(int $profile_id)
  131. {
  132. return Redis::zcard(self::USER_BLOCKS_KEY . $profile_id);
  133. }
  134. public static function muteCount(int $profile_id)
  135. {
  136. return Redis::zcard(self::USER_MUTES_KEY . $profile_id);
  137. }
  138. public static function domainBlocks($pid, $purge = false)
  139. {
  140. if($purge) {
  141. Cache::forget(self::USER_DOMAIN_KEY . $pid);
  142. }
  143. return Cache::remember(
  144. self::USER_DOMAIN_KEY . $pid,
  145. 21600,
  146. function() use($pid) {
  147. return UserDomainBlock::whereProfileId($pid)->pluck('domain')->toArray();
  148. });
  149. }
  150. }