AccountController.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\EmailVerification;
  4. use App\Follower;
  5. use App\FollowRequest;
  6. use App\Jobs\FollowPipeline\FollowPipeline;
  7. use App\Mail\ConfirmEmail;
  8. use App\Notification;
  9. use App\Profile;
  10. use App\User;
  11. use App\UserFilter;
  12. use Auth;
  13. use Cache;
  14. use Carbon\Carbon;
  15. use Illuminate\Http\Request;
  16. use Mail;
  17. use Redis;
  18. use PragmaRX\Google2FA\Google2FA;
  19. class AccountController extends Controller
  20. {
  21. protected $filters = [
  22. 'user.mute',
  23. 'user.block',
  24. ];
  25. public function __construct()
  26. {
  27. $this->middleware('auth');
  28. }
  29. public function notifications(Request $request)
  30. {
  31. $this->validate($request, [
  32. 'page' => 'nullable|min:1|max:3',
  33. 'a' => 'nullable|alpha_dash',
  34. ]);
  35. $profile = Auth::user()->profile;
  36. $action = $request->input('a');
  37. $timeago = Carbon::now()->subMonths(6);
  38. if ($action && in_array($action, ['comment', 'follow', 'mention'])) {
  39. $notifications = Notification::whereProfileId($profile->id)
  40. ->whereAction($action)
  41. ->whereDate('created_at', '>', $timeago)
  42. ->orderBy('id', 'desc')
  43. ->simplePaginate(30);
  44. } else {
  45. $notifications = Notification::whereProfileId($profile->id)
  46. ->whereDate('created_at', '>', $timeago)
  47. ->orderBy('id', 'desc')
  48. ->simplePaginate(30);
  49. }
  50. return view('account.activity', compact('profile', 'notifications'));
  51. }
  52. public function followingActivity(Request $request)
  53. {
  54. $this->validate($request, [
  55. 'page' => 'nullable|min:1|max:3',
  56. 'a' => 'nullable|alpha_dash',
  57. ]);
  58. $profile = Auth::user()->profile;
  59. $action = $request->input('a');
  60. $timeago = Carbon::now()->subMonths(1);
  61. $following = $profile->following->pluck('id');
  62. $notifications = Notification::whereIn('actor_id', $following)
  63. ->where('profile_id', '!=', $profile->id)
  64. ->whereDate('created_at', '>', $timeago)
  65. ->orderBy('notifications.id', 'desc')
  66. ->simplePaginate(30);
  67. return view('account.following', compact('profile', 'notifications'));
  68. }
  69. public function verifyEmail(Request $request)
  70. {
  71. return view('account.verify_email');
  72. }
  73. public function sendVerifyEmail(Request $request)
  74. {
  75. $timeLimit = Carbon::now()->subDays(1)->toDateTimeString();
  76. $recentAttempt = EmailVerification::whereUserId(Auth::id())
  77. ->where('created_at', '>', $timeLimit)->count();
  78. $exists = EmailVerification::whereUserId(Auth::id())->count();
  79. if ($recentAttempt == 1 && $exists == 1) {
  80. return redirect()->back()->with('error', 'A verification email has already been sent recently. Please check your email, or try again later.');
  81. } elseif ($recentAttempt == 0 && $exists !== 0) {
  82. // Delete old verification and send new one.
  83. EmailVerification::whereUserId(Auth::id())->delete();
  84. }
  85. $user = User::whereNull('email_verified_at')->find(Auth::id());
  86. $utoken = hash('sha512', $user->id);
  87. $rtoken = str_random(40);
  88. $verify = new EmailVerification();
  89. $verify->user_id = $user->id;
  90. $verify->email = $user->email;
  91. $verify->user_token = $utoken;
  92. $verify->random_token = $rtoken;
  93. $verify->save();
  94. Mail::to($user->email)->send(new ConfirmEmail($verify));
  95. return redirect()->back()->with('status', 'Verification email sent!');
  96. }
  97. public function confirmVerifyEmail(Request $request, $userToken, $randomToken)
  98. {
  99. $verify = EmailVerification::where('user_token', $userToken)
  100. ->where('random_token', $randomToken)
  101. ->firstOrFail();
  102. if (Auth::id() === $verify->user_id) {
  103. $user = User::find(Auth::id());
  104. $user->email_verified_at = Carbon::now();
  105. $user->save();
  106. return redirect('/');
  107. }
  108. }
  109. public function fetchNotifications($id)
  110. {
  111. $key = config('cache.prefix').":user.{$id}.notifications";
  112. $redis = Redis::connection();
  113. $notifications = $redis->lrange($key, 0, 30);
  114. if (empty($notifications)) {
  115. $notifications = Notification::whereProfileId($id)
  116. ->orderBy('id', 'desc')->take(30)->get();
  117. } else {
  118. $notifications = $this->hydrateNotifications($notifications);
  119. }
  120. return $notifications;
  121. }
  122. public function hydrateNotifications($keys)
  123. {
  124. $prefix = 'notification.';
  125. $notifications = collect([]);
  126. foreach ($keys as $key) {
  127. $notifications->push(Cache::get("{$prefix}{$key}"));
  128. }
  129. return $notifications;
  130. }
  131. public function messages()
  132. {
  133. return view('account.messages');
  134. }
  135. public function showMessage(Request $request, $id)
  136. {
  137. return view('account.message');
  138. }
  139. public function mute(Request $request)
  140. {
  141. $this->validate($request, [
  142. 'type' => 'required|string',
  143. 'item' => 'required|integer|min:1',
  144. ]);
  145. $user = Auth::user()->profile;
  146. $type = $request->input('type');
  147. $item = $request->input('item');
  148. $action = "{$type}.mute";
  149. if (!in_array($action, $this->filters)) {
  150. return abort(406);
  151. }
  152. $filterable = [];
  153. switch ($type) {
  154. case 'user':
  155. $profile = Profile::findOrFail($item);
  156. if ($profile->id == $user->id) {
  157. return abort(403);
  158. }
  159. $class = get_class($profile);
  160. $filterable['id'] = $profile->id;
  161. $filterable['type'] = $class;
  162. break;
  163. default:
  164. // code...
  165. break;
  166. }
  167. $filter = UserFilter::firstOrCreate([
  168. 'user_id' => $user->id,
  169. 'filterable_id' => $filterable['id'],
  170. 'filterable_type' => $filterable['type'],
  171. 'filter_type' => 'mute',
  172. ]);
  173. return redirect()->back();
  174. }
  175. public function block(Request $request)
  176. {
  177. $this->validate($request, [
  178. 'type' => 'required|string',
  179. 'item' => 'required|integer|min:1',
  180. ]);
  181. $user = Auth::user()->profile;
  182. $type = $request->input('type');
  183. $item = $request->input('item');
  184. $action = "{$type}.block";
  185. if (!in_array($action, $this->filters)) {
  186. return abort(406);
  187. }
  188. $filterable = [];
  189. switch ($type) {
  190. case 'user':
  191. $profile = Profile::findOrFail($item);
  192. $class = get_class($profile);
  193. $filterable['id'] = $profile->id;
  194. $filterable['type'] = $class;
  195. break;
  196. default:
  197. // code...
  198. break;
  199. }
  200. $filter = UserFilter::firstOrCreate([
  201. 'user_id' => $user->id,
  202. 'filterable_id' => $filterable['id'],
  203. 'filterable_type' => $filterable['type'],
  204. 'filter_type' => 'block',
  205. ]);
  206. return redirect()->back();
  207. }
  208. public function followRequests(Request $request)
  209. {
  210. $pid = Auth::user()->profile->id;
  211. $followers = FollowRequest::whereFollowingId($pid)->orderBy('id','desc')->whereIsRejected(0)->simplePaginate(10);
  212. return view('account.follow-requests', compact('followers'));
  213. }
  214. public function followRequestHandle(Request $request)
  215. {
  216. $this->validate($request, [
  217. 'action' => 'required|string|max:10',
  218. 'id' => 'required|integer|min:1'
  219. ]);
  220. $pid = Auth::user()->profile->id;
  221. $action = $request->input('action') === 'accept' ? 'accept' : 'reject';
  222. $id = $request->input('id');
  223. $followRequest = FollowRequest::whereFollowingId($pid)->findOrFail($id);
  224. $follower = $followRequest->follower;
  225. switch ($action) {
  226. case 'accept':
  227. $follow = new Follower();
  228. $follow->profile_id = $follower->id;
  229. $follow->following_id = $pid;
  230. $follow->save();
  231. FollowPipeline::dispatch($follow);
  232. $followRequest->delete();
  233. break;
  234. case 'reject':
  235. $followRequest->is_rejected = true;
  236. $followRequest->save();
  237. break;
  238. }
  239. return response()->json(['msg' => 'success'], 200);
  240. }
  241. public function sudoMode(Request $request)
  242. {
  243. return view('auth.sudo');
  244. }
  245. public function sudoModeVerify(Request $request)
  246. {
  247. $this->validate($request, [
  248. 'password' => 'required|string|max:500'
  249. ]);
  250. $user = Auth::user();
  251. $password = $request->input('password');
  252. $next = $request->session()->get('redirectNext', '/');
  253. if(password_verify($password, $user->password) === true) {
  254. $request->session()->put('sudoMode', time());
  255. return redirect($next);
  256. } else {
  257. return redirect()
  258. ->back()
  259. ->withErrors(['password' => __('auth.failed')]);
  260. }
  261. }
  262. public function twoFactorCheckpoint(Request $request)
  263. {
  264. return view('auth.checkpoint');
  265. }
  266. public function twoFactorVerify(Request $request)
  267. {
  268. $this->validate($request, [
  269. 'code' => 'required|string|max:32'
  270. ]);
  271. $user = Auth::user();
  272. $code = $request->input('code');
  273. $google2fa = new Google2FA();
  274. $verify = $google2fa->verifyKey($user->{'2fa_secret'}, $code);
  275. if($verify) {
  276. $request->session()->push('2fa.session.active', true);
  277. return redirect('/');
  278. } else {
  279. return redirect()->back()->withErrors([
  280. 'code' => 'Invalid code'
  281. ]);
  282. }
  283. }
  284. }