AccountController.php 9.0 KB

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