AccountController.php 6.8 KB

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