AccountController.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\EmailVerification;
  4. use App\Mail\ConfirmEmail;
  5. use App\Notification;
  6. use App\Profile;
  7. use App\User;
  8. use App\UserFilter;
  9. use Auth;
  10. use Cache;
  11. use Carbon\Carbon;
  12. use Illuminate\Http\Request;
  13. use Mail;
  14. use Redis;
  15. class AccountController extends Controller
  16. {
  17. protected $filters = [
  18. 'user.mute',
  19. 'user.block',
  20. ];
  21. public function __construct()
  22. {
  23. $this->middleware('auth');
  24. }
  25. public function notifications(Request $request)
  26. {
  27. $this->validate($request, [
  28. 'page' => 'nullable|min:1|max:3',
  29. 'a' => 'nullable|alpha_dash',
  30. ]);
  31. $profile = Auth::user()->profile;
  32. $action = $request->input('a');
  33. $timeago = Carbon::now()->subMonths(6);
  34. if ($action && in_array($action, ['comment', 'follow', 'mention'])) {
  35. $notifications = Notification::whereProfileId($profile->id)
  36. ->whereAction($action)
  37. ->whereDate('created_at', '>', $timeago)
  38. ->orderBy('id', 'desc')
  39. ->simplePaginate(30);
  40. } else {
  41. $notifications = Notification::whereProfileId($profile->id)
  42. ->whereDate('created_at', '>', $timeago)
  43. ->orderBy('id', 'desc')
  44. ->simplePaginate(30);
  45. }
  46. return view('account.activity', compact('profile', 'notifications'));
  47. }
  48. public function followingActivity(Request $request)
  49. {
  50. $this->validate($request, [
  51. 'page' => 'nullable|min:1|max:3',
  52. 'a' => 'nullable|alpha_dash',
  53. ]);
  54. $profile = Auth::user()->profile;
  55. $action = $request->input('a');
  56. $timeago = Carbon::now()->subMonths(1);
  57. $following = $profile->following->pluck('id');
  58. $notifications = Notification::whereIn('actor_id', $following)
  59. ->where('profile_id', '!=', $profile->id)
  60. ->whereDate('created_at', '>', $timeago)
  61. ->orderBy('notifications.id', 'desc')
  62. ->simplePaginate(30);
  63. return view('account.following', compact('profile', 'notifications'));
  64. }
  65. public function verifyEmail(Request $request)
  66. {
  67. return view('account.verify_email');
  68. }
  69. public function sendVerifyEmail(Request $request)
  70. {
  71. $timeLimit = Carbon::now()->subDays(1)->toDateTimeString();
  72. $recentAttempt = EmailVerification::whereUserId(Auth::id())
  73. ->where('created_at', '>', $timeLimit)->count();
  74. $exists = EmailVerification::whereUserId(Auth::id())->count();
  75. if ($recentAttempt == 1 && $exists == 1) {
  76. return redirect()->back()->with('error', 'A verification email has already been sent recently. Please check your email, or try again later.');
  77. } elseif ($recentAttempt == 0 && $exists !== 0) {
  78. // Delete old verification and send new one.
  79. EmailVerification::whereUserId(Auth::id())->delete();
  80. }
  81. $user = User::whereNull('email_verified_at')->find(Auth::id());
  82. $utoken = hash('sha512', $user->id);
  83. $rtoken = str_random(40);
  84. $verify = new EmailVerification();
  85. $verify->user_id = $user->id;
  86. $verify->email = $user->email;
  87. $verify->user_token = $utoken;
  88. $verify->random_token = $rtoken;
  89. $verify->save();
  90. Mail::to($user->email)->send(new ConfirmEmail($verify));
  91. return redirect()->back()->with('status', 'Verification email sent!');
  92. }
  93. public function confirmVerifyEmail(Request $request, $userToken, $randomToken)
  94. {
  95. $verify = EmailVerification::where('user_token', $userToken)
  96. ->where('random_token', $randomToken)
  97. ->firstOrFail();
  98. if (Auth::id() === $verify->user_id) {
  99. $user = User::find(Auth::id());
  100. $user->email_verified_at = Carbon::now();
  101. $user->save();
  102. return redirect('/');
  103. }
  104. }
  105. public function fetchNotifications($id)
  106. {
  107. $key = config('cache.prefix').":user.{$id}.notifications";
  108. $redis = Redis::connection();
  109. $notifications = $redis->lrange($key, 0, 30);
  110. if (empty($notifications)) {
  111. $notifications = Notification::whereProfileId($id)
  112. ->orderBy('id', 'desc')->take(30)->get();
  113. } else {
  114. $notifications = $this->hydrateNotifications($notifications);
  115. }
  116. return $notifications;
  117. }
  118. public function hydrateNotifications($keys)
  119. {
  120. $prefix = 'notification.';
  121. $notifications = collect([]);
  122. foreach ($keys as $key) {
  123. $notifications->push(Cache::get("{$prefix}{$key}"));
  124. }
  125. return $notifications;
  126. }
  127. public function messages()
  128. {
  129. return view('account.messages');
  130. }
  131. public function showMessage(Request $request, $id)
  132. {
  133. return view('account.message');
  134. }
  135. public function mute(Request $request)
  136. {
  137. $this->validate($request, [
  138. 'type' => 'required|string',
  139. 'item' => 'required|integer|min:1',
  140. ]);
  141. $user = Auth::user()->profile;
  142. $type = $request->input('type');
  143. $item = $request->input('item');
  144. $action = "{$type}.mute";
  145. if (!in_array($action, $this->filters)) {
  146. return abort(406);
  147. }
  148. $filterable = [];
  149. switch ($type) {
  150. case 'user':
  151. $profile = Profile::findOrFail($item);
  152. if ($profile->id == $user->id) {
  153. return abort(403);
  154. }
  155. $class = get_class($profile);
  156. $filterable['id'] = $profile->id;
  157. $filterable['type'] = $class;
  158. break;
  159. default:
  160. // code...
  161. break;
  162. }
  163. $filter = UserFilter::firstOrCreate([
  164. 'user_id' => $user->id,
  165. 'filterable_id' => $filterable['id'],
  166. 'filterable_type' => $filterable['type'],
  167. 'filter_type' => 'mute',
  168. ]);
  169. return redirect()->back();
  170. }
  171. public function block(Request $request)
  172. {
  173. $this->validate($request, [
  174. 'type' => 'required|string',
  175. 'item' => 'required|integer|min:1',
  176. ]);
  177. $user = Auth::user()->profile;
  178. $type = $request->input('type');
  179. $item = $request->input('item');
  180. $action = "{$type}.block";
  181. if (!in_array($action, $this->filters)) {
  182. return abort(406);
  183. }
  184. $filterable = [];
  185. switch ($type) {
  186. case 'user':
  187. $profile = Profile::findOrFail($item);
  188. $class = get_class($profile);
  189. $filterable['id'] = $profile->id;
  190. $filterable['type'] = $class;
  191. break;
  192. default:
  193. // code...
  194. break;
  195. }
  196. $filter = UserFilter::firstOrCreate([
  197. 'user_id' => $user->id,
  198. 'filterable_id' => $filterable['id'],
  199. 'filterable_type' => $filterable['type'],
  200. 'filter_type' => 'block',
  201. ]);
  202. return redirect()->back();
  203. }
  204. }