RelationshipService.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. namespace App\Services;
  3. use App\Follower;
  4. use App\FollowRequest;
  5. use App\UserFilter;
  6. use Illuminate\Support\Facades\Cache;
  7. class RelationshipService
  8. {
  9. const CACHE_KEY = 'pf:services:urel:';
  10. public static function get($aid, $tid)
  11. {
  12. $actor = AccountService::get($aid, true);
  13. $target = AccountService::get($tid, true);
  14. if (! $actor || ! $target) {
  15. return self::defaultRelation($tid);
  16. }
  17. if ($actor['id'] === $target['id']) {
  18. return self::defaultRelation($tid);
  19. }
  20. return Cache::remember(self::key("a_{$aid}:t_{$tid}"), 1209600, function () use ($aid, $tid) {
  21. return [
  22. 'id' => (string) $tid,
  23. 'following' => Follower::whereProfileId($aid)->whereFollowingId($tid)->exists(),
  24. 'followed_by' => Follower::whereProfileId($tid)->whereFollowingId($aid)->exists(),
  25. 'blocking' => UserFilter::whereUserId($aid)
  26. ->whereFilterableType('App\Profile')
  27. ->whereFilterableId($tid)
  28. ->whereFilterType('block')
  29. ->exists(),
  30. 'muting' => UserFilter::whereUserId($aid)
  31. ->whereFilterableType('App\Profile')
  32. ->whereFilterableId($tid)
  33. ->whereFilterType('mute')
  34. ->exists(),
  35. 'muting_notifications' => false,
  36. 'requested' => FollowRequest::whereFollowerId($aid)
  37. ->whereFollowingId($tid)
  38. ->exists(),
  39. 'domain_blocking' => false,
  40. 'showing_reblogs' => false,
  41. 'endorsed' => false,
  42. ];
  43. });
  44. }
  45. public static function delete($aid, $tid)
  46. {
  47. Cache::forget(self::key("wd:a_{$aid}:t_{$tid}"));
  48. return Cache::forget(self::key("a_{$aid}:t_{$tid}"));
  49. }
  50. public static function refresh($aid, $tid)
  51. {
  52. Cache::forget('pf:services:follower:audience:'.$aid);
  53. Cache::forget('pf:services:follower:audience:'.$tid);
  54. self::delete($tid, $aid);
  55. self::delete($aid, $tid);
  56. self::get($tid, $aid);
  57. return self::get($aid, $tid);
  58. }
  59. public static function forget($aid, $tid)
  60. {
  61. Cache::forget('pf:services:follower:audience:'.$aid);
  62. Cache::forget('pf:services:follower:audience:'.$tid);
  63. self::delete($tid, $aid);
  64. self::delete($aid, $tid);
  65. }
  66. public static function defaultRelation($tid)
  67. {
  68. return [
  69. 'id' => (string) $tid,
  70. 'following' => false,
  71. 'followed_by' => false,
  72. 'blocking' => false,
  73. 'muting' => false,
  74. 'muting_notifications' => false,
  75. 'requested' => false,
  76. 'domain_blocking' => false,
  77. 'showing_reblogs' => false,
  78. 'endorsed' => false,
  79. ];
  80. }
  81. protected static function key($suffix)
  82. {
  83. return self::CACHE_KEY.$suffix;
  84. }
  85. public static function getWithDate($aid, $tid)
  86. {
  87. $res = self::get($aid, $tid);
  88. if (! $res || ! $res['following']) {
  89. $res['following_since'] = null;
  90. return $res;
  91. }
  92. return Cache::remember(self::key("wd:a_{$aid}:t_{$tid}"), 1209600, function () use ($aid, $tid, $res) {
  93. $tmp = Follower::whereProfileId($aid)->whereFollowingId($tid)->first();
  94. if (! $tmp) {
  95. $res['following_since'] = null;
  96. return $res;
  97. }
  98. $res['following_since'] = str_replace('+00:00', 'Z', $tmp->created_at->format(DATE_RFC3339_EXTENDED));
  99. return $res;
  100. });
  101. }
  102. }