RelationshipService.php 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. <?php
  2. namespace App\Services;
  3. use Illuminate\Support\Facades\Cache;
  4. use App\Follower;
  5. use App\FollowRequest;
  6. use App\Profile;
  7. use App\UserFilter;
  8. class RelationshipService
  9. {
  10. const CACHE_KEY = 'pf:services:urel:';
  11. public static function get($aid, $tid)
  12. {
  13. $actor = AccountService::get($aid);
  14. $target = AccountService::get($tid);
  15. if(!$actor || !$target) {
  16. return self::defaultRelation($tid);
  17. }
  18. if($actor['id'] === $target['id']) {
  19. return self::defaultRelation($tid);
  20. }
  21. return Cache::remember(self::key("a_{$aid}:t_{$tid}"), 1209600, function() use($aid, $tid) {
  22. return [
  23. 'id' => (string) $tid,
  24. 'following' => Follower::whereProfileId($aid)->whereFollowingId($tid)->exists(),
  25. 'followed_by' => Follower::whereProfileId($tid)->whereFollowingId($aid)->exists(),
  26. 'blocking' => UserFilter::whereUserId($aid)
  27. ->whereFilterableType('App\Profile')
  28. ->whereFilterableId($tid)
  29. ->whereFilterType('block')
  30. ->exists(),
  31. 'muting' => UserFilter::whereUserId($aid)
  32. ->whereFilterableType('App\Profile')
  33. ->whereFilterableId($tid)
  34. ->whereFilterType('mute')
  35. ->exists(),
  36. 'muting_notifications' => null,
  37. 'requested' => FollowRequest::whereFollowerId($aid)
  38. ->whereFollowingId($tid)
  39. ->exists(),
  40. 'domain_blocking' => null,
  41. 'showing_reblogs' => null,
  42. 'endorsed' => false
  43. ];
  44. });
  45. }
  46. public static function delete($aid, $tid)
  47. {
  48. return Cache::forget(self::key("a_{$aid}:t_{$tid}"));
  49. }
  50. public static function refresh($aid, $tid)
  51. {
  52. self::delete($tid, $aid);
  53. self::delete($aid, $tid);
  54. self::get($tid, $aid);
  55. return self::get($aid, $tid);
  56. }
  57. public static function defaultRelation($tid)
  58. {
  59. return [
  60. 'id' => (string) $tid,
  61. 'following' => false,
  62. 'followed_by' => false,
  63. 'blocking' => false,
  64. 'muting' => false,
  65. 'muting_notifications' => null,
  66. 'requested' => false,
  67. 'domain_blocking' => null,
  68. 'showing_reblogs' => null,
  69. 'endorsed' => false
  70. ];
  71. }
  72. protected static function key($suffix)
  73. {
  74. return self::CACHE_KEY . $suffix;
  75. }
  76. }