AccountService.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <?php
  2. namespace App\Services;
  3. use Cache;
  4. use App\Profile;
  5. use App\Status;
  6. use App\UserSetting;
  7. use App\Transformer\Api\AccountTransformer;
  8. use League\Fractal;
  9. use League\Fractal\Serializer\ArraySerializer;
  10. use Illuminate\Support\Facades\DB;
  11. use Illuminate\Support\Str;
  12. class AccountService
  13. {
  14. const CACHE_KEY = 'pf:services:account:';
  15. public static function get($id, $softFail = false)
  16. {
  17. return Cache::remember(self::CACHE_KEY . $id, 43200, function() use($id, $softFail) {
  18. $fractal = new Fractal\Manager();
  19. $fractal->setSerializer(new ArraySerializer());
  20. $profile = Profile::find($id);
  21. if(!$profile) {
  22. if($softFail) {
  23. return null;
  24. }
  25. abort(404);
  26. }
  27. $resource = new Fractal\Resource\Item($profile, new AccountTransformer());
  28. return $fractal->createData($resource)->toArray();
  29. });
  30. }
  31. public static function del($id)
  32. {
  33. return Cache::forget(self::CACHE_KEY . $id);
  34. }
  35. public static function settings($id)
  36. {
  37. $settings = UserSetting::whereUserId($id)->first();
  38. if(!$settings) {
  39. return self::defaultSettings();
  40. }
  41. return collect($settings)
  42. ->filter(function($item, $key) {
  43. return in_array($key, array_keys(self::defaultSettings())) == true;
  44. })
  45. ->map(function($item, $key) {
  46. if($key == 'compose_settings') {
  47. $cs = self::defaultSettings()['compose_settings'];
  48. return array_merge($cs, $item ?? []);
  49. }
  50. if($key == 'other') {
  51. $other = self::defaultSettings()['other'];
  52. return array_merge($other, $item ?? []);
  53. }
  54. return $item;
  55. });
  56. }
  57. public static function canEmbed($id)
  58. {
  59. return self::settings($id)['other']['disable_embeds'] == false;
  60. }
  61. public static function defaultSettings()
  62. {
  63. return [
  64. 'crawlable' => true,
  65. 'public_dm' => false,
  66. 'reduce_motion' => false,
  67. 'high_contrast_mode' => false,
  68. 'video_autoplay' => false,
  69. 'show_profile_follower_count' => true,
  70. 'show_profile_following_count' => true,
  71. 'compose_settings' => [
  72. 'default_scope' => 'public',
  73. 'default_license' => 1,
  74. 'media_descriptions' => false
  75. ],
  76. 'other' => [
  77. 'advanced_atom' => false,
  78. 'disable_embeds' => false,
  79. 'mutual_mention_notifications' => false,
  80. 'hide_collections' => false,
  81. 'hide_like_counts' => false,
  82. 'hide_groups' => false,
  83. 'hide_stories' => false,
  84. 'disable_cw' => false,
  85. ]
  86. ];
  87. }
  88. public static function syncPostCount($id)
  89. {
  90. $profile = Profile::find($id);
  91. if(!$profile) {
  92. return false;
  93. }
  94. $key = self::CACHE_KEY . 'pcs:' . $id;
  95. if(Cache::has($key)) {
  96. return;
  97. }
  98. $count = Status::whereProfileId($id)
  99. ->whereNull('in_reply_to_id')
  100. ->whereNull('reblog_of_id')
  101. ->whereIn('scope', ['public', 'unlisted', 'private'])
  102. ->count();
  103. $profile->status_count = $count;
  104. $profile->save();
  105. Cache::put($key, 1, 900);
  106. return true;
  107. }
  108. public static function usernameToId($username)
  109. {
  110. $key = self::CACHE_KEY . 'u2id:' . hash('sha256', $username);
  111. return Cache::remember($key, 900, function() use($username) {
  112. $s = Str::of($username);
  113. if($s->contains('@') && !$s->startsWith('@')) {
  114. $username = "@{$username}";
  115. }
  116. $profile = DB::table('profiles')
  117. ->whereUsername($username)
  118. ->first();
  119. if(!$profile) {
  120. return null;
  121. }
  122. return (string) $profile->id;
  123. });
  124. }
  125. }