Просмотр файла app/Http/Controllers/WallController.php

Размер файла: 4.27Kb
  1. <?php
  2.  
  3. declare(strict_types=1);
  4.  
  5. namespace App\Http\Controllers;
  6.  
  7. use App\Classes\Validator;
  8. use App\Models\Flood;
  9. use App\Models\Ignore;
  10. use App\Models\Wall;
  11. use Illuminate\Http\JsonResponse;
  12. use Illuminate\Http\RedirectResponse;
  13. use Illuminate\Http\Request;
  14. use Illuminate\View\View;
  15.  
  16. class WallController extends Controller
  17. {
  18. /**
  19. * Главная страница
  20. *
  21. * @param string $login
  22. *
  23. * @return View
  24. */
  25. public function index(string $login): View
  26. {
  27. $user = getUserByLogin($login);
  28.  
  29. if (! $user) {
  30. abort(404, __('validator.user'));
  31. }
  32.  
  33. $newWall = getUser('newwall');
  34.  
  35. $messages = Wall::query()
  36. ->where('user_id', $user->id)
  37. ->orderByDesc('created_at')
  38. ->with('user', 'author')
  39. ->paginate(setting('wallpost'));
  40.  
  41. if ($newWall && getUser('id') === $user->id) {
  42. $user->update([
  43. 'newwall' => 0,
  44. ]);
  45. }
  46.  
  47. return view('walls/index', compact('messages', 'user', 'newWall'));
  48. }
  49.  
  50. /**
  51. * Добавление сообщения
  52. *
  53. * @param string $login
  54. * @param Request $request
  55. * @param Validator $validator
  56. * @param Flood $flood
  57. *
  58. * @return RedirectResponse
  59. */
  60. public function create(string $login, Request $request, Validator $validator, Flood $flood): RedirectResponse
  61. {
  62. if (! getUser()) {
  63. abort(403, __('main.not_authorized'));
  64. }
  65.  
  66. $user = getUserByLogin($login);
  67.  
  68. if (! $user) {
  69. abort(404, __('validator.user'));
  70. }
  71.  
  72. if ($request->isMethod('post')) {
  73. $msg = $request->input('msg');
  74.  
  75. $validator->equal($request->input('_token'), csrf_token(), __('validator.token'))
  76. ->length($msg, 5, setting('comment_length'), ['msg' => __('validator.text')])
  77. ->false($flood->isFlood(), ['msg' => __('validator.flood', ['sec' => $flood->getPeriod()])]);
  78.  
  79. $ignoring = Ignore::query()
  80. ->where('user_id', $user->id)
  81. ->where('ignore_id', getUser('id'))
  82. ->first();
  83.  
  84. $validator->empty($ignoring, __('ignores.you_are_ignoring'));
  85.  
  86. if ($validator->isValid()) {
  87. if (getUser() && getUser('id') !== $user->id) {
  88. $user->increment('newwall');
  89. }
  90.  
  91. Wall::query()->create([
  92. 'user_id' => $user->id,
  93. 'author_id' => getUser('id'),
  94. 'text' => antimat($msg),
  95. 'created_at' => SITETIME,
  96. ]);
  97.  
  98. $flood->saveState();
  99. sendNotify($msg, '/walls/' . $user->login, __('index.wall_posts_login', ['login' => $user->getName()]));
  100.  
  101. setFlash('success', __('main.record_added_success'));
  102. } else {
  103. setInput($request->all());
  104. setFlash('danger', $validator->getErrors());
  105. }
  106.  
  107. return redirect('walls/' . $user->login);
  108. }
  109. }
  110.  
  111. /**
  112. * Удаление сообщений
  113. *
  114. * @param string $login
  115. * @param Request $request
  116. * @param Validator $validator
  117. *
  118. * @return JsonResponse
  119. */
  120. public function delete(string $login, Request $request, Validator $validator): JsonResponse
  121. {
  122. $id = int($request->input('id'));
  123. $user = getUserByLogin($login);
  124.  
  125. if (! $user) {
  126. abort(404, __('validator.user'));
  127. }
  128.  
  129. $validator
  130. ->true($request->ajax(), __('validator.not_ajax'))
  131. ->equal($request->input('_token'), csrf_token(), __('validator.token'))
  132. ->notEmpty($id, __('validator.deletion'))
  133. ->notEmpty($user, __('validator.user'))
  134. ->true(isAdmin() || getUser('id') === $user->id, __('main.deleted_only_admins'));
  135.  
  136. if ($validator->isValid()) {
  137. Wall::query()
  138. ->where('id', $id)
  139. ->where('user_id', $user->id)
  140. ->delete();
  141.  
  142. return response()->json(['success' => true]);
  143. }
  144.  
  145. return response()->json([
  146. 'success' => false,
  147. 'message' => current($validator->getErrors()),
  148. ]);
  149. }
  150. }