PrivacySettings.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. <?php
  2. namespace App\Http\Controllers\Settings;
  3. use App\Follower;
  4. use App\Profile;
  5. use App\Services\RelationshipService;
  6. use App\UserFilter;
  7. use Auth;
  8. use Cache;
  9. use DB;
  10. use Illuminate\Http\Request;
  11. trait PrivacySettings
  12. {
  13. public function privacy()
  14. {
  15. $user = Auth::user();
  16. $settings = $user->settings;
  17. $profile = $user->profile;
  18. $is_private = $profile->is_private;
  19. $settings['is_private'] = (bool) $is_private;
  20. return view('settings.privacy', compact('settings', 'profile'));
  21. }
  22. public function privacyStore(Request $request)
  23. {
  24. $settings = $request->user()->settings;
  25. $profile = $request->user()->profile;
  26. $fields = [
  27. 'is_private',
  28. 'crawlable',
  29. 'public_dm',
  30. 'show_profile_follower_count',
  31. 'show_profile_following_count',
  32. 'indexable',
  33. 'show_atom',
  34. ];
  35. $profile->indexable = $request->input('indexable') == 'on';
  36. $profile->is_suggestable = $request->input('is_suggestable') == 'on';
  37. $profile->save();
  38. foreach ($fields as $field) {
  39. $form = $request->input($field);
  40. if ($field == 'is_private') {
  41. if ($form == 'on') {
  42. $profile->{$field} = true;
  43. $settings->show_guests = false;
  44. $settings->show_discover = false;
  45. $profile->save();
  46. } else {
  47. $profile->{$field} = false;
  48. $profile->save();
  49. }
  50. Cache::forget('profiles:private');
  51. } elseif ($field == 'crawlable') {
  52. if ($form == 'on') {
  53. $settings->{$field} = false;
  54. } else {
  55. $settings->{$field} = true;
  56. }
  57. } elseif ($field == 'public_dm') {
  58. if ($form == 'on') {
  59. $settings->{$field} = true;
  60. } else {
  61. $settings->{$field} = false;
  62. }
  63. } elseif ($field == 'indexable') {
  64. } else {
  65. if ($form == 'on') {
  66. $settings->{$field} = true;
  67. } else {
  68. $settings->{$field} = false;
  69. }
  70. }
  71. $settings->save();
  72. }
  73. $pid = $profile->id;
  74. Cache::forget('profile:settings:'.$pid);
  75. Cache::forget('user:account:id:'.$profile->user_id);
  76. Cache::forget('profile:follower_count:'.$pid);
  77. Cache::forget('profile:following_count:'.$pid);
  78. Cache::forget('profile:atom:enabled:'.$pid);
  79. Cache::forget('profile:embed:'.$pid);
  80. Cache::forget('pf:acct:settings:hidden-followers:'.$pid);
  81. Cache::forget('pf:acct:settings:hidden-following:'.$pid);
  82. Cache::forget('pf:acct-trans:hideFollowing:'.$pid);
  83. Cache::forget('pf:acct-trans:hideFollowers:'.$pid);
  84. Cache::forget('pfc:cached-user:wt:'.strtolower($profile->username));
  85. Cache::forget('pfc:cached-user:wot:'.strtolower($profile->username));
  86. return redirect(route('settings.privacy'))->with('status', 'Settings successfully updated!');
  87. }
  88. public function mutedUsers()
  89. {
  90. $pid = Auth::user()->profile->id;
  91. $ids = (new UserFilter())->mutedUserIds($pid);
  92. $users = Profile::whereIn('id', $ids)->simplePaginate(15);
  93. return view('settings.privacy.muted', compact('users'));
  94. }
  95. public function mutedUsersUpdate(Request $request)
  96. {
  97. $this->validate($request, [
  98. 'profile_id' => 'required|integer|min:1',
  99. ]);
  100. $fid = $request->input('profile_id');
  101. $pid = Auth::user()->profile->id;
  102. DB::transaction(function () use ($fid, $pid) {
  103. $filter = UserFilter::whereUserId($pid)
  104. ->whereFilterableId($fid)
  105. ->whereFilterableType('App\Profile')
  106. ->whereFilterType('mute')
  107. ->firstOrFail();
  108. $filter->delete();
  109. });
  110. RelationshipService::refresh($pid, $fid);
  111. return redirect()->back();
  112. }
  113. public function blockedUsers()
  114. {
  115. $pid = Auth::user()->profile->id;
  116. $ids = (new UserFilter())->blockedUserIds($pid);
  117. $users = Profile::whereIn('id', $ids)->simplePaginate(15);
  118. return view('settings.privacy.blocked', compact('users'));
  119. }
  120. public function blockedUsersUpdate(Request $request)
  121. {
  122. $this->validate($request, [
  123. 'profile_id' => 'required|integer|min:1',
  124. ]);
  125. $fid = $request->input('profile_id');
  126. $pid = Auth::user()->profile->id;
  127. DB::transaction(function () use ($fid, $pid) {
  128. $filter = UserFilter::whereUserId($pid)
  129. ->whereFilterableId($fid)
  130. ->whereFilterableType('App\Profile')
  131. ->whereFilterType('block')
  132. ->firstOrFail();
  133. $filter->delete();
  134. });
  135. RelationshipService::refresh($pid, $fid);
  136. return redirect()->back();
  137. }
  138. public function blockedInstances()
  139. {
  140. // deprecated
  141. abort(404);
  142. }
  143. public function domainBlocks()
  144. {
  145. return view('settings.privacy.domain-blocks');
  146. }
  147. public function blockedInstanceStore(Request $request)
  148. {
  149. // deprecated
  150. abort(404);
  151. }
  152. public function blockedInstanceUnblock(Request $request)
  153. {
  154. // deprecated
  155. abort(404);
  156. }
  157. public function blockedKeywords()
  158. {
  159. return view('settings.privacy.blocked-keywords');
  160. }
  161. public function privateAccountOptions(Request $request)
  162. {
  163. $this->validate($request, [
  164. 'mode' => 'required|string|in:keep-all,mutual-only,only-followers,remove-all',
  165. 'duration' => 'required|integer|min:60|max:525600',
  166. ]);
  167. $mode = $request->input('mode');
  168. $duration = $request->input('duration');
  169. // $newRequests = $request->input('newrequests');
  170. $profile = Auth::user()->profile;
  171. $settings = Auth::user()->settings;
  172. if ($mode !== 'keep-all') {
  173. switch ($mode) {
  174. case 'mutual-only':
  175. $following = $profile->following()->pluck('profiles.id');
  176. Follower::whereFollowingId($profile->id)->whereNotIn('profile_id', $following)->delete();
  177. break;
  178. case 'only-followers':
  179. $ts = now()->subMinutes($duration);
  180. Follower::whereFollowingId($profile->id)->where('created_at', '>', $ts)->delete();
  181. break;
  182. case 'remove-all':
  183. Follower::whereFollowingId($profile->id)->delete();
  184. break;
  185. default:
  186. // code...
  187. break;
  188. }
  189. }
  190. $profile->is_private = true;
  191. $settings->show_guests = false;
  192. $settings->show_discover = false;
  193. $settings->save();
  194. $profile->save();
  195. Cache::forget('profiles:private');
  196. return [200];
  197. }
  198. }