RelationshipService.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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, true);
  14. $target = AccountService::get($tid, true);
  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. Cache::forget(self::key("wd:a_{$aid}:t_{$tid}"));
  49. return Cache::forget(self::key("a_{$aid}:t_{$tid}"));
  50. }
  51. public static function refresh($aid, $tid)
  52. {
  53. Cache::forget('pf:services:follower:audience:' . $aid);
  54. Cache::forget('pf:services:follower:audience:' . $tid);
  55. self::delete($tid, $aid);
  56. self::delete($aid, $tid);
  57. self::get($tid, $aid);
  58. return self::get($aid, $tid);
  59. }
  60. public static function forget($aid, $tid)
  61. {
  62. Cache::forget('pf:services:follower:audience:' . $aid);
  63. Cache::forget('pf:services:follower:audience:' . $tid);
  64. self::delete($tid, $aid);
  65. self::delete($aid, $tid);
  66. }
  67. public static function defaultRelation($tid)
  68. {
  69. return [
  70. 'id' => (string) $tid,
  71. 'following' => false,
  72. 'followed_by' => false,
  73. 'blocking' => false,
  74. 'muting' => false,
  75. 'muting_notifications' => null,
  76. 'requested' => false,
  77. 'domain_blocking' => null,
  78. 'showing_reblogs' => null,
  79. 'endorsed' => false
  80. ];
  81. }
  82. protected static function key($suffix)
  83. {
  84. return self::CACHE_KEY . $suffix;
  85. }
  86. public static function getWithDate($aid, $tid)
  87. {
  88. $res = self::get($aid, $tid);
  89. if(!$res || !$res['following']) {
  90. $res['following_since'] = null;
  91. return $res;
  92. }
  93. return Cache::remember(self::key("wd:a_{$aid}:t_{$tid}"), 1209600, function() use($aid, $tid, $res) {
  94. $tmp = Follower::whereProfileId($aid)->whereFollowingId($tid)->first();
  95. if(!$tmp) {
  96. $res['following_since'] = null;
  97. return $res;
  98. }
  99. $res['following_since'] = str_replace('+00:00', 'Z', $tmp->created_at->format(DATE_RFC3339_EXTENDED));
  100. return $res;
  101. });
  102. }
  103. }