AccountService.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. <?php
  2. namespace App\Services;
  3. use Cache;
  4. use App\Profile;
  5. use App\Status;
  6. use App\User;
  7. use App\UserSetting;
  8. use App\Transformer\Api\AccountTransformer;
  9. use League\Fractal;
  10. use League\Fractal\Serializer\ArraySerializer;
  11. use Illuminate\Support\Facades\DB;
  12. use Illuminate\Support\Str;
  13. class AccountService
  14. {
  15. const CACHE_KEY = 'pf:services:account:';
  16. public static function get($id, $softFail = false)
  17. {
  18. return Cache::remember(self::CACHE_KEY . $id, 43200, function() use($id, $softFail) {
  19. $fractal = new Fractal\Manager();
  20. $fractal->setSerializer(new ArraySerializer());
  21. $profile = Profile::find($id);
  22. if(!$profile) {
  23. if($softFail) {
  24. return null;
  25. }
  26. abort(404);
  27. }
  28. $resource = new Fractal\Resource\Item($profile, new AccountTransformer());
  29. return $fractal->createData($resource)->toArray();
  30. });
  31. }
  32. public static function getMastodon($id, $softFail = false)
  33. {
  34. $account = self::get($id, $softFail);
  35. if(!$account) {
  36. return null;
  37. }
  38. if(config('exp.emc') == false) {
  39. return $account;
  40. }
  41. unset(
  42. $account['header_bg'],
  43. $account['is_admin'],
  44. $account['last_fetched_at'],
  45. $account['local'],
  46. $account['location'],
  47. $account['note_text'],
  48. $account['pronouns'],
  49. $account['website']
  50. );
  51. $account['avatar_static'] = $account['avatar'];
  52. $account['bot'] = false;
  53. $account['emojis'] = [];
  54. $account['fields'] = [];
  55. $account['header'] = url('/storage/headers/missing.png');
  56. $account['header_static'] = url('/storage/headers/missing.png');
  57. $account['last_status_at'] = null;
  58. return $account;
  59. }
  60. public static function del($id)
  61. {
  62. return Cache::forget(self::CACHE_KEY . $id);
  63. }
  64. public static function settings($id)
  65. {
  66. $settings = UserSetting::whereUserId($id)->first();
  67. if(!$settings) {
  68. return self::defaultSettings();
  69. }
  70. return collect($settings)
  71. ->filter(function($item, $key) {
  72. return in_array($key, array_keys(self::defaultSettings())) == true;
  73. })
  74. ->map(function($item, $key) {
  75. if($key == 'compose_settings') {
  76. $cs = self::defaultSettings()['compose_settings'];
  77. $ms = is_array($item) ? $item : [];
  78. return array_merge($cs, $ms);
  79. }
  80. if($key == 'other') {
  81. $other = self::defaultSettings()['other'];
  82. $mo = is_array($item) ? $item : [];
  83. return array_merge($other, $mo);
  84. }
  85. return $item;
  86. });
  87. }
  88. public static function canEmbed($id)
  89. {
  90. return self::settings($id)['other']['disable_embeds'] == false;
  91. }
  92. public static function defaultSettings()
  93. {
  94. return [
  95. 'crawlable' => true,
  96. 'public_dm' => false,
  97. 'reduce_motion' => false,
  98. 'high_contrast_mode' => false,
  99. 'video_autoplay' => false,
  100. 'show_profile_follower_count' => true,
  101. 'show_profile_following_count' => true,
  102. 'compose_settings' => [
  103. 'default_scope' => 'public',
  104. 'default_license' => 1,
  105. 'media_descriptions' => false
  106. ],
  107. 'other' => [
  108. 'advanced_atom' => false,
  109. 'disable_embeds' => false,
  110. 'mutual_mention_notifications' => false,
  111. 'hide_collections' => false,
  112. 'hide_like_counts' => false,
  113. 'hide_groups' => false,
  114. 'hide_stories' => false,
  115. 'disable_cw' => false,
  116. ]
  117. ];
  118. }
  119. public static function syncPostCount($id)
  120. {
  121. $profile = Profile::find($id);
  122. if(!$profile) {
  123. return false;
  124. }
  125. $key = self::CACHE_KEY . 'pcs:' . $id;
  126. if(Cache::has($key)) {
  127. return;
  128. }
  129. $count = Status::whereProfileId($id)
  130. ->whereNull('in_reply_to_id')
  131. ->whereNull('reblog_of_id')
  132. ->whereIn('scope', ['public', 'unlisted', 'private'])
  133. ->count();
  134. $profile->status_count = $count;
  135. $profile->save();
  136. Cache::put($key, 1, 900);
  137. return true;
  138. }
  139. public static function usernameToId($username)
  140. {
  141. $key = self::CACHE_KEY . 'u2id:' . hash('sha256', $username);
  142. return Cache::remember($key, 900, function() use($username) {
  143. $s = Str::of($username);
  144. if($s->contains('@') && !$s->startsWith('@')) {
  145. $username = "@{$username}";
  146. }
  147. $profile = DB::table('profiles')
  148. ->whereUsername($username)
  149. ->first();
  150. if(!$profile) {
  151. return null;
  152. }
  153. return (string) $profile->id;
  154. });
  155. }
  156. public static function hiddenFollowers($id)
  157. {
  158. $account = self::get($id, true);
  159. if(!$account || !isset($account['local']) || $account['local'] == false) {
  160. return false;
  161. }
  162. return Cache::remember('pf:acct:settings:hidden-followers:' . $id, 43200, function() use($id) {
  163. $user = User::whereProfileId($id)->first();
  164. if(!$user) {
  165. return false;
  166. }
  167. $settings = UserSetting::whereUserId($user->id)->first();
  168. if($settings) {
  169. return $settings->show_profile_follower_count == false;
  170. }
  171. return false;
  172. });
  173. }
  174. public static function hiddenFollowing($id)
  175. {
  176. $account = self::get($id, true);
  177. if(!$account || !isset($account['local']) || $account['local'] == false) {
  178. return false;
  179. }
  180. return Cache::remember('pf:acct:settings:hidden-following:' . $id, 43200, function() use($id) {
  181. $user = User::whereProfileId($id)->first();
  182. if(!$user) {
  183. return false;
  184. }
  185. $settings = UserSetting::whereUserId($user->id)->first();
  186. if($settings) {
  187. return $settings->show_profile_following_count == false;
  188. }
  189. return false;
  190. });
  191. }
  192. }